]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/record_buckets.js
a way to add a record to a bucket, and the correct btype
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / record_buckets.js
1 dump('entering cat.record_buckets.js\n');
2
3 if (typeof cat == 'undefined') cat = {};
4 cat.record_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.record_buckets.prototype = {
13         'selection_list1' : [],
14         'selection_list2' : [],
15         'bucket_id_name_map' : {},
16
17         'render_pending_records' : function() {
18                 var obj = this;
19                 obj.list1.clear();
20                 for (var i = 0; i < obj.record_ids.length; i++) {
21                         var item = obj.flesh_item_for_list( obj.record_ids[i] );
22                         if (item) obj.list1.append( item );
23                 }
24         },
25
26         'init' : function( params ) {
27
28                 var obj = this;
29
30                 obj.record_ids = params['record_ids'] || [];
31
32                 JSAN.use('circ.util');
33                 var columns = circ.util.columns( 
34                         { 
35                                 'title' : { 'hidden' : false },
36                                 'author' : { 'hidden' : false },
37                                 'edition' : { 'hidden' : false },
38                                 'publisher' : { 'hidden' : false },
39                                 'pubdate' : { 'hidden' : false },
40                                 'isbn' : { 'hidden' : false },
41                                 'tcn' : { 'hidden' : false },
42                         } 
43                 );
44
45                 JSAN.use('util.list'); 
46
47                 obj.list1 = new util.list('pending_records_list');
48                 obj.list1.init(
49                         {
50                                 'columns' : columns,
51                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
52                                 'on_select' : function(ev) {
53                                         try {
54                                                 JSAN.use('util.functional');
55                                                 var sel = obj.list1.retrieve_selection();
56                                                 obj.selection_list1 = util.functional.map_list(
57                                                         sel,
58                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
59                                                 );
60                                                 obj.error.sdump('D_TRACE','circ/record_buckets: selection list 1 = ' + js2JSON(obj.selection_list1) );
61                                                 if (obj.selection_list1.length == 0) {
62                                                         obj.controller.view.record_buckets_sel_add.disabled = true;
63                                                 } else {
64                                                         obj.controller.view.record_buckets_sel_add.disabled = false;
65                                                 }
66                                         } catch(E) {
67                                                 alert('FIXME: ' + E);
68                                         }
69                                 },
70
71                         }
72                 );
73
74                 obj.render_pending_records();
75         
76                 obj.list2 = new util.list('records_in_bucket_list');
77                 obj.list2.init(
78                         {
79                                 'columns' : columns,
80                                 'map_row_to_column' : circ.util.std_map_row_to_column(),
81                                 'on_select' : function(ev) {
82                                         try {
83                                                 JSAN.use('util.functional');
84                                                 var sel = obj.list2.retrieve_selection();
85                                                 obj.selection_list2 = util.functional.map_list(
86                                                         sel,
87                                                         function(o) { return JSON2js(o.getAttribute('retrieve_id')); }
88                                                 );
89                                                 obj.error.sdump('D_TRACE','circ/record_buckets: selection list 2 = ' + js2JSON(obj.selection_list2) );
90                                                 if (obj.selection_list2.length == 0) {
91                                                         obj.controller.view.record_buckets_delete_item.disabled = true;
92                                                 } else {
93                                                         obj.controller.view.record_buckets_delete_item.disabled = false;
94                                                 }
95                                         } catch(E) {
96                                                 alert('FIXME: ' + E);
97                                         }
98                                 },
99                         }
100                 );
101                 
102                 JSAN.use('util.controller'); obj.controller = new util.controller();
103                 obj.controller.init(
104                         {
105                                 'control_map' : {
106                                         'record_buckets_menulist_placeholder' : [
107                                                 ['render'],
108                                                 function(e) {
109                                                         return function() {
110                                                                 JSAN.use('util.widgets'); JSAN.use('util.functional');
111                                                                 var buckets = obj.network.simple_request(
112                                                                         'BUCKET_RETRIEVE_VIA_USER',
113                                                                         [ ses(), obj.data.list.au[0].id() ]
114                                                                 );
115                                                                 if (typeof buckets.ilsevent != 'undefined') {
116                                                                         obj.error.standard_unexpected_error_alert('Could not retrieve your buckets.',buckets);
117                                                                         return;
118                                                                 }
119                                                                 var items = [ ['Choose a bucket...',''] ].concat(
120                                                                         util.functional.map_list(
121                                                                                 util.functional.filter_list(
122                                                                                         buckets.biblio,
123                                                                                         function(o) {
124                                                                                                 return o.btype() == 'staff_client';
125                                                                                         }
126                                                                                 ),
127                                                                                 function(o) {
128                                                                                         obj.bucket_id_name_map[ o.id() ] = o.name();
129                                                                                         return [ o.name(), o.id() ];
130                                                                                 }
131                                                                         )
132                                                                 );
133                                                                 g.error.sdump('D_TRACE','items = ' + js2JSON(items));
134                                                                 util.widgets.remove_children( e );
135                                                                 var ml = util.widgets.make_menulist(
136                                                                         items
137                                                                 );
138                                                                 e.appendChild( ml );
139                                                                 ml.setAttribute('id','bucket_menulist');
140                                                                 ml.setAttribute('accesskey','');
141
142                                                                 function change_bucket(ev) {
143                                                                         var bucket_id = ev.target.value;
144                                                                         if (!bucket_id) return;
145                                                                         var bucket = obj.network.simple_request(
146                                                                                 'BUCKET_FLESH',
147                                                                                 [ ses(), 'biblio', bucket_id ]
148                                                                         );
149                                                                         var items = bucket.items() || [];
150                                                                         obj.list2.clear();
151                                                                         for (var i = 0; i < items.length; i++) {
152                                                                                 var item = obj.flesh_item_for_list( 
153                                                                                         items[i].target_record(),
154                                                                                         items[i].id()
155                                                                                 );
156                                                                                 if (item) obj.list2.append( item );
157                                                                         }
158                                                                 }
159
160                                                                 ml.addEventListener( 'change_bucket', change_bucket , false);
161                                                                 ml.addEventListener( 'command', function() {
162                                                                         JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
163                                                                 }, false);
164                                                                 obj.controller.view.bucket_menulist = ml;
165                                                                 JSAN.use('util.widgets'); util.widgets.dispatch('change_bucket',ml);
166                                                         };
167                                                 },
168                                         ],
169
170                                         'record_buckets_add' : [
171                                                 ['command'],
172                                                 function() {
173                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
174                                                         if (!bucket_id) return;
175                                                         for (var i = 0; i < obj.record_ids.length; i++) {
176                                                                 var bucket_item = new cbrebi();
177                                                                 bucket_item.isnew('1');
178                                                                 bucket_item.bucket(bucket_id);
179                                                                 bucket_item.target_biblio_record_entry( obj.record_ids[i] );
180                                                                 try {
181                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
182                                                                                 [ ses(), 'bilbio', bucket_item ]);
183
184                                                                         if (typeof robj == 'object') throw robj;
185
186                                                                         var item = obj.flesh_item_for_list( obj.record_ids[i], robj );
187                                                                         if (!item) continue;
188
189                                                                         obj.list2.append( item );
190                                                                 } catch(E) {
191                                                                         alert( js2JSON(E) );
192                                                                 }
193                                                         }
194                                                 }
195                                         ],
196                                         'record_buckets_sel_add' : [
197                                                 ['command'],
198                                                 function() {                                                        
199                                                         var bucket_id = obj.controller.view.bucket_menulist.value;
200                                                         if (!bucket_id) return;
201                                                         for (var i = 0; i < obj.selection_list1.length; i++) {
202                                                                 var docid = obj.selection_list1[i].docid;
203                                                                 var bucket_item = new cbrebi();
204                                                                 bucket_item.isnew('1');
205                                                                 bucket_item.bucket(bucket_id);
206                                                                 bucket_item.target_biblio_record_entry( docid );
207                                                                 try {
208                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_CREATE',
209                                                                                 [ ses(), 'biblio', bucket_item ]);
210
211                                                                         if (typeof robj == 'object') throw robj;
212
213                                                                         var item = obj.flesh_item_for_list( docid, robj );
214                                                                         if (!item) continue;
215
216                                                                         obj.list2.append( item );
217                                                                 } catch(E) {
218                                                                         alert( js2JSON(E) );
219                                                                 }
220                                                         }
221
222                                                 }
223                                         ],
224                                         'record_buckets_export' : [
225                                                 ['command'],
226                                                 function() {                                                        
227                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
228                                                                 var docid = obj.selection_list2[i].docid;
229                                                                 var item = obj.flesh_item_for_list( docid );
230                                                                 if (item) {
231                                                                         obj.list1.append( item );
232                                                                         obj.record_ids.push( docid );
233                                                                 }
234                                                         }
235                                                 }
236                                         ],
237
238                                         'record_buckets_delete_item' : [
239                                                 ['command'],
240                                                 function() {
241                                                         for (var i = 0; i < obj.selection_list2.length; i++) {
242                                                                 try {
243                                                                         var bucket_item_id = obj.selection_list2[i].bucket_item_id;
244                                                                         var robj = obj.network.simple_request('BUCKET_ITEM_DELETE',
245                                                                                 [ ses(), 'biblio', bucket_item_id ]);
246                                                                         if (typeof robj == 'object') throw robj;
247                                                                 } catch(E) {
248                                                                         alert(js2JSON(E));
249                                                                 }
250                                                         }
251                                                         obj.controller.render('record_buckets_menulist_placeholder');
252                                                 }
253                                         ],
254                                         'record_buckets_delete_bucket' : [
255                                                 ['command'],
256                                                 function() {
257                                                         try {
258                                                                 var bucket = obj.controller.view.bucket_menulist.value;
259                                                                 var name = obj.bucket_id_name_map[ bucket ];
260                                                                 var conf = prompt('To delete this bucket, re-type its name:','','Delete Bucket');
261                                                                 if (conf != name) return;
262                                                                 obj.list2.clear();
263                                                                 var robj = obj.network.simple_request('BUCKET_DELETE',[ses(),'biblio',bucket]);
264                                                                 if (typeof robj == 'object') throw robj;
265                                                                 obj.controller.render('record_buckets_menulist_placeholder');
266                                                         } catch(E) {
267                                                                 alert('FIXME -- ' + E);
268                                                         }
269                                                 }
270                                         ],
271                                         'record_buckets_new_bucket' : [
272                                                 ['command'],
273                                                 function() {
274                                                         try {
275                                                                 var name = prompt('What would you like to name the bucket?','','Bucket Creation');
276
277                                                                 if (name) {
278                                                                         var bucket = new ccb();
279                                                                         bucket.btype('staff_client');
280                                                                         bucket.owner( obj.data.list.au[0].id() );
281                                                                         bucket.name( name );
282
283                                                                         var robj = obj.network.simple_request('BUCKET_CREATE',[ses(),'biblio',bucket]);
284
285                                                                         if (typeof robj == 'object') throw robj;
286
287                                                                         obj.controller.render('record_buckets_menulist_placeholder');
288                                                                         obj.controller.view.bucket_menulist.value = robj;
289                                                                         setTimeout(
290                                                                                 function() {
291                                                                                         JSAN.use('util.widgets'); 
292                                                                                         util.widgets.dispatch('change_bucket',obj.controller.view.bucket_menulist);
293                                                                                 }, 0
294                                                                         );
295                                                                 }
296                                                         } catch(E) {
297                                                                 alert( js2JSON(E) );
298                                                         }
299                                                 }
300                                         ],
301                                         'record_buckets_batch_record_edit' : [
302                                                 ['command'],
303                                                 function() {
304                                                         try {
305                                                                 JSAN.use('util.functional');
306                                                                 JSAN.use('util.window'); var win = new util.window();
307                                                                 win.open(
308                                                                         urls.XUL_COPY_EDITOR 
309                                                                         + '?record_ids=' + window.escape( js2JSON(
310                                                                                 util.functional.map_list(
311                                                                                         obj.list2.dump_retrieve_ids(),
312                                                                                         function (o) {
313                                                                                                 return JSON2js(o).docid; // docid
314                                                                                         }
315                                                                                 )
316                                                                         ) )
317                                                                         + '&single_edit=1'
318                                                                         + '&handle_update=1',
319                                                                         'batch_record_editor_win_' + win.window_name_increment(),
320                                                                         'chrome,resizable,modal'
321                                                                 );
322                                                                 obj.controller.render('record_buckets_menulist_placeholder');           
323                                                                 obj.render_pending_records(); // FIXME -- need a generic refresh for lists
324                                                         } catch(E) {
325                                                                 alert( js2JSON(E) );
326                                                         }
327                                                 }
328                                         ],
329                                         'record_buckets_transfer_to_volume' : [
330                                                 ['command'],
331                                                 function() {
332                                                         // FM_ACN_RETRIEVE
333                                                         obj.data.stash_retrieve();
334                                                         if (!obj.data.marked_volume) {
335                                                                 alert('Please mark a volume as the destination from within the record browser and then try this again.');
336                                                                 return;
337                                                         }
338                                                         var volume = obj.network.simple_request('FM_ACN_RETRIEVE',[ obj.data.marked_volume ]);
339                                                         // FIXME -- later, show some brief details for the record
340                                                         var confirm = prompt('Moving records to volume "' + volume.label() + '".  To confirm, please retype the volume label.','','Copy Transfer');
341                                                         if (confirm == volume.label()) {
342                                                                 JSAN.use('util.functional');
343
344                                                                 var records = obj.network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [
345                                                                         util.functional.map_list(
346                                                                                 obj.list2.dump_retrieve_ids(),
347                                                                                 function (o) {
348                                                                                         return JSON2js(o).docid; // docid
349                                                                                 }
350                                                                         )
351                                                                 ]);
352
353                                                                 for (var i = 0; i < records.length; i++) {
354                                                                         records[i].call_number( obj.data.marked_volume );
355                                                                         records[i].ischanged( 1 );
356                                                                 }
357
358                                                                 var robj = obj.network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE',
359                                                                         [ ses(), records ]);
360                                                                 // FIXME -- check return value at some point
361
362                                                                 obj.controller.render('record_buckets_menulist_placeholder');           
363                                                                 obj.render_pending_records(); // FIXME -- need a generic refresh for lists
364
365                                                         }
366                                                 }
367                                         ],
368                                         'cmd_broken' : [
369                                                 ['command'],
370                                                 function() { alert('Not Yet Implemented'); }
371                                         ],
372                                         'cmd_record_buckets_print' : [
373                                                 ['command'],
374                                                 function() {
375                                                         dump( js2JSON( obj.list2.dump() ) );
376                                                         alert( js2JSON( obj.list2.dump() ) );
377                                                 }
378                                         ],
379                                         'cmd_record_buckets_reprint' : [
380                                                 ['command'],
381                                                 function() {
382                                                 }
383                                         ],
384                                         'cmd_record_buckets_done' : [
385                                                 ['command'],
386                                                 function() {
387                                                         window.close();
388                                                 }
389                                         ],
390                                 }
391                         }
392                 );
393                 this.controller.render();
394
395         },
396
397         'flesh_item_for_list' : function(docid,bucket_item_id) {
398                 var obj = this;
399                 try {
400                         var record = obj.network.simple_request( 'MODS_SLIM_RECORD_RETRIEVE', [ docid ]);
401                         if (record == null || typeof(record.ilsevent) != 'undefined') {
402                                 throw(record);
403                         } else {
404                                 var item = {
405                                         'retrieve_id' : js2JSON( { 'docid' : record.id(), 'bucket_item_id' : bucket_item_id } ),
406                                         'row' : {
407                                                 'my' : {
408                                                         'mvr' : record,
409                                                 }
410                                         }
411                                 };
412                                 return item;
413                         }
414                 } catch(E) {
415                         obj.error.standard_unexpected_error_alert('Could not retrieve this record: ' + docid,E);
416                         return null;
417                 }
418
419         },
420         
421 }
422
423 dump('exiting cat.record_buckets.js\n');