]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/cat/util.js
refactor spawn_copy_editor and perm check
[working/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;
21         try {
22                         copy = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ old_bc ]);
23                         if (typeof copy.ilsevent != 'undefined') throw(copy); 
24                         if (!copy) throw(copy);
25                 } catch(E) {
26                         alert('We were unable to retrieve an item with barcode "' + old_bc + '".\n');
27                         return old_bc;
28                 }
29         
30                 // Why did I want to do this twice?  Because this copy is more fleshed?
31                 try {
32                         copy = network.simple_request('FM_ACP_RETRIEVE',[ copy.id() ]);
33                         if (typeof copy.ilsevent != 'undefined') throw(copy);
34                         if (!copy) throw(copy);
35                 } catch(E) {
36                         try { alert('We were unable to retrieve an item with barcode "' + old_bc + '".\n' + (typeof E.ilsevent == 'undefined' ? '' : E.textcode + ' : ' + E.desc)); } catch(F) { alert(E + '\n' + F); }
37                         return old_bc;
38                 }
39         
40                 var new_bc = window.prompt('Enter the replacement barcode for the copy:','','Replace Barcode');
41                 new_bc = String( new_bc ).replace(/\s/g,'');
42                 if (!new_bc) {
43                         alert('Rename aborted.  Blank for barcode not allowed.');
44                         return old_bc;
45                 }
46         
47                 var test = network.simple_request('FM_ACP_RETRIEVE_VIA_BARCODE',[ new_bc ]);
48                 if (typeof test.ilsevent == 'undefined') {
49                         alert('Rename aborted.  Another copy has barcode "' + new_bc + '".');
50                         return old_bc;
51                 } else {
52                         if (test.ilsevent != 1502 /* ASSET_COPY_NOT_FOUND */) {
53                                 obj.error.standard_unexpected_error_alert('Error testing replacement barcode "' + new_bc + '".',test);
54                                 return old_bc;
55                         }       
56                 }
57
58                 copy.barcode(new_bc); copy.ischanged('1');
59                 var r = network.simple_request('FM_ACP_FLESHED_BATCH_UPDATE', [ ses(), [ copy ] ]);
60                 if (typeof r.ilsevent != 'undefined') { 
61                         if (r.ilsevent != 0) {
62                                 if (r.ilsevent == 5000 /* PERM_FAILURE */) {
63                                         alert('Renamed aborted.  Insufficient permission.');
64                                         return old_bc;
65                                 } else {
66                                         obj.error.standard_unexpected_error_alert('Error renaming item.',r);
67                                         return old_bc;
68                                 }
69                         }
70                 }
71
72                 return new_bc;
73         } catch(E) {
74                 JSAN.use('util.error'); var error = new util.error();
75                 error.standard_unexpected_error_alert('Rename did not likely occur.',E);
76                 return old_bc;
77         }
78 }
79
80 cat.util.transfer_copies = function(params) {
81         JSAN.use('util.error'); var error = new util.error();
82         JSAN.use('OpenILS.data'); var data = new OpenILS.data();
83         JSAN.use('util.network'); var network = new util.network();
84         try {
85                 data.stash_retrieve();
86                 if (!data.marked_volume) {
87                         alert('Please mark a volume as the destination from within holdings maintenance and then try this again.');
88                         return;
89                 }
90                 netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserWrite');
91                 var xml = '<vbox xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" flex="1" style="overflow: auto">';
92                 if (!params.message) {
93                         params.message = 'Transfer items from their original volumes to ';
94                         params.message += data.hash.aou[ params.owning_lib ].shortname() + "'s volume labelled ";
95                         params.message += '"' + params.volume_label + '" on the following record (and change their circ libs to match)?';
96                 }
97
98                 xml += '<description>' + params.message.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;') + '</description>';
99                 xml += '<hbox><button label="Transfer" name="fancy_submit"/>';
100                 xml += '<button label="Cancel" accesskey="C" name="fancy_cancel"/></hbox>';
101                 xml += '<iframe style="overflow: scroll" flex="1" src="' + urls.XUL_BIB_BRIEF + '?docid=' + params.docid + '"/>';
102                 xml += '</vbox>';
103                 //data.temp_transfer = xml; data.stash('temp_transfer');
104                 JSAN.use('util.window'); var win = new util.window();
105                 var fancy_prompt_data = win.open(
106                         urls.XUL_FANCY_PROMPT,
107                         //+ '?xml_in_stash=temp_transfer'
108                         //+ '&title=' + window.escape('Item Transfer'),
109                         'fancy_prompt', 'chrome,resizable,modal,width=500,height=300',
110                         { 'xml' : xml, 'title' : 'Item Transfer' }
111                 );
112                 if (fancy_prompt_data.fancy_status == 'incomplete') { alert('Transfer Aborted'); return; }
113
114                 JSAN.use('util.functional');
115
116                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ params.copy_ids ]);
117
118                 for (var i = 0; i < copies.length; i++) {
119                         copies[i].call_number( data.marked_volume );
120                         copies[i].circ_lib( params.owning_lib );
121                         copies[i].ischanged( 1 );
122                 }
123
124                 var robj = network.simple_request(
125                         'FM_ACP_FLESHED_BATCH_UPDATE', 
126                         [ ses(), copies, true ], 
127                         null,
128                         {
129                                 'title' : 'Override Transfer Failure?',
130                                 'overridable_events' : [
131                                         1208 /* TITLE_LAST_COPY */,
132                                         1227 /* COPY_DELETE_WARNING */,
133                                 ]
134                         }
135                 );
136                 
137                 if (typeof robj.ilsevent != 'undefined') {
138                         throw(robj);
139                 } else {
140                         alert('Items transferred.');
141                 }
142
143         } catch(E) {
144                 error.standard_unexpected_error_alert('All items not likely transferred.',E);
145         }
146 }
147
148 cat.util.spawn_spine_editor = function(selection_list) {
149         JSAN.use('util.error'); var error = new util.error();
150         try {
151                 JSAN.use('util.functional');
152                 JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.stash_retrieve();
153                 data.temp_barcodes_for_labels = util.functional.map_list( selection_list, function(o){return o.barcode;}) ; 
154                 data.stash('temp_barcodes_for_labels');
155                 xulG.new_tab(
156                         xulG.url_prefix( urls.XUL_SPINE_LABEL ),
157                         { 'tab_name' : 'Spine Labels' },
158                         {}
159                 );
160         } catch(E) {
161                 error.standard_unexpected_error_alert('Spine Labels',E);
162         }
163 }
164
165 cat.util.show_in_opac = function(selection_list) {
166         JSAN.use('util.error'); var error = new util.error();
167         var doc_id; var seen = {};
168         try {
169                 for (var i = 0; i < selection_list.length; i++) {
170                         doc_id = selection_list[i].doc_id;
171                         if (!doc_id) {
172                                 alert(selection_list[i].barcode + ' is not cataloged');
173                                 continue;
174                         }
175                         if (typeof seen[doc_id] != 'undefined') {
176                                 continue;
177                         }
178                         seen[doc_id] = true;
179                         var opac_url = xulG.url_prefix( urls.opac_rdetail ) + '?r=' + doc_id;
180                         var content_params = { 
181                                 'session' : ses(),
182                                 'authtime' : ses('authtime'),
183                                 'opac_url' : opac_url,
184                         };
185                         xulG.new_tab(
186                                 xulG.url_prefix(urls.XUL_OPAC_WRAPPER), 
187                                 {'tab_name':'Retrieving title...'}, 
188                                 content_params
189                         );
190                 }
191         } catch(E) {
192                 error.standard_unexpected_error_alert('Error opening catalog for document id = ' + doc_id,E);
193         }
194 }
195
196 cat.util.add_copies_to_bucket = function(selection_list) {
197         JSAN.use('util.functional');
198         JSAN.use('util.window'); var win = new util.window();
199         JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
200         data.cb_temp_copy_ids = js2JSON(
201                 util.functional.map_list(
202                         selection_list,
203                         function (o) {
204                                 if (typeof o.copy_id != 'undefined' && o.copy_id != null) {
205                                         return o.copy_id;
206                                 } else {
207                                         return o;
208                                 }
209                         }
210                 )
211         );
212         data.stash('cb_temp_copy_ids');
213         win.open( 
214                 xulG.url_prefix(urls.XUL_COPY_BUCKETS_QUICK),
215                 'sel_bucket_win' + win.window_name_increment(),
216                 'chrome,resizable,center'
217         );
218 }
219
220 cat.util.spawn_copy_editor = function(params) {
221         try {
222         if (!params.copy_ids && !params.copies) return;
223                 if (params.copy_ids && params.copy_ids.length == 0) return;
224                 if (params.copies && params.copies.length == 0) return;
225         if (params.copy_ids) params.copy_ids = js2JSON(params.copy_ids); // legacy
226         if (!params.caller_handles_update) params.handle_update = 1; // legacy
227
228                 var obj = {};
229                 JSAN.use('util.network'); obj.network = new util.network();
230                 JSAN.use('util.error'); obj.error = new util.error();
231         
232                 var title = '';
233                 if ((params.copy_ids && params.copy_ids.length > 1) || (params.copies && params.copies.length > 1 )) title += 'Batch ';
234                 title += params.edit == 1 ? 'Edit' : 'View';
235                 title += ' Copy Attributes';
236         
237                 JSAN.use('util.window'); var win = new util.window();
238                 var my_xulG = win.open(
239                         (urls.XUL_COPY_EDITOR),
240                         title,
241                         'chrome,modal,resizable',
242             params
243                 );
244                 if (!my_xulG.copies && params.edit) {
245             alert(typeof params.no_copies_modified_msg != 'undefined' ? params.no_copies_modified_msg : 'Copies not modified.');
246         } else {
247             return my_xulG.copies;
248         }
249         return [];
250         } catch(E) {
251                 JSAN.use('util.error'); var error = new util.error();
252                 error.standard_unexpected_error_alert('error in cat.util.spawn_copy_editor',E);
253         }
254 }
255
256 cat.util.mark_item_damaged = function(copy_ids) {
257         var error;
258         try {
259                 JSAN.use('util.error'); error = new util.error();
260                 JSAN.use('util.functional');
261                 JSAN.use('util.network'); var network = new util.network();
262                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
263                 if (typeof copies.ilsevent != 'undefined') throw(copies);
264                 var magic_status = false;
265                 for (var i = 0; i < copies.length; i++) {
266                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
267                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
268                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
269                 }
270                 if (magic_status) {
271                 
272                         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');
273
274                 } else {
275
276                         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');
277
278                         if (r == 0) {
279                                 var count = 0;
280                                 for (var i = 0; i < copies.length; i++) {
281                                         try {
282                                                 var robj = network.simple_request('MARK_ITEM_DAMAGED',[ses(),copies[i].id()]);
283                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
284                                                 count++;
285                                         } catch(E) {
286                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' damaged.',E);
287                                         }
288                                 }
289                                 alert(count == 1 ? 'Item marked Damaged' : count + ' items marked Damaged.');
290                         }
291                 }
292
293         } catch(E) {
294                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_damaged',E); else alert('FIXME: ' + E);
295         }
296 }
297
298 cat.util.mark_item_missing = function(copy_ids) {
299         var error;
300         try {
301                 JSAN.use('util.error'); error = new util.error();
302                 JSAN.use('util.functional');
303                 JSAN.use('util.network'); var network = new util.network();
304                 var copies = network.simple_request('FM_ACP_FLESHED_BATCH_RETRIEVE', [ copy_ids ]);
305                 if (typeof copies.ilsevent != 'undefined') throw(copies);
306                 var magic_status = false;
307                 for (var i = 0; i < copies.length; i++) {
308                         var status = copies[i].status(); if (typeof status == 'object') status = status.id();
309                         if (typeof my_constants.magical_statuses[ status ] != 'undefined') 
310                                 if (my_constants.magical_statuses[ status ].block_mark_item_action) magic_status = true;
311                 }
312                 if (magic_status) {
313                 
314                         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');
315
316                 } else {
317
318                         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');
319
320                         if (r == 0) {
321                                 var count = 0;
322                                 for (var i = 0; i < copies.length; i++) {
323                                         try {
324                                                 var robj = network.simple_request('MARK_ITEM_MISSING',[ses(),copies[i].id()]);
325                                                 if (typeof robj.ilsevent != 'undefined') throw(robj);
326                                                 count++;
327                                         } catch(E) {
328                                                 error.standard_unexpected_error_alert('Error marking item ' + copies[i].barcode() + ' missing.',E);
329                                         }
330                                 }
331                                 alert(count == 1 ? 'Item marked Missing' : count + ' items marked Missing.');
332                         }
333                 }
334
335         } catch(E) {
336                 if (error) error.standard_unexpected_error_alert('cat.util.mark_item_missing',E); else alert('FIXME: ' + E);
337         }
338 }
339
340
341 dump('exiting cat/util.js\n');