1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form no_op_set code interface theme KohaTable columns_settings */
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ __('Loading...') +"</div>";
7 if( add_form == 1 && code !== '' ){
11 function checkCodes( new_lettercode, new_branchcode ){
14 data: { code: new_lettercode, branchcode: new_branchcode },
16 url: '/cgi-bin/koha/svc/letters/get/',
18 success: function (data) {
19 if ( data.letters.length > 0 ) {
20 if( new_branchcode === '' ) {
21 alert(__("A default letter with the code '%s' already exists.").format(new_lettercode));
23 alert(__("A letter with the code '%s' already exists for '%s'.").format(new_lettercode, new_branchcode));
35 $(document).ready(function() {
36 if( add_form || copy_form ){
37 Sticky = $("#toolbar");
40 stickyClass: "floating"
44 var ntable = KohaTable("lettert", {
47 "aaSorting": [[ 3, "asc" ]],
51 $('#branch').change(function() {
53 $('#selectlibrary').submit();
55 $('#newnotice').click(function() {
56 $('#op').val("add_form");
61 $("#newmodule").on("change",function(){
63 if( $("#branch").val() === ""){
66 branchcode = $("#branch").val();
68 window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
71 $("#submit_form").on("click",function(){
72 $("#add_notice").submit();
75 $("#add_notice").validate({
76 submitHandler: function(form){
77 var at_least_one_exists = 0;
79 $("fieldset.mtt").each( function(){
80 var title = $(this).find('input[name="title"]').val();
81 var content = $(this).find('textarea[name="content"]').val();
83 ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
85 var mtt = $(this).find('input[name="message_transport_type"]').val();
86 at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
87 alert(__("Please specify title and content for %s").format(mtt));
89 } else if ( title.length > 0 && content.length > 0 ) {
90 at_least_one_exists = 1;
94 if ( ! at_least_one_exists ) {
95 // No templates were filled out
96 alert( __("Please fill at least one template.") );
104 // Test if code already exists in DB
105 if( editing == 1 ){ // This is an edit operation
106 // We don't need to check for an existing Code
108 var new_lettercode = $("#code").val();
109 var new_branchcode = $("#branch").val();
110 var code_check = checkCodes( new_lettercode, new_branchcode );
111 if( code_check.responseJSON.letters.length > 0 ){
120 $(".content_sms").on("keyup", function(){
121 var length = $(this).val().length;
122 var sms_counter = ("#sms_counter_" + $(this).data('lang'));
123 $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
124 if ( length > sms_limit ) {
125 $(sms_counter).css("color", "red");
127 $(sms_counter).css("color", "black");
131 $( ".transport-types" ).accordion({
133 active: parseInt( $("#section").val(), 10),
135 activate: function() {
136 var active = $( ".transport-types" ).accordion( "option", "active" );
137 if( active === false ){
138 $("#section").val("");
140 $("#section").val( active );
145 $(".insert").on("click",function(){
146 var containerid = $(this).data("containerid");
147 insertValueQuery( containerid );
150 $("#saveandcontinue").on("click",function(e){
152 $("#redirect").val("just_save");
153 $("#submit_form").click();
158 $("body").on("click", ".preview_template", function(e){
160 var mtt = $(this).data("mtt");
161 var lang = $(this).data("lang");
163 var code = $("#code").val();
164 var content = $("#content_"+mtt+"_"+lang).val();
165 var title = $("#title_"+mtt+"_"+lang).val();
167 var is_html = $("#is_html_"+mtt+"_"+lang).val();
168 var page = $(this).attr("href");
169 var data_preview = $("#data_preview").val();
170 page += '?code='+encodeURIComponent(code);
171 page += '&title='+encodeURIComponent(title);
172 page += '&content='+encodeURIComponent(content);
173 page += '&data_preview='+encodeURIComponent(data_preview);
174 page += '&is_html='+encodeURIComponent(is_html);
175 $("#preview_template .modal-body").load(page + " .main");
176 $('#preview_template').modal('show');
177 $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
180 $("#preview_template").on("hidden.bs.modal", function(){
181 $("#preview_template_label").html("");
182 $("#preview_template .modal-body").html( modal_loading );
185 function insertValueQuery(containerid) {
186 var fieldset = $("#" + containerid);
187 var myQuery = $(fieldset).find('textarea[name="content"]');
188 var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
190 if($(myListBox).find('option').length > 0) {
191 $(myListBox).find('option').each( function (){
192 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
193 $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");