]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
offline printer role
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / data.js
1 dump('entering OpenILS/data.js\n');
2
3 if (typeof OpenILS == 'undefined') OpenILS = {};
4 OpenILS.data = function () {
5
6     try {
7         /* We're going to turn this guy into a singleton, at least for a given window, and look for it in xulG */
8         if (! window.xulG) { window.xulG = {}; }
9         if (window.xulG._data) { return window.xulG._data; }
10
11         JSAN.use('util.error'); this.error = new util.error();
12         JSAN.use('util.network'); this.network = new util.network();
13     } catch(E) {
14         alert(location.href + '\nError in OpenILS.data constructor: ' + E);
15         throw(E);
16     }
17
18     window.xulG._data = this;
19     return this;
20 }
21
22 OpenILS.data.prototype = {
23
24     'list' : {},
25     'hash' : {},
26     'tree' : {},
27
28     'temp' : '',
29
30     'data_progress' : function(msg) {
31         try {
32             var x = document.getElementById('data_progress');
33             if (x) {
34                 x.appendChild( document.createTextNode( msg ) );
35             }
36         } catch(E) {
37             this.error.sdump('D_ERROR',msg + '\n' + E);
38         }
39     },
40
41     'init' : function (params) {
42
43         try {
44             if (params && params.via == 'stash') {    
45                 this.stash_retrieve();
46             } else {
47                 this.network_retrieve();
48             }
49         
50         } catch(E) {
51             this.error.sdump('D_ERROR','Error in OpenILS.data.init('
52                 +js2JSON(params)+'): ' + js2JSON(E) );
53         }
54
55
56     },
57
58     // This should be invoked only once per application, in a persistant window
59     'init_observer_functions' : function() {
60         try {
61             var obj = this;                // OpenILS.data
62             obj.observers = {};            //
63             obj.observers.id = 1;        // Unique id for each observer function added
64             obj.observers.id2path = {}; // Lookup for full_path via observer id
65             obj.observers.cache = {};    // Observer funcs go in here
66
67             // For a given path, this executes all the registered observer funcs
68             obj.observers.dispatch = function(full_path, old_value, new_value) {
69                 obj.error.sdump('D_OBSERVERS', 'entering observers.dispatch\nfull_path = ' + full_path + '\nold_value = ' + js2JSON(old_value) + '\nnew_value = ' + js2JSON(new_value) + '\n');
70                 try {
71                     var path = full_path.split(/\./).pop();
72                     for (var i in obj.observers.cache[full_path]) {
73                         try {
74                             var o = obj.observers.cache[full_path][i];
75                             if (typeof o.func == 'function') o.func(path, old_value, new_value);
76                         } catch(E) {
77                             obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
78                         }
79                     }
80                 } catch(E) {
81                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.dispatch(): ' + js2JSON(E) );
82                 }
83             }
84
85             // This registers an observer function for a given path
86             obj.observers.add = function(full_path, func) {
87                 try {
88                     obj.error.sdump('D_OBSERVERS', 'entering observers.add\nfull_path = ' + full_path + '\nfunc = ' + func + '\n');
89                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
90                     const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
91                     var data_cache=new OpenILS( );
92                     var stash = data_cache.wrappedJSObject.OpenILS.prototype.data;
93
94                     var id = obj.observers.id++;
95                     if (typeof obj.observers.cache[ full_path ] == 'undefined') obj.observers.cache[ full_path ] = {};
96                     obj.observers.cache[ full_path ][ id ] = { 'func' : func, 'time_added' : new Date() };
97                     obj.observers.id2path[ id ] = [ full_path ];
98
99                     var path_list = full_path.split(/\./);
100                     var observed_prop = path_list.pop();
101
102                     // Convert soft path to object reference.  Error if any but the last node is undefined
103                     for (var i in path_list) stash = stash[ path_list[i] ];
104
105                     /*
106
107                     // experiment with storing only json in cache to avoid the [ ] -> { '0' : .., '1' : .. } bug
108
109                     if (stash[observed_prop] && getKeys( obj.observers.cache[ full_path ] ).length == 0) {
110                         stash['_' + observed_prop] = js2JSON(stash[observed_prop]);
111                     }
112
113                     stash.__defineSetter__(observed_prop, function(x) { this['_'+observed_prop] = js2JSON(x); });
114                     stash.__defineGetter__(observed_prop, function() { return JSON2js(this['_'+observed_prop]); });
115                     */
116
117                     stash.watch(
118                         observed_prop,
119                         function(p,old_value,new_value) {
120                             obj.observers.dispatch(full_path,old_value,new_value);
121                             return new_value;
122                         }
123                     );
124
125                     return id;
126                 } catch(E) {
127                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.add(): ' + js2JSON(E) );
128                 }
129             }
130
131             // This unregisters an observer function for a given observer id
132             obj.observers.remove = function(id) {
133                 try {
134                     obj.error.sdump('D_OBSERVERS', 'entering observers.remove\nid = ' + id + '\n');
135                     var path = obj.observers.id2path[ id ];
136                     delete obj.observers.cache[ path ][ id ];
137                     delete obj.observers.id2path[ id ];
138                 } catch(E) {
139                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.remove(): ' + js2JSON(E) );
140                 }
141             }
142
143             // This purges observer functions for a given path
144             obj.observers.purge = function(full_path) {
145                 obj.error.sdump('D_OBSERVERS', 'entering observers.purge\nfull_path = ' + full_path + '\n');
146                 try {
147                     var remove_these = [];
148                     for (var id in obj.observers.cache[ full_path ]) remove_these.push( id );
149                     for (var id in remove_these) delete obj.observers.id2path[ id ];
150                     delete obj.observers.cache[ full_path ];
151                 } catch(E) {
152                     obj.error.sdump('D_ERROR','Error in OpenILS.data.observers.purge(): ' + js2JSON(E) );
153                 }
154             }
155
156             obj.stash('observers'); // make this accessible globally
157
158         } catch(E) {
159             this.error.sdump('D_ERROR','Error in OpenILS.data.init_observer_functions(): ' + js2JSON(E) );
160         }
161     },
162
163     'stash' : function () {
164         try {
165             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
166             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
167             var data_cache=new OpenILS( );
168             for (var i = 0; i < arguments.length; i++) {
169                 try {
170                     if (arguments[i] != 'hash' && arguments[i] != 'list') this.error.sdump('D_DATA_STASH','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + (typeof this[arguments[i]] == 'object' ? ' = ' + (this[arguments[i]]) : '') + '\n');
171                 } catch(F) { alert(F); }
172                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
173             }
174         } catch(E) {
175             this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
176         }
177     },
178
179     'lookup' : function(key,value) {
180         try {
181             var obj = this; var found;
182             if (obj.hash[key] && obj.hash[key][value]) return obj.hash[key][value];
183             switch(key) {
184                 case 'acpl': 
185                     found = obj.network.simple_request('FM_ACPL_RETRIEVE_VIA_ID.authoritative',[ value ]);
186                 break;
187                 case 'actsc':
188                     found = obj.network.simple_request('FM_ACTSC_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
189                 break;
190                 default: return undefined; break;
191             }
192             if (typeof found.ilsevent != 'undefined') throw(found);
193             if (!obj.hash[key]) obj.hash[key] = {};
194             obj.hash[key][value] = found; obj.list[key].push( found ); obj.stash('hash','list');
195             return found;
196         } catch(E) {
197             this.error.sdump('D_ERROR','Error in OpenILS.data.lookup('+key+','+value+'): ' + js2JSON(E) );
198             return undefined;
199         }
200     },
201
202     '_debug_stash' : function() {
203         try {
204             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
205             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
206             var data_cache=new OpenILS( );
207             for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
208                 dump('_debug_stash ' + i + '\n');
209             }
210         } catch(E) {
211             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
212         }
213     },
214
215     '_fm_objects' : {
216
217         'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
218         'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
219         'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [], true ],
220         /*
221         'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
222         */
223         /*
224         'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
225         */
226         'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
227         'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
228         'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
229         'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ]
230     },
231
232     'stash_retrieve' : function() {
233         try {
234             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
235             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
236             var data_cache=new OpenILS( );
237             var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
238             for (var i in dc) {
239                 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
240                 this[i] = dc[i];
241             }
242         } catch(E) {
243             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
244         }
245     },
246
247     'load_saved_print_templates' : function() {
248         var obj = this;
249         try {
250             JSAN.use('util.file'); var file = new util.file('print_list_templates');
251             if (file._file.exists()) {
252                 try {
253                     var x = file.get_object();
254                     if (x) {
255                         for (var i in x) {
256                             obj.print_list_templates[i] = x[i];
257                         }
258                         obj.stash('print_list_templates');
259                         obj.data_progress('Saved print templates retrieved from file. ');
260                     }
261                 } catch(E) {
262                     alert(E);
263                 }
264             }
265             file.close();
266         } catch(E) {
267             alert("Error in OpenILS.data, load_saved_print_templates(): " + E);
268         }
269     },
270
271     'fetch_print_strategy' : function() {
272         var obj = this;
273         try {
274             obj.print_strategy = {};
275             var print_contexts = [ 'default', 'receipt', 'label', 'mail', 'offline' ];
276             for (var i in print_contexts) {
277                 JSAN.use('util.file'); var file = new util.file('print_strategy.' + print_contexts[i]);
278                 if (file._file.exists()) {
279                     try {
280                         var x = file.get_content();
281                         if (x) {
282                             obj.print_strategy[ print_contexts[i] ] = x;
283                             obj.data_progress('Print strategy ' + print_contexts[i] + ' retrieved from file. ');
284                         }
285                     } catch(E) {
286                         alert(E);
287                     }
288                 }
289                 file.close();
290             }
291             obj.stash('print_strategy');
292         } catch(E) {
293             alert('Error in OpenILS.data, fetch_print_strategy(): ' + E);
294         }
295     },
296
297     'print_list_defaults' : function() {
298         var obj = this;
299         //if (typeof obj.print_list_templates == 'undefined') {
300         {
301             obj.print_list_types = [ 
302                 'offline_checkout', 
303                 'offline_checkin', 
304                 'offline_renew', 
305                 'offline_inhouse_use', 
306                 'items', 
307                 'bills', 
308                 'payment', 
309                 'holds', 
310                 /* 'patrons' */
311             ];
312             obj.print_list_templates = { 
313                 'item_status' : {
314                     'type' : 'items',
315                     'header' : 'The following items have been examined:<hr/><ol>',
316                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
317                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
318                 }, 
319                 'transit_list' : {
320                     'type' : 'transits',
321                     'header' : 'Transits:<hr/><ol>',
322                     'line_item' : '<li>From: %transit_source% To: %transit_dest_lib%<br/>\r\nWhen: %transit_source_send_time%<br />\r\nBarcode: %transit_item_barcode% Title: %transit_item_title%<br/>\r\n',
323                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
324                 }, 
325                 'items_out' : {
326                     'type' : 'items',
327                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
328                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
329                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
330                 }, 
331                 'renew' : {
332                     'type' : 'items',
333                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have renewed the following items:<hr/><ol>',
334                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
335                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
336                 }, 
337                 'checkout' : {
338                     'type' : 'items',
339                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
340                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
341                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
342                 }, 
343                 'offline_checkout' : {
344                     'type' : 'offline_checkout',
345                     'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
346                     'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
347                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
348                 },
349                 'checkin' : {
350                     'type' : 'items',
351                     'header' : 'You checked in the following items:<hr/><ol>',
352                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
353                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
354                 }, 
355                 'bill_payment' : {
356                     'type' : 'payment',
357                     'header' : 'Welcome to %LIBRARY%!<br/>A receipt of your  transaction:<hr/> <table width="100%"> <tr> <td>Original Balance:</td> <td align="right">$%original_balance%</td> </tr> <tr> <td>Payment Method:</td> <td align="right">%payment_type%</td> </tr> <tr> <td>Payment Received:</td> <td align="right">$%payment_received%</td> </tr> <tr> <td>Payment Applied:</td> <td align="right">$%payment_applied%</td> </tr> <tr> <td>Billings Voided:</td> <td align="right">%voided_balance%</td> </tr> <tr> <td>Change Given:</td> <td align="right">$%change_given%</td> </tr> <tr> <td>New Balance:</td> <td align="right">$%new_balance%</td> </tr> </table> <p> Note: %note% </p> <p> Specific bills: <blockquote>',
358                     'line_item' : 'Bill #%bill_id%  %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
359                     'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> '
360                 },
361                 'bills_historical' : {
362                     'type' : 'bills',
363                     'header' : 'Welcome to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
364                     'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
365                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
366                 }, 
367                 'bills_current' : {
368                     'type' : 'bills',
369                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
370                     'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
371                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
372                 },
373                 'bills_main_view' : {
374                     'type' : 'bills',
375                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
376                     'line_item' : '<dt><b>Bill #%id%</b> %title%</dt> <dd> <table width="100%"> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
377                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
378                 },
379                 'offline_checkin' : {
380                     'type' : 'offline_checkin',
381                     'header' : 'You checked in the following items:<hr/><ol>',
382                     'line_item' : '<li>Barcode: %barcode%\r\n',
383                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
384                 },
385                 'offline_renew' : {
386                     'type' : 'offline_renew',
387                     'header' : 'You renewed the following items:<hr/><ol>',
388                     'line_item' : '<li>Barcode: %barcode%\r\n',
389                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
390                 },
391                 'offline_inhouse_use' : {
392                     'type' : 'offline_inhouse_use',
393                     'header' : 'You marked the following in-house items used:<hr/><ol>',
394                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
395                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
396                 },
397                 'in_house_use' : {
398                     'type' : 'items',
399                     'header' : 'You marked the following in-house items used:<hr/><ol>',
400                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
401                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
402                 },
403                 'holds' : {
404                     'type' : 'holds',
405                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
406                     'line_item' : '<li>%title%\r\n',
407                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
408                 },
409                 'hold_slip' : {
410                     'type' : 'holds',
411                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
412                     'line_item' : '%formatted_note%<br/>\r\n',
413                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
414                 },
415                 'transit_slip' : {
416                     'type' : 'transits',
417                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\n',
418                     'line_item' : 'Barcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n',
419                     'footer' : 'Slip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
420                 },
421                 'hold_transit_slip' : {
422                     'type' : 'transits',
423                     'header' : 'This item needs to be routed to <b>%route_to%</b>:<br/>\r\n%route_to_org_fullname%<br/>\r\n%street1%<br/>\r\n%street2%<br/>\r\n%city_state_zip%<br/>\r\n<br/>\r\nBarcode: %item_barcode%<br/>\r\nTitle: %item_title%<br/>\r\nAuthor: %item_author%<br>\r\n<br/>\r\n%hold_for_msg%<br/>\r\nBarcode: %PATRON_BARCODE%<br/>\r\nNotify by phone: %notify_by_phone%<br/>\r\nNotify by email: %notify_by_email%<br/>\r\n',
424                     'line_item' : '%formatted_note%<br/>\r\n',
425                     'footer' : '<br/>\r\nRequest date: %request_date%<br/>\r\nSlip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
426                 }
427             }; 
428
429             obj.stash( 'print_list_templates', 'print_list_types' );
430         }
431     },
432
433     'network_retrieve' : function() {
434         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
435         var obj = this;
436
437         JSAN.use('util.file'); var file = new util.file('global_font_adjust');
438         if (file._file.exists()) {
439             try {
440                 var x = file.get_object();
441                 if (x) {
442                     obj.global_font_adjust = x;
443                     obj.stash('global_font_adjust');
444                     obj.data_progress('Saved font settings retrieved from file. ');
445                 }
446             } catch(E) {
447                 alert(E);
448             }
449         }
450         file.close();
451
452         JSAN.use('util.file'); var file = new util.file('no_sound');
453         if (file._file.exists()) {
454             try {
455                 var x = file.get_content();
456                 if (x) {
457                     obj.no_sound = x;
458                     obj.stash('no_sound');
459                     obj.data_progress('Saved sound settings retrieved from file. ');
460                 }
461             } catch(E) {
462                 alert(E);
463             }
464         }
465         file.close();
466
467         obj.print_list_defaults();
468         obj.data_progress('Default print templates set. ');
469         obj.load_saved_print_templates();
470         obj.fetch_print_strategy();
471         JSAN.use('util.print'); (new util.print()).GetPrintSettings();
472         obj.data_progress('Printer settings retrieved. ');
473
474         JSAN.use('util.functional');
475         JSAN.use('util.fm_utils');
476
477         function gen_fm_retrieval_func(classname,data) {
478             var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
479             return function () {
480                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
481
482                 function convert() {
483                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
484                     try {
485                         if (obj.list[classname].constructor.name == 'Array') {
486                             obj.hash[classname] = 
487                                 util.functional.convert_object_list_to_hash(
488                                     obj.list[classname]
489                                 );
490                         }
491                     } catch(E) {
492
493                         obj.error.sdump('D_ERROR',E + '\n');
494                     }
495
496                 }
497
498                 try {
499                     var level = obj.error.sdump_levels.D_SES_RESULT;
500                     if (classname == 'aou' || classname == 'my_aou')
501                         obj.error.sdump_levels.D_SES_RESULT = false;
502                     var robj = obj.network.request( app, method, params);
503                     if (robj != null && typeof robj.ilsevent != 'undefined') {
504                         obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
505                         throw(robj);
506                     }
507                     obj.list[classname] = robj == null ? [] : robj;
508                     obj.error.sdump_levels.D_SES_RESULT = level;
509                     convert();
510                     obj.data_progress('Retrieved list for ' + classname + ' objects. ');
511
512                 } catch(E) {
513                     // if cacheable, try offline
514                     if (cacheable) {
515                         /* FIXME -- we're going to revisit caching and do it differently
516                         try {
517                             var file = new util.file( classname );
518                             obj.list[classname] = file.get_object(); file.close();
519                             convert();
520                         } catch(E) {
521                             throw(E);
522                         }
523                         */
524                         throw(E); // for now
525                     } else {
526                         throw(E); // for now
527                     }
528                 }
529             }
530         }
531
532         this.chain = [];
533
534         this.chain.push(
535             function() {
536                 try {
537                     var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[{'full':true}]);
538                     if (typeof robj.ilsevent != 'undefined') throw(robj);
539                     obj.list.ccm = robj == null ? [] : robj;
540                     obj.hash.ccm = util.functional.convert_object_list_to_hash( obj.list.ccm );
541                     obj.list.circ_modifier = util.functional.map_list( obj.list.ccm, function(o) { return o.code(); } );
542                     obj.data_progress('Retrieved circ modifier list. ');
543                 } catch(E) {
544                     var error = 'Error: ' + js2JSON(E);
545                     obj.error.sdump('D_ERROR',error);
546                     throw(E);
547                 }
548             }
549         );
550
551         this.chain.push(
552             function() {
553                 var f = gen_fm_retrieval_func(
554                     'cnal',
555                     [
556                         api.FM_CNAL_RETRIEVE.app,
557                         api.FM_CNAL_RETRIEVE.method,
558                         [ obj.session.key ],
559                         false
560                     ]
561                 );
562                 try {
563                     f();
564                 } catch(E) {
565                     var error = 'Error: ' + js2JSON(E);
566                     obj.error.sdump('D_ERROR',error);
567                     throw(E);
568                 }
569             }
570         );
571
572         this.chain.push(
573             function() {
574                 var f = gen_fm_retrieval_func(
575                     'csp',
576                     [
577                         api.FM_CSP_PCRUD_SEARCH.app,
578                         api.FM_CSP_PCRUD_SEARCH.method,
579                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
580                         false
581                     ]
582                 );
583                 try {
584                     f();
585                 } catch(E) {
586                     var error = 'Error: ' + js2JSON(E);
587                     obj.error.sdump('D_ERROR',error);
588                     throw(E);
589                 }
590             }
591         );
592
593         this.chain.push(
594             function() {
595                 var f = gen_fm_retrieval_func(
596                     'ahrcc',
597                     [
598                         api.FM_AHRCC_PCRUD_SEARCH.app,
599                         api.FM_AHRCC_PCRUD_SEARCH.method,
600                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
601                         false
602                     ]
603                 );
604                 try {
605                     f();
606                 } catch(E) {
607                     var error = 'Error: ' + js2JSON(E);
608                     obj.error.sdump('D_ERROR',error);
609                     throw(E);
610                 }
611             }
612         );
613
614
615         this.chain.push(
616             function() {
617                 var f = gen_fm_retrieval_func(
618                     'au',
619                     [
620                         api.FM_AU_RETRIEVE_VIA_SESSION.app,
621                         api.FM_AU_RETRIEVE_VIA_SESSION.method,
622                         [ obj.session.key ],
623                         false
624                     ]
625                 );
626                 try {
627                     f();
628                 } catch(E) {
629                     var error = 'Error: ' + js2JSON(E);
630                     obj.error.sdump('D_ERROR',error);
631                     throw(E);
632                 }
633                 obj.list.au = [ obj.list.au ];
634             }
635         );
636
637         this.chain.push(
638             function() {
639                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
640                 var f = gen_fm_retrieval_func(
641                     'my_asv',
642                     [
643                         api.FM_ASV_RETRIEVE_REQUIRED.app,
644                         api.FM_ASV_RETRIEVE_REQUIRED.method,
645                         [ obj.session.key ],
646                         true
647                     ]
648                 );
649                 try {
650                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
651                     f();
652                 } catch(E) {
653                     var error = 'Error: ' + js2JSON(E);
654                     obj.error.sdump('D_ERROR',error);
655                     throw(E);
656                 }
657             }
658         );
659
660         this.chain.push(
661             function() {
662                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
663                 var f = gen_fm_retrieval_func(
664                     'asv',
665                     [
666                         api.FM_ASV_RETRIEVE.app,
667                         api.FM_ASV_RETRIEVE.method,
668                         [ obj.session.key ],
669                         true
670                     ]
671                 );
672                 try {
673                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
674                     f();
675                 } catch(E) {
676                     var error = 'Error: ' + js2JSON(E);
677                     obj.error.sdump('D_ERROR',error);
678                     throw(E);
679                 }
680             }
681         );
682
683         obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
684
685         for (var i in this._fm_objects) {
686             this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
687         }
688
689         // The previous org_tree call returned a tree, not a list or hash.
690         this.chain.push(
691             function () {
692                 obj.tree.aou = obj.list.aou;
693                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
694                 for (var i = 0; i < obj.list.aou.length; i++) {
695                     var c = obj.list.aou[i].children();
696                     if (!c) c = [];
697                     c = c.sort(
698                         function( a, b ) {
699                             if (a.shortname() < b.shortname()) return -1;
700                             if (a.shortname() > b.shortname()) return 1;
701                             return 0;
702                         }
703                     );
704                     obj.list.aou[i].children( c );
705                 }
706                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
707                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
708             }
709         );
710
711         // The previous pgt call returned a tree, not a list or hash.
712         this.chain.push(
713             function () {
714                 obj.tree.pgt = obj.list.pgt;
715                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
716                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
717             }
718         );
719
720         // Do these after we get the user object
721
722         this.chain.push(
723             function() {
724                 try {
725                     var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
726                     if (typeof robj.ilsevent != 'undefined') throw(robj);
727                     obj.hash.aous = robj;
728                     obj.data_progress('Retrieved org unit settings. ');
729                 } catch(E) {
730                     var error = 'Error: ' + js2JSON(E);
731                     obj.error.sdump('D_ERROR',error);
732                     throw(E);
733                 }
734             }
735         );
736
737         this.chain.push(
738
739             function() {
740
741                 gen_fm_retrieval_func('my_aou', 
742                     [ 
743                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
744                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
745                         [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
746                         true
747                     ]
748                 )();
749             }
750         );
751
752         this.chain.push(
753
754             function () {
755
756                 gen_fm_retrieval_func( 'my_actsc', 
757                     [ 
758                         api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
759                         api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
760                         [ obj.session.key, obj.list.au[0].ws_ou() ],
761                         true
762                     ]
763                 )();
764             }
765         );
766
767         this.chain.push(
768
769             function () {
770
771                 gen_fm_retrieval_func( 'my_asc', 
772                     [ 
773                         api.FM_ASC_RETRIEVE_VIA_AOU.app,
774                         api.FM_ASC_RETRIEVE_VIA_AOU.method,
775                         [ obj.session.key, obj.list.au[0].ws_ou() ],
776                         true
777                     ]
778                 )();
779             }
780         );
781
782
783         this.chain.push(
784             function() {
785                 var f = gen_fm_retrieval_func(
786                     'cnct',
787                     [
788                         api.FM_CNCT_RETRIEVE.app,
789                         api.FM_CNCT_RETRIEVE.method,
790                         [ obj.list.au[0].ws_ou() ], 
791                         false
792                     ]
793                 );
794                 try {
795                     f();
796                 } catch(E) {
797                     var error = 'Error: ' + js2JSON(E);
798                     obj.error.sdump('D_ERROR',error);
799                     throw(E);
800                 }
801             }
802         );
803
804         this.chain.push(
805             function() {
806                 var f = gen_fm_retrieval_func(
807                     'my_cnct',
808                     [
809                         api.FM_CNCT_RETRIEVE.app,
810                         api.FM_CNCT_RETRIEVE.method,
811                         [ obj.list.au[0].ws_ou() ], 
812                         false
813                     ]
814                 );
815                 try {
816                     f();
817                 } catch(E) {
818                     var error = 'Error: ' + js2JSON(E);
819                     obj.error.sdump('D_ERROR',error);
820                     throw(E);
821                 }
822             }
823         );
824
825
826         this.chain.push(
827             function() {
828                 var f = gen_fm_retrieval_func(
829                     'acpl',
830                     [
831                         api.FM_ACPL_RETRIEVE.app,
832                         api.FM_ACPL_RETRIEVE.method,
833                         [ obj.list.au[0].ws_ou() ],
834                         false
835                     ]
836                 );
837                 try {
838                     f();
839                 } catch(E) {
840                     var error = 'Error: ' + js2JSON(E);
841                     obj.error.sdump('D_ERROR',error);
842                     throw(E);
843                 }
844             }
845         );
846
847         this.chain.push(
848             function() {
849                 var f = gen_fm_retrieval_func(
850                     'cbt',
851                     [
852                         api.FM_CBT_RETRIEVE.app,
853                         api.FM_CBT_RETRIEVE.method,
854                         [ obj.session.key, obj.list.au[0].ws_ou() ],
855                         false
856                     ]
857                 );
858                 try {
859                     f();
860                 } catch(E) {
861                     var error = 'Error: ' + js2JSON(E);
862                     obj.error.sdump('D_ERROR',error);
863                     throw(E);
864                 }
865             }
866         );
867
868         if (typeof this.on_complete == 'function') {
869
870             this.chain.push( this.on_complete );
871         }
872         JSAN.use('util.exec'); this.exec = new util.exec();
873         this.exec.on_error = function(E) { 
874         
875             if (typeof obj.on_error == 'function') {
876                 return obj.on_error(E); /* false breaks chain */
877             } else {
878                 alert('oops: ' + E ); 
879                 return false; /* break chain */
880             }
881
882         }
883
884         this.exec.chain( this.chain );
885
886     }
887 }
888
889 dump('exiting OpenILS/data.js\n');