]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
some cosmetics, and batch item delete which doesnt work
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / copy_buckets.js
1 dump('entering cat.copy_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.copy_buckets = 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('util.date');
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init({'via':'stash'});
10 }
11
12 cat.copy_buckets.prototype = {
13         'selection_list1' : [],
14         'selection_list2' : [],
15         'bucket_id_name_map' : {},
16
17         'render_pending_copies' : function() {
18                 var obj = this;
19                 obj.list1.clear();
20                 for (var i = 0; i < obj.copy_ids.length; i++) {
21                         var item = obj.flesh_item_for_list( obj.copy_ids[i] );
22                         if (item) obj.list1.append( item );
23                 }
24         },
25
26         'init' : function( params ) {
27
28                 var obj = this;
29
30                 obj.copy_ids = params['copy_ids'] || [];
31
32                 JSAN.use('circ.util');
33                 var columns = circ.util.columns( 
34                         { 
35                                 'barcode' : { 'hidden' : false },
36                                 'title' : { 'hidden' : false },
37                                 'location' : { 'hidden' : false },
38                                 'call_number' : { 'hidden' : false },
39                                 'status' : { 'hidden' : false },
40                         } 
41                 );
42
43                 JSAN.use('util.list'); 
44
45                 obj.list1 = new util.list('pending_copies_list');
46                 obj.list1.init(
47                         {
48                                 'columns' : columns,
49                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
50                                 'on_select' : function(ev) {
51                                         try {
52                                                 JSAN.use('util.functional');
53                                                 var sel = obj.list1.retrieve_selection();
54                                                 obj.selection_list1 = util.functional.map_list(
55                                                         sel,
56                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
57                                                 );
58                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
59                                                 if (obj.selection_list1.length == 0) {
60                                                         obj.controller.view.copy_buckets_sel_add.disabled = true;
61                                                 } else {
62                                                         obj.controller.view.copy_buckets_sel_add.disabled = false;
63                                                 }
64                                         } catch(E) {
65                                                 alert('FIXME: ' + E);
66                                         }
67                                 },
68
69                         }
70                 );
71
72                 obj.render_pending_copies();
73         
74                 obj.list2 = new util.list('copies_in_bucket_list');
75                 obj.list2.init(
76                         {
77                                 'columns' : columns,
78                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
79                                 'on_select' : function(ev) {
80                                         try {
81                                                 JSAN.use('util.functional');
82                                                 var sel = obj.list2.retrieve_selection();
83                                                 obj.selection_list2 = util.functional.map_list(
84                                                         sel,
85                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
86                                                 );
87                                                 obj.error.sdump('D_TRACE','circ/copy_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
88                                                 if (obj.selection_list2.length == 0) {
89                                                         obj.controller.view.copy_buckets_delete_item.disabled = true;
90                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','true');
91                                                         obj.controller.view.copy_buckets_export.disabled = true;
92                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','true');
93                                                 } else {
94                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
95                                                         obj.controller.view.copy_buckets_delete_item.setAttribute('disabled','false');
96                                                         obj.controller.view.copy_buckets_export.disabled = false;
97                                                         obj.controller.view.copy_buckets_export.setAttribute('disabled','false');
98                                                 }
99                                         } catch(E) {
100                                                 alert('FIXME: ' + E);
101                                         }
102                                 },
103                         }
104                 );
105                 
106                 JSAN.use('util.controller'); obj.controller = new util.controller();
107                 obj.controller.init(
108                         {
109                                 'control_map' : {
110                                         'copy_buckets_menulist_placeholder' : [
111                                                 ['render'],
112                                                 function(e) {
113                                                         return function() {
114                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
115                                                                 var items = [ ['Choose a bucket...',''] ].concat(
116                                                                         util.functional.map_list(
117                                                                                 obj.network.simple_request(
118                                                                                         'BUCKET_RETRIEVE_VIA_USER',
119                                                                                         [ ses(), obj.data.list.au[0].id() ]
120                                                                                 ).copy,
121                                                                                 function(o) {
122                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
123                                                                                         return [ o.name(), o.id() ];
124                                                                                 }
125                                                                         )
126                                                                 );
127                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
128                                                                 util.widgets.remove_children( e );
129                                                                 var ml = util.widgets.make_menulist(
130                                                                         items
131                                                                 );
132                                                                 e.appendChild( ml );
133                                                                 ml.setAttribute('id','bucket_menulist');
134                                                                 ml.setAttribute('accesskey','');
135
136                                                                 function change_bucket(ev) {
137                                                                         var bucket_id = ev.target.value;
138                                                                         if (!bucket_id) return;
139                                                                         var bucket = obj.network.simple_request(
140                                                                                 'BUCKET_FLESH',
141                                                                                 [ ses(), 'copy', bucket_id ]
142                                                                         );
143                                                                         var items = bucket.items() || [];
144                                                                         obj.list2.clear();
145                                                                         for (var i = 0; i < items.length; i++) {
146                                                                                 var item = obj.flesh_item_for_list( 
147                                                                                         items[i].target_copy(),
148                                                                                         items[i].id()
149                                                                                 );
150                                                                                 if (item) obj.list2.append( item );
151                                                                         }
152                                                                 }
153
154                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
155                                                                 ml.addEventListener( 'command', function() {
156                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
157                                                                 }, false);
158                                                                 obj.controller.view.bucket_menulist = ml;
159                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
160                                                         };
161                                                 },
162                                         ],
163
164                                         'copy_buckets_add' : [
165                                                 ['command'],
166                                                 function() {
167                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
168                                                         if (!bucket_id) return;
169                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
170                                                                 var bucket_item = new ccbi();
171                                                                 bucket_item.isnew('1');
172                                                                 bucket_item.bucket(bucket_id);
173                                                                 bucket_item.target_copy( obj.copy_ids[i] );
174                                                                 try {
175                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
176                                                                                 [ ses(), 'copy', bucket_item ]);
177
178                                                                         if (typeof robj == 'object') throw robj;
179
180                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
181                                                                         if (!item) continue;
182
183                                                                         obj.list2.append( item );
184                                                                 } catch(E) {
185                                                                         obj.error.standard_unexpected_error_alert('Addition likely failed.',E);
186                                                                 }
187                                                         }
188                                                 }
189                                         ],
190                                         'copy_buckets_sel_add' : [
191                                                 ['command'],
192                                                 function() {                                                        
193                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
194                                                         if (!bucket_id) return;
195                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
196                                                                 var acp_id = obj.selection_list1[i][0];
197                                                                 //var barcode = obj.selection_list1[i][1];
198                                                                 var bucket_item = new ccbi();
199                                                                 bucket_item.isnew('1');
200                                                                 bucket_item.bucket(bucket_id);
201                                                                 bucket_item.target_copy( acp_id );
202                                                                 try {
203                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
204                                                                                 [ ses(), 'copy', bucket_item ]);
205
206                                                                         if (typeof robj == 'object') throw robj;
207
208                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
209                                                                         if (!item) continue;
210
211                                                                         obj.list2.append( item );
212                                                                 } catch(E) {
213                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
214                                                                 }
215                                                         }
216
217                                                 }
218                                         ],
219                                         'copy_buckets_export' : [
220                                                 ['command'],
221                                                 function() {                                                        
222                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
223                                                                 var acp_id = obj.selection_list2[i][0];
224                                                                 //var barcode = obj.selection_list1[i][1];
225                                                                 //var bucket_item_id = obj.selection_list1[i][2];
226                                                                 var item = obj.flesh_item_for_list( acp_id );
227                                                                 if (item) {
228                                                                         obj.list1.append( item );
229                                                                         obj.copy_ids.push( acp_id );
230                                                                 }
231                                                         }
232                                                 }
233                                         ],
234
235                                         'copy_buckets_delete_item' : [
236                                                 ['command'],
237                                                 function() {
238                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
239                                                                 try {
240                                                                         //var acp_id = obj.selection_list2[i][0];
241                                                                         //var barcode = obj.selection_list2[i][1];
242                                                                         var bucket_item_id = obj.selection_list2[i][2];
243                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
244                                                                                 [ ses(), 'copy', bucket_item_id ]);
245                                                                         if (typeof robj == 'object') throw robj;
246                                                                 } catch(E) {
247                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
248                                                                 }
249                                                         }
250                                                         setTimeout(
251                                                                 function() {
252                                                                         JSAN.use('util.widgets'); 
253                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
254                                                                 }, 0
255                                                         );
256                                                 }
257                                         ],
258                                         'copy_buckets_delete_bucket' : [
259                                                 ['command'],
260                                                 function() {
261                                                         try {
262                                                                 var bucket = obj.controller.view.bucket_menulist.value;
263                                                                 var name = obj.bucket_id_name_map[ bucket ];
264                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
265                                                                 if (!conf) return;
266                                                                 obj.list2.clear();
267                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'copy',bucket]);
268                                                                 if (typeof robj == 'object') throw robj;
269                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
270                                                         } catch(E) {
271                                                                 obj.error.standard_unexpected_error_alert('Bucket deletion likely failed.',E);
272                                                         }
273                                                 }
274                                         ],
275                                         'copy_buckets_new_bucket' : [
276                                                 ['command'],
277                                                 function() {
278                                                         try {
279                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
280
281                                                                 if (name) {
282                                                                         var bucket = new ccb();
283                                                                         bucket.btype('staff_client');
284                                                                         bucket.owner( obj.data.list.au[0].id() );
285                                                                         bucket.name( name );
286
287                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'copy',bucket]);
288
289                                                                         if (typeof robj == 'object') throw robj;
290
291                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
292                                                                         obj.controller.view.bucket_menulist.value = robj;
293                                                                         setTimeout(
294                                                                                 function() {
295                                                                                         JSAN.use('util.widgets'); 
296                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
297                                                                                 }, 0
298                                                                         );
299                                                                 }
300                                                         } catch(E) {
301                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
302                                                         }
303                                                 }
304                                         ],
305                                         'copy_buckets_batch_copy_edit' : [
306                                                 ['command'],
307                                                 function() {
308                                                         try {
309                                                         
310                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
311
312                                                                 var list = util.functional.map_list(
313                                                                         obj.list2.dump_retrieve_ids(),
314                                                                         function (o) {
315                                                                                 return JSON2js(o)[0]; // acp_id
316                                                                         }
317                                                                 );
318
319                                                                 var copies = util.functional.map_list(
320                                                                         list,
321                                                                         function (acp_id) {
322                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
323                                                                         }
324                                                                 );
325
326                                                                 var edit = 0;
327                                                                 try {
328                                                                         edit = obj.network.request(
329                                                                                 api.PERM_MULTI_ORG_CHECK.app,
330                                                                                 api.PERM_MULTI_ORG_CHECK.method,
331                                                                                 [ 
332                                                                                         ses(), 
333                                                                                         obj.data.list.au[0].id(), 
334                                                                                         util.functional.map_list(
335                                                                                                 copies,
336                                                                                                 function (o) {
337                                                                                                         return obj.network.simple_request('FM_ACN_RETRIEVE',[o.call_number()]).owning_lib();
338                                                                                                 }
339                                                                                         ),
340                                                                                         [ 'UPDATE_COPY', 'UPDATE_BATCH_COPY' ]
341                                                                                 ]
342                                                                         ).length == 0 ? 1 : 0;
343                                                                 } catch(E) {
344                                                                         obj.error.sdump('D_ERROR','batch permission check: ' + E);
345                                                                 }
346
347                                                                 JSAN.use('cat.util'); cat.util.spawn_copy_editor(list,edit);
348
349                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
350                                                                 setTimeout(
351                                                                         function() {
352                                                                                 JSAN.use('util.widgets'); 
353                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
354                                                                         }, 0
355                                                                 );
356                                                         } catch(E) {
357                                                                 alert( js2JSON(E) );
358                                                         }
359                                                 }
360                                         ],
361                                         'copy_buckets_batch_copy_delete' : [
362                                                 ['command'],
363                                                 function() {
364                                                         try {
365                                                         
366                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
367
368                                                                 var list = util.functional.map_list(
369                                                                         obj.list2.dump_retrieve_ids(),
370                                                                         function (o) {
371                                                                                 return JSON2js(o)[0]; // acp_id
372                                                                         }
373                                                                 );
374
375                                                                 var copies = util.functional.map_list(
376                                                                         list,
377                                                                         function (acp_id) {
378                                                                                 return obj.network.simple_request('FM_ACP_RETRIEVE',[acp_id]);
379                                                                         }
380                                                                 );
381
382                                                                 for (var i = 0; i < copies.length; i++) {
383                                                                         copies[i].ischanged(1);
384                                                                         copies[i].isdeleted(1);
385                                                                 }
386
387                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',[ ses, copies, true]);
388                                                                 if (typeof robj.ilsevent != 'undefined') obj.error.standard_unexpected_error_alert('Batch Item Deletion',robj);
389
390                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
391                                                                 setTimeout(
392                                                                         function() {
393                                                                                 JSAN.use('util.widgets'); 
394                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
395                                                                         }, 0
396                                                                 );
397                                                         } catch(E) {
398                                                                 alert( js2JSON(E) );
399                                                         }
400                                                 }
401                                         ],
402
403                                         'copy_buckets_transfer_to_volume' : [
404                                                 ['command'],
405                                                 function() {
406                                                         try {
407                                                                 // FM_ACN_RETRIEVE
408                                                                 obj.data.stash_retrieve();
409                                                                 if (!obj.data.marked_volume) {
410                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
411                                                                         return;
412                                                                 }
413                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
414                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
415                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
416                                                                 xml += '<description>Transfer the copies in bucket "';
417                                                                 xml += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
418                                                                 xml += 'from their original volumes to ';
419                                                                 xml += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
420                                                                 xml += '"' + volume.label() + '" on the following record?</description>';
421                                                                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
422                                                                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
423                                                                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + volume.record() + '"/>';
424                                                                 xml += '</vbox>';
425                                                                 obj.data.temp_transfer = xml; obj.data.stash('temp_transfer');
426                                                                 window.open(
427                                                                         urls.XUL_FANCY_PROMPT
428                                                                         + '?xml_in_stash=temp_transfer'
429                                                                         + '&title=' + window.escape('Copy Transfer'),
430                                                                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300'
431                                                                 );
432                                                                 JSAN.use('OpenILS.data');
433                                                                 var data = new OpenILS.data(); data.init({'via':'stash'});
434                                                                 if (data.fancy_prompt_data == '') { alert('Transfer Aborted'); return; }
435
436                                                                 JSAN.use('util.functional');
437
438                                                                 var copies = obj.network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [
439                                                                         util.functional.map_list(
440                                                                                 obj.list2.dump_retrieve_ids(),
441                                                                                 function (o) {
442                                                                                         return JSON2js(o)[0]; // acp_id
443                                                                                 }
444                                                                         )
445                                                                 ]);
446
447                                                                 for (var i = 0; i < copies.length; i++) {
448                                                                         copies[i].call_number( obj.data.marked_volume );
449                                                                         copies[i].ischanged( 1 );
450                                                                 }
451
452                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), copies, true ]);
453
454                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
455                                                                 setTimeout(
456                                                                         function() {
457                                                                                 JSAN.use('util.widgets'); 
458                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
459                                                                         }, 0
460                                                                 );
461                                                                 
462                                                                 if (typeof robj.ilsevent != 'undefined') {
463                                                                         throw(robj);
464                                                                 } else {
465                                                                         alert('Copies transferred.');
466                                                                 }
467
468                                                         } catch(E) {
469                                                                 obj.error.standard_unexpected_error_alert('Copies not likely transferred.',E);
470                                                         }
471                                                 }
472                                         ],
473                                         'cmd_broken' : [
474                                                 ['command'],
475                                                 function() { alert('Not Yet Implemented'); }
476                                         ],
477                                         'cmd_copy_buckets_print' : [
478                                                 ['command'],
479                                                 function() {
480                                                         dump( js2JSON( obj.list2.dump() ) );
481                                                         alert( js2JSON( obj.list2.dump() ) );
482                                                 }
483                                         ],
484                                         'cmd_copy_buckets_reprint' : [
485                                                 ['command'],
486                                                 function() {
487                                                 }
488                                         ],
489                                         'cmd_copy_buckets_done' : [
490                                                 ['command'],
491                                                 function() {
492                                                         window.close();
493                                                 }
494                                         ],
495                                         'cmd_export_to_copy_status' : [
496                                                 ['command'],
497                                                 function() {
498                                                         try {
499                                                                 JSAN.use('util.functional');
500                                                                 var barcodes = util.functional.map_list(
501                                                                         obj.list2.dump_retrieve_ids(),
502                                                                         function(o) { return JSON2js(o)[1]; }
503                                                                 );
504                                                                 var url = urls.XUL_COPY_STATUS + '?barcodes=' + window.escape( js2JSON(barcodes) );
505                                                                 xulG.new_tab( url, {}, {});
506                                                         } catch(E) {
507                                                                 obj.error.standard_unexpected_error_alert('Copy Status from Copy Buckets',E);
508                                                         }
509                                                 }
510                                         ],
511                                 }
512                         }
513                 );
514                 this.controller.render();
515
516                 if (typeof xulG == 'undefined') {
517                         obj.controller.view.cmd_export_to_copy_status.disabled = true;
518                         obj.controller.view.cmd_export_to_copy_status.setAttribute('disabled',true);
519                 } else {
520                         obj.controller.view.cmd_copy_buckets_done.disabled = true;
521                         obj.controller.view.cmd_copy_buckets_done.setAttribute('disabled',true);
522                 }
523         
524         },
525
526         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
527                 var obj = this;
528                 try {
529                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
530                         if (copy == null || typeof copy.ilsevent != 'undefined') {
531                                 throw(copy);
532                         } else {
533                                 var item = {
534                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
535                                         'row' : {
536                                                 'my' : {
537                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
538                                                         'acp' : copy,
539                                                 }
540                                         }
541                                 };
542                                 return item;
543                         }
544                 } catch(E) {
545                         obj.error.standard_unexpected_error_alert('List building failed.',E);
546                         return null;
547                 }
548
549         },
550         
551 }
552
553 dump('exiting cat.copy_buckets.js\n');