]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
roll our own persist
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / cat / volume_copy_creator.js
1 function my_init() {
2         try {
3
4                 /***********************************************************************************************************/
5                 /* Initial setup */
6
7                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8                                 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
9                 JSAN.errorLevel = "die"; // none, warn, or die
10                 JSAN.addRepository('/xul/server/');
11                 JSAN.use('util.error'); g.error = new util.error();
12                 g.error.sdump('D_TRACE','my_init() for cat/volume_copy_creator.xul');
13
14                 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
15                 JSAN.use('util.widgets'); JSAN.use('util.functional');
16
17                 JSAN.use('util.network'); g.network = new util.network();
18
19                 g.cgi = new CGI();
20
21                 /***********************************************************************************************************/
22                 /* What record am I dealing with?  Am I adding just copies or copies and volumes? */
23
24                 g.doc_id = g.cgi.param('doc_id');
25                 document.getElementById('summary').setAttribute('src',urls.XUL_BIB_BRIEF + '?docid=' + window.escape(g.doc_id));
26
27                 g.copy_shortcut = g.cgi.param('copy_shortcut');
28                 g.error.sdump('D_ERROR','location.href = ' + location.href + '\n\ncopy_short cut = ' + g.copy_shortcut + '\n\nou_ids = ' + g.cgi.param('ou_ids'));
29                 if (g.copy_shortcut) g.copy_shortcut = JSON2js( g.copy_shortcut );
30
31                 var ou_ids = [];
32                 if (g.cgi.param('ou_ids')) 
33                         ou_ids = JSON2js( g.cgi.param('ou_ids') );
34                 if (!ou_ids) ou_ids = [];
35                 if (window.xulG && window.xulG.ou_ids) 
36                         ou_ids = ou_ids.concat( window.xulG.ou_ids );
37
38                 /***********************************************************************************************************/
39                 /* For the call number drop down */
40
41                 var cn_blob;
42                 try {
43                         cn_blob = g.network.simple_request('BLOB_MARC_CALLNUMBERS_RETRIEVE',[g.doc_id]);
44                 } catch(E) {
45                         cn_blob = [];
46                 }
47                 if ((!g.copy_shortcut)) {
48                         var hbox = document.getElementById('marc_cn');
49                         var ml = util.widgets.make_menulist(
50                                 util.functional.map_list(
51                                         cn_blob,
52                                         function(o) {
53                                                 for (var i in o) {
54                                                         return [ o[i], i ];
55                                                 }
56                                         }
57                                 ).sort(
58                                         function(a,b) {
59                                                 a = a[1]; b = b[1];
60                                                 if (a == '082') return -1; 
61                                                 if (b == '082') return 1; 
62                                                 if (a == '092')  return -1; 
63                                                 if (b == '092')  return 1; 
64                                                 if (a < b) return -1; 
65                                                 if (a > b) return 1; 
66                                                 return 0;
67                                         }
68                                 )
69                         ); hbox.appendChild(ml);
70                         ml.setAttribute('editable','true');
71                         ml.setAttribute('width', '200');
72                         var btn = document.createElement('button');
73                         btn.setAttribute('label','Apply');
74                         btn.setAttribute('accesskey','A');
75                         btn.setAttribute('image','/xul/server/skin/media/images/down_arrow.gif');
76                         hbox.appendChild(btn);
77                         btn.addEventListener(
78                                 'command',
79                                 function() {
80                                         var nl = document.getElementsByTagName('textbox');
81                                         for (var i = 0; i < nl.length; i++) {
82                                                 if (nl[i].getAttribute('rel_vert_pos')==2 
83                                                         && !nl[i].disabled) nl[i].value = ml.value;
84                                         }
85                                         if (g.last_focus) setTimeout( function() { g.last_focus.focus(); }, 0 );
86                                 }, 
87                                 false
88                         );
89                 }
90
91                 /***********************************************************************************************************/
92                 /* render the orgs and volumes/input */
93
94                 var rows = document.getElementById('rows');
95
96                 var node_id = 0;
97                 for (var i = 0; i < ou_ids.length; i++) {
98                         try {
99                                 var org = g.data.hash.aou[ ou_ids[i] ];
100                                 if ( get_bool( g.data.hash.aout[ org.ou_type() ].can_have_vols() ) ) {
101                                         var row = document.createElement('row'); rows.appendChild(row); row.setAttribute('ou_id',ou_ids[i]);
102                                         g.render_library_label(row,ou_ids[i]);
103                                         g.render_volume_count_entry(row,ou_ids[i]);
104                                 }
105                         } catch(E) {
106                                 g.error.sdump('D_ERROR',E);
107                         }
108                 }
109
110                 g.load_prefs();
111
112         } catch(E) {
113                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
114                         "system administrator or software developer the following:\ncat/volume_copy_creator.xul\n" +E+ '\n';
115                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); dump(js2JSON(E)); }
116                 alert(err_msg);
117         }
118 }
119
120 g.render_library_label = function(row,ou_id) {
121         var label = document.createElement('label'); row.appendChild(label);
122         label.setAttribute('ou_id',ou_id);
123         label.setAttribute('value',g.data.hash.aou[ ou_id ].shortname());
124 }
125
126 g.render_volume_count_entry = function(row,ou_id) {
127         var hb = document.createElement('vbox'); row.appendChild(hb);
128         var tb = document.createElement('textbox'); hb.appendChild(tb);
129         tb.setAttribute('ou_id',ou_id); tb.setAttribute('size','3'); tb.setAttribute('cols','3');
130         tb.setAttribute('rel_vert_pos','1'); 
131         if ( (!g.copy_shortcut) && (!g.last_focus) ) { tb.focus(); g.last_focus = tb; }
132         var node;
133         function render_copy_count_entry(ev) {
134                 if (ev.target.disabled) return;
135                 if (! isNaN( Number( ev.target.value) ) ) {
136                         if (node) { row.removeChild(node); node = null; }
137                         //ev.target.disabled = true;
138                         node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
139                 }
140         }
141         util.widgets.apply_vertical_tab_on_enter_handler( 
142                 tb, 
143                 function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
144         );
145         tb.addEventListener( 'change', render_copy_count_entry, false);
146         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
147         setTimeout(
148                 function() {
149                         try {
150                         if (g.copy_shortcut) {
151                                 JSAN.use('util.functional');
152                                 tb.value = util.functional.map_object_to_list(
153                                         g.copy_shortcut[ou_id],
154                                         function(o,i) {
155                                                 return g.copy_shortcut[ou_id][i];
156                                         }
157                                 ).length
158                                 render_copy_count_entry({'target':tb});
159                                 tb.disabled = true;
160                         }
161                         } catch(E) {
162                                 alert(E);
163                         }
164                 }, 0
165         );
166 }
167
168 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
169         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
170         grid.setAttribute('flex','1');
171         grid.setAttribute('ou_id',ou_id);
172         var rows = grid.lastChild;
173         var r = document.createElement('row'); rows.appendChild( r );
174         var x = document.createElement('label'); r.appendChild(x);
175         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
176         x = document.createElement('label'); r.appendChild(x);
177         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
178
179         function handle_change(tb1,tb2,hb3) {
180                 if (tb1.value == '') return;
181                 if (isNaN( Number( tb2.value ) )) return;
182
183                 //if (tb1.disabled || tb2.disabled) return;
184
185                 //tb1.disabled = true;
186                 //tb2.disabled = true;
187
188                 util.widgets.remove_children(hb3);
189
190                 g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
191                 document.getElementById("Create").disabled = false;
192         }
193
194         function handle_change_tb1(ev) {
195                 var _tb1 = ev.target;   
196                 var _hb1 = _tb1.parentNode;
197                 var _hb2 = _hb1.nextSibling;
198                 var _tb2 = _hb2.firstChild;
199                 var _hb3 = _hb2.nextSibling;
200                 handle_change(_tb1,_tb2,_hb3);
201         }
202
203         function handle_change_tb2(ev) {
204                 var _tb2 = ev.target;   
205                 var _hb2 = _tb2.parentNode;
206                 var _hb1 = _hb2.previousSibling;
207                 var _tb1 = _hb1.firstChild;
208                 var _hb3 = _hb2.nextSibling;
209                 handle_change(_tb1,_tb2,_hb3);
210         }
211
212         for (var i = 0; i < count; i++) {
213                 var r = document.createElement('row'); rows.appendChild(r);
214                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
215                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
216                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
217                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
218                 tb1.setAttribute('rel_vert_pos','2');
219                 tb1.setAttribute('ou_id',ou_id);
220                 util.widgets.apply_vertical_tab_on_enter_handler( 
221                         tb1, 
222                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
223                 );
224                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
225                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
226                 tb2.setAttribute('rel_vert_pos','3');
227                 tb2.setAttribute('ou_id',ou_id);
228                 util.widgets.apply_vertical_tab_on_enter_handler( 
229                         tb2, 
230                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
231                 );
232
233                 tb1.addEventListener( 'change', handle_change_tb1, false);
234                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
235                 tb2.addEventListener( 'change', handle_change_tb2, false);
236                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
237                 if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
238
239                 setTimeout(
240                         function(idx,tb){
241                                 return function() {
242                                         try {
243                                         JSAN.use('util.functional');
244                                         if (g.copy_shortcut) {
245                                                 var label = util.functional.map_object_to_list(
246                                                         g.copy_shortcut[ou_id],
247                                                         function(o,i) {
248                                                                 return i;
249                                                         }
250                                                 )[idx];
251                                                 tb.value = label; handle_change_tb1({'target':tb});
252                                                 tb.disabled = true;
253                                         }
254                                         } catch(E) {
255                                                 alert(E);
256                                         }
257                                 }
258                         }(i,tb1),0
259                 );
260         }
261
262         return grid;
263 }
264
265 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
266         try {
267                 function ready_to_create(ev) {
268                         document.getElementById("Create").disabled = false;
269                 }
270
271                 JSAN.use('util.barcode'); 
272
273                 for (var i = 0; i < count; i++) {
274                         var tb = document.createElement('textbox'); node.appendChild(tb);
275                         tb.setAttribute('ou_id',ou_id);
276                         tb.setAttribute('callnumber',callnumber);
277                         tb.setAttribute('rel_vert_pos','4');
278                         util.widgets.apply_vertical_tab_on_enter_handler( 
279                                 tb, 
280                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
281                         );
282                         //tb.addEventListener('change',ready_to_create,false);
283                         tb.addEventListener('change', function(ev) {
284                                 var barcode = ev.target.value;
285                                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
286                                         g.error.yns_alert( '"' + barcode + '" is an invalid barcode.','Invalid Barcode','OK',null,null,'Check here to confirm this message.');
287                                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
288                                 }
289                         }, false);
290                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
291                 }
292         } catch(E) {
293                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
294         }
295 }
296
297 g.new_node_id = -1;
298
299 g.stash_and_close = function() {
300
301         try {
302
303                 var nl = document.getElementsByTagName('textbox');
304
305                 var volumes_hash = {};
306
307                 var barcodes = [];
308                 
309                 for (var i = 0; i < nl.length; i++) {
310                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
311                         if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
312                                 var ou_id = nl[i].getAttribute('ou_id');
313                                 var callnumber = nl[i].value;
314                                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
315                                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
316                         }
317                 };
318         
319                 for (var i = 0; i < barcodes.length; i++) {
320                         var ou_id = barcodes[i].getAttribute('ou_id');
321                         var callnumber = barcodes[i].getAttribute('callnumber');
322                         var barcode = barcodes[i].value;
323
324                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
325                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
326
327                         if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
328                 }
329
330                 var volumes = [];
331                 var copies = [];
332                 var volume_labels = {};
333
334                 for (var ou_id in volumes_hash) {
335                         for (var cn in volumes_hash[ou_id]) {
336
337                                 var acn_id = g.network.simple_request(
338                                         'FM_ACN_FIND_OR_CREATE',
339                                         [ ses(), cn, g.doc_id, ou_id ]
340                                 );
341
342                                 if (typeof acn_id.ilsevent != 'undefined') {
343                                         g.error.standard_unexpected_error_alert('Problem finding or creating ' + cn + '.  We will skip item creation for this volume.',acn_id);
344                                         continue;
345                                 }
346
347                                 volume_labels[ acn_id ] = { 'label' : cn, 'owning_lib' : ou_id };
348
349                                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
350                                         var copy = new acp();
351                                         copy.id( g.new_node_id-- );
352                                         copy.isnew('1');
353                                         copy.barcode( volumes_hash[ou_id][cn][i] );
354                                         copy.call_number( acn_id );
355                                         copy.circ_lib(ou_id);
356                                         /* FIXME -- use constants */
357                                         copy.deposit(0);
358                                         copy.price(0);
359                                         copy.deposit_amount(0);
360                                         copy.fine_level(2);
361                                         copy.loan_duration(2);
362                                         copy.location(1);
363                                         copy.status(0);
364                                         copy.circulate(get_db_true());
365                                         copy.holdable(get_db_true());
366                                         copy.opac_visible(get_db_true());
367                                         copy.ref(get_db_false());
368                                         copies.push( copy );
369                                 }
370                         }
371                 }
372
373                 JSAN.use('util.window'); var win = new util.window();
374                 if (copies.length > 0) {
375                         g.data.temp_copies = js2JSON(copies); g.data.stash('temp_copies');
376                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
377                         //g.data.temp_callnumbers = js2JSON(volume_labels); g.data.stash('temp_callnumbers');
378                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
379                         var w = win.open(
380                                 urls.XUL_COPY_EDITOR
381                                         +'?edit=1&handle_update=1&docid='+window.escape(g.doc_id),
382                                 title,
383                                 'chrome,modal,resizable'
384                         );
385                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
386                         g.data.stash_retrieve();
387                         copies = JSON2js( g.data.temp_copies );
388                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
389                         g.data.temp_copies = undefined; g.data.stash('temp_copies');
390                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
391                         if (!copies) {
392                                 alert('Items were not created.');
393                                 return;
394                         } else {
395                                 try {
396                                         //case 1706 /* ITEM_BARCODE_EXISTS */ :
397                                         if (copies.length > 0 && $('print_labels').checked) {
398                                                 JSAN.use('util.functional');
399                                                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
400                                                 data.temp_barcodes_for_labels = util.functional.map_list( copies, function(o){return o.barcode();}) ; 
401                                                 data.stash('temp_barcodes_for_labels');
402                                                 var w = win.open(
403                                                         urls.XUL_SPINE_LABEL,
404                                                         'spine_labels',
405                                                         'chrome,resizable,width=750,height=550'
406                                                 );
407                                         }
408                                 } catch(E) {
409                                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
410                                 }
411
412                                 if (typeof window.refresh == 'function') window.refresh();
413
414                         }
415                 }
416
417                 window.close();
418
419         } catch(E) {
420                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
421         }
422 }
423
424 g.load_prefs = function() {
425         try {
426                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
427                 JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
428                 if (file._file.exists()) {
429                         var prefs = file.get_object(); file.close();
430                         if (prefs.check_barcodes) {
431                                 if ( prefs.check_barcodes == 'false' ) {
432                                         $('check_barcodes').checked = false;
433                                 } else {
434                                         $('check_barcodes').checked = prefs.check_barcodes;
435                                 }
436                         } else {
437                                 $('check_barcodes').checked = false;
438                         }
439                         if (prefs.print_labels) {
440                                 if ( prefs.print_labels == 'false' ) {
441                                         $('print_labels').checked = false;
442                                 } else {
443                                         $('print_labels').checked = prefs.print_labels;
444                                 }
445                         } else {
446                                 $('print_labels').checked = false;
447                         }
448
449                 }
450         } catch(E) {
451                 g.error.standard_unexpected_error_alert('Error retrieving stored preferences',E);
452         }
453 }
454
455 g.save_prefs = function () {
456         try {
457                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
458                 JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
459                 file.set_object(
460                         {
461                                 'check_barcodes' : $('check_barcodes').checked,
462                                 'print_labels' : $('print_labels').checked,
463                         }
464                 );
465                 file.close();
466         } catch(E) {
467                 g.error.standard_unexpected_error_alert('Error storing preferences',E);
468         }
469 }
470
471