]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/holds.js
redrawing hold box when the hold is not possible
[Evergreen.git] / Open-ILS / web / opac / skin / default / js / holds.js
1
2 var currentHoldRecord;
3 var currentHoldType;
4 var currentHoldRecordObj;
5 var holdsOrgSelectorBuilt = false;
6 var holdRecipient;
7 var holdRequestor
8 var holdEmail;
9 var holdPhone;
10 var holdEditHold;
11 var holdEditCallback;
12
13
14 function holdsHandleStaff() {
15         swapCanvas($('xulholds_box'));
16         $('xul_recipient_barcode').focus();
17         $('xul_recipient_barcode').onkeypress = function(evt) 
18                 {if(userPressedEnter(evt)) { _holdsHandleStaff(); } };
19         $('xul_recipient_barcode_submit').onclick = _holdsHandleStaff;
20         $('xul_recipient_me').onclick = _holdsHandleStaffMe;
21 }
22
23 function _holdsHandleStaffMe() {
24         holdRecipient = G.user;
25         holdsDrawWindow( currentHoldRecord, null );
26 }
27
28 function _holdsHandleStaff() {
29         var barcode = $('xul_recipient_barcode').value;
30         var user = grabUserByBarcode( G.user.session, barcode );
31         var code = checkILSEvent(user);
32         if(code || !user) {
33                 alertILSEvent(user, barcode);
34                 showCanvas();
35                 return;
36         }
37         holdRecipient = user;
38         holdsDrawWindow( currentHoldRecord, null );
39 }
40
41 function holdsDrawWindow(recid, type, edithold, done_callback) {
42
43         if(recid == null) {
44                 recid = currentHoldRecord;
45                 if(recid == null) return;
46         }       
47
48         currentHoldRecord = recid;
49         currentHoldType = type;
50         holdEditHold = edithold;
51         holdEditCallback = done_callback;
52         
53         if(isXUL() && holdRecipient == null && holdEditHold == null) { 
54                 holdsHandleStaff();
55                 return;
56         }
57
58         if( holdRecipient == null ) holdRecipient = G.user;
59         if( holdRequestor == null ) holdRequestor = G.user;
60
61         if(!(holdRequestor && holdRequestor.session)) {
62
63                 detachAllEvt('common','locationChanged');
64                 attachEvt('common','loggedIn', holdsDrawWindow)
65                 initLogin();
66                 return;
67         }
68
69         _holdsDrawWindow(recid, type);
70         if(edithold) {
71                 hideMe($('holds_submit'));
72                 unHideMe($('holds_update'));
73
74                 var req = new Request(FETCH_HOLD_STATUS, G.user.session, edithold.id() );
75                 req.send(true);
76                 status = req.result();
77
78                 _holdsUpdateEditHold(edithold, status);
79         }  
80 }
81
82 function _holdsUpdateEditHold(hold, status) {
83
84         if( hold.capture_time() )
85                 $('holds_org_selector').disabled = true;
86         else
87                 setSelector($('holds_org_selector'), hold.pickup_lib());
88
89         if( status > 2 ) $('holds_org_selector').disabled = true;
90
91         $('holds_submit').onclick = holdsEditHold;
92         $('holds_update').onclick = holdsEditHold;
93
94
95         if(hold.phone_notify()) {
96                 $('holds_enable_phone').checked = true;
97                 $('holds_phone').value = hold.phone_notify();
98
99         } else {
100                 $('holds_phone').disabled = true;
101                 $('holds_enable_phone').checked = false;
102         }
103
104         if(hold.email_notify()) {
105                 $('holds_enable_email').checked = true;
106
107         } else {
108                 $('holds_enable_email').checked = false;
109         }
110 }
111
112 function holdsEditHold() {
113         var hold = holdsBuildHoldFromWindow();
114         //hold.id( holdEditHold.id() );
115         if(!hold) return;
116         holdsUpdate(hold);
117         showCanvas();
118         if(holdEditCallback) holdEditCallback(hold);
119 }
120
121
122 function _holdsDrawWindow(recid, type) {
123
124         swapCanvas($('holds_box'));
125
126         var rec = findRecord( recid, type );
127         currentHoldsRecordObj = rec;
128
129         if(!holdsOrgSelectorBuilt) {
130                 holdsBuildOrgSelector(null,0);
131                 holdsOrgSelectorBuilt = true;
132         }
133
134         if(isXUL()) {
135                 var dsel = $('holds_depth_selector');
136                 unHideMe($('holds_depth_selector_row'));
137                 if(dsel.getElementsByTagName('option').length == 0) {
138                         var types = globalOrgTypes;
139                         var depth = findOrgDepth(G.user.ws_ou());
140                         iterate(types, 
141                                 function(t) {
142                                         if(t.depth() > depth) return;
143                                         insertSelectorVal(dsel, -1, t.opac_label(), t.depth());
144                                 }
145                         );
146                 }
147         }
148
149         appendClear($('holds_recipient'), text(
150                 holdRecipient.family_name() + ', ' +  
151                         holdRecipient.first_given_name()));
152         appendClear($('holds_title'), text(rec.title()));
153         appendClear($('holds_author'), text(rec.author()));
154
155         removeChildren($('holds_format'));
156         for( var i in rec.types_of_resource() ) {
157                 var res = rec.types_of_resource()[i];
158                 var img = elem("img");
159                 setResourcePic(img, res);
160                 $('holds_format').appendChild(text(' '+res+' '));
161                 $('holds_format').appendChild(img);
162                 $('holds_format').appendChild(text(' '));
163         }
164
165
166         $('holds_phone').value = holdRecipient.day_phone();
167         appendClear( $('holds_email'), text(holdRecipient.email()));
168
169         var pref = G.user.prefs[PREF_HOLD_NOTIFY];
170
171         if(pref) {
172                 if( ! pref.match(/email/i) ) 
173                         $('holds_enable_email').checked = false;
174
175                 if( ! pref.match(/phone/i) ) {
176                         $('holds_phone').disabled = true;
177                         $('holds_enable_phone').checked = false;
178                 }
179         }
180
181         $('holds_cancel').onclick = function(){ runEvt('common', 'holdUpdateCanceled'), showCanvas() };
182         $('holds_submit').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
183         $('holds_update').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
184         appendClear($('holds_physical_desc'), text(rec.physical_description()));
185         if(type == 'M') hideMe($('hold_physical_desc_row'));
186 }
187
188
189 function holdsCheckPossibility(recid, type, pickuplib) {
190         var req = new Request(CHECK_HOLD_POSSIBLE, G.user.session, 
191                         { titleid : recid, patronid : G.user.id(), depth : 0, pickup_lib : pickuplib } );
192         req.send(true);
193         return req.result();
194 }
195
196
197 function holdsBuildOrgSelector(node) {
198
199         if(!node) node = globalOrgTree;
200
201         var selector = $('holds_org_selector');
202         var index = selector.options.length;
203
204         var type = findOrgType(node.ou_type());
205         var indent = type.depth() - 1;
206         var opt = setSelectorVal( selector, index, node.name(), node.id(), null, indent );
207         if(!type.can_have_vols()) opt.disabled = true;
208         
209         if( node.id() == holdRecipient.home_ou() ) {
210                 selector.selectedIndex = index;
211                 selector.options[index].selected = true;        
212         }
213
214         for( var i in node.children() ) {
215                 var child = node.children()[i];
216                 if(child) holdsBuildOrgSelector(child);
217         }
218 }
219
220 function holdsBuildHoldFromWindow() {
221
222         var org = $('holds_org_selector').options[
223                 $('holds_org_selector').selectedIndex].value;
224
225         var hold = new ahr();
226         if(holdEditHold) {
227                 hold = holdEditHold;
228                 holdEditHold = null;
229         }
230
231
232         if( $('holds_enable_phone').checked ) {
233                 var phone = $('holds_phone').value;
234                 if( !phone || !phone.match(REGEX_PHONE) ) {
235                         alert($('holds_bad_phone').innerHTML);
236                         return null;
237                 }
238                 hold.phone_notify(phone);
239
240         } else {
241                 hold.phone_notify("");
242         }
243
244         if( $('holds_enable_email').checked ) 
245                 hold.email_notify(1);
246         else
247                 hold.email_notify(0);
248
249
250         hold.pickup_lib(org); 
251         hold.request_lib(org); 
252         hold.requestor(holdRequestor.id());
253         hold.usr(holdRecipient.id());
254         hold.hold_type('T');
255         hold.target(currentHoldRecord);
256         if(isXUL())             
257                 hold.selection_depth(getSelectorVal($('holds_depth_selector')));
258         return hold;
259 }
260         
261 function holdsPlaceHold(hold) {
262
263         if(!hold) return;
264
265         swapCanvas($('check_holds_box'));
266
267         if( ! holdsCheckPossibility(currentHoldRecord, currentHoldType, hold.pickup_lib() ) ) {
268                 alert($('hold_not_allowed').innerHTML);
269                 swapCanvas($('holds_box'));
270                 return;
271         }
272
273         var req = new Request( CREATE_HOLD, holdRequestor.session, hold );
274         req.send(true);
275         var res = req.result();
276
277         if( res == '1' ) alert($('holds_success').innerHTML);
278         else alert($('holds_failure').innerHTML);
279         
280         showCanvas();
281
282         holdRecipient = null;
283         holdRequestor = null;
284         currentHoldRecord = null;
285         currentHoldType = null;
286
287         runEvt('common', 'holdUpdated');
288 }
289
290 function holdsCancel(holdid, user) {
291         if(!user) user = G.user;
292         var req = new Request(CANCEL_HOLD, user.session, holdid);
293         req.send(true);
294         return req.result();
295         runEvt('common', 'holdUpdated');
296 }
297
298 function holdsUpdate(hold, user) {
299         if(!user) user = G.user;
300         var req = new Request(UPDATE_HOLD, user.session, hold);
301         req.send(true);
302         var x = req.result(); /* cause an exception if there is one */
303         runEvt('common', 'holdUpdated');
304 }
305
306
307
308