1 /* Variables defined in letter.tt: */
2 /* global _ module add_form copy_form no_op_set code interface theme KohaTable table_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 !== '' || copy_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));
33 function confirmOverwrite( new_lettercode, new_branchcode ){
36 data: { code: new_lettercode, branchcode: new_branchcode },
38 url: '/cgi-bin/koha/svc/letters/get/',
40 success: function (data) {
41 if ( data.letters.length > 0 ) {
47 return confirm(__("A letter with the code '%s' already exists for '%s'. Overwrite this letter?").format(new_lettercode, new_branchcode));
53 $(document).ready(function() {
54 if( add_form || copy_form ){
55 Sticky = $("#toolbar");
58 stickyClass: "floating"
62 var ntable = KohaTable("lettert", {
68 $('#branch').change(function() {
70 $('#selectlibrary').submit();
72 $('#newnotice').click(function() {
73 $('#op').val("add_form");
78 $("#newmodule").on("change",function(){
80 if( $("#branch").val() === ""){
83 branchcode = $("#branch").val();
85 window.location.href = "/cgi-bin/koha/tools/letter.pl?op=add_form&module=" + $(this).val() + "&branchcode=" + branchcode;
88 $("#submit_form").on("click",function(){
89 $("#add_notice").submit();
92 $("#add_notice").validate({
93 submitHandler: function(form){
94 var at_least_one_exists = 0;
96 $("fieldset.mtt").each( function(){
97 var title = $(this).find('input[name="title"]').val();
98 var content = $(this).find('textarea[name="content"]').val();
100 ( title.length === 0 && content.length > 0 ) || ( title.length > 0 && content.length === 0 )
102 var mtt = $(this).find('input[name="message_transport_type"]').val();
103 at_least_one_exists = 1; // Only one template has to be filled in for form to be valid
104 alert(__("Please specify title and content for %s").format(mtt));
106 } else if ( title.length > 0 && content.length > 0 ) {
107 at_least_one_exists = 1;
111 if ( ! at_least_one_exists ) {
112 // No templates were filled out
113 alert( __("Please fill at least one template.") );
121 // Test if code already exists in DB
122 if( editing == 1 ){ // This is an edit operation
123 // We don't need to check for an existing Code
124 // However if we're copying, provide confirm
125 // pop up of overwriting existing notice or slip
127 var new_lettercode = $("#code").val();
128 var new_branchcode = $("#branch").val();
129 var confirm = confirmOverwrite( new_lettercode, new_branchcode );
130 if(confirm === false){
135 var new_lettercode = $("#code").val();
136 var new_branchcode = $("#branch").val();
137 var code_check = checkCodes( new_lettercode, new_branchcode );
138 if( code_check.responseJSON.letters.length > 0 ){
147 $(".content_sms").on("keyup", function(){
148 var length = $(this).val().length;
149 var sms_counter = ("#sms_counter_" + $(this).data('lang'));
150 $(sms_counter).html(length + "/" + sms_limit + __(" characters"));
151 if ( length > sms_limit ) {
152 $(sms_counter).css("color", "red");
154 $(sms_counter).css("color", "black");
158 let section = $("#section").val();
160 $("a[href='#" + section + "']").click();
163 $(".panel-group").on("shown.bs.collapse", function (e) {
164 $("#section").val( e.target.id );
165 }).on("hidden.bs.collapse", function (e) {
166 $("#section").val("");
169 if( $("#tabs").length > 0 ){
170 let langtab = $("#langtab").val();
171 $("#tabs a[data-toggle='tab']").on("shown.bs.tab", function (e) {
172 var link = e.target.hash.replace("#","");
173 $("#langtab").val( link );
177 $("#tabs a[href='#" + langtab + "']").tab("show");
179 $("#tabs a[href='#lang_default_panel']").tab("show");
183 $(".insert").on("click",function(){
184 var containerid = $(this).data("containerid");
185 insertValueQuery( containerid );
188 $("#saveandcontinue").on("click",function(e){
190 $("#redirect").val("just_save");
191 $("#submit_form").click();
194 $("body").on("click", ".preview_template", function(e){
196 var mtt = $(this).data("mtt");
197 var lang = $(this).data("lang");
199 var code = $("#code").val();
200 var content = $("#content_"+mtt+"_"+lang).val();
201 var title = $("#title_"+mtt+"_"+lang).val();
203 var is_html = $("#is_html_"+mtt+"_"+lang).val();
204 var page = $(this).attr("href");
205 var data_preview = $("#data_preview").val();
206 page += '?code='+encodeURIComponent(code);
207 page += '&title='+encodeURIComponent(title);
208 page += '&content='+encodeURIComponent(content);
209 page += '&data_preview='+encodeURIComponent(data_preview);
210 page += '&is_html='+encodeURIComponent(is_html);
211 $("#preview_template .modal-body").load(page + " .main");
212 $('#preview_template').modal('show');
213 $("#preview_template_button").attr("href", "/cgi-bin/koha/svc/letters/convert?module="+module+"&code="+code+"&mtt="+mtt+"&lang="+lang);
216 $("#preview_template").on("hidden.bs.modal", function(){
217 $("#preview_template_label").html("");
218 $("#preview_template .modal-body").html( modal_loading );
221 function insertValueQuery(containerid) {
222 var fieldset = $("#" + containerid);
223 var myQuery = $(fieldset).find('textarea[name="content"]');
224 var myListBox = $(fieldset).find('select[name="SQLfieldname"]');
226 if($(myListBox).find('option').length > 0) {
227 $(myListBox).find('option').each( function (){
228 if ( $(this).prop('selected') && $(this).val().length > 0 ) {
229 $(myQuery).insertAtCaret("<<" + $(this).val() + ">>");