]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
Fix Circulate as Type in the item editor.
[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 'acnp':
185                     found = obj.network.simple_request('FM_ACNP_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
186                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
187                     found = found[0];
188                 break;
189                 case 'acns':
190                     found = obj.network.simple_request('FM_ACNS_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
191                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
192                     found = found[0];
193                 break;
194                 case 'acpl': 
195                     found = obj.network.simple_request('FM_ACPL_RETRIEVE_VIA_ID.authoritative',[ value ]);
196                 break;
197                 case 'actsc':
198                     found = obj.network.simple_request('FM_ACTSC_RETRIEVE_VIA_PCRUD',[ ses(), { 'id' : { '=' : value } }]);
199                     if (typeof found.ilsevent != 'undefined') throw(js2JSON(found));
200                     found = found[0];
201                 break;
202                 default: return undefined; break;
203             }
204             if (typeof found.ilsevent != 'undefined') throw(found);
205             if (!obj.hash[key]) obj.hash[key] = {};
206             obj.hash[key][value] = found;
207             if (!obj.list[key]) obj.list[key] = [];
208             obj.list[key].push( found );
209             obj.stash('hash','list');
210             return found;
211         } catch(E) {
212             alert('Error in OpenILS.data.lookup('+key+','+value+'): ' + E );
213             return undefined;
214         }
215     },
216
217     '_debug_stash' : function() {
218         try {
219             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
220             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
221             var data_cache=new OpenILS( );
222             for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
223                 dump('_debug_stash ' + i + '\n');
224             }
225         } catch(E) {
226             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
227         }
228     },
229
230     '_fm_objects' : {
231
232         'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
233         'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
234         'citm' : [ api.FM_CITM_RETRIEVE.app, api.FM_CITM_RETRIEVE.method, [{'query':{'ctype' : 'item_type'}}], true ],
235         /*
236         'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
237         */
238         /*
239         'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
240         */
241         'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
242         'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
243         'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ],
244         'crahp' : [ api.FM_CRAHP_RETRIEVE.app, api.FM_CRAHP_RETRIEVE.method, [], true ]
245     },
246
247     'stash_retrieve' : function() {
248         try {
249             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
250             const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
251             var data_cache=new OpenILS( );
252             var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
253             for (var i in dc) {
254                 this.error.sdump('D_DATA_RETRIEVE','Retrieving ' + i + ' : ' + dc[i] + '\n');
255                 this[i] = dc[i];
256             }
257         } catch(E) {
258             this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
259         }
260     },
261
262     'load_saved_print_templates' : function() {
263         var obj = this;
264         try {
265             JSAN.use('util.file'); var file = new util.file('print_list_templates');
266             if (file._file.exists()) {
267                 try {
268                     var x = file.get_object();
269                     if (x) {
270                         for (var i in x) {
271                             obj.print_list_templates[i] = x[i];
272                         }
273                         obj.stash('print_list_templates');
274                         obj.data_progress('Saved print templates retrieved from file. ');
275                     }
276                 } catch(E) {
277                     alert(E);
278                 }
279             }
280             file.close();
281         } catch(E) {
282             alert("Error in OpenILS.data, load_saved_print_templates(): " + E);
283         }
284     },
285
286     'fetch_print_strategy' : function() {
287         var obj = this;
288         try {
289             obj.print_strategy = {};
290             var print_contexts = [ 'default', 'receipt', 'label', 'mail', 'offline' ];
291             for (var i in print_contexts) {
292                 JSAN.use('util.file'); var file = new util.file('print_strategy.' + print_contexts[i]);
293                 if (file._file.exists()) {
294                     try {
295                         var x = file.get_content();
296                         if (x) {
297                             obj.print_strategy[ print_contexts[i] ] = x;
298                             obj.data_progress('Print strategy ' + print_contexts[i] + ' retrieved from file. ');
299                         }
300                     } catch(E) {
301                         alert(E);
302                     }
303                 }
304                 file.close();
305             }
306             obj.stash('print_strategy');
307         } catch(E) {
308             alert('Error in OpenILS.data, fetch_print_strategy(): ' + E);
309         }
310     },
311
312     'print_list_defaults' : function() {
313         var obj = this;
314         //if (typeof obj.print_list_templates == 'undefined') {
315         {
316             obj.print_list_types = [ 
317                 'offline_checkout', 
318                 'offline_checkin', 
319                 'offline_renew', 
320                 'offline_inhouse_use', 
321                 'items', 
322                 'bills', 
323                 'payment', 
324                 'holds', 
325                 /* 'patrons' */
326             ];
327             obj.print_list_templates = { 
328                 'item_status' : {
329                     'type' : 'items',
330                     'header' : 'The following items have been examined:<hr/><ol>',
331                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
332                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
333                 }, 
334                 'transit_list' : {
335                     'type' : 'transits',
336                     'header' : 'Transits:<hr/><ol>',
337                     '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',
338                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
339                 }, 
340                 'items_out' : {
341                     'type' : 'items',
342                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
343                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
344                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
345                 }, 
346                 'renew' : {
347                     'type' : 'items',
348                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have renewed the following items:<hr/><ol>',
349                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
350                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
351                 }, 
352                 'checkout' : {
353                     'type' : 'items',
354                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
355                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
356                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
357                 }, 
358                 'offline_checkout' : {
359                     'type' : 'offline_checkout',
360                     'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
361                     'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
362                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
363                 },
364                 'checkin' : {
365                     'type' : 'items',
366                     'header' : 'You checked in the following items:<hr/><ol>',
367                     'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
368                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
369                 }, 
370                 'bill_payment' : {
371                     'type' : 'payment',
372                     '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>',
373                     'line_item' : 'Bill #%bill_id%  %last_billing_type% Received: $%payment%<br />%barcode% %title%<br /><br />',
374                     'footer' : '</blockquote> </p> <hr />%SHORTNAME% %TODAY_TRIM%<br/> <br/> '
375                 },
376                 'bills_historical' : {
377                     'type' : 'bills',
378                     'header' : 'Welcome to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
379                     'line_item' : '<dt><b>Bill #%mbts_id%</b> %title% </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/>',
380                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
381                 }, 
382                 'bills_current' : {
383                     'type' : 'bills',
384                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
385                     '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/>',
386                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
387                 },
388                 'bills_main_view' : {
389                     'type' : 'bills',
390                     'header' : 'Welcome to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
391                     '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/>',
392                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\n<br/>\r\n'
393                 },
394                 'offline_checkin' : {
395                     'type' : 'offline_checkin',
396                     'header' : 'You checked in the following items:<hr/><ol>',
397                     'line_item' : '<li>Barcode: %barcode%\r\n',
398                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
399                 },
400                 'offline_renew' : {
401                     'type' : 'offline_renew',
402                     'header' : 'You renewed the following items:<hr/><ol>',
403                     'line_item' : '<li>Barcode: %barcode%\r\n',
404                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
405                 },
406                 'offline_inhouse_use' : {
407                     'type' : 'offline_inhouse_use',
408                     'header' : 'You marked the following in-house items used:<hr/><ol>',
409                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
410                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
411                 },
412                 'in_house_use' : {
413                     'type' : 'items',
414                     'header' : 'You marked the following in-house items used:<hr/><ol>',
415                     'line_item' : '<li>Barcode: %barcode%\r\nUses: %uses%\r\n<br />%alert_message%',
416                     'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n'
417                 },
418                 'holds' : {
419                     'type' : 'holds',
420                     'header' : 'Welcome to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
421                     'line_item' : '<li>%title%\r\n',
422                     'footer' : '</ol><hr />%SHORTNAME% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME%<br/>\r\n<br/>\r\n'
423                 },
424                 'hold_slip' : {
425                     'type' : 'holds',
426                     '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',
427                     'line_item' : '%formatted_note%<br/>\r\n',
428                     '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'
429                 },
430                 'transit_slip' : {
431                     'type' : 'transits',
432                     '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',
433                     'line_item' : '',
434                     'footer' : 'Slip Date: %TODAY_TRIM%<br/>\r\nPrinted by %STAFF_FIRSTNAME% at %SHORTNAME%<br/>\r\n<br/>\r\n'
435                 },
436                 'hold_transit_slip' : {
437                     'type' : 'transits',
438                     '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',
439                     'line_item' : '%formatted_note%<br/>\r\n',
440                     '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'
441                 }
442             }; 
443
444             obj.stash( 'print_list_templates', 'print_list_types' );
445         }
446     },
447
448     'network_retrieve' : function() {
449         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
450         var obj = this;
451
452         JSAN.use('util.file'); var file = new util.file('global_font_adjust');
453         if (file._file.exists()) {
454             try {
455                 var x = file.get_object();
456                 if (x) {
457                     obj.global_font_adjust = x;
458                     obj.stash('global_font_adjust');
459                     obj.data_progress('Saved font settings retrieved from file. ');
460                 }
461             } catch(E) {
462                 alert(E);
463             }
464         }
465         file.close();
466
467         JSAN.use('util.file'); var file = new util.file('no_sound');
468         if (file._file.exists()) {
469             try {
470                 var x = file.get_content();
471                 if (x) {
472                     obj.no_sound = x;
473                     obj.stash('no_sound');
474                     obj.data_progress('Saved sound settings retrieved from file. ');
475                 }
476             } catch(E) {
477                 alert(E);
478             }
479         }
480         file.close();
481
482         obj.print_list_defaults();
483         obj.data_progress('Default print templates set. ');
484         obj.load_saved_print_templates();
485         obj.fetch_print_strategy();
486         JSAN.use('util.print'); (new util.print()).GetPrintSettings();
487         obj.data_progress('Printer settings retrieved. ');
488
489         JSAN.use('util.functional');
490         JSAN.use('util.fm_utils');
491
492         function gen_fm_retrieval_func(classname,data) {
493             var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
494             return function () {
495                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
496
497                 function convert() {
498                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
499                     try {
500                         if (obj.list[classname].constructor.name == 'Array') {
501                             obj.hash[classname] = 
502                                 util.functional.convert_object_list_to_hash(
503                                     obj.list[classname],
504                                     classname == 'citm' ? 'code' : null
505                                 );
506                         }
507                     } catch(E) {
508
509                         obj.error.sdump('D_ERROR',E + '\n');
510                     }
511
512                 }
513
514                 try {
515                     var level = obj.error.sdump_levels.D_SES_RESULT;
516                     if (classname == 'aou' || classname == 'my_aou')
517                         obj.error.sdump_levels.D_SES_RESULT = false;
518                     var robj = obj.network.request( app, method, params);
519                     if (robj != null && typeof robj.ilsevent != 'undefined') {
520                         obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
521                         throw(robj);
522                     }
523                     obj.list[classname] = robj == null ? [] : robj;
524                     obj.error.sdump_levels.D_SES_RESULT = level;
525                     convert();
526                     obj.data_progress('Retrieved list for ' + classname + ' objects. ');
527
528                 } catch(E) {
529                     // if cacheable, try offline
530                     if (cacheable) {
531                         /* FIXME -- we're going to revisit caching and do it differently
532                         try {
533                             var file = new util.file( classname );
534                             obj.list[classname] = file.get_object(); file.close();
535                             convert();
536                         } catch(E) {
537                             throw(E);
538                         }
539                         */
540                         throw(E); // for now
541                     } else {
542                         throw(E); // for now
543                     }
544                 }
545             }
546         }
547
548         // If we don't clear these, then things like obj.list['acnp_for_lib_1'] may stick around
549         obj.hash = {}; obj.list = {};
550
551         this.chain = [];
552
553         this.chain.push(
554             function() {
555                 try {
556                     var robj = obj.network.simple_request('CIRC_MODIFIER_LIST',[{'full':true}]);
557                     if (typeof robj.ilsevent != 'undefined') throw(robj);
558                     obj.list.ccm = robj == null ? [] : robj;
559                     obj.hash.ccm = util.functional.convert_object_list_to_hash( obj.list.ccm );
560                     obj.list.circ_modifier = util.functional.map_list( obj.list.ccm, function(o) { return o.code(); } );
561                     obj.data_progress('Retrieved circ modifier list. ');
562                 } catch(E) {
563                     var error = 'Error: ' + js2JSON(E);
564                     obj.error.sdump('D_ERROR',error);
565                     throw(E);
566                 }
567             }
568         );
569
570         this.chain.push(
571             function() {
572                 var f = gen_fm_retrieval_func(
573                     'cnal',
574                     [
575                         api.FM_CNAL_RETRIEVE.app,
576                         api.FM_CNAL_RETRIEVE.method,
577                         [ obj.session.key ],
578                         false
579                     ]
580                 );
581                 try {
582                     f();
583                 } catch(E) {
584                     var error = 'Error: ' + js2JSON(E);
585                     obj.error.sdump('D_ERROR',error);
586                     throw(E);
587                 }
588             }
589         );
590
591         this.chain.push(
592             function() {
593                 var f = gen_fm_retrieval_func(
594                     'bpt',
595                     [
596                         api.FM_BPT_PCRUD_SEARCH.app,
597                         api.FM_BPT_PCRUD_SEARCH.method,
598                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"bpt":"id"}} ],
599                         false
600                     ]
601                 );
602                 try {
603                     f();
604                 } catch(E) {
605                     var error = 'Error: ' + js2JSON(E);
606                     obj.error.sdump('D_ERROR',error);
607                     throw(E);
608                 }
609             }
610         );
611
612         this.chain.push(
613             function() {
614                 var f = gen_fm_retrieval_func(
615                     'csp',
616                     [
617                         api.FM_CSP_PCRUD_SEARCH.app,
618                         api.FM_CSP_PCRUD_SEARCH.method,
619                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"csp":"id"}} ],
620                         false
621                     ]
622                 );
623                 try {
624                     f();
625                 } catch(E) {
626                     var error = 'Error: ' + js2JSON(E);
627                     obj.error.sdump('D_ERROR',error);
628                     throw(E);
629                 }
630             }
631         );
632
633         this.chain.push(
634             function() {
635                 var f = gen_fm_retrieval_func(
636                     'acnc',
637                     [
638                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.app,
639                         api.FM_ACNC_RETRIEVE_VIA_PCRUD.method,
640                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"acnc":"name"}} ],
641                         false
642                     ]
643                 );
644                 try {
645                     f();
646                 } catch(E) {
647                     var error = 'Error: ' + js2JSON(E);
648                     obj.error.sdump('D_ERROR',error);
649                     throw(E);
650                 }
651             }
652         );
653
654         this.chain.push(
655             function() {
656                 var f = gen_fm_retrieval_func(
657                     'ahrcc',
658                     [
659                         api.FM_AHRCC_PCRUD_SEARCH.app,
660                         api.FM_AHRCC_PCRUD_SEARCH.method,
661                         [ obj.session.key, {"id":{"!=":null}}, {"order_by":{"ahrcc":"label"}} ],
662                         false
663                     ]
664                 );
665                 try {
666                     f();
667                 } catch(E) {
668                     var error = 'Error: ' + js2JSON(E);
669                     obj.error.sdump('D_ERROR',error);
670                     throw(E);
671                 }
672             }
673         );
674
675
676         this.chain.push(
677             function() {
678                 var f = gen_fm_retrieval_func(
679                     'au',
680                     [
681                         api.FM_AU_RETRIEVE_VIA_SESSION.app,
682                         api.FM_AU_RETRIEVE_VIA_SESSION.method,
683                         [ obj.session.key ],
684                         false
685                     ]
686                 );
687                 try {
688                     f();
689                 } catch(E) {
690                     var error = 'Error: ' + js2JSON(E);
691                     obj.error.sdump('D_ERROR',error);
692                     throw(E);
693                 }
694                 obj.list.au = [ obj.list.au ];
695             }
696         );
697
698         this.chain.push(
699             function() {
700                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
701                 var f = gen_fm_retrieval_func(
702                     'my_asv',
703                     [
704                         api.FM_ASV_RETRIEVE_REQUIRED.app,
705                         api.FM_ASV_RETRIEVE_REQUIRED.method,
706                         [ obj.session.key ],
707                         true
708                     ]
709                 );
710                 try {
711                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
712                     f();
713                 } catch(E) {
714                     var error = 'Error: ' + js2JSON(E);
715                     obj.error.sdump('D_ERROR',error);
716                     throw(E);
717                 }
718             }
719         );
720
721         this.chain.push(
722             function() {
723                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
724                 var f = gen_fm_retrieval_func(
725                     'asv',
726                     [
727                         api.FM_ASV_RETRIEVE.app,
728                         api.FM_ASV_RETRIEVE.method,
729                         [ obj.session.key ],
730                         true
731                     ]
732                 );
733                 try {
734                     netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
735                     f();
736                 } catch(E) {
737                     var error = 'Error: ' + js2JSON(E);
738                     obj.error.sdump('D_ERROR',error);
739                     throw(E);
740                 }
741             }
742         );
743
744         obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
745
746         for (var i in this._fm_objects) {
747             this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
748         }
749
750         // The previous org_tree call returned a tree, not a list or hash.
751         this.chain.push(
752             function () {
753                 obj.tree.aou = obj.list.aou;
754                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
755                 for (var i = 0; i < obj.list.aou.length; i++) {
756                     var c = obj.list.aou[i].children();
757                     if (!c) c = [];
758                     c = c.sort(
759                         function( a, b ) {
760                             if (a.shortname() < b.shortname()) return -1;
761                             if (a.shortname() > b.shortname()) return 1;
762                             return 0;
763                         }
764                     );
765                     obj.list.aou[i].children( c );
766                 }
767                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
768                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
769             }
770         );
771
772         // The previous pgt call returned a tree, not a list or hash.
773         this.chain.push(
774             function () {
775                 obj.tree.pgt = obj.list.pgt;
776                 obj.list.pgt = util.fm_utils.flatten_ou_branch( obj.tree.pgt );
777                 obj.hash.pgt = util.functional.convert_object_list_to_hash( obj.list.pgt );
778             }
779         );
780
781         // Do these after we get the user object
782
783         this.chain.push(
784             function() {
785                 try {
786                     var robj = obj.network.simple_request('FM_AOUS_RETRIEVE',[ obj.session.key, obj.list.au[0].ws_ou() ]);
787                     if (typeof robj.ilsevent != 'undefined') throw(robj);
788                     obj.hash.aous = robj;
789                     obj.data_progress('Retrieved org unit settings. ');
790                 } catch(E) {
791                     var error = 'Error: ' + js2JSON(E);
792                     obj.error.sdump('D_ERROR',error);
793                     throw(E);
794                 }
795             }
796         );
797
798         this.chain.push(
799
800             function() {
801
802                 gen_fm_retrieval_func('my_aou', 
803                     [ 
804                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
805                         api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
806                         [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
807                         true
808                     ]
809                 )();
810             }
811         );
812
813         this.chain.push(
814
815             function () {
816
817                 gen_fm_retrieval_func( 'my_actsc', 
818                     [ 
819                         api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
820                         api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
821                         [ obj.session.key, obj.list.au[0].ws_ou() ],
822                         true
823                     ]
824                 )();
825             }
826         );
827
828         this.chain.push(
829
830             function () {
831
832                 gen_fm_retrieval_func( 'my_asc', 
833                     [ 
834                         api.FM_ASC_RETRIEVE_VIA_AOU.app,
835                         api.FM_ASC_RETRIEVE_VIA_AOU.method,
836                         [ obj.session.key, obj.list.au[0].ws_ou() ],
837                         true
838                     ]
839                 )();
840             }
841         );
842
843
844         this.chain.push(
845             function() {
846                 var f = gen_fm_retrieval_func(
847                     'cnct',
848                     [
849                         api.FM_CNCT_RETRIEVE.app,
850                         api.FM_CNCT_RETRIEVE.method,
851                         [ obj.list.au[0].ws_ou() ], 
852                         false
853                     ]
854                 );
855                 try {
856                     f();
857                 } catch(E) {
858                     var error = 'Error: ' + js2JSON(E);
859                     obj.error.sdump('D_ERROR',error);
860                     throw(E);
861                 }
862             }
863         );
864
865         this.chain.push(
866             function() {
867                 var f = gen_fm_retrieval_func(
868                     'my_cnct',
869                     [
870                         api.FM_CNCT_RETRIEVE.app,
871                         api.FM_CNCT_RETRIEVE.method,
872                         [ obj.list.au[0].ws_ou() ], 
873                         false
874                     ]
875                 );
876                 try {
877                     f();
878                 } catch(E) {
879                     var error = 'Error: ' + js2JSON(E);
880                     obj.error.sdump('D_ERROR',error);
881                     throw(E);
882                 }
883             }
884         );
885
886         this.chain.push(
887             function() {
888                 var f = gen_fm_retrieval_func(
889                     'acpl',
890                     [
891                         api.FM_ACPL_RETRIEVE.app,
892                         api.FM_ACPL_RETRIEVE.method,
893                         [ obj.list.au[0].ws_ou() ],
894                         false
895                     ]
896                 );
897                 try {
898                     f();
899                 } catch(E) {
900                     var error = 'Error: ' + js2JSON(E);
901                     obj.error.sdump('D_ERROR',error);
902                     throw(E);
903                 }
904             }
905         );
906
907         this.chain.push(
908             function() {
909                 var f = gen_fm_retrieval_func(
910                     'acnp',
911                     [
912                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.app,
913                         api.FM_ACNP_RETRIEVE_VIA_PCRUD.method,
914                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acnp":"label_sortkey"}} ],
915                         false
916                     ]
917                 );
918                 try {
919                     f();
920                     obj.list['acnp_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acnp;
921                 } catch(E) {
922                     var error = 'Error: ' + js2JSON(E);
923                     obj.error.sdump('D_ERROR',error);
924                     throw(E);
925                 }
926             }
927         );
928
929         this.chain.push(
930             function() {
931                 var f = gen_fm_retrieval_func(
932                     'acns',
933                     [
934                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.app,
935                         api.FM_ACNS_RETRIEVE_VIA_PCRUD.method,
936                         [ obj.session.key, {"owning_lib":{"=":obj.list.au[0].ws_ou()}}, {"order_by":{"acns":"label_sortkey"}} ],
937                         false
938                     ]
939                 );
940                 try {
941                     f();
942                     obj.list['acns_for_lib_'+obj.list.au[0].ws_ou()] = obj.list.acns;
943                 } catch(E) {
944                     var error = 'Error: ' + js2JSON(E);
945                     obj.error.sdump('D_ERROR',error);
946                     throw(E);
947                 }
948             }
949         );
950
951         this.chain.push(
952             function() {
953                 var f = gen_fm_retrieval_func(
954                     'cbt',
955                     [
956                         api.FM_CBT_RETRIEVE.app,
957                         api.FM_CBT_RETRIEVE.method,
958                         [ obj.session.key, obj.list.au[0].ws_ou() ],
959                         false
960                     ]
961                 );
962                 try {
963                     f();
964                 } catch(E) {
965                     var error = 'Error: ' + js2JSON(E);
966                     obj.error.sdump('D_ERROR',error);
967                     throw(E);
968                 }
969             }
970         );
971
972         if (typeof this.on_complete == 'function') {
973
974             this.chain.push( this.on_complete );
975         }
976         JSAN.use('util.exec'); this.exec = new util.exec();
977         this.exec.on_error = function(E) { 
978         
979             if (typeof obj.on_error == 'function') {
980                 return obj.on_error(E); /* false breaks chain */
981             } else {
982                 alert('oops: ' + E ); 
983                 return false; /* break chain */
984             }
985
986         }
987
988         this.exec.chain( this.chain );
989
990     }
991 }
992
993 dump('exiting OpenILS/data.js\n');