]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/copy_buckets.js
info for copy transfer
[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                                                 } else {
91                                                         obj.controller.view.copy_buckets_delete_item.disabled = false;
92                                                 }
93                                         } catch(E) {
94                                                 alert('FIXME: ' + E);
95                                         }
96                                 },
97                         }
98                 );
99                 
100                 JSAN.use('util.controller'); obj.controller = new util.controller();
101                 obj.controller.init(
102                         {
103                                 'control_map' : {
104                                         'copy_buckets_menulist_placeholder' : [
105                                                 ['render'],
106                                                 function(e) {
107                                                         return function() {
108                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
109                                                                 var items = [ ['Choose a bucket...',''] ].concat(
110                                                                         util.functional.map_list(
111                                                                                 obj.network.simple_request(
112                                                                                         'BUCKET_RETRIEVE_VIA_USER',
113                                                                                         [ ses(), obj.data.list.au[0].id() ]
114                                                                                 ).copy,
115                                                                                 function(o) {
116                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
117                                                                                         return [ o.name(), o.id() ];
118                                                                                 }
119                                                                         )
120                                                                 );
121                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
122                                                                 util.widgets.remove_children( e );
123                                                                 var ml = util.widgets.make_menulist(
124                                                                         items
125                                                                 );
126                                                                 e.appendChild( ml );
127                                                                 ml.setAttribute('id','bucket_menulist');
128                                                                 ml.setAttribute('accesskey','');
129
130                                                                 function change_bucket(ev) {
131                                                                         var bucket_id = ev.target.value;
132                                                                         if (!bucket_id) return;
133                                                                         var bucket = obj.network.simple_request(
134                                                                                 'BUCKET_FLESH',
135                                                                                 [ ses(), 'copy', bucket_id ]
136                                                                         );
137                                                                         var items = bucket.items() || [];
138                                                                         obj.list2.clear();
139                                                                         for (var i = 0; i < items.length; i++) {
140                                                                                 var item = obj.flesh_item_for_list( 
141                                                                                         items[i].target_copy(),
142                                                                                         items[i].id()
143                                                                                 );
144                                                                                 if (item) obj.list2.append( item );
145                                                                         }
146                                                                 }
147
148                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
149                                                                 ml.addEventListener( 'command', function() {
150                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
151                                                                 }, false);
152                                                                 obj.controller.view.bucket_menulist = ml;
153                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
154                                                         };
155                                                 },
156                                         ],
157
158                                         'copy_buckets_add' : [
159                                                 ['command'],
160                                                 function() {
161                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
162                                                         if (!bucket_id) return;
163                                                         for (var i = 0; i < obj.copy_ids.length; i++) {
164                                                                 var bucket_item = new ccbi();
165                                                                 bucket_item.isnew('1');
166                                                                 bucket_item.bucket(bucket_id);
167                                                                 bucket_item.target_copy( obj.copy_ids[i] );
168                                                                 try {
169                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
170                                                                                 [ ses(), 'copy', bucket_item ]);
171
172                                                                         if (typeof robj == 'object') throw robj;
173
174                                                                         var item = obj.flesh_item_for_list( obj.copy_ids[i], robj );
175                                                                         if (!item) continue;
176
177                                                                         obj.list2.append( item );
178                                                                 } catch(E) {
179                                                                         obj.error.standard_unexpected_error_alert('Addition likely failed.',E);
180                                                                 }
181                                                         }
182                                                 }
183                                         ],
184                                         'copy_buckets_sel_add' : [
185                                                 ['command'],
186                                                 function() {                                                        
187                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
188                                                         if (!bucket_id) return;
189                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
190                                                                 var acp_id = obj.selection_list1[i][0];
191                                                                 //var barcode = obj.selection_list1[i][1];
192                                                                 var bucket_item = new ccbi();
193                                                                 bucket_item.isnew('1');
194                                                                 bucket_item.bucket(bucket_id);
195                                                                 bucket_item.target_copy( acp_id );
196                                                                 try {
197                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
198                                                                                 [ ses(), 'copy', bucket_item ]);
199
200                                                                         if (typeof robj == 'object') throw robj;
201
202                                                                         var item = obj.flesh_item_for_list( acp_id, robj );
203                                                                         if (!item) continue;
204
205                                                                         obj.list2.append( item );
206                                                                 } catch(E) {
207                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
208                                                                 }
209                                                         }
210
211                                                 }
212                                         ],
213                                         'copy_buckets_export' : [
214                                                 ['command'],
215                                                 function() {                                                        
216                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
217                                                                 var acp_id = obj.selection_list2[i][0];
218                                                                 //var barcode = obj.selection_list1[i][1];
219                                                                 //var bucket_item_id = obj.selection_list1[i][2];
220                                                                 var item = obj.flesh_item_for_list( acp_id );
221                                                                 if (item) {
222                                                                         obj.list1.append( item );
223                                                                         obj.copy_ids.push( acp_id );
224                                                                 }
225                                                         }
226                                                 }
227                                         ],
228
229                                         'copy_buckets_delete_item' : [
230                                                 ['command'],
231                                                 function() {
232                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
233                                                                 try {
234                                                                         //var acp_id = obj.selection_list2[i][0];
235                                                                         //var barcode = obj.selection_list2[i][1];
236                                                                         var bucket_item_id = obj.selection_list2[i][2];
237                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
238                                                                                 [ ses(), 'copy', bucket_item_id ]);
239                                                                         if (typeof robj == 'object') throw robj;
240                                                                 } catch(E) {
241                                                                         obj.error.standard_unexpected_error_alert('Deletion likely failed.',E);
242                                                                 }
243                                                         }
244                                                         setTimeout(
245                                                                 function() {
246                                                                         JSAN.use('util.widgets'); 
247                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
248                                                                 }, 0
249                                                         );
250                                                 }
251                                         ],
252                                         'copy_buckets_delete_bucket' : [
253                                                 ['command'],
254                                                 function() {
255                                                         try {
256                                                                 var bucket = obj.controller.view.bucket_menulist.value;
257                                                                 var name = obj.bucket_id_name_map[ bucket ];
258                                                                 var conf = window.confirm('Delete the bucket named ' + name + '?');
259                                                                 if (conf) return;
260                                                                 obj.list2.clear();
261                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'copy',bucket]);
262                                                                 if (typeof robj == 'object') throw robj;
263                                                                 obj.controller.render('copy_buckets_menulist_placeholder');
264                                                         } catch(E) {
265                                                                 obj.error.standard_unexpected_error_alert('Bucket deletion likely failed.',E);
266                                                         }
267                                                 }
268                                         ],
269                                         'copy_buckets_new_bucket' : [
270                                                 ['command'],
271                                                 function() {
272                                                         try {
273                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
274
275                                                                 if (name) {
276                                                                         var bucket = new ccb();
277                                                                         bucket.btype('staff_client');
278                                                                         bucket.owner( obj.data.list.au[0].id() );
279                                                                         bucket.name( name );
280
281                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'copy',bucket]);
282
283                                                                         if (typeof robj == 'object') throw robj;
284
285                                                                         obj.controller.render('copy_buckets_menulist_placeholder');
286                                                                         obj.controller.view.bucket_menulist.value = robj;
287                                                                         setTimeout(
288                                                                                 function() {
289                                                                                         JSAN.use('util.widgets'); 
290                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
291                                                                                 }, 0
292                                                                         );
293                                                                 }
294                                                         } catch(E) {
295                                                                 obj.error.standard_unexpected_error_alert('Bucket creation failed.',E);
296                                                         }
297                                                 }
298                                         ],
299                                         'copy_buckets_batch_copy_edit' : [
300                                                 ['command'],
301                                                 function() {
302                                                         try {
303                                                                 JSAN.use('util.functional');
304                                                                 JSAN.use('util.window'); var win = new util.window();
305                                                                 win.open(
306                                                                         urls.XUL_COPY_EDITOR 
307                                                                         + '?copy_ids=' + window.escape( js2JSON(
308                                                                                 util.functional.map_list(
309                                                                                         obj.list2.dump_retrieve_ids(),
310                                                                                         function (o) {
311                                                                                                 return JSON2js(o)[0]; // acp_id
312                                                                                         }
313                                                                                 )
314                                                                         ) )
315                                                                         + '&single_edit=1'
316                                                                         + '&handle_update=1',
317                                                                         'batch_copy_editor_win_' + win.window_name_increment(),
318                                                                         'chrome,resizable,modal'
319                                                                 );
320                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
321                                                                 setTimeout(
322                                                                         function() {
323                                                                                 JSAN.use('util.widgets'); 
324                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
325                                                                         }, 0
326                                                                 );
327                                                         } catch(E) {
328                                                                 alert( js2JSON(E) );
329                                                         }
330                                                 }
331                                         ],
332                                         'copy_buckets_transfer_to_volume' : [
333                                                 ['command'],
334                                                 function() {
335                                                         try {
336                                                                 // FM_ACN_RETRIEVE
337                                                                 obj.data.stash_retrieve();
338                                                                 if (!obj.data.marked_volume) {
339                                                                         alert('Please mark a volume as the destination from within the copy browser and then try this again.');
340                                                                         return;
341                                                                 }
342                                                                 var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
343                                                                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
344                                                                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
345                                                                 xml += '<description>Transfer the copies in bucket "';
346                                                                 xml += obj.controller.view.bucket_menulist.getAttribute('label') + '" ';
347                                                                 xml += 'from their original volumes to ';
348                                                                 xml += obj.data.hash.aou[ volume.owning_lib() ].shortname() + "'s volume labelled ";
349                                                                 xml += '"' + volume.label() + '" on the following record?</description>';
350                                                                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
351                                                                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
352                                                                 xml += '<iframe style="overflow: scroll" flex="1" src="' + xulG.url_prefix( urls.XUL_BIB_BRIEF ) + '?docid=' + volume.record() + '"/>';
353                                                                 xml += '</vbox>';
354                                                                 obj.data.temp_transfer = xml; obj.data.stash('temp_transfer');
355                                                                 window.open(
356                                                                         urls.XUL_FANCY_PROMPT
357                                                                         + '?xml_in_stash=temp_transfer'
358                                                                         + '&title=' + window.escape('Copy Transfer'),
359                                                                         'fancy_prompt', 'chrome,resizable,modal,width=700,height=500'
360                                                                 );
361                                                                 JSAN.use('OpenILS.data');
362                                                                 var data = new OpenILS.data(); data.init({'via':'stash'});
363                                                                 if (data.fancy_prompt_data == '') { alert('Transfer Aborted'); return; }
364
365                                                                 JSAN.use('util.functional');
366
367                                                                 var copies = obj.network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [
368                                                                         util.functional.map_list(
369                                                                                 obj.list2.dump_retrieve_ids(),
370                                                                                 function (o) {
371                                                                                         return JSON2js(o)[0]; // acp_id
372                                                                                 }
373                                                                         )
374                                                                 ]);
375
376                                                                 for (var i = 0; i < copies.length; i++) {
377                                                                         copies[i].call_number( obj.data.marked_volume );
378                                                                         copies[i].ischanged( 1 );
379                                                                 }
380
381                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), copies ]);
382
383                                                                 obj.render_pending_copies(); // FIXME -- need a generic refresh for lists
384                                                                 setTimeout(
385                                                                         function() {
386                                                                                 JSAN.use('util.widgets'); 
387                                                                                 util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
388                                                                         }, 0
389                                                                 );
390                                                                 
391                                                                 if (typeof robj.ilsevent != 'undefined') {
392                                                                         throw(robj);
393                                                                 } else {
394                                                                         alert('Copies transferred.');
395                                                                 }
396
397                                                         } catch(E) {
398                                                                 obj.error.standard_unexpected_error_alert('Copies not likely transferred.',E);
399                                                         }
400                                                 }
401                                         ],
402                                         'cmd_broken' : [
403                                                 ['command'],
404                                                 function() { alert('Not Yet Implemented'); }
405                                         ],
406                                         'cmd_copy_buckets_print' : [
407                                                 ['command'],
408                                                 function() {
409                                                         dump( js2JSON( obj.list2.dump() ) );
410                                                         alert( js2JSON( obj.list2.dump() ) );
411                                                 }
412                                         ],
413                                         'cmd_copy_buckets_reprint' : [
414                                                 ['command'],
415                                                 function() {
416                                                 }
417                                         ],
418                                         'cmd_copy_buckets_done' : [
419                                                 ['command'],
420                                                 function() {
421                                                         window.close();
422                                                 }
423                                         ],
424                                 }
425                         }
426                 );
427                 this.controller.render();
428
429         },
430
431         'flesh_item_for_list' : function(acp_id,bucket_item_id) {
432                 var obj = this;
433                 try {
434                         var copy = obj.network.simple_request( 'FM_ACP_RETRIEVE', [ acp_id ]);
435                         if (copy == null || typeof copy.ilsevent != 'undefined') {
436                                 throw(copy);
437                         } else {
438                                 var item = {
439                                         'retrieve_id' : js2JSON( [ copy.id(), copy.barcode(), bucket_item_id ] ),
440                                         'row' : {
441                                                 'my' : {
442                                                         'mvr' : obj.network.simple_request('MODS_SLIM_RECORD_RETRIEVE_VIA_COPY', [ copy.id() ]),
443                                                         'acp' : copy,
444                                                 }
445                                         }
446                                 };
447                                 return item;
448                         }
449                 } catch(E) {
450                         obj.error.standard_unexpected_error_alert('List building failed.',E);
451                         return null;
452                 }
453
454         },
455         
456 }
457
458 dump('exiting cat.copy_buckets.js\n');