]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
Fix LP904860, Cannot print a patron address label
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / print.js
1 dump('entering util/print.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.print = function (context) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init( { 'via':'stash' } );
8     JSAN.use('util.window'); this.win = new util.window();
9     JSAN.use('util.functional');
10     JSAN.use('util.file');
11
12     this.set_context(context, true);
13
14     try {
15         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
16
17         if (prefs.prefHasUserValue('print.always_print_silent')) {
18             if (! prefs.getBoolPref('print.always_print_silent')) {
19                 prefs.clearUserPref('print.always_print_silent');
20             }
21         }
22     } catch(E) {
23         dump('Error in print.js trying to clear print.always_print_silent\n');
24     }
25
26     return this;
27 };
28
29 util.print.prototype = {
30
31     'set_context' : function(context, set_default) {
32         this.context = context || 'default';
33         if(set_default) this.default_context = this.context;
34     
35         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
36         var key = 'oils.printer.external.cmd.' + this.context;
37         var has_key = prefs.prefHasUserValue(key);
38         if(!has_key && this.context != 'default') {
39             key = 'oils.printer.external.cmd.default';
40             has_key = prefs.prefHasUserValue(key);
41         }
42         this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
43     },
44
45     'reprint_last' : function() {
46         try {
47             var obj = this; obj.data.init({'via':'stash'});
48             if (!obj.data.last_print) {
49                 alert(
50                     document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
51                 );
52                 return;
53             }
54             var last_print = JSON2js(obj.data.last_print);
55             if(last_print.context) this.set_context(last_print.context);
56             var msg = last_print.msg;
57             var params = last_print.params; params.no_prompt = false;
58             obj.simple( msg, params );
59         } catch(E) {
60             this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
61         }
62         if(this.context != this.default_context) this.set_context(this.default_context);
63     },
64
65     'html2txt' : function(html) {
66         JSAN.use('util.text');
67         //dump('html2txt, before:\n' + html + '\n');
68         var lines = html.split(/\n/);
69         var new_lines = [];
70         for (var i = 0; i < lines.length; i++) {
71             var line = lines[i];
72             if (line) {
73                 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
74                 line = util.text.reverse_preserve_string_in_html(line);
75                 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
76                 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
77                     var raw_chars = '';
78                     var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
79                     for (var j = 0; j < hex_chars.length; j++) {
80                         raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
81                     }
82                     return p1 + p3 + raw_chars;
83                 });
84                 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
85                 line = line.replace(/<br.*?>/gi,'\r\n');
86                 line = line.replace(/<table.*?>/gi,'');
87                 line = line.replace(/<tr.*?>/gi,'');
88                 line = line.replace(/<hr.*?>/gi,'\r\n');
89                 line = line.replace(/<p.*?>/gi,'');
90                 line = line.replace(/<block.*?>/gi,'');
91                 line = line.replace(/<li.*?>/gi,' * ');
92                 line = line.replace(/<.+?>/gi,'');
93                 line = line.replace(/&lt;/gi,'<');
94                 line = line.replace(/&gt;/gi,'>');
95                 line = line.replace(/&amp;/gi,'&');
96                 if (line) { new_lines.push(line); }
97             } else {
98                 new_lines.push(line);
99             }
100         }
101         var new_html = new_lines.join('\n');
102         //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
103         return new_html;
104     },
105
106     'escape_html' : function(data) {
107         if (typeof data == 'object') { return ''; }
108         if (typeof data != 'string') { return data; }
109         return data.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
110     },
111
112     'simple' : function(msg,params) {
113         try {
114             if (!params) params = {};
115             params.msg = msg;
116
117             var obj = this;
118
119             obj.data.last_print = js2JSON({ 'msg' : msg, 'params' : params, 'context' : this.context});
120             obj.data.stash('last_print');
121
122             var silent = false;
123             if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
124                 silent = true;
125             }
126
127             var content_type;
128             if (params && params.content_type) {
129                 content_type = params.content_type;
130             } else {
131                 content_type = 'text/html';
132             }
133
134             var w;
135
136             obj.data.init({'via':'stash'});
137
138             if (typeof obj.data.print_strategy == 'undefined') {
139                 obj.data.print_strategy = {};
140                 obj.data.stash('print_strategy');
141             }
142
143             if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
144
145                 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
146                     case 'dos.print':
147                         params.dos_print = true;
148                     case 'custom.print':
149                         /* FIXME - this is a kludge.. we're going to sidestep window-based html rendering for printing */
150                         /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
151                         /* templates aren't guaranteed to be valid xml.  The unadulterated msg is still preserved in */
152                         /* params */
153                         if (content_type=='text/html') {
154                             w = obj.html2txt(msg);
155                         } else {
156                             w = msg;
157                         }
158                         if (! params.no_form_feed) { w = w + '\f'; }
159                         obj.NSPrint(w, silent, params);
160                         return;
161                     break;
162                 }
163             }
164
165             switch(content_type) {
166                 case 'text/html' :
167                     if(!params.type) {
168                         params.type = '';
169                     }
170                     var my_prefix = 'oils://remote/xul/server/';
171                     if(window.location.protocol == "chrome:") {
172                         // Likely in offline interface
173                         my_prefix = 'chrome://open_ils_staff_client/content/';
174                     } else {
175                         if(xulG && xulG.url_prefix) {
176                             my_prefix = xulG.url_prefix(my_prefix);
177                         }
178                     }
179                     var print_url = '<html id="top"><head><title>EG</title>'
180                         + '<script src="' + my_prefix + 'util/print_win.js"></script>'
181                         + '<script src="' + my_prefix + 'util/print_custom.js"></script>';
182                     if(this.data.hash.aous['print.custom_js_file']) {
183                         print_url += '<script src="' + this.data.hash.aous['print.custom_js_file'] + '"></script>';
184                     }
185                     print_url += '</head><body onload="try{print_init(\'' + params.type + '\');}catch(E){alert(E);}">' + msg.replace(/<script[^>]*>.*?<\/script>/gi,'') + '</body></html>';
186                     print_url = 'data:text/html;charset=utf-8,' + encodeURIComponent(print_url);
187                     obj.win.openDialog(print_url,'receipt_temp','chrome,resizable,modal', { "data" : params.data, "list" : params.list}, function(w) { 
188                         try {
189                             obj.NSPrint(w, silent, params);
190                         } catch(E) {
191                             obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
192                             w.print();
193                         }
194                         w.close();
195                     });
196                 break;
197                 default:
198                     w = obj.win.open('data:' + content_type + ',' + window.encodeURIComponent(msg),'receipt_temp','chrome,resizable');
199                     w.minimize();
200                     setTimeout(
201                         function() {
202                             try {
203                                 obj.NSPrint(w, silent, params);
204                             } catch(E) {
205                                 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
206                                 w.print();
207                             }
208                             w.minimize(); w.close();
209                         }, 1000
210                     );
211                 break;
212             }
213
214         } catch(E) {
215             this.error.standard_unexpected_error_alert('util.print.simple',E);
216         }
217     },
218     
219     'tree_list' : function (params) { 
220         try {
221             dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
222             dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
223         } catch(E) {
224             dump(E+'\n');
225         }
226         var cols = [];
227         var s = '';
228         if (params.context) this.set_context(params.context);
229         if (params.header) s += this.template_sub( params.header, cols, params );
230         if (params.list) {
231             // Pre-templating sort
232             // %SORT(field[ AS type][ ASC|DESC][,...])%
233             var sort_blocks = params.line_item.match(/%SORT\([^)]+\)%/g);
234             if(sort_blocks) {
235                 for(var i = 0; i < sort_blocks.length; i++) {
236                     sort_blocks[i] = sort_blocks[i].substring(6,sort_blocks[i].length-2);
237                 }
238                 sort_blocks = sort_blocks.join(',').split(/\s*,\s*/); // Supports %SORT(a,b)% and %SORT(a)% %SORT(b)% methods
239                 for(var i = 0; i < sort_blocks.length; i++) {
240                     sort_blocks[i] = sort_blocks[i].match(/([^ ]+)(?:\s+AS\s+([^ ]+))?(?:\s+(ASC|DESC))?/);
241                     sort_blocks[i].shift(); // Removes the "full match" entry
242                 }
243
244                 function sorter(a, b) {
245                     var return_val = 0;
246                     for(var i = 0; i < sort_blocks.length && return_val == 0; i++) {
247                         var sort = sort_blocks[i];
248                         var a_test = a[sort[0]];
249                         var b_test = b[sort[0]];
250                         sort[1] = sort[1] || '';
251                         sort[2] = sort[2] || 'ASC';
252                         switch(sort[1].toUpperCase()) {
253                             case 'DATE':
254                                 a_test = new Date(a_test);
255                                 b_test = new Date(b_test);
256                                 break;
257                             case 'INT':
258                                 a_test = parseInt(a_test);
259                                 b_test = parseInt(b_test);
260                                 break;
261                             case 'FLOAT':
262                             case 'NUMBER':
263                                 a_test = parseFloat(a_test);
264                                 b_test = parseFloat(b_test);
265                                 break;
266                             case 'LOWER':
267                                 a_test = a_test.toLowerCase();
268                                 b_test = b_test.toLowerCase();
269                                 break;
270                             case 'UPPER':
271                                 a_test = a_test.toUpperCase();
272                                 b_test = b_test.toUpperCase();
273                                 break;
274                         }
275                         if(a_test > b_test) return_val = 1;
276                         if(a_test < b_test) return_val = -1;
277                         if(sort[2] == 'DESC') return_val *= -1;
278                     }
279                     return return_val;
280                 }
281                 params.list.sort(sorter);
282                 params.line_item = params.line_item.replace(/%SORT\([^)]*\)%/g,'');
283             }
284
285             for (var i = 0; i < params.list.length; i++) {
286                 params.row = params.list[i];
287                 params.row_idx = i;
288                 s += this.template_sub( params.line_item, cols, params );
289             }
290         }
291         if (params.footer) s += this.template_sub( params.footer, cols, params );
292
293         // Sanity check, no javascript in templates
294         // Note: [\s\S] is a workaround for . not including newlines.
295         s=s.replace(/<script[^>]*>[\s\S]*?<\/script[^>]*>/gi,'')
296         s=s.replace(/onload\s*=\s*"[^"]*"/gi,'');
297         s=s.replace(/onload\s*=\s*'[^']*'/gi,'');
298
299         if (params.sample_frame) {
300             var jsrc = 'data:text/javascript,' + encodeURIComponent('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
301             params.sample_frame.setAttribute('src','data:text/html;charset=utf-8,' + encodeURIComponent('<html id="top"><head><title>EG</title><script src="' + jsrc + '"></script></head><body>' + s + '</body></html>'));
302         } else {
303             this.simple(s,params);
304         }
305         if(this.context != this.default_context) this.set_context(this.default_context);
306     },
307
308     'template_sub' : function( msg, cols, params ) {
309         try {
310             var obj = this;
311             if (!msg) { dump('template sub called with empty string\n'); return; }
312             JSAN.use('util.date');
313             var s = msg; var b;
314
315             // Includes
316             // Note that we keep track of already included settings
317             // This ensures that we don't infinite loop through includes
318             try {
319                 var match;
320                 var include_patt=/%INCLUDE\(\s*([^)]*?)\s*\)%/;
321                 var included = {};
322                 while(match = include_patt.exec(s)) {
323                     if(match[1] == '' || included[match[1]]) {
324                         s = s.replace(match[0], '');
325                     } else {
326                         included[match[1]] = true;
327                         s = s.replace(new RegExp("%INCLUDE\\(\\s*" + match[1].replace(/([.?*+^$[\]\\(){}-])/g, "\\$1") + "\\s*\\)%","g"), obj.data.hash.aous['circ.staff_client.receipt.' + match[1]] || '');
328                     }
329                 }
330             } catch(E) { dump(E+'\n'); }
331
332             try{b = s; s = s.replace(/%LINE_NO%/g,Number(params.row_idx)+1);}
333                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
334
335             try{b = s; s = s.replace(/%patron_barcode%/g,this.escape_html(params.patron_barcode));}
336                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
337
338             try{b = s; s = s.replace(/%LIBRARY%/g,this.escape_html(params.lib.name()));}
339                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
340             try{b = s; s = s.replace(/%PINES_CODE%/g,this.escape_html(params.lib.shortname()));}
341                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
342             try{b = s; s = s.replace(/%SHORTNAME%/g,this.escape_html(params.lib.shortname()));}
343                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
344             try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/g,this.escape_html(params.staff.first_given_name()));}
345                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
346             try{b = s; s = s.replace(/%STAFF_MIDDLENAME%/g,this.escape_html(params.staff.second_given_name() || ''));}
347                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
348             try{b = s; s = s.replace(/%STAFF_LASTNAME%/g,this.escape_html(params.staff.family_name()));}
349                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
350             try{b = s; s = s.replace(/%STAFF_BARCODE%/g,this.escape_html(params.staff.barcode)); }
351                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
352             try{b = s; s = s.replace(/%STAFF_PROFILE%/g,this.escape_html(obj.data.hash.pgt[ params.staff.profile() ].name() )); }
353                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
354             try{b = s; s = s.replace(/%PATRON_ALIAS_OR_FIRSTNAME%/g,this.escape_html((params.patron.alias() == '' || params.patron.alias() == null) ? params.patron.first_given_name() : params.patron.alias()));}
355                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
356             try{b = s; s = s.replace(/%PATRON_ALIAS%/g,this.escape_html((params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias()));}
357                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
358             try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/g,this.escape_html(params.patron.first_given_name()));}
359                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
360             try{b = s; s = s.replace(/%PATRON_MIDDLENAME%/g,this.escape_html(params.patron.second_given_name() || ''));}
361                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
362             try{b = s; s = s.replace(/%PATRON_LASTNAME%/g,this.escape_html(params.patron.family_name()));}
363                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
364             try{b = s; s = s.replace(/%PATRON_BARCODE%/g,this.escape_html(typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() )) ;}
365                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
366             try{b = s; s = s.replace(/%PATRON_EXPIRE_DATE%/g,this.escape_html(params.patron.expire_date()));}
367                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
368             try{b = s; s = s.replace(/%PATRON_EXPIRE_DATE_YMD%/g,util.date.formatted_date(params.patron.expire_date(), '%Y-%m-%d'));}
369                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
370
371             try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
372                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
373             try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
374                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
375             try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
376                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
377             try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
378                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
379             try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
380                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
381             try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
382                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
383             try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
384                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
385             try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
386                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
387             try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
388                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
389             try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
390                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
391
392             try {
393                 if (typeof params.row != 'undefined') {
394                     if (params.row.length >= 0) {
395                         alert('debug - please tell the developers that deprecated template code tried to execute');
396                         for (var i = 0; i < cols.length; i++) {
397                             var re = new RegExp(cols[i],"g");
398                             try{b = s; s=s.replace(re, this.escape_html(params.row[i]));}
399                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
400                         }
401                     } else { 
402                         /* for dump_with_keys */
403                         for (var i in params.row) {
404                             var re = new RegExp('%'+i+'%',"g");
405                             try{b = s; s=s.replace(re, this.escape_html(params.row[i].toString()));}
406                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
407                         }
408                     }
409                 }
410
411                 if (typeof params.data != 'undefined') {
412                     for (var i in params.data) {
413                         var re = new RegExp('%'+i+'%',"g");
414                         if (typeof params.data[i] == 'string' || typeof params.data[i] == 'number') {
415                             try{b = s; s=s.replace(re, this.escape_html(params.data[i]));}
416                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
417                         } else {
418                             /* likely a null, print as an empty string */
419                             try{b = s; s=s.replace(re, '');}
420                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
421                         }
422                     }
423                 }
424             } catch(E) { dump(E+'\n'); }
425
426             // Date Format
427             try {
428                 var match;
429                 var date_format_patt=/%DATE_FORMAT\(\s*([^,]*?)\s*,\s*([^)]*?)\s*\)%/;
430                 while(match = date_format_patt.exec(s)) {
431                     if(match[1] == '' || match[2] == '')
432                         s = s.replace(match[0], '');
433                     else
434                         s = s.replace(match[0], util.date.formatted_date(match[1], match[2]));
435                 }
436             } catch(E) { dump(E+'\n'); }
437
438             // Substrings
439             try {
440                 var match;
441                 // Pre-trim inside of substrings, and only inside of them
442                 // This keeps the trim commands from being truncated
443                 var substr_trim_patt=/(%SUBSTR\(-?\d+,?\s*(-?\d+)?\)%.*?)(\s*%-TRIM%|%TRIM-%\s*)(.*?%SUBSTR_END%)/;
444                 while(match = substr_trim_patt.exec(s))
445                     s = s.replace(match[0], match[1] + match[4]);
446                 // Then do the substrings themselves
447                 var substr_patt=/%SUBSTR\((-?\d+),?\s*(-?\d+)?\)%(.*?)%SUBSTR_END%/;
448                 while(match = substr_patt.exec(s)) {
449                     var substring_start = parseInt(match[1]);
450                     if(substring_start < 0) substring_start = match[3].length + substring_start;
451                     var substring_length = parseInt(match[2]);
452                     if(substring_length > 0)
453                         s = s.replace(match[0], match[3].substring(substring_start, substring_start + substring_length));
454                     else if(substring_length < 0)
455                         s = s.replace(match[0], match[3].substring(substring_start + substring_length, substring_start));
456                     else
457                         s = s.replace(match[0], match[3].substring(substring_start));
458                 }
459             } catch(E) { dump(E+'\n'); }
460
461             // Cleanup unwanted whitespace
462             try {
463                 s = s.replace(/%TRIM-%\s*/g,'');
464                 s = s.replace(/\s*%-TRIM%/g,'');
465             } catch(E) { dump(E+'\n'); }
466
467             return s;
468         } catch(E) {
469             alert('Error in print.js, template_sub(): ' + E);
470         }
471     },
472
473
474     'NSPrint' : function(w,silent,params) {
475         if (!w) w = window;
476         var obj = this;
477         try {
478             if (!params) params = {};
479
480             obj.data.init({'via':'stash'});
481
482             if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
483
484                 dump('params.print_strategy = ' + params.print_strategy
485                     + ' || obj.data.print_strategy[' + obj.context + '] = ' + obj.data.print_strategy[obj.context] 
486                     + ' || obj.data.print_strategy[default] = ' + obj.data.print_strategy['default'] 
487                     + ' => ' + ( params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default'] ) + '\n');
488                 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
489                     case 'dos.print':
490                         params.dos_print = true;
491                     case 'custom.print':
492                         if (typeof w != 'string') {
493                             try {
494                                 var temp_w = params.msg || w.document.firstChild.innerHTML;
495                                 if (!params.msg) { params.msg = temp_w; }
496                                 if (typeof temp_w != 'string') { throw(temp_w); }
497                                 w = obj.html2txt(temp_w);
498                             } catch(E) {
499                                 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
500                                 w.getSelection().selectAllChildren(w.document.firstChild);
501                                 w = w.getSelection().toString();
502                             }
503                         }
504                         obj._NSPrint_custom_print(w,silent,params);
505                     break;    
506                     case 'window.print':
507                         var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
508                         var originalPrinter = false;
509                         if (prefs.prefHasUserValue('print.print_printer')) {
510                             // This is for restoring print.print_printer after any print dialog, so that when
511                             // window.print gets used again, it uses the configured printer for the right context
512                             // (which should only be default--window.print is a kludge and is in limited use),
513                             // rather than the printer last used.
514                             originalPrinter = prefs.getCharPref('print.print_printer');
515                         }
516                         if (typeof w == 'object') {
517                             w.print();
518                             if (originalPrinter) {
519                                 prefs.setCharPref('print.print_printer',originalPrinter);
520                             }
521                         } else {
522                             if (params.content_type == 'text/plain') {
523                                 w = window.open('data:text/plain;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
524                             } else {
525                                 w = window.open('data:text/html;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
526                             }
527                             setTimeout(
528                                 function() {
529                                     w.print();
530                                     if (originalPrinter) {
531                                         prefs.setCharPref('print.print_printer',originalPrinter);
532                                     }
533                                     setTimeout(
534                                         function() {
535                                             w.close(); 
536                                         }, 2000
537                                     );
538                                 }, 0
539                             );
540                         }
541                     break;    
542                     case 'webBrowserPrint':
543                     default:
544                         if (typeof w == 'object') {
545                             obj._NSPrint_webBrowserPrint(w,silent,params);
546                         } else {
547                             if (params.content_type == 'text/plain') {
548                                 w = window.open('data:text/plain;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
549                             } else {
550                                 w = window.open('data:text/html;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
551                             }
552                             setTimeout(
553                                 function() {
554                                     obj._NSPrint_webBrowserPrint(w,silent,params);
555                                     setTimeout(
556                                         function() {
557                                             w.close(); 
558                                         }, 2000
559                                     );
560                                 }, 0
561                             );
562                         }
563                     break;    
564                 }
565
566             } else {
567                 //w.print();
568                 obj._NSPrint_webBrowserPrint(w,silent,params);
569             }
570
571         } catch (e) {
572             alert('Probably not printing: ' + e);
573             this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
574         }
575
576     },
577
578     '_NSPrint_custom_print' : function(w,silent,params) {
579         var obj = this;
580         try {
581
582             var text = w;
583             var html = params.msg || w;
584
585             var txt_file = new util.file('receipt.txt');
586             txt_file.write_content('truncate',text); 
587             var text_path = '"' + txt_file._file.path + '"';
588             txt_file.close();
589
590             var html_file = new util.file('receipt.html');
591             html_file.write_content('truncate',html); 
592             var html_path = '"' + html_file._file.path + '"';
593             html_file.close();
594             
595             var cmd = params.dos_print ?
596                 'copy ' + text_path + ' lpt1 /b\n'
597                 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
598             ;
599
600             file = new util.file('receipt.bat');
601             file.write_content('truncate+exec',cmd);
602             file.close();
603             file = new util.file('receipt.bat');
604
605             dump('print exec: ' + cmd + '\n');
606             var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
607             process.init(file._file);
608
609             var args = [];
610
611             dump('process.run = ' + process.run(true, args, args.length) + '\n');
612
613             file.close();
614
615         } catch (e) {
616             //alert('Probably not printing: ' + e);
617             this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
618         }
619     },
620
621     '_NSPrint_webBrowserPrint' : function(w,silent,params) {
622         var obj = this;
623         try {
624             var webBrowserPrint = w
625                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
626                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
627             this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
628             if (webBrowserPrint) {
629                 var gPrintSettings = obj.GetPrintSettings();
630                 if (silent) gPrintSettings.printSilent = true;
631                 else gPrintSettings.printSilent = false;
632                 if (params) {
633                     if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
634                 }
635                 webBrowserPrint.print(gPrintSettings, null);
636                 this.error.sdump('D_PRINT','Should be printing\n');
637             } else {
638                 this.error.sdump('D_ERROR','Should not be printing\n');
639             }
640         } catch (e) {
641             //alert('Probably not printing: ' + e);
642             // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
643             // causing an exception to be thrown which we catch here.
644             // Unfortunately this will also consume helpful failures
645             this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
646         }
647     },
648
649     'GetPrintSettings' : function() {
650         try {
651             //alert('entering GetPrintSettings');
652             var pref = Components.classes["@mozilla.org/preferences-service;1"]
653                 .getService(Components.interfaces.nsIPrefBranch);
654             //alert('pref = ' + pref);
655             if (pref) {
656                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
657                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
658                 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + '  gSavePrintSettings = ' + this.gSavePrintSettings);
659             }
660  
661             var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
662                 .getService(Components.interfaces.nsIPrintSettingsService);
663             if (this.gPrintSettingsAreGlobal) {
664                 this.gPrintSettings = printService.globalPrintSettings;
665                 //alert('called setPrinterDefaultsForSelectedPrinter');
666                 this.setPrinterDefaultsForSelectedPrinter(printService);
667             } else {
668                 this.gPrintSettings = printService.newPrintSettings;
669                 //alert('used printService.newPrintSettings');
670             }
671         } catch (e) {
672             this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
673             //alert("GetPrintSettings() "+e+"\n");
674         }
675  
676         return this.gPrintSettings;
677     },
678
679     'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
680         try {
681             if (this.gPrintSettings.printerName == "") {
682                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
683                 //alert('used .defaultPrinterName');
684             }
685             //alert('printerName = ' + this.gPrintSettings.printerName);
686      
687             // First get any defaults from the printer 
688             aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
689      
690             // now augment them with any values from last time
691             aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
692
693             // now augment from our own saved settings if they exist
694             this.load_settings();
695
696         } catch(E) {
697             this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
698         }
699     },
700
701     'page_settings' : function() {
702         try {
703             this.GetPrintSettings();
704             var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
705             PO.ShowPrintSetupDialog(this.gPrintSettings);
706         } catch(E) {
707             this.error.standard_unexpected_error_alert("page_settings()",E);
708         }
709     },
710
711     'load_settings' : function() {
712         try {
713             var error_msg = '';
714             var file = new util.file('gPrintSettings.' + this.context);
715             if (file._file.exists()) {
716                 temp = file.get_object(); file.close();
717                 for (var i in temp) {
718                     try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
719                 }
720             }  else if (this.context != 'default') {
721                 var file = new util.file('gPrintSettings.default');
722                 if (file._file.exists()) {
723                     temp = file.get_object(); file.close();
724                     for (var i in temp) {
725                         try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
726                     }
727                 } else {
728                     this.gPrintSettings.marginTop = 0;
729                     this.gPrintSettings.marginLeft = 0;
730                     this.gPrintSettings.marginBottom = 0;
731                     this.gPrintSettings.marginRight = 0;
732                     this.gPrintSettings.headerStrLeft = '';
733                     this.gPrintSettings.headerStrCenter = '';
734                     this.gPrintSettings.headerStrRight = '';
735                     this.gPrintSettings.footerStrLeft = '';
736                     this.gPrintSettings.footerStrCenter = '';
737                     this.gPrintSettings.footerStrRight = '';
738                 }
739             }
740             if (error_msg) {
741                 this.error.sdump('D_PRINT',error_msg);
742                 this.error.yns_alert(
743                     document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
744                     document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
745                     document.getElementById('offlineStrings').getString('common.ok'),
746                     null,
747                     null,
748                     null
749                 );
750             }
751         } catch(E) {
752             this.error.standard_unexpected_error_alert("load_settings()",E);
753         }
754     },
755
756     'save_settings' : function() {
757         try {
758             var obj = this;
759             var file = new util.file('gPrintSettings.' + this.context);
760             if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
761             if (obj.gPrintSettings) file.set_object(obj.gPrintSettings); 
762             file.close();
763             if (this.context == 'default') {
764                 // print.print_printer gets used by bare window.print()'s.  We sometimes use window.print for the
765                 // WebBrowserPrint strategy to workaround bugs with the NSPrint xpcom, and only in the default context.
766                 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
767                 prefs.setCharPref('print.print_printer',obj.gPrintSettings.printerName);
768             }
769         } catch(E) {
770             this.error.standard_unexpected_error_alert("save_settings()",E);
771         }
772     }
773 }
774
775 dump('exiting util/print.js\n');