]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/holds.js
be more careful with copy objects vs copy ids
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / holds.js
1 dump('entering patron.holds.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.holds = function (params) {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
9 }
10
11 patron.holds.prototype = {
12
13         'foreign_shelf' : null,
14
15         'retrieve_ids' : [],
16
17         'holds_map' : {},
18
19         'init' : function( params ) {
20
21                 var obj = this;
22
23                 obj.patron_id = params['patron_id'];
24                 obj.docid = params['docid'];
25                 obj.shelf = params['shelf'];
26                 obj.tree_id = params['tree_id'];
27
28                 JSAN.use('circ.util');
29                 var columns = circ.util.hold_columns( 
30                         { 
31                                 'title' : { 'hidden' : false, 'flex' : '3' },
32                                 'request_time' : { 'hidden' : false },
33                                 'pickup_lib_shortname' : { 'hidden' : false },
34                                 'hold_type' : { 'hidden' : false },
35                                 'current_copy' : { 'hidden' : false },
36                                 'capture_time' : { 'hidden' : false },
37                                 'notify_time' : { 'hidden' : false },
38                                 'notify_count' : { 'hidden' : false },
39                         } 
40                 );
41
42                 JSAN.use('util.list'); obj.list = new util.list( obj.tree_id || 'holds_list');
43                 obj.list.init(
44                         {
45                                 'columns' : columns,
46                                 'map_row_to_columns' : circ.util.std_map_row_to_columns(),
47                                 'retrieve_row' : function(params) {
48                                         var row = params.row;
49                                         try {
50                                                 obj.network.simple_request('FM_AHR_BLOB_RETRIEVE', [ ses(), row.my.hold_id ],
51                                                         function(blob_req) {
52                                                                 try {
53                                                                         var blob = blob_req.getResultObject();
54                                                                         if (typeof blob.ilsevent != 'undefined') throw(blob);
55                                                                         row.my.ahr = blob.hold;
56                                                                         row.my.status = blob.status;
57                                                                         row.my.acp = blob.copy;
58                                                                         row.my.acn = blob.volume;
59                                                                         row.my.mvr = blob.mvr;
60                                                                         row.my.patron_family_name = blob.patron_last;
61                                                                         row.my.patron_first_given_name = blob.patron_first;
62                                                                         row.my.patron_barcode = blob.patron_barcode;
63
64                                                                         var copy_id = row.my.ahr.current_copy();
65                                                                         if (typeof copy_id == 'object') {
66                                                                                 if (copy_id == null) {
67                                                                                         if (typeof row.my.acp == 'object' && row.my.acp != null) copy_id = row.my.acp.id();
68                                                                                 } else {
69                                                                                         copy_id = copy_id.id();
70                                                                                 }
71                                                                         } else {
72                                                                                 copy_id = row.my.acp.id();
73                                                                         }
74
75                                                                         obj.holds_map[ row.my.ahr.id() ] = row.my.ahr;
76                                                                         params.row_node.setAttribute('retrieve_id', 
77                                                                                 js2JSON({
78                                                                                         'copy_id':copy_id,
79                                                                                         'id':row.my.ahr.id(),
80                                                                                         'type':row.my.ahr.hold_type(),
81                                                                                         'target':row.my.ahr.target(),
82                                                                                         'usr':row.my.ahr.usr(),
83                                                                                 })
84                                                                         );
85                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
86
87                                                                 } catch(E) {
88                                                                         obj.error.standard_unexpected_error_alert('Error retrieving details for hold #' + row.my.hold_id, E);
89                                                                 }
90                                                         }
91                                                 );
92                                                 /*
93                                                 obj.network.simple_request('FM_AHR_RETRIEVE', [ ses(), row.my.hold_id ],
94                                                         function(ahr_req) {
95                                                                 try {
96                                                                         var ahr_robj = ahr_req.getResultObject();
97                                                                         if (typeof ahr_robj.ilsevent != 'undefined') throw(ahr_robj);
98                                                                         row.my.ahr = ahr_robj[0];
99                                                                         obj.holds_map[ row.my.ahr.id() ] = row.my.ahr;
100                                                                         params.row_node.setAttribute('retrieve_id', 
101                                                                                 js2JSON({
102                                                                                         'copy_id':row.my.ahr.current_copy(),
103                                                                                         'id':row.my.ahr.id(),
104                                                                                         'type':row.my.ahr.hold_type(),
105                                                                                         'target':row.my.ahr.target(),
106                                                                                         'usr':row.my.ahr.usr(),
107                                                                                 })
108                                                                         );
109
110                                                                         obj.network.simple_request('FM_AHR_STATUS',[ ses(), row.my.ahr.id() ],
111                                                                                 function(status_req) {
112                                                                                         try {
113                                                                                                 var status_robj = status_req.getResultObject();
114                                                                                                 row.my.status = status_robj;
115                                                                                                 switch(row.my.ahr.hold_type()) {
116                                                                                                         case 'M' :
117                                                                                                                 obj.network.request(
118                                                                                                                         api.MODS_SLIM_METARECORD_RETRIEVE.app,
119                                                                                                                         api.MODS_SLIM_METARECORD_RETRIEVE.method,
120                                                                                                                         [ row.my.ahr.target() ],
121                                                                                                                         function(mvr_req) {
122                                                                                                                                 row.my.mvr = mvr_req.getResultObject();
123                                                                                                                                 if ( row.my.ahr.current_copy() && ! row.my.acp) {
124                                                                                                                                         obj.network.simple_request( 'FM_ACP_RETRIEVE', [ row.my.ahr.current_copy() ],
125                                                                                                                                                 function(acp_req) {
126                                                                                                                                                         row.my.acp = acp_req.getResultObject();
127                                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
128                                                                                                                                                 }
129                                                                                                                                         );
130                                                                                                                                 } else {
131                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
132                                                                                                                                 }
133                                                                                                                         }
134                                                                                                                 );
135                                                                                                         break;
136                                                                                                         case 'T' :
137                                                                                                                 obj.network.request(
138                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.app,
139                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.method,
140                                                                                                                         [ row.my.ahr.target() ],
141                                                                                                                         function(mvr_req) {
142                                                                                                                                 row.my.mvr = mvr_req.getResultObject();
143                                                                                                                                 if ( row.my.ahr.current_copy() && ! row.my.acp) {
144                                                                                                                                         obj.network.simple_request( 'FM_ACP_RETRIEVE', [ row.my.ahr.current_copy() ],
145                                                                                                                                                 function(acp_req) {
146                                                                                                                                                         row.my.acp = acp_req.getResultObject();
147                                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
148                                                                                                                                                 }
149                                                                                                                                         );
150                                                                                                                                 } else {
151                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
152                                                                                                                                 }
153         
154                                                                                                                         }
155                                                                                                                 );
156                                                                                                         break;
157                                                                                                         case 'V' :
158                                                                                                                 row.my.acn = obj.network.simple_request( 'FM_ACN_RETRIEVE', [ row.my.ahr.target() ],
159                                                                                                                         function(acn_req) {
160                                                                                                                                 row.my.acn = acn_req.getResultObject();
161                                                                                                                                 obj.network.request(
162                                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.app,
163                                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.method,
164                                                                                                                                         [ row.my.acn.record() ],
165                                                                                                                                         function(mvr_req) {
166                                                                                                                                                 try { row.my.mvr = mvr_req.getResultObject(); } catch(E) {}
167                                                                                                                                                 if ( row.my.ahr.current_copy() && ! row.my.acp) {
168                                                                                                                                                         obj.network.simple_request( 'FM_ACP_RETRIEVE', [ row.my.ahr.current_copy() ],
169                                                                                                                                                                 function(acp_req) {
170                                                                                                                                                                         row.my.acp = acp_req.getResultObject();
171                                                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
172                                                                                                                                                                 }
173                                                                                                                                                         );
174                                                                                                                                                 } else {
175                                                                                                                                                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
176                                                                                                                                                 }
177                                                                                                                                         }
178                                                                                                                                 );
179                                                                                                                         }
180                                                                                                                 );
181                                                                                                         break;
182                                                                                                         case 'C' :
183                                                                                                                 obj.network.simple_request( 'FM_ACP_RETRIEVE', [ row.my.ahr.target() ],
184                                                                                                                         function(acp_req) {
185                                                                                                                                 row.my.acp = acp_req.getResultObject();
186                                                                                                                                 obj.network.simple_request( 'FM_ACN_RETRIEVE', [ typeof row.my.acp.call_number() == 'object' ? row.my.acp.call_number().id() : row.my.acp.call_number() ],
187                                                                                                                                         function(acn_req) {
188                                                                                                                                                 row.my.acn = acn_req.getResultObject();
189                                                                                                                                                 obj.network.request(
190                                                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.app,
191                                                                                                                                                         api.MODS_SLIM_RECORD_RETRIEVE.method,
192                                                                                                                                                         [ row.my.acn.record() ],
193                                                                                                                                                         function(mvr_req) {
194                                                                                                                                                                 try { row.my.mvr = mvr_req.getResultObject(); } catch(E) {}
195                                                                                                                                                                 if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
196                                                                                                                                                         }
197                                                                                                                                                 );
198                                                                                                                                         }
199                                                                                                                                 );
200                                                                                                                         }
201                                                                                                                 );
202                                                                                                         break;
203                                                                                                 }
204                                                                                         } catch(E) {
205                                                                                                 obj.error.standard_unexpected_error_alert('Error retrieving status for hold #' + row.my.hold_id, E);
206                                                                                         }
207                                                                                 }
208                                                                         );
209                                                                 } catch(E) {
210                                                                         obj.error.standard_unexpected_error_alert('Error retrieving hold #' + row.my.hold_id, E);
211                                                                 }
212                                                         }
213                                                 );
214                                                 */
215                                         } catch(E) {
216                                                 obj.error.sdump('D_ERROR','retrieve_row: ' + E );
217                                         }
218                                         return row;
219                                 },
220                                 'on_select' : function(ev) {
221                                         JSAN.use('util.functional');
222                                         var sel = obj.list.retrieve_selection();
223                                         obj.controller.view.sel_clip.setAttribute('disabled',sel.length < 1);
224                                         obj.retrieve_ids = util.functional.map_list(
225                                                 sel,
226                                                 function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
227                                         );
228                                         if (obj.retrieve_ids.length > 0) {
229                                                 obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','false');
230                                                 obj.controller.view.sel_mark_items_missing.setAttribute('disabled','false');
231                                                 obj.controller.view.sel_copy_details.setAttribute('disabled','false');
232                                                 obj.controller.view.sel_patron.setAttribute('disabled','false');
233                                                 obj.controller.view.cmd_retrieve_patron.setAttribute('disabled','false');
234                                                 obj.controller.view.cmd_holds_edit_pickup_lib.setAttribute('disabled','false');
235                                                 obj.controller.view.cmd_holds_edit_phone_notify.setAttribute('disabled','false');
236                                                 obj.controller.view.cmd_holds_edit_email_notify.setAttribute('disabled','false');
237                                                 obj.controller.view.cmd_holds_edit_selection_depth.setAttribute('disabled','false');
238                                                 obj.controller.view.cmd_show_notifications.setAttribute('disabled','false');
239                                                 obj.controller.view.cmd_holds_retarget.setAttribute('disabled','false');
240                                                 obj.controller.view.cmd_holds_cancel.setAttribute('disabled','false');
241                                                 obj.controller.view.cmd_show_catalog.setAttribute('disabled','false');
242                                         } else {
243                                                 obj.controller.view.sel_mark_items_damaged.setAttribute('disabled','true');
244                                                 obj.controller.view.sel_mark_items_missing.setAttribute('disabled','true');
245                                                 obj.controller.view.sel_copy_details.setAttribute('disabled','true');
246                                                 obj.controller.view.sel_patron.setAttribute('disabled','true');
247                                                 obj.controller.view.cmd_retrieve_patron.setAttribute('disabled','true');
248                                                 obj.controller.view.cmd_holds_edit_pickup_lib.setAttribute('disabled','true');
249                                                 obj.controller.view.cmd_holds_edit_phone_notify.setAttribute('disabled','true');
250                                                 obj.controller.view.cmd_holds_edit_email_notify.setAttribute('disabled','true');
251                                                 obj.controller.view.cmd_holds_edit_selection_depth.setAttribute('disabled','true');
252                                                 obj.controller.view.cmd_show_notifications.setAttribute('disabled','true');
253                                                 obj.controller.view.cmd_holds_retarget.setAttribute('disabled','true');
254                                                 obj.controller.view.cmd_holds_cancel.setAttribute('disabled','true');
255                                                 obj.controller.view.cmd_show_catalog.setAttribute('disabled','true');
256                                         }
257                                 },
258
259                         }
260                 );
261                 
262                 JSAN.use('util.controller'); obj.controller = new util.controller();
263                 obj.controller.init(
264                         {
265                                 'control_map' : {
266                                         'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
267                                         'sel_clip' : [
268                                                 ['command'],
269                                                 function() { obj.list.clipboard(); }
270                                         ],
271                                         'cmd_broken' : [
272                                                 ['command'],
273                                                 function() { alert('Not Yet Implemented'); }
274                                         ],
275                                         'sel_patron' : [
276                                                 ['command'],
277                                                 function() {
278                                                         JSAN.use('circ.util');
279                                                         circ.util.show_last_few_circs(obj.retrieve_ids);
280                                                 }
281                                         ],
282                                         'sel_mark_items_damaged' : [
283                                                 ['command'],
284                                                 function() {
285                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
286                                                         cat.util.mark_item_damaged( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
287                                                 }
288                                         ],
289                                         'sel_mark_items_missing' : [
290                                                 ['command'],
291                                                 function() {
292                                                         JSAN.use('cat.util'); JSAN.use('util.functional');
293                                                         cat.util.mark_item_missing( util.functional.map_list( obj.retrieve_ids, function(o) { return o.copy_id; } ) );
294                                                 }
295                                         ],
296                                         'sel_copy_details' : [
297                                                 ['command'],
298                                                 function() {
299                                                         JSAN.use('circ.util');
300                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
301                                                                 if (obj.retrieve_ids[i].copy_id) circ.util.show_copy_details( obj.retrieve_ids[i].copy_id );
302                                                         }
303                                                 }
304                                         ],
305                                         'cmd_holds_print' : [
306                                                 ['command'],
307                                                 function() {
308                                                         try {
309                                                                 dump(js2JSON(obj.list.dump_with_keys()) + '\n');
310                                                                 function flesh_callback() {
311                                                                         try {
312                                                                                 JSAN.use('patron.util');
313                                                                                 var params = { 
314                                                                                         'patron' : patron.util.retrieve_au_via_id(ses(),obj.patron_id), 
315                                                                                         'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
316                                                                                         'staff' : obj.data.list.au[0],
317                                                                                         'header' : obj.data.print_list_templates.holds.header,
318                                                                                         'line_item' : obj.data.print_list_templates.holds.line_item,
319                                                                                         'footer' : obj.data.print_list_templates.holds.footer,
320                                                                                         'type' : obj.data.print_list_templates.holds.type,
321                                                                                         'list' : obj.list.dump_with_keys(),
322                                                                                 };
323                                                                                 JSAN.use('util.print'); var print = new util.print();
324                                                                                 print.tree_list( params );
325                                                                                 setTimeout(function(){obj.list.on_all_fleshed = null;},0);
326                                                                         } catch(E) {
327                                                                                 obj.error.standard_unexpected_error_alert('print 2',E);
328                                                                         }
329                                                                 }
330                                                                 obj.list.on_all_fleshed = flesh_callback;
331                                                                 obj.list.full_retrieve();
332                                                         } catch(E) {
333                                                                 obj.error.standard_unexpected_error_alert('print 1',E);
334                                                         }
335                                                 }
336                                         ],
337                                         'cmd_holds_export' : [
338                                                 ['command'],
339                                                 function() {
340                                                         try {
341                                                                 function flesh_callback() {
342                                                                         try {
343                                                                                 dump(obj.list.dump_csv() + '\n');
344                                                                                 copy_to_clipboard(obj.list.dump_csv());
345                                                                                 setTimeout(function(){obj.list.on_all_fleshed = null;},0);
346                                                                         } catch(E) {
347                                                                                 obj.error.standard_unexpected_error_alert('export 2',E);
348                                                                         }
349                                                                 }
350                                                                 obj.list.on_all_fleshed = flesh_callback;
351                                                                 obj.list.full_retrieve();
352                                                         } catch(E) {
353                                                                 obj.error.standard_unexpected_error_alert('export 1',E);
354                                                         }
355                                                 }
356                                         ],
357
358                                         'cmd_show_notifications' : [
359                                                 ['command'],
360                                                 function() {
361                                                         try {
362                                                                 JSAN.use('util.window'); var win = new util.window();
363                                                                 for (var i = 0; i < obj.retrieve_ids.length; i++) {
364                                                                         netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
365                                                                         win.open(
366                                                                                 xulG.url_prefix(urls.XUL_HOLD_NOTICES) 
367                                                                                 + '?ahr_id=' + obj.retrieve_ids[i].id,
368                                                                                 'hold_notices_' + obj.retrieve_ids[i].id,
369                                                                                 'chrome,resizable'
370                                                                         );
371                                                                 }
372                                                         } catch(E) {
373                                                                 obj.error.standard_unexpected_error_alert('Error rendering/retrieving hold notifications.',E);
374                                                         }
375                                                 }
376                                         ],
377                                         'cmd_holds_edit_selection_depth' : [
378                                                 ['command'],
379                                                 function() {
380                                                         try {
381                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional'); 
382                                                                 var ws_type = obj.data.hash.aout[ obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].ou_type() ];
383                                                                 var list = util.functional.map_list(
384                                                                         util.functional.filter_list(    
385                                                                                 obj.data.list.aout,
386                                                                                 function(o) {
387                                                                                         if (o.depth() > ws_type.depth()) return false;
388                                                                                         if (o.depth() < ws_type.depth()) return true;
389                                                                                         return (o.id() == ws_type.id());
390                                                                                 }
391                                                                         ),
392                                                                         function(o) { 
393                                                                                 return [
394                                                                                         o.opac_label(),
395                                                                                         o.id(),
396                                                                                         false,
397                                                                                         ( o.depth() * 2),
398                                                                                 ]; 
399                                                                         }
400                                                                 );
401                                                                 ml = util.widgets.make_menulist( list, obj.data.list.au[0].ws_ou() );
402                                                                 ml.setAttribute('id','selection');
403                                                                 ml.setAttribute('name','fancy_data');
404                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
405                                                                 xml += '<description>Please choose a Hold Range:</description>';
406                                                                 xml += util.widgets.serialize_node(ml);
407                                                                 xml += '</vbox>';
408                                                                 var bot_xml = '<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
409                                                                 bot_xml += '<spacer flex="1"/><button label="Done" accesskey="D" name="fancy_submit"/>';
410                                                                 bot_xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
411                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
412                                                                 obj.data.temp_mid = xml; obj.data.stash('temp_mid');
413                                                                 obj.data.temp_bot = bot_xml; obj.data.stash('temp_bot');
414                                                                 window.open(
415                                                                         urls.XUL_FANCY_PROMPT
416                                                                         + '?xml_in_stash=temp_mid'
417                                                                         + '&bottom_xml_in_stash=temp_bot'
418                                                                         + '&title=' + window.escape('Choose a Pick Up Library'),
419                                                                         'fancy_prompt', 'chrome,resizable,modal'
420                                                                 );
421                                                                 obj.data.init({'via':'stash'});
422                                                                 if (obj.data.fancy_prompt_data == '') { return; }
423                                                                 var selection = obj.data.fancy_prompt_data.selection;
424                                                                 var msg = 'Are you sure you would like to change the Hold Range for hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + ' to "' + obj.data.hash.aout[selection].opac_label() + '"?';
425                                                                 var r = obj.error.yns_alert(msg,'Modifying Holds','Yes','No',null,'Check here to confirm this message');
426                                                                 if (r == 0) {
427                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
428                                                                                 var hold = obj.holds_map[ obj.retrieve_ids[i].id ];
429                                                                                 hold.selection_depth( obj.data.hash.aout[selection].depth() ); hold.ischanged('1');
430                                                                                 var robj = obj.network.simple_request('FM_AHR_UPDATE',[ ses(), hold ]);
431                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
432                                                                         }
433                                                                         obj.retrieve();
434                                                                 }
435                                                         } catch(E) {
436                                                                 obj.error.standard_unexpected_error_alert('Holds not likely modified.',E);
437                                                         }
438                                                 }
439                                         ],
440
441                                         'cmd_holds_edit_pickup_lib' : [
442                                                 ['command'],
443                                                 function() {
444                                                         try {
445                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional'); 
446                                                                 var list = util.functional.map_list(
447                                                                         obj.data.list.aou,
448                                                                         function(o) { 
449                                                                                 var sname = o.shortname(); for (i = sname.length; i < 20; i++) sname += ' ';
450                                                                                 return [
451                                                                                         o.name() ? sname + ' ' + o.name() : o.shortname(),
452                                                                                         o.id(),
453                                                                                         ( obj.data.hash.aout[ o.ou_type() ].can_have_users() == 0),
454                                                                                         ( obj.data.hash.aout[ o.ou_type() ].depth() * 2),
455                                                                                 ]; 
456                                                                         }
457                                                                 );
458                                                                 ml = util.widgets.make_menulist( list, obj.data.list.au[0].ws_ou() );
459                                                                 ml.setAttribute('id','lib');
460                                                                 ml.setAttribute('name','fancy_data');
461                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
462                                                                 xml += '<description>Please choose a new Pickup Library:</description>';
463                                                                 xml += util.widgets.serialize_node(ml);
464                                                                 xml += '</vbox>';
465                                                                 var bot_xml = '<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
466                                                                 bot_xml += '<spacer flex="1"/><button label="Done" accesskey="D" name="fancy_submit"/>';
467                                                                 bot_xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
468                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
469                                                                 obj.data.temp_mid = xml; obj.data.stash('temp_mid');
470                                                                 obj.data.temp_bot = bot_xml; obj.data.stash('temp_bot');
471                                                                 window.open(
472                                                                         urls.XUL_FANCY_PROMPT
473                                                                         + '?xml_in_stash=temp_mid'
474                                                                         + '&bottom_xml_in_stash=temp_bot'
475                                                                         + '&title=' + window.escape('Choose a Pick Up Library'),
476                                                                         'fancy_prompt', 'chrome,resizable,modal'
477                                                                 );
478                                                                 obj.data.init({'via':'stash'});
479                                                                 if (obj.data.fancy_prompt_data == '') { return; }
480                                                                 var pickup_lib = obj.data.fancy_prompt_data.lib;
481                                                                 var msg = 'Are you sure you would like to change the Pick Up Lib for hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + ' to ' + obj.data.hash.aou[pickup_lib].shortname() + '?';
482                                                                 var r = obj.error.yns_alert(msg,'Modifying Holds','Yes','No',null,'Check here to confirm this message');
483                                                                 if (r == 0) {
484                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
485                                                                                 var hold = obj.holds_map[ obj.retrieve_ids[i].id ];
486                                                                                 hold.pickup_lib(  pickup_lib ); hold.ischanged('1');
487                                                                                 var robj = obj.network.simple_request('FM_AHR_UPDATE',[ ses(), hold ]);
488                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
489                                                                         }
490                                                                         obj.retrieve();
491                                                                 }
492                                                         } catch(E) {
493                                                                 obj.error.standard_unexpected_error_alert('Holds not likely modified.',E);
494                                                         }
495                                                 }
496                                         ],
497                                         'cmd_holds_edit_phone_notify' : [
498                                                 ['command'],
499                                                 function() {
500                                                         try {
501                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
502                                                                 xml += '<description>Please enter a new phone number for hold notification (leave the field empty to disable phone notification):</description>';
503                                                                 xml += '<textbox id="phone" name="fancy_data"/>';
504                                                                 xml += '</vbox>';
505                                                                 var bot_xml = '<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
506                                                                 bot_xml += '<spacer flex="1"/><button label="Done" accesskey="D" name="fancy_submit"/>';
507                                                                 bot_xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
508                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
509                                                                 obj.data.temp_mid = xml; obj.data.stash('temp_mid');
510                                                                 obj.data.temp_bot = bot_xml; obj.data.stash('temp_bot');
511                                                                 window.open(
512                                                                         urls.XUL_FANCY_PROMPT
513                                                                         + '?xml_in_stash=temp_mid'
514                                                                         + '&bottom_xml_in_stash=temp_bot'
515                                                                         + '&title=' + window.escape('Choose a Hold Notification Phone Number')
516                                                                         + '&focus=phone',
517                                                                         'fancy_prompt', 'chrome,resizable,modal'
518                                                                 );
519                                                                 obj.data.init({'via':'stash'});
520                                                                 if (obj.data.fancy_prompt_data == '') { return; }
521                                                                 var phone = obj.data.fancy_prompt_data.phone;
522                                                                 var msg = 'Are you sure you would like to change the Notification Phone Number for hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + ' to "' + phone + '"?';
523                                                                 var r = obj.error.yns_alert(msg,'Modifying Holds','Yes','No',null,'Check here to confirm this message');
524                                                                 if (r == 0) {
525                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
526                                                                                 var hold = obj.holds_map[ obj.retrieve_ids[i].id ];
527                                                                                 hold.phone_notify(  phone ); hold.ischanged('1');
528                                                                                 var robj = obj.network.simple_request('FM_AHR_UPDATE',[ ses(), hold ]);
529                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
530                                                                         }
531                                                                         obj.retrieve();
532                                                                 }
533                                                         } catch(E) {
534                                                                 obj.error.standard_unexpected_error_alert('Holds not likely modified.',E);
535                                                         }
536                                                 }
537                                         ],
538                                         'cmd_holds_edit_email_notify' : [
539                                                 ['command'],
540                                                 function() {
541                                                         try {
542                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
543                                                                 xml += '<description>Send email notifications (when appropriate)?  The email address used is found in the hold recepient account.</description>';
544                                                                 xml += '<hbox><button value="email" label="Email" accesskey="E" name="fancy_submit"/>';
545                                                                 xml += '<button value="noemail" label="No Email" accesskey="N" name="fancy_submit"/></hbox>';
546                                                                 xml += '</vbox>';
547                                                                 var bot_xml = '<hbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: vertical">';
548                                                                 bot_xml += '<spacer flex="1"/><button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
549                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
550                                                                 obj.data.temp_mid = xml; obj.data.stash('temp_mid');
551                                                                 obj.data.temp_bot = bot_xml; obj.data.stash('temp_bot');
552                                                                 window.open(
553                                                                         urls.XUL_FANCY_PROMPT
554                                                                         + '?xml_in_stash=temp_mid'
555                                                                         + '&bottom_xml_in_stash=temp_bot'
556                                                                         + '&title=' + window.escape('Set Email Notification for Holds'),
557                                                                         'fancy_prompt', 'chrome,resizable,modal'
558                                                                 );
559                                                                 obj.data.init({'via':'stash'});
560                                                                 if (obj.data.fancy_prompt_data == '') { return; }
561                                                                 var email = obj.data.fancy_prompt_data.fancy_submit == 'email' ? get_db_true() : get_db_false();
562                                                                 var msg = 'Are you sure you would like ' + ( get_bool( email ) ? 'enable' : 'disable' ) + ' email notification for hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + '?';
563                                                                 var r = obj.error.yns_alert(msg,'Modifying Holds','Yes','No',null,'Check here to confirm this message');
564                                                                 if (r == 0) {
565                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
566                                                                                 var hold = obj.holds_map[ obj.retrieve_ids[i].id ];
567                                                                                 hold.email_notify(  email ); hold.ischanged('1');
568                                                                                 var robj = obj.network.simple_request('FM_AHR_UPDATE',[ ses(), hold ]);
569                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
570                                                                         }
571                                                                         obj.retrieve();
572                                                                 }
573                                                         } catch(E) {
574                                                                 obj.error.standard_unexpected_error_alert('Holds not likely modified.',E);
575                                                         }
576                                                 }
577                                         ],
578
579
580                                         'cmd_holds_retarget' : [
581                                                 ['command'],
582                                                 function() {
583                                                         try {
584                                                                 JSAN.use('util.functional');
585                                                                 var msg = 'Are you sure you would like to reset hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + '?';
586                                                                 var r = obj.error.yns_alert(msg,'Resetting Holds','Yes','No',null,'Check here to confirm this message');
587                                                                 if (r == 0) {
588                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
589                                                                                 var robj = obj.network.simple_request('FM_AHR_RESET',[ ses(), obj.retrieve_ids[i].id]);
590                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
591                                                                         }
592                                                                         obj.retrieve();
593                                                                 }
594                                                         } catch(E) {
595                                                                 obj.error.standard_unexpected_error_alert('Holds not likely reset.',E);
596                                                         }
597
598                                                 }
599                                         ],
600
601                                         'cmd_holds_cancel' : [
602                                                 ['command'],
603                                                 function() {
604                                                         try {
605                                                                 JSAN.use('util.functional');
606                                                                 var msg = 'Are you sure you would like to cancel hold' + ( obj.retrieve_ids.length > 1 ? 's ' : ' ') + util.functional.map_list( obj.retrieve_ids, function(o){return o.id;}).join(', ') + '?';
607                                                                 var r = obj.error.yns_alert(msg,'Cancelling Holds','Yes','No',null,'Check here to confirm this message');
608                                                                 if (r == 0) {
609                                                                         for (var i = 0; i < obj.retrieve_ids.length; i++) {
610                                                                                 var robj = obj.network.simple_request('FM_AHR_CANCEL',[ ses(), obj.retrieve_ids[i].id]);
611                                                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
612                                                                         }
613                                                                         obj.retrieve();
614                                                                 }
615                                                         } catch(E) {
616                                                                 obj.error.standard_unexpected_error_alert('Holds not likely cancelled.',E);
617                                                         }
618                                                 }
619                                         ],
620                                         'cmd_retrieve_patron' : [
621                                                 ['command'],
622                                                 function() {
623                                                         try {
624                                                                 var seen = {};
625                                                                 for (var i = 0; i < obj.retrieve_ids.length; i++) {
626                                                                         var patron_id = obj.retrieve_ids[i].usr;
627                                                                         if (seen[patron_id]) continue; seen[patron_id] = true;
628                                                                         xulG.new_tab(
629                                                                                 xulG.url_prefix(urls.XUL_PATRON_DISPLAY) + '?id=' + patron_id, 
630                                                                                 {}, 
631                                                                                 {}
632                                                                         );
633                                                                 }
634                                                         } catch(E) {
635                                                                 obj.error.standard_unexpected_error_alert('',E);
636                                                         }
637                                                 }
638                                         ],
639                                         'cmd_show_catalog' : [
640                                                 ['command'],
641                                                 function() {
642                                                         try {
643                                                                 for (var i = 0; i < obj.retrieve_ids.length; i++) {
644                                                                         var htarget = obj.retrieve_ids[i].target;
645                                                                         var htype = obj.retrieve_ids[i].type;
646                                                                         var opac_url;
647                                                                         switch(htype) {
648                                                                                 case 'M' :
649                                                                                         opac_url = xulG.url_prefix( urls.opac_rresult ) + '?m=' + htarget;
650                                                                                 break;
651                                                                                 case 'T' : 
652                                                                                         opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + htarget;
653                                                                                 break;
654                                                                                 case 'V' :
655                                                                                         var my_acn = obj.network.simple_request( 'FM_ACN_RETRIEVE', [ htarget ]);
656                                                                                         opac_url = xulG.url_prefix( urls.opac_rdetail) + '?r=' + my_acn.record();
657                                                                                 break;
658                                                                                 case 'C' :
659                                                                                         var my_acp = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ htarget ]);
660                                                                                         var my_acn;
661                                                                                         if (typeof my_acp.call_number() == 'object') {
662                                                                                                 my_acn = my.acp.call_number();
663                                                                                         } else {
664                                                                                                 my_acn = obj.network.simple_request( 'FM_ACN_RETRIEVE', 
665                                                                                                         [ my_acp.call_number() ]);
666                                                                                         }
667                                                                                         opac_url = xulG.url_prefix( urls.opac_rdetail) + '?r=' + my_acn.record();
668                                                                                 break;
669                                                                                 default:
670                                                                                         obj.error.standard_unexpected_error_alert("I don't understand the hold type of " + htype + ", so I can't jump to the appropriate record in the catalog.", obj.retrieve_ids[i]);
671                                                                                         continue;
672                                                                                 break;
673                                                                         }
674                                                                         var content_params = { 
675                                                                                 'session' : ses(),
676                                                                                 'authtime' : ses('authtime'),
677                                                                                 'opac_url' : opac_url,
678                                                                         };
679                                                                         xulG.new_tab(
680                                                                                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
681                                                                                 {'tab_name': htype == 'M' ? 'Catalog' : 'Retrieving title...'}, 
682                                                                                 content_params
683                                                                         );
684                                                                 }
685                                                         } catch(E) {
686                                                                 obj.error.standard_unexpected_error_alert('',E);
687                                                         }
688                                                 }
689                                         ],
690                                 }
691                         }
692                 );
693                 obj.controller.render();
694
695                 obj.retrieve();
696
697                 obj.controller.view.cmd_retrieve_patron.setAttribute('disabled','true');
698                 obj.controller.view.cmd_holds_edit_pickup_lib.setAttribute('disabled','true');
699                 obj.controller.view.cmd_holds_edit_phone_notify.setAttribute('disabled','true');
700                 obj.controller.view.cmd_holds_edit_email_notify.setAttribute('disabled','true');
701                 obj.controller.view.cmd_holds_edit_selection_depth.setAttribute('disabled','true');
702                 obj.controller.view.cmd_show_notifications.setAttribute('disabled','true');
703                 obj.controller.view.cmd_holds_retarget.setAttribute('disabled','true');
704                 obj.controller.view.cmd_holds_cancel.setAttribute('disabled','true');
705                 obj.controller.view.cmd_show_catalog.setAttribute('disabled','true');
706         },
707
708         'retrieve' : function(dont_show_me_the_list_change) {
709                 var obj = this;
710                 if (window.xulG && window.xulG.holds) {
711                         obj.holds = window.xulG.holds;
712                 } else {
713                         var method; var params = [ ses() ];
714                         if (obj.patron_id) {                 /*************************************************** PATRON ******************************/
715                                 method = 'FM_AHR_ID_LIST_RETRIEVE_VIA_AU'; 
716                                 params.push( obj.patron_id ); 
717                                 obj.controller.view.cmd_retrieve_patron.setAttribute('hidden','true');
718                         } else if (obj.docid) {                 /*************************************************** RECORD ******************************/
719                                 method = 'FM_AHR_RETRIEVE_ALL_VIA_BRE'; 
720                                 params.push( obj.docid ); 
721                                 obj.controller.view.cmd_retrieve_patron.setAttribute('hidden','false');
722                         } else if (obj.pull) {                 /*************************************************** PULL ******************************/
723                                 method = 'FM_AHR_ID_LIST_PULL_LIST'; 
724                                 params.push( 50 ); params.push( 0 );
725                         } else if (obj.shelf) {
726                                 method = 'FM_AHR_ID_LIST_ONSHELF_RETRIEVE';                  /*************************************************** HOLD SHELF ******************************/
727                                 params.push( obj.foreign_shelf || obj.data.list.au[0].ws_ou() ); 
728                                 obj.controller.view.cmd_retrieve_patron.setAttribute('hidden','false');
729                                 obj.render_lib_menu();
730                         } else {
731                                 //method = 'FM_AHR_RETRIEVE_VIA_PICKUP_AOU'; 
732                                 method = 'FM_AHR_ID_LIST_PULL_LIST';                  /*************************************************** PULL ******************************/
733                                 params.push( 50 ); params.push( 0 );
734                                 obj.controller.view.cmd_retrieve_patron.setAttribute('hidden','false');
735                         }
736                         var robj = obj.network.simple_request( method, params );
737                         if (typeof robj.ilsevent != 'undefined') throw(robj);
738                         if (method == 'FM_AHR_RETRIEVE_ALL_VIA_BRE') {
739                                 obj.holds = [];
740                                 obj.holds = obj.holds.concat( robj.copy_holds );
741                                 obj.holds = obj.holds.concat( robj.volume_holds );
742                                 obj.holds = obj.holds.concat( robj.title_holds );
743                                 obj.holds = obj.holds.sort();
744                         } else {
745                                 obj.holds = robj;
746                         }
747                         //alert('method = ' + method + ' params = ' + js2JSON(params));
748                 }
749
750                 function list_append(hold_id) {
751                         obj.list.append(
752                                 {
753                                         'row' : {
754                                                 'my' : {
755                                                         'hold_id' : hold_id,
756                                                 }
757                                         }
758                                 }
759                         );
760                 }
761
762                 function gen_list_append(hold) {
763                         return function() {
764                                 if (typeof obj.controller.view.lib_menu == 'undefined') {
765                                         list_append(typeof hold == 'object' ? hold.id() : hold);
766                                 } else {
767                                         /*
768                                         var pickup_lib = hold.pickup_lib();
769                                         if (typeof pickup_lib == 'object') pickup_lib = pickup_lib.id();
770                                         if (pickup_lib == obj.controller.view.lib_menu.value) {
771                                         */
772                                                 list_append(typeof hold == 'object' ? hold.id() : hold);
773                                         /*
774                                         }
775                                         */
776                                 }
777                         };
778                 }
779
780                 obj.list.clear();
781
782                 //alert('obj.holds = ' + js2JSON(obj.holds));
783                 JSAN.use('util.exec'); var exec = new util.exec(2);
784                 var rows = [];
785                 for (var i in obj.holds) {
786                         rows.push( gen_list_append(obj.holds[i]) );
787                 }
788                 exec.chain( rows );
789         
790                 if (!dont_show_me_the_list_change) {
791                         if (window.xulG && typeof window.xulG.on_list_change == 'function') {
792                                 try { window.xulG.on_list_change(obj.holds); } catch(E) { this.error.sdump('D_ERROR',E); }
793                         }
794                 }
795         },
796
797         'render_lib_menu' : function() {
798                 try {
799                         var obj = this;
800                         JSAN.use('util.widgets'); JSAN.use('util.functional'); JSAN.use('util.fm_utils');
801                         var x = document.getElementById('menu_placeholder');
802                         if (x.firstChild) return;
803                         util.widgets.remove_children( x );
804         
805                         var ml = util.widgets.make_menulist( 
806                                 util.functional.map_list( 
807                                         obj.data.list.my_aou.concat(
808                                                 util.functional.filter_list(
809                                                         util.fm_utils.find_ou(
810                                                                 obj.data.tree.aou,
811                                                                 obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ].parent_ou()
812                                                         ).children(),
813                                                         function(o) {
814                                                                 return o.id() != obj.data.list.au[0].ws_ou();
815                                                         }
816                                                 )
817                                         ),
818                                         function(o) { return [ 
819                                                 o.shortname(), 
820                                                 o.id(), 
821                                                 ( ! get_bool( obj.data.hash.aout[ o.ou_type() ].can_have_users() ) ),
822                                                 ( obj.data.hash.aout[ o.ou_type() ].depth() ),
823                                         ]; }
824                                 ).sort(
825                                         function( a, b ) {
826                                                 var A = obj.data.hash.aou[ a[1] ];
827                                                 var B = obj.data.hash.aou[ b[1] ];
828                                                 var X = obj.data.hash.aout[ A.ou_type() ];
829                                                 var Y = obj.data.hash.aout[ B.ou_type() ];
830                                                 if (X.depth() < Y.depth()) return -1;
831                                                 if (X.depth() > Y.depth()) return 1;
832                                                 if (A.shortname() < B.shortname()) return -1;
833                                                 if (A.shortname() > B.shortname()) return 1;
834                                                 return 0;
835                                         }
836                                 ),
837                                 obj.data.list.au[0].ws_ou()
838                         );
839                         x.appendChild( ml );
840                         ml.addEventListener(
841                                 'command',
842                                 function(ev) {
843                                         /*
844                                         obj.list.on_all_fleshed = function() {
845                                                 obj.list.clear();
846                                                 obj.foreign_shelf = ev.target.value;
847                                                 obj.retrieve();
848                                                 setTimeout( function() { obj.list.on_all_fleshed = null; }, 0);
849                                         };
850                                         obj.list.full_retrieve();
851                                         */
852                                         obj.list.clear();
853                                         obj.foreign_shelf = ev.target.value;
854                                         obj.retrieve();
855                                 },
856                                 false
857                         );
858                         obj.controller.view.lib_menu = ml;
859                 } catch(E) {
860                         this.error.standard_unexpected_error_alert('rendering lib menu',E);
861                 }
862         },
863 }
864
865 dump('exiting patron.holds.js\n');