]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/volume_copy_creator.js
sanity check input
[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 ( Number( ev.target.value ) > 100 ) {
137                                 if (!window.confirm('Are you sure you would like to create ' + ev.target.value + ' volumes?')) return;
138                         }
139                         if (node) { row.removeChild(node); node = null; }
140                         //ev.target.disabled = true;
141                         node = g.render_callnumber_copy_count_entry(row,ou_id,ev.target.value);
142                 }
143         }
144         util.widgets.apply_vertical_tab_on_enter_handler( 
145                 tb, 
146                 function() { render_copy_count_entry({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
147         );
148         tb.addEventListener( 'change', render_copy_count_entry, false);
149         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
150         setTimeout(
151                 function() {
152                         try {
153                         if (g.copy_shortcut) {
154                                 JSAN.use('util.functional');
155                                 tb.value = util.functional.map_object_to_list(
156                                         g.copy_shortcut[ou_id],
157                                         function(o,i) {
158                                                 return g.copy_shortcut[ou_id][i];
159                                         }
160                                 ).length
161                                 render_copy_count_entry({'target':tb});
162                                 tb.disabled = true;
163                         }
164                         } catch(E) {
165                                 alert(E);
166                         }
167                 }, 0
168         );
169 }
170
171 g.render_callnumber_copy_count_entry = function(row,ou_id,count) {
172         var grid = util.widgets.make_grid( [ {}, {} ] ); row.appendChild(grid);
173         grid.setAttribute('flex','1');
174         grid.setAttribute('ou_id',ou_id);
175         var rows = grid.lastChild;
176         var r = document.createElement('row'); rows.appendChild( r );
177         var x = document.createElement('label'); r.appendChild(x);
178         x.setAttribute('value','Call Numbers'); x.setAttribute('style','font-weight: bold');
179         x = document.createElement('label'); r.appendChild(x);
180         x.setAttribute('value','# of Copies'); x.setAttribute('style','font-weight: bold');
181         x.setAttribute('size','3'); x.setAttribute('cols','3');
182
183
184         function handle_change(tb1,tb2,hb3) {
185                 if (tb1.value == '') return;
186                 if (isNaN( Number( tb2.value ) )) return;
187                 if ( Number( tb2.value ) > 100 ) {
188                         if (!window.confirm('Are you sure you would like to create ' + tb2.value + ' copies?')) return;
189                 }
190
191                 //if (tb1.disabled || tb2.disabled) return;
192
193                 //tb1.disabled = true;
194                 //tb2.disabled = true;
195
196                 util.widgets.remove_children(hb3);
197
198                 g.render_barcode_entry(hb3,tb1.value,Number(tb2.value),ou_id);
199                 document.getElementById("Create").disabled = false;
200         }
201
202         function handle_change_tb1(ev) {
203                 var _tb1 = ev.target;   
204                 var _hb1 = _tb1.parentNode;
205                 var _hb2 = _hb1.nextSibling;
206                 var _tb2 = _hb2.firstChild;
207                 var _hb3 = _hb2.nextSibling;
208                 handle_change(_tb1,_tb2,_hb3);
209         }
210
211         function handle_change_tb2(ev) {
212                 var _tb2 = ev.target;   
213                 var _hb2 = _tb2.parentNode;
214                 var _hb1 = _hb2.previousSibling;
215                 var _tb1 = _hb1.firstChild;
216                 var _hb3 = _hb2.nextSibling;
217                 handle_change(_tb1,_tb2,_hb3);
218         }
219
220         for (var i = 0; i < count; i++) {
221                 var r = document.createElement('row'); rows.appendChild(r);
222                 var hb1 = document.createElement('vbox'); r.appendChild(hb1);
223                 var hb2 = document.createElement('vbox'); r.appendChild(hb2);
224                 var hb3 = document.createElement('vbox'); r.appendChild(hb3);
225                 var tb1 = document.createElement('textbox'); hb1.appendChild(tb1);
226                 tb1.setAttribute('rel_vert_pos','2');
227                 tb1.setAttribute('ou_id',ou_id);
228                 util.widgets.apply_vertical_tab_on_enter_handler( 
229                         tb1, 
230                         function() { handle_change_tb1({'target':tb1}); setTimeout(function(){util.widgets.vertical_tab(tb1);},0); }
231                 );
232                 var tb2 = document.createElement('textbox'); hb2.appendChild(tb2);
233                 tb2.setAttribute('size','3'); tb2.setAttribute('cols','3');
234                 tb2.setAttribute('rel_vert_pos','3');
235                 tb2.setAttribute('ou_id',ou_id);
236                 util.widgets.apply_vertical_tab_on_enter_handler( 
237                         tb2, 
238                         function() { handle_change_tb2({'target':tb2}); setTimeout(function(){util.widgets.vertical_tab(tb2);},0); }
239                 );
240
241                 tb1.addEventListener( 'change', handle_change_tb1, false);
242                 tb1.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
243                 tb2.addEventListener( 'change', handle_change_tb2, false);
244                 tb2.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
245                 if ( !g.last_focus ) { tb2.focus(); g.last_focus = tb2; }
246
247                 setTimeout(
248                         function(idx,tb){
249                                 return function() {
250                                         try {
251                                         JSAN.use('util.functional');
252                                         if (g.copy_shortcut) {
253                                                 var label = util.functional.map_object_to_list(
254                                                         g.copy_shortcut[ou_id],
255                                                         function(o,i) {
256                                                                 return i;
257                                                         }
258                                                 )[idx];
259                                                 tb.value = label; handle_change_tb1({'target':tb});
260                                                 tb.disabled = true;
261                                         }
262                                         } catch(E) {
263                                                 alert(E);
264                                         }
265                                 }
266                         }(i,tb1),0
267                 );
268         }
269
270         return grid;
271 }
272
273 g.render_barcode_entry = function(node,callnumber,count,ou_id) {
274         try {
275                 function ready_to_create(ev) {
276                         document.getElementById("Create").disabled = false;
277                 }
278
279                 JSAN.use('util.barcode'); 
280
281                 for (var i = 0; i < count; i++) {
282                         var tb = document.createElement('textbox'); node.appendChild(tb);
283                         tb.setAttribute('ou_id',ou_id);
284                         tb.setAttribute('callnumber',callnumber);
285                         tb.setAttribute('rel_vert_pos','4');
286                         util.widgets.apply_vertical_tab_on_enter_handler( 
287                                 tb, 
288                                 function() { ready_to_create({'target':tb}); setTimeout(function(){util.widgets.vertical_tab(tb);},0); }
289                         );
290                         //tb.addEventListener('change',ready_to_create,false);
291                         tb.addEventListener('change', function(ev) {
292                                 var barcode = ev.target.value;
293                                 if ($('check_barcodes').checked && ! util.barcode.check(barcode) ) {
294                                         g.error.yns_alert( '"' + barcode + '" is an invalid barcode.','Invalid Barcode','OK',null,null,'Check here to confirm this message.');
295                                         setTimeout( function() { ev.target.select(); ev.target.focus(); }, 0);
296                                 }
297                         }, false);
298                         tb.addEventListener( 'focus', function(ev) { g.last_focus = ev.target; }, false );
299                 }
300         } catch(E) {
301                 g.error.sdump('D_ERROR','g.render_barcode_entry: ' + E);
302         }
303 }
304
305 g.new_node_id = -1;
306
307 g.stash_and_close = function() {
308
309         try {
310
311                 var nl = document.getElementsByTagName('textbox');
312
313                 var volumes_hash = {};
314
315                 var barcodes = [];
316                 
317                 for (var i = 0; i < nl.length; i++) {
318                         if ( nl[i].getAttribute('rel_vert_pos') == 4 ) barcodes.push( nl[i] );
319                         if ( nl[i].getAttribute('rel_vert_pos') == 2 )  {
320                                 var ou_id = nl[i].getAttribute('ou_id');
321                                 var callnumber = nl[i].value;
322                                 if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
323                                 if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
324                         }
325                 };
326         
327                 for (var i = 0; i < barcodes.length; i++) {
328                         var ou_id = barcodes[i].getAttribute('ou_id');
329                         var callnumber = barcodes[i].getAttribute('callnumber');
330                         var barcode = barcodes[i].value;
331
332                         if (typeof volumes_hash[ou_id] == 'undefined') { volumes_hash[ou_id] = {} }
333                         if (typeof volumes_hash[ou_id][callnumber] == 'undefined') { volumes_hash[ou_id][callnumber] = [] }
334
335                         if (barcode != '') volumes_hash[ou_id][callnumber].push( barcode );
336                 }
337
338                 var volumes = [];
339                 var copies = [];
340                 var volume_labels = {};
341
342                 for (var ou_id in volumes_hash) {
343                         for (var cn in volumes_hash[ou_id]) {
344
345                                 var acn_id = g.network.simple_request(
346                                         'FM_ACN_FIND_OR_CREATE',
347                                         [ ses(), cn, g.doc_id, ou_id ]
348                                 );
349
350                                 if (typeof acn_id.ilsevent != 'undefined') {
351                                         g.error.standard_unexpected_error_alert('Problem finding or creating ' + cn + '.  We will skip item creation for this volume.',acn_id);
352                                         continue;
353                                 }
354
355                                 volume_labels[ acn_id ] = { 'label' : cn, 'owning_lib' : ou_id };
356
357                                 for (var i = 0; i < volumes_hash[ou_id][cn].length; i++) {
358                                         var copy = new acp();
359                                         copy.id( g.new_node_id-- );
360                                         copy.isnew('1');
361                                         copy.barcode( volumes_hash[ou_id][cn][i] );
362                                         copy.call_number( acn_id );
363                                         copy.circ_lib(ou_id);
364                                         /* FIXME -- use constants */
365                                         copy.deposit(0);
366                                         copy.price(0);
367                                         copy.deposit_amount(0);
368                                         copy.fine_level(2);
369                                         copy.loan_duration(2);
370                                         copy.location(1);
371                                         copy.status(0);
372                                         copy.circulate(get_db_true());
373                                         copy.holdable(get_db_true());
374                                         copy.opac_visible(get_db_true());
375                                         copy.ref(get_db_false());
376                                         copies.push( copy );
377                                 }
378                         }
379                 }
380
381                 JSAN.use('util.window'); var win = new util.window();
382                 if (copies.length > 0) {
383                         g.data.temp_copies = js2JSON(copies); g.data.stash('temp_copies');
384                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
385                         //g.data.temp_callnumbers = js2JSON(volume_labels); g.data.stash('temp_callnumbers');
386                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
387                         var w = win.open(
388                                 urls.XUL_COPY_EDITOR
389                                         +'?edit=1&handle_update=1&docid='+window.escape(g.doc_id),
390                                 title,
391                                 'chrome,modal,resizable'
392                         );
393                         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
394                         g.data.stash_retrieve();
395                         copies = JSON2js( g.data.temp_copies );
396                         g.data.temp_copy_ids = undefined; g.data.stash('temp_copy_ids');
397                         g.data.temp_copies = undefined; g.data.stash('temp_copies');
398                         g.data.temp_callnumbers = undefined; g.data.stash('temp_callnumbers');
399                         if (!copies) {
400                                 alert('Items were not created.');
401                                 return;
402                         } else {
403                                 try {
404                                         //case 1706 /* ITEM_BARCODE_EXISTS */ :
405                                         if (copies.length > 0 && $('print_labels').checked) {
406                                                 JSAN.use('util.functional');
407                                                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
408                                                 data.temp_barcodes_for_labels = util.functional.map_list( copies, function(o){return o.barcode();}) ; 
409                                                 data.stash('temp_barcodes_for_labels');
410                                                 var w = win.open(
411                                                         urls.XUL_SPINE_LABEL,
412                                                         'spine_labels',
413                                                         'chrome,resizable,width=750,height=550'
414                                                 );
415                                         }
416                                 } catch(E) {
417                                         g.error.standard_unexpected_error_alert('volume tree update 2',E);
418                                 }
419
420                                 if (typeof window.refresh == 'function') window.refresh();
421
422                         }
423                 }
424
425                 window.close();
426
427         } catch(E) {
428                 g.error.standard_unexpected_error_alert('volume tree update 3',E);
429         }
430 }
431
432 g.load_prefs = function() {
433         try {
434                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
435                 JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
436                 if (file._file.exists()) {
437                         var prefs = file.get_object(); file.close();
438                         if (prefs.check_barcodes) {
439                                 if ( prefs.check_barcodes == 'false' ) {
440                                         $('check_barcodes').checked = false;
441                                 } else {
442                                         $('check_barcodes').checked = prefs.check_barcodes;
443                                 }
444                         } else {
445                                 $('check_barcodes').checked = false;
446                         }
447                         if (prefs.print_labels) {
448                                 if ( prefs.print_labels == 'false' ) {
449                                         $('print_labels').checked = false;
450                                 } else {
451                                         $('print_labels').checked = prefs.print_labels;
452                                 }
453                         } else {
454                                 $('print_labels').checked = false;
455                         }
456
457                 }
458         } catch(E) {
459                 g.error.standard_unexpected_error_alert('Error retrieving stored preferences',E);
460         }
461 }
462
463 g.save_prefs = function () {
464         try {
465                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
466                 JSAN.use('util.file'); var file = new util.file('volume_copy_creator.prefs');
467                 file.set_object(
468                         {
469                                 'check_barcodes' : $('check_barcodes').checked,
470                                 'print_labels' : $('print_labels').checked,
471                         }
472                 );
473                 file.close();
474         } catch(E) {
475                 g.error.standard_unexpected_error_alert('Error storing preferences',E);
476         }
477 }
478
479