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