]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/util.js
stringify before split
[Evergreen.git] / Open-ILS / xul / staff_client / server / cat / util.js
1 dump('entering cat/util.js\n');
2
3 if (typeof cat == 'undefined') var cat = {};
4 cat.util = {};
5
6 cat.util.EXPORT_OK      = [ 
7         'spawn_copy_editor', 'add_copies_to_bucket', 'show_in_opac', 'spawn_spine_editor', 'transfer_copies', 
8         'mark_item_missing', 'mark_item_damaged', 'replace_barcode',
9 ];
10 cat.util.EXPORT_TAGS    = { ':all' : cat.util.EXPORT_OK };
11
12 cat.util.replace_barcode = function(old_bc) {
13         try {
14                 JSAN.use('util.network');
15                 var network = new util.network();
16
17                 if (!old_bc) old_bc = window.prompt('Enter original barcode for the copy:','','Replace Barcode');
18                 if (!old_bc) return;
19
20                 var copy = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ old_bc ]);
21                 if (typeof copy.ilsevent != 'undefined') throw(copy); 
22                 if (!copy) throw(copy);
23
24                 // Why did I want to do this twice?  Because this copy is more fleshed?
25                 copy = network.simple_request('FM_ACP_RETRIEVE',[ copy.id() ]);
26                 if (typeof copy.ilsevent != 'undefined') throw(copy);
27                 if (!copy) throw(copy);
28
29                 var new_bc = window.prompt('Enter the replacement barcode for the copy with barcode ' + old_bc + ':','','Replace Barcode');
30
31                 var test = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ ses(), new_bc ]);
32                 if (typeof test.ilsevent == 'undefined') {
33                         alert('Rename aborted.  Another copy has that barcode');
34                         return;
35                 }
36                 copy.barcode(new_bc); copy.ischanged('1');
37                 var r = network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), [ copy ] ]);
38                 if (typeof r.ilsevent != 'undefined') { if (r.ilsevent != 0) throw(r); }
39                 return new_bc;
40         } catch(E) {
41                 JSAN.use('util.error'); var error = new util.error();
42                 error.standard_unexpected_error_alert('Rename did not likely occur.',E);
43         }
44 }
45
46 cat.util.transfer_copies = function(params) {
47         JSAN.use('util.error'); var error = new util.error();
48         JSAN.use('OpenILS.data'); var data = new OpenILS.data();
49         JSAN.use('util.network'); var network = new util.network();
50         try {
51                 data.stash_retrieve();
52                 if (!data.marked_volume) {
53                         alert('Please mark a volume as the destination from within holdings maintenance and then try this again.');
54                         return;
55                 }
56                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
57                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
58                 if (!params.message) {
59                         params.message = 'Transfer items from their original volumes to ';
60                         params.message += data.hash.aou[ params.owning_lib ].shortname() + "'s volume labelled ";
61                         params.message += '"' + params.volume_label + '" on the following record (and change their circ libs to match)?';
62                 }
63
64                 xml += '<description>' + params.message + '</description>';
65                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
66                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
67                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + params.docid + '"/>';
68                 xml += '</vbox>';
69                 data.temp_transfer = xml; data.stash('temp_transfer');
70                 window.open(
71                         urls.XUL_FANCY_PROMPT
72                         + '?xml_in_stash=temp_transfer'
73                         + '&title=' + window.escape('Item Transfer'),
74                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300'
75                 );
76                 data.stash_retrieve();
77                 if (data.fancy_prompt_data == '') { alert('Transfer Aborted'); return; }
78
79                 JSAN.use('util.functional');
80
81                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ params.copy_ids ]);
82
83                 for (var i = 0; i < copies.length; i++) {
84                         copies[i].call_number( data.marked_volume );
85                         copies[i].circ_lib( params.owning_lib );
86                         copies[i].ischanged( 1 );
87                 }
88
89                 var robj = network.simple_request(
90                         'FM_ACP_FLESHED_BATCH_UPDATE', 
91                         [ ses(), copies, true ], 
92                         null,
93                         {
94                                 'title' : 'Override Transfer Failure?',
95                                 'overridable_events' : [
96                                         1208 /* TITLE_LAST_COPY */,
97                                 ]
98                         }
99                 );
100                 
101                 if (typeof robj.ilsevent != 'undefined') {
102                         throw(robj);
103                 } else {
104                         alert('Items transferred.');
105                 }
106
107         } catch(E) {
108                 error.standard_unexpected_error_alert('All items not likely transferred.',E);
109         }
110 }
111
112 cat.util.spawn_spine_editor = function(selection_list) {
113         JSAN.use('util.error'); var error = new util.error();
114         try {
115                 JSAN.use('util.functional');
116                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
117                 data.temp_barcodes_for_labels = util.functional.map_list( selection_list, function(o){return o.barcode;}) ; 
118                 data.stash('temp_barcodes_for_labels');
119                 xulG.new_tab(
120                         xulG.url_prefix( urls.XUL_SPINE_LABEL ),
121                         { 'tab_name' : 'Spine Labels' },
122                         {}
123                 );
124         } catch(E) {
125                 error.standard_unexpected_error_alert('Spine Labels',E);
126         }
127 }
128
129 cat.util.show_in_opac = function(selection_list) {
130         JSAN.use('util.error'); var error = new util.error();
131         var doc_id; var seen = {};
132         try {
133                 for (var i = 0; i < selection_list.length; i++) {
134                         doc_id = selection_list[i].doc_id;
135                         if (!doc_id) {
136                                 alert(selection_list[i].barcode + ' is not cataloged');
137                                 continue;
138                         }
139                         if (typeof seen[doc_id] != 'undefined') {
140                                 continue;
141                         }
142                         seen[doc_id] = true;
143                         var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
144                         var content_params = { 
145                                 'session' : ses(),
146                                 'authtime' : ses('authtime'),
147                                 'opac_url' : opac_url,
148                         };
149                         xulG.new_tab(
150                                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
151                                 {'tab_name':'Retrieving title...'}, 
152                                 content_params
153                         );
154                 }
155         } catch(E) {
156                 error.standard_unexpected_error_alert('Error opening catalog for document id = ' + doc_id,E);
157         }
158 }
159
160 cat.util.add_copies_to_bucket = function(selection_list) {
161         JSAN.use('util.functional');
162         JSAN.use('util.window'); var win = new util.window();
163         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
164         data.cb_temp_copy_ids = js2JSON(
165                 util.functional.map_list(
166                         selection_list,
167                         function (o) {
168                                 if (typeof o.copy_id != 'undefined' && o.copy_id != null) {
169                                         return o.copy_id;
170                                 } else {
171                                         return o;
172                                 }
173                         }
174                 )
175         );
176         data.stash('cb_temp_copy_ids');
177         win.open( 
178                 xulG.url_prefix(urls.XUL_COPY_BUCKETS_QUICK),
179                 'sel_bucket_win' + win.window_name_increment(),
180                 'chrome,resizable,center'
181         );
182 }
183
184 cat.util.spawn_copy_editor = function(list,edit) {
185         try {
186         var obj = {};
187         JSAN.use('OpenILS.data'); obj.data = new OpenILS.data(); obj.data.init({'via':'stash'});
188         JSAN.use('util.network'); obj.network = new util.network();
189         JSAN.use('util.error'); obj.error = new util.error();
190
191         var title = list.length == 1 ? '' : 'Batch '; 
192         title += edit == 1 ? 'Edit' : 'View';
193         title += ' Copy Attributes';
194
195         JSAN.use('util.window'); var win = new util.window();
196         obj.data.temp_copies = undefined; obj.data.stash('temp_copies');
197         obj.data.temp_callnumbers = undefined; obj.data.stash('temp_callnumbers');
198         obj.data.temp_copy_ids = js2JSON(list);
199         obj.data.stash('temp_copy_ids');
200         var w = win.open(
201                 window.xulG.url_prefix(urls.XUL_COPY_EDITOR)
202                         +'?edit='+edit,
203                 title,
204                 'chrome,modal,resizable'
205         );
206         /* FIXME -- need to unique the temp space, and not rely on modalness of window */
207         obj.data.stash_retrieve();
208         if (!obj.data.temp_copies) return;
209         var copies = JSON2js( obj.data.temp_copies );
210         obj.data.temp_copies = undefined; obj.data.stash('temp_copies');
211         obj.data.temp_callnumbers = undefined; obj.data.stash('temp_callnumbers');
212         obj.data.temp_copy_ids = undefined; obj.data.stash('temp_copy_ids');
213         obj.error.sdump('D_CAT','in cat/copy_status, copy editor, copies =\n<<' + copies + '>>');
214         if (edit=='1' && copies!='' && typeof copies != 'undefined') {
215                 try {
216                         var r = obj.network.request(
217                                 api.FM_ACP_FLESHED_BATCH_UPDATE.app,
218                                 api.FM_ACP_FLESHED_BATCH_UPDATE.method,
219                                 [ ses(), copies, true ]
220                         );
221                         /* FIXME -- revisit the return value here */
222                 } catch(E) {
223                         obj.error.standard_unexpected_error_alert('copy update error',E);
224                 }
225         } else {
226                 //alert('not updating');
227         }
228         } catch(E) {
229                 alert(E);
230         }
231 }
232
233 cat.util.mark_item_damaged = function(copy_ids) {
234         var error;
235         try {
236                 JSAN.use('util.error'); error = new util.error();
237                 JSAN.use('util.functional');
238                 JSAN.use('util.network'); var network = new util.network();
239                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
240                 if (typeof copies.ilsevent != 'undefined') throw(copies);
241                 var magic_status = false;
242                 for (var i = 0; i < copies.length; i++) {
243                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
244                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
245                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
246                 }
247                 if (magic_status) {
248                 
249                         error.yns_alert('Action failed.  One or more of these items is in a special status (Checked Out, In Transit, etc.) and cannot be changed to the Damaged status.','Action failed.','OK',null,null,'Check here to confirm this message');
250
251                 } else {
252
253                         var r = error.yns_alert('Change the status for these items to Damaged?  You will have to manually retrieve the last circulation if you need to bill a patron.  Barcodes: ' + util.functional.map_list( copies, function(o) { return o.barcode(); } ).join(", "), 'Mark Damaged', 'OK', 'Cancel', null, 'Check here to confirm this action');
254
255                         if (r == 0) {
256                                 var count = 0;
257                                 for (var i = 0; i < copies.length; i++) {
258                                         try {
259                                                 var robj = network.simple_request('MARK_ITEM_DAMAGED',[ses(),copies[i].id()]);
260                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
261                                                 count++;
262                                         } catch(E) {
263                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' damaged.',E);
264                                         }
265                                 }
266                                 alert(count == 1 ? 'Item marked Damaged' : count + ' items marked Damaged.');
267                         }
268                 }
269
270         } catch(E) {
271                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_damaged',E); else alert('FIXME: ' + E);
272         }
273 }
274
275 cat.util.mark_item_missing = function(copy_ids) {
276         var error;
277         try {
278                 JSAN.use('util.error'); error = new util.error();
279                 JSAN.use('util.functional');
280                 JSAN.use('util.network'); var network = new util.network();
281                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
282                 if (typeof copies.ilsevent != 'undefined') throw(copies);
283                 var magic_status = false;
284                 for (var i = 0; i < copies.length; i++) {
285                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
286                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
287                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
288                 }
289                 if (magic_status) {
290                 
291                         error.yns_alert('Action failed.  One or more of these items is in a special status (Checked Out, In Transit, etc.) and cannot be changed to the Missing status.','Action failed.','OK',null,null,'Check here to confirm this message');
292
293                 } else {
294
295                         var r = error.yns_alert('Change the status for these items to Missing?  Barcodes: ' + util.functional.map_list( copies, function(o) { return o.barcode(); } ).join(", "), 'Mark Missing', 'OK', 'Cancel', null, 'Check here to confirm this action');
296
297                         if (r == 0) {
298                                 var count = 0;
299                                 for (var i = 0; i < copies.length; i++) {
300                                         try {
301                                                 var robj = network.simple_request('MARK_ITEM_MISSING',[ses(),copies[i].id()]);
302                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
303                                                 count++;
304                                         } catch(E) {
305                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' missing.',E);
306                                         }
307                                 }
308                                 alert(count == 1 ? 'Item marked Missing' : count + ' items marked Missing.');
309                         }
310                 }
311
312         } catch(E) {
313                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_missing',E); else alert('FIXME: ' + E);
314         }
315 }
316
317
318 dump('exiting cat/util.js\n');