]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/admin/transit_list.js
Patch from Lebbeous Fogle-Weekley to change the behavior of the "Show Item Details...
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / admin / transit_list.js
1 dump('entering admin.transit_list.js\n');
2
3 if (typeof admin == 'undefined') admin = {};
4 admin.transit_list = function (params) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('util.network'); this.network = new util.network(); JSAN.use('util.file');
8     JSAN.use('util.date'); JSAN.use('util.widgets'); JSAN.use('util.fm_utils'); JSAN.use('util.functional');
9     JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10 }
11
12 admin.transit_list.prototype = {
13
14     'init' : function( params ) {
15
16         var obj = this;
17
18         var x = document.getElementById('libmenu_placeholder');
19         util.widgets.remove_children( x );
20
21         var file; var list_data; var ml; 
22
23         file = new util.file('offline_ou_list'); 
24         if (file._file.exists()) {
25             list_data = file.get_object(); file.close();
26             ml = util.widgets.make_menulist( list_data[0], list_data[1] );
27             ml.setAttribute('id','libmenu'); document.getElementById('libmenu_placeholder').appendChild(ml);
28             ml.addEventListener(
29                 'command',
30                 function(ev) {
31                     var file = new util.file('transit_list_prefs.'+obj.data.server_unadorned);
32                     util.widgets.save_attributes(file, { 'libmenu' : [ 'value' ] });
33                 },
34                 false
35             );
36         } else {
37             throw(document.getElementById('adminStrings').getString('staff.admin.transit_list.missing_list') + '\n');
38         }
39
40         file = new util.file('transit_list_prefs.'+obj.data.server_unadorned);
41         util.widgets.load_attributes(file);
42         ml.value = ml.getAttribute('value');
43         if (! ml.value) {
44             ml.value = obj.data.list.au[0].ws_ou();
45             ml.setAttribute('value',ml.value);
46         }
47
48         x.appendChild( ml );
49
50         obj.list_init();
51         obj.controller_init();
52         //obj.kick_off();
53
54     },
55
56     'sdate' : null,
57     'edate' : null,
58
59     'handle_date' : function(value,end_of_day) {
60         try {
61             var _date = null;
62
63             /* The Beginning */
64             if (value.match(/The Beginning/)) {
65                 _date = new Date(); _date.setTime( 0 );
66             }
67
68             /* Today */
69             if (value.match(/^Today$/i)) {
70                 _date = new Date(); _date = util.date.buildDate( _date.getFullYear(), _date.getMonth()+1, _date.getDate(), 0, 0, 0); // morning
71             }
72
73             /* handle YYYY-MM-DD */
74             var _string = value.match(/(\d\d\d\d)[\-\/](\d\d?)[\-\/](\d\d?)/);
75             if (_string) {
76                 if (util.date.check('YYYY-MM-DD',_string[1]+'-'+_string[2]+'-'+_string[3])) {
77                     _date = util.date.buildDate( _string[1], _string[2], _string[3], 0, 0, 0);
78                 } else {
79                     alert(document.getElementById('adminStrings').getFormattedString('staff.admin.transit_list.invalid_date', [_string])); 
80                     _date = new Date(); _date = util.date.buildDate( _date.getFullYear(), _date.getMonth()+1, _date.getDate(), 0, 0, 0); // morning
81                 }
82             }
83
84             /* handle relative dates */
85             var interval = value.match(/Today \- (.+)/);
86             if (interval) {
87                 _date = new Date(); _date = util.date.buildDate( _date.getFullYear(), _date.getMonth()+1, _date.getDate(), 0, 0, 0); // morning
88                 _date.setTime( _date.getTime() - util.date.interval_to_seconds(interval[1])*1000 );
89             }
90
91             if (! util.date.check('YYYY-MM-DD',util.date.formatted_date(_date,"%F")) ) { 
92                 alert(document.getElementById('adminStrings').getFormattedString('staff.admin.transit_list.invalid_date', [_date, util.date.formatted_date(_date,"%F")])); 
93                 _date = new Date(); _date = util.date.buildDate( _date.getFullYear(), _date.getMonth()+1, _date.getDate(), 0, 0, 0); // morning
94             }
95                 
96             if (end_of_day) { // This just handles calendar days.. if we wanted to support (Today-1 month,Today-1 month), I'll need a better library, or a query to postgres
97                 _date.setTime( _date.getTime() + util.date.interval_to_seconds('1 day')*1000 - util.date.interval_to_seconds('1 second')*1000 );
98             }
99
100             return util.date.formatted_date(_date,'%{iso8601}');
101         } catch(E) {
102             try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.date_processing.error'),E); } catch(F) { alert(E); }
103             _date = new Date(); _date = util.date.buildDate( _date.getFullYear(), _date.getMonth()+1, _date.getDate(), 0, 0, 0); // morning
104             return util.date.formatted_date(_date,'%{iso8601}');
105         }
106     },
107
108     'kick_off' : function() {
109         var obj = this;
110         try {
111             obj.list.clear();
112             obj.sdate = obj.handle_date( document.getElementById('sdate').value, false );
113             obj.edate = obj.handle_date( document.getElementById('edate').value, true );
114             var sdate; var edate;
115             if (obj.sdate < obj.edate) {
116                 sdate = obj.sdate; edate = obj.edate;
117             } else {
118                 sdate = obj.edate; edate = obj.sdate;
119             }
120             obj.network.simple_request('FM_ATC_RETRIEVE_VIA_AOU',[ ses(), /*obj.data.list.au[ 0 ].ws_ou()*/ document.getElementById('libmenu').value, sdate, edate ], 
121                 function(req) {
122                     try {
123                         var robj = req.getResultObject();
124                         if (typeof robj.ilsevent != 'undefined') throw(robj);
125
126                         JSAN.use('util.exec'); 
127                         var exec = new util.exec(2);
128                         var exec2 = new util.exec(2);
129
130                         function gen_list_append(id,which_list) {
131                             return function() {
132                                 switch(which_list) {
133                                     case 0: obj.list.append( { 'row' : { 'my' : { 'transit_id' : id } }, 'no_auto_select' : true } ); break;
134                                     case 1: obj.list.append( { 'row' : { 'my' : { 'transit_id' : id } }, 'no_auto_select' : true } ); break;
135                                 }
136                             };
137                         }
138
139                         var rows = []; 
140
141                         if (document.getElementById('transit_direction').value=='transit_from') for (var i = 0; i < robj.from.length; i++) {
142                             //get_transit(robj.from[i], 0);
143                             rows.push( gen_list_append(robj.from[i],0) );
144                         }
145
146                         if (document.getElementById('transit_direction').value=='transit_to') for (var i = 0; i < robj.to.length; i++) {
147                             //get_transit(robj.to[i], 1);
148                             rows.push( gen_list_append(robj.to[i],1) );
149                         }
150                 
151                         if (rows.length > 0) {
152                             exec.chain( rows );
153                         } else {
154                             alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.no_match'));
155                         }
156
157                     } catch(E) {
158                         try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.retrieving_transit.error'),E); } catch(F) { alert(E); }
159                     }
160                 }
161             );
162         } catch(E) {
163             try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.preretrieving_transit.error'),E); } catch(F) { alert(E); }
164         }
165     },
166
167     'list_init' : function() {
168
169         var obj = this;
170
171         obj.selection_list = [];
172
173         JSAN.use('circ.util'); 
174         var columns = circ.util.transit_columns(
175             {
176                 'transit_source' : { 'hidden' : false },
177                 'transit_source_send_time' : { 'hidden' : false },
178                 'transit_dest_lib' : { 'hidden' : false },
179                 'transit_item_barcode' : { 'hidden' : false },
180                 'transit_item_title' : { 'hidden' : false },
181             },
182             {
183                 'just_these' : [
184                     'transit_id',
185                     'transit_source',
186                     'transit_source_send_time',
187                     'transit_dest_lib',
188                     'transit_item_barcode',
189                     'transit_item_title',
190                     'transit_item_author',
191                     'transit_item_callnumber',
192                     'transit_target_copy',
193                 ]
194             }
195         ).concat( 
196             circ.util.hold_columns(
197                 {
198                     'request_time' : { 'hidden' : false },
199                 },
200                 {
201                     'just_these' : [
202                         'request_timestamp',
203                         'request_time',
204                         'capture_timestamp',
205                         'capture_time',
206                         'hold_type',
207                         'expire_time',
208                         'patron_first_given_name',
209                         'patron_family_name',
210                         'patron_barcode',
211                     ],
212                 }
213             ) 
214         );
215
216         JSAN.use('util.list'); 
217         obj.list = new util.list('transit_list');
218         obj.list.init( 
219             { 
220                 'columns' : columns, 
221                 'map_row_to_columns' : circ.util.std_map_row_to_columns(), 
222                 'retrieve_row' : function(params) {
223                     var row = params.row;
224                     try {
225                         obj.get_transit_and_hold_and_run_func(
226                             row.my.transit_id,
227                             function(transit,hold) { return obj.get_rest_of_row_given_transit_and_hold(params,transit,hold); }
228                         );
229                     } catch(E) {
230                         try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.retrieving_row.error'),E); } catch(F) { alert(E); }
231                     }
232                 },
233                 'on_select' : function(ev) {
234                     try {
235                         JSAN.use('util.functional');
236                         var sel = obj.list.retrieve_selection();
237                         obj.selection_list = util.functional.map_list(
238                             sel,
239                             function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
240                         );
241                         obj.error.sdump('D_TRACE','admin.transit_list: selection list = ' + js2JSON(obj.selection_list) );
242                         if (obj.selection_list.length == 0) {
243                             obj.controller.view.sel_edit.setAttribute('disabled','true');
244                             obj.controller.view.sel_opac.setAttribute('disabled','true');
245                             obj.controller.view.sel_bucket.setAttribute('disabled','true');
246                             obj.controller.view.sel_copy_details.setAttribute('disabled','true');
247                             obj.controller.view.sel_patron.setAttribute('disabled','true');
248                             obj.controller.view.sel_transit_abort.setAttribute('disabled','true');
249                             obj.controller.view.sel_clip.setAttribute('disabled','true');
250                         } else {
251                             obj.controller.view.sel_edit.setAttribute('disabled','false');
252                             obj.controller.view.sel_opac.setAttribute('disabled','false');
253                             obj.controller.view.sel_patron.setAttribute('disabled','false');
254                             obj.controller.view.sel_bucket.setAttribute('disabled','false');
255                             obj.controller.view.sel_copy_details.setAttribute('disabled','false');
256                             obj.controller.view.sel_transit_abort.setAttribute('disabled','false');
257                             obj.controller.view.sel_clip.setAttribute('disabled','false');
258                         }
259                     } catch(E) {
260                         alert('FIXME: ' + E);
261                     }
262                 },
263             }
264         );
265     },
266
267     'get_transit_and_hold_and_run_func' : function (transit_id,do_this) {
268         var obj = this;
269         obj.network.simple_request('FM_ATC_RETRIEVE', [ ses(), transit_id ],
270             function(req2) {
271                 try {
272                     var r_atc = req2.getResultObject();
273                     if (typeof r_atc.ilsevent != 'undefined') throw(r_atc);
274
275                     if (instanceOf(r_atc,atc)) {
276                         do_this(r_atc,null);
277                     } else if (instanceOf(r_atc,ahtc)) {
278                         obj.network.simple_request('FM_AHR_RETRIEVE', [ ses(), r_atc.hold() ],
279                             function(req3) {
280                                 try {
281                                     var r_ahr = req3.getResultObject();
282                                     if (typeof r_ahr.ilsevent != 'undefined') throw(r_ahr);
283                                     if (r_ahr.length == 0) {
284                                         try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getString('staff.admin.transit_list.empty_array.error') + document.getElementById('adminStrings').getFormattedString('staff.admin.transit_list.empty_array.error', [r_atc.hold(), transit_id]),E); } catch(F) { alert(E); }
285                                         do_this(r_atc,null);
286                                     } else {
287                                         if (instanceOf(r_ahr[0],ahr)) {
288                                             do_this(r_atc,r_ahr[0]);
289                                         } else {
290                                             throw(r_ahr);
291                                         }
292                                     }
293                                 } catch(E) {
294                                     try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getFormattedString('staff.admin.transit_list.empty_array.error', [r_atc.hold(), transit_id]),E); } catch(F) { alert(E); }
295                                     do_this(r_atc,null);
296                                 }
297                             }
298                         );
299                     } else {
300                         throw(r_atc);
301                     }
302
303                 } catch(E) {
304                     try { obj.error.standard_unexpected_error_alert(document.getElementById('adminStrings').getFormattedString('staff.admin.transit_list.transit_id.error', [transit_id]),E); } catch(F) { alert(E); }
305                 }
306             }
307         );
308     },
309
310     'get_rest_of_row_given_transit_and_hold' : function(params,transit,hold) {
311         var obj = this;
312         var row = params.row;
313
314         row.my.atc = transit;
315         if (hold) row.my.ahr = hold;
316
317         obj.network.simple_request(
318             'FM_ACP_RETRIEVE',
319             [ row.my.atc.target_copy() ],
320             function(req) {
321                 try { 
322                     var r_acp = req.getResultObject();
323                     if (typeof r_acp.ilsevent != 'undefined') throw(r_acp);
324                     row.my.acp = r_acp;
325
326                     obj.network.simple_request(
327                         'FM_ACN_RETRIEVE.authoritative',
328                         [ r_acp.call_number() ],
329                         function(req2) {
330                             try {
331                                 var r_acn = req2.getResultObject();
332                                 if (typeof r_acn.ilsevent != 'undefined') throw(r_acn);
333                                 row.my.acn = r_acn;
334
335                                 if (row.my.acn.record() > 0) {
336                                     obj.network.simple_request(
337                                         'MODS_SLIM_RECORD_RETRIEVE.authoritative',
338                                         [ r_acn.record() ],
339                                         function(req3) {
340                                             try {
341                                                 var r_mvr = req3.getResultObject();
342                                                 if (typeof r_mvr.ilsevent != 'undefined') throw(r_mvr);
343                                                 row.my.mvr = r_mvr;
344
345                                                 params.row_node.setAttribute(
346                                                     'retrieve_id', js2JSON( { 
347                                                         'copy_id' : row.my.acp ? row.my.acp.id() : null, 
348                                                         'doc_id' : row.my.mvr ? row.my.mvr.doc_id() : null,  
349                                                         'barcode' : row.my.acp ? row.my.acp.barcode() : null, 
350                                                         'acp_id' : row.my.acp ? row.my.acp.id() : null, 
351                                                         'acn_id' : row.my.acn ? row.my.acn.id() : null,  
352                                                         'atc_id' : row.my.atc ? row.my.atc.id() : null,  
353                                                         'ahr_id' : row.my.ahr ? row.my.ahr.id() : null,  
354                                                     } )
355                                                 );
356                                                 if (typeof params.on_retrieve == 'function') {
357                                                     params.on_retrieve(row);
358                                                 }
359                                             } catch(E) {
360                                                 try { obj.error.standard_unexpected_error_alert('retrieving mvr',E); } catch(F) { alert(E); }
361                                             }
362                                         }
363                                     );
364                                 } else {
365                                     params.row_node.setAttribute(
366                                         'retrieve_id', js2JSON( { 
367                                             'copy_id' : row.my.acp ? row.my.acp.id() : null, 
368                                             'doc_id' : row.my.mvr ? row.my.mvr.doc_id() : null,  
369                                             'barcode' : row.my.acp ? row.my.acp.barcode() : null, 
370                                             'acp_id' : row.my.acp ? row.my.acp.id() : null, 
371                                             'acn_id' : row.my.acn ? row.my.acn.id() : null,  
372                                             'atc_id' : row.my.atc ? row.my.atc.id() : null,  
373                                             'ahr_id' : row.my.ahr ? row.my.ahr.id() : null,  
374                                         } )
375                                     );
376                                     if (typeof params.on_retrieve == 'function') {
377                                         params.on_retrieve(row);
378                                     }
379                                 }
380                     
381                             } catch(E) {
382                                 try { obj.error.standard_unexpected_error_alert('retrieving acn',E); } catch(F) { alert(E); }
383                             }
384                         }
385                     );
386
387
388                 } catch(E) {
389                     try { obj.error.standard_unexpected_error_alert('retrieving acp',E); } catch(F) { alert(E); }
390                 }
391             }
392         );
393     },
394
395     'controller_init' : function() {
396         var obj = this;
397
398         JSAN.use('util.controller'); obj.controller = new util.controller();
399         obj.controller.init(
400             {
401                 'control_map' : {
402                     'save_columns' : [ [ 'command' ], function() { obj.list.save_columns(); } ],
403                     'sel_clip' : [ ['command'], function() { obj.list.clipboard(); } ],
404                     'sel_edit' : [ ['command'], function() { try { obj.spawn_copy_editor(0); } catch(E) { alert(E); } } ],
405                     'sel_opac' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.show_in_opac(obj.selection_list); } ],
406                     'sel_transit_abort' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.abort_transits(obj.selection_list); } ],
407                     'sel_patron' : [ ['command'], function() { JSAN.use('circ.util'); circ.util.show_last_few_circs(obj.selection_list); } ],
408                     'sel_copy_details' : [ ['command'],
409                         function() {
410                             JSAN.use('circ.util');
411                             circ.util.item_details_new(
412                                 util.functional.map_list(
413                                     obj.selection_list,
414                                     function(o) { return o.barcode; }
415                                 )
416                             );
417                         } ],
418                     'sel_bucket' : [ ['command'], function() { JSAN.use('cat.util'); cat.util.add_copies_to_bucket(obj.selection_list); } ],
419                     'cmd_print_list' : [ ['command'], function() { obj.print_list(0); } ],
420                     'cmd_kick_off' : [ ['command'], function(ev) { ev.target.disabled = true; obj.kick_off(); } ],
421                     'sdate' : [ ['change'], function(ev) { ev.target.value = obj.handle_date(ev.target.value,false); obj.sdate = ev.target.value; /*alert('obj.sdate='+obj.sdate);*/ } ],
422                     'edate' : [ ['change'], function(ev) { ev.target.value = obj.handle_date(ev.target.value,true); obj.edate = ev.target.value; /*alert('obj.edate='+obj.edate);*/ } ],
423                 }
424             }
425         );
426         this.controller.render();
427
428     },
429
430     'print_list' : function(which_list) {
431         var obj = this;
432         try {
433             var list = which_list == 0 ? obj.list : obj.list2;
434             var p = { 
435                 'template' : 'transit_list'
436             };
437             list.print(p);
438         } catch(E) {
439             obj.error.standard_unexpected_error_alert('print',E); 
440         }
441     },
442     
443     'spawn_copy_editor' : function(which_list) {
444
445         var obj = this;
446
447         JSAN.use('util.functional');
448
449         var list = which_list == 0 ? obj.selection_list : obj.selection_list2;
450
451         list = util.functional.map_list(
452             list,
453             function (o) {
454                 return o.copy_id;
455             }
456         );
457
458         JSAN.use('cat.util'); cat.util.spawn_copy_editor( { 'copy_ids' : list, 'edit' : 1 } );
459
460     },
461
462 }
463
464 dump('exiting admin.transit_list.js\n');