1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form dataTablesDefaults no_op_set MSG_EMPTY_TITLE_AND_CONTENT MSG_EMPTY_TEMPLATES code MSG_CODE_EXISTS MSG_CODE_EXISTS_FOR_LIBRARY MSG_DT_LOADING_RECORDS interface theme */
4 var modal_loading = "<div id=\"loading\"><img src=\"" + interface + "/" + theme + "/img/spinner-small.gif\" alt=\"\" /> "+ MSG_DT_LOADING_RECORDS +"</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( MSG_CODE_EXISTS.format(new_lettercode));
23 alert( MSG_CODE_EXISTS_FOR_LIBRARY.format(new_lettercode, new_branchcode) );
33 $(document).ready(function() {
34 if( add_form || copy_form ){
35 $('#toolbar').fixFloat();
38 $("#lettert:has(tbody tr)").dataTable($.extend(true, {}, dataTablesDefaults, {
41 { "bSortable": false, "bSearchable": false, 'aTargets': [ 'nosort' ] }
47 $('#branch').change(function() {
49 $('#selectlibrary').submit();
51 $('#newnotice').click(function() {
52 $('#op').val("add_form");
57 $("#newmodule").on("change",function(){
59 if( $("#branch").val() === ""){
62 branchcode = $("#branch").val();
64 window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
67 $("#submit_form").on("click",function(){
68 $("#add_notice").submit();
71 $("#add_notice").validate({
72 submitHandler: function(form){
73 var at_least_one_exists = 0;
75 $("fieldset.mtt").each( function(){
76 var title = $(this).find('input[name="title"]').val();
77 var content = $(this).find('textarea[name="content"]').val();
79 ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
81 var mtt = $(this).find('input[name="message_transport_type"]').val();
82 at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
83 alert( MSG_EMPTY_TITLE_AND_CONTENT.format( mtt ) );
85 } else if ( title.length > 0 && content.length > 0 ) {
86 at_least_one_exists = 1;
90 if ( ! at_least_one_exists ) {
91 // No templates were filled out
92 alert( MSG_EMPTY_TEMPLATES );
100 // Test if code already exists in DB
101 if( editing == 1 ){ // This is an edit operation
102 // We don't need to check for an existing Code
104 var new_lettercode = $("#code").val();
105 var new_branchcode = $("#branch").val();
106 var code_check = checkCodes( new_lettercode, new_branchcode );
107 if( code_check.responseJSON.letters.length > 0 ){
116 $(".content_sms").on("keyup", function(){
117 var length = $(this).val().length;
118 var sms_counter = ("#sms_counter_" + $(this).data('lang'));
119 $(sms_counter).html(length + "/" + sms_limit + _(" characters"));
120 if ( length > sms_limit ) {
121 $(sms_counter).css("color", "red");
123 $(sms_counter).css("color", "black");
127 $( ".transport-types" ).accordion({ collapsible: true, active:false, animate: 200 });
129 $(".insert").on("click",function(){
130 var containerid = $(this).data("containerid");
131 insertValueQuery( containerid );
134 $("#saveandcontinue").on("click",function(e){
136 $("#redirect").val("just_save");
137 $("#submit_form").click();
142 $("body").on("click", ".preview_template", function(e){
144 var mtt = $(this).data("mtt");
145 var lang = $(this).data("lang");
147 var code = $("#code").val();
148 var content = $("#content_"+mtt+"_"+lang).val();
149 var title = $("#title_"+mtt+"_"+lang).val();
151 var is_html = $("#is_html_"+mtt+"_"+lang).val();
152 var page = $(this).attr("href");
153 var data_preview = $("#data_preview").val();
154 page += '?code='+encodeURIComponent(code);
155 page += '&title='+encodeURIComponent(title);
156 page += '&content='+encodeURIComponent(content);
157 page += '&data_preview='+encodeURIComponent(data_preview);
158 page += '&is_html='+encodeURIComponent(is_html);
159 $("#preview_template .modal-body").load(page + " #main");
160 $('#preview_template').modal('show');
161 $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
164 $("#preview_template").on("hidden.bs.modal", function(){
165 $("#preview_template_label").html("");
166 $("#preview_template .modal-body").html( modal_loading );
169 function insertValueQuery(containerid) {
170 var fieldset = $("#" + containerid);
171 var myQuery = $(fieldset).find('textarea[name="content"]');
172 var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
174 if($(myListBox).find('option').length > 0) {
175 $(myListBox).find('option').each( function (){
176 if ( $(this).attr('selected') && $(this).val().length > 0 ) {
177 $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");