]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/skin/default/js/holds.js
hold possibility is now checked after filling out the hold form to
[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 ) { 
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                 _holdsUpdateEditHold(edithold);
74         }  
75 }
76
77 function _holdsUpdateEditHold(hold) {
78
79         if( hold.capture_time() )
80                 $('holds_org_selector').disabled = true;
81         else
82                 setSelector($('holds_org_selector'), hold.pickup_lib());
83
84         $('holds_submit').onclick = holdsEditHold;
85         $('holds_update').onclick = holdsEditHold;
86
87
88         if(hold.phone_notify()) {
89                 $('holds_enable_phone').checked = true;
90                 $('holds_phone').value = hold.phone_notify();
91
92         } else {
93                 $('holds_phone').disabled = true;
94                 $('holds_enable_phone').checked = false;
95         }
96
97         if(hold.email_notify()) {
98                 $('holds_enable_email').checked = true;
99
100         } else {
101                 $('holds_enable_email').checked = false;
102         }
103 }
104
105 function holdsEditHold() {
106         var hold = holdsBuildHoldFromWindow();
107         //hold.id( holdEditHold.id() );
108         holdsUpdate(hold);
109         showCanvas();
110         if(holdEditCallback) holdEditCallback(hold);
111 }
112
113
114 function _holdsDrawWindow(recid, type) {
115
116         swapCanvas($('holds_box'));
117
118         var rec = findRecord( recid, type );
119         currentHoldsRecordObj = rec;
120
121         if(!holdsOrgSelectorBuilt) {
122                 holdsBuildOrgSelector(null,0);
123                 holdsOrgSelectorBuilt = true;
124         }
125
126         if(isXUL()) {
127                 var dsel = $('holds_depth_selector');
128                 unHideMe($('holds_depth_selector_row'));
129                 if(dsel.getElementsByTagName('option').length == 0) {
130                         var types = globalOrgTypes;
131                         var depth = findOrgDepth(G.user.ws_ou());
132                         iterate(types, 
133                                 function(t) {
134                                         if(t.depth() > depth) return;
135                                         insertSelectorVal(dsel, -1, t.opac_label(), t.depth());
136                                 }
137                         );
138                 }
139         }
140
141         appendClear($('holds_recipient'), text(
142                 holdRecipient.family_name() + ', ' +  
143                         holdRecipient.first_given_name()));
144         appendClear($('holds_title'), text(rec.title()));
145         appendClear($('holds_author'), text(rec.author()));
146
147         removeChildren($('holds_format'));
148         for( var i in rec.types_of_resource() ) {
149                 var res = rec.types_of_resource()[i];
150                 var img = elem("img");
151                 setResourcePic(img, res);
152                 $('holds_format').appendChild(text(' '+res+' '));
153                 $('holds_format').appendChild(img);
154                 $('holds_format').appendChild(text(' '));
155         }
156
157
158         $('holds_phone').value = holdRecipient.day_phone();
159         appendClear( $('holds_email'), text(holdRecipient.email()));
160
161         var pref = G.user.prefs[PREF_HOLD_NOTIFY];
162
163         if(pref) {
164                 if( ! pref.match(/email/i) ) 
165                         $('holds_enable_email').checked = false;
166
167                 if( ! pref.match(/phone/i) ) {
168                         $('holds_phone').disabled = true;
169                         $('holds_enable_phone').checked = false;
170                 }
171         }
172
173         $('holds_cancel').onclick = function(){ runEvt('common', 'holdUpdateCanceled'), showCanvas() };
174         $('holds_submit').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
175         $('holds_update').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
176         appendClear($('holds_physical_desc'), text(rec.physical_description()));
177         if(type == 'M') hideMe($('hold_physical_desc_row'));
178 }
179
180
181 function holdsCheckPossibility(recid, type, pickuplib) {
182         var req = new Request(CHECK_HOLD_POSSIBLE, G.user.session, 
183                         { titleid : recid, patronid : G.user.id(), depth : 0, pickup_lib : pickuplib } );
184         req.send(true);
185         return req.result();
186 }
187
188
189 function holdsBuildOrgSelector(node) {
190
191         if(!node) node = globalOrgTree;
192
193         var selector = $('holds_org_selector');
194         var index = selector.options.length;
195
196         var type = findOrgType(node.ou_type());
197         var indent = type.depth() - 1;
198         var opt = setSelectorVal( selector, index, node.name(), node.id(), null, indent );
199         if(!type.can_have_vols()) opt.disabled = true;
200         
201         if( node.id() == holdRecipient.home_ou() ) {
202                 selector.selectedIndex = index;
203                 selector.options[index].selected = true;        
204         }
205
206         for( var i in node.children() ) {
207                 var child = node.children()[i];
208                 if(child) holdsBuildOrgSelector(child);
209         }
210 }
211
212 function holdsBuildHoldFromWindow() {
213
214         var org = $('holds_org_selector').options[
215                 $('holds_org_selector').selectedIndex].value;
216
217         var hold = new ahr();
218         if(holdEditHold) {
219                 hold = holdEditHold;
220                 holdEditHold = null;
221         }
222
223
224         if( $('holds_enable_phone').checked ) {
225                 var phone = $('holds_phone').value;
226                 if( !phone.match(REGEX_PHONE) ) {
227                         alert($('holds_bad_phone').innerHTML);
228                         return;
229                 }
230                 hold.phone_notify(phone);
231
232         } else {
233                 hold.phone_notify("");
234         }
235
236         if( $('holds_enable_email').checked ) 
237                 hold.email_notify(1);
238         else
239                 hold.email_notify(0);
240
241
242         hold.pickup_lib(org); 
243         hold.request_lib(org); 
244         hold.requestor(holdRequestor.id());
245         hold.usr(holdRecipient.id());
246         hold.hold_type('T');
247         hold.target(currentHoldRecord);
248         if(isXUL())             
249                 hold.selection_depth(getSelectorVal($('holds_depth_selector')));
250         return hold;
251 }
252         
253 function holdsPlaceHold(hold) {
254
255         swapCanvas($('check_holds_box'));
256         alert(currentHoldRecord + ' : ' +  hold.pickup_lib() );
257
258         if( ! holdsCheckPossibility(currentHoldRecord, currentHoldType, hold.pickup_lib() ) ) {
259                 alert($('hold_not_allowed').innerHTML);
260                 drawCanvas();
261                 return;
262         }
263
264         var req = new Request( CREATE_HOLD, holdRequestor.session, hold );
265         req.send(true);
266         var res = req.result();
267
268         if( res == '1' ) alert($('holds_success').innerHTML);
269         else alert($('holds_failure').innerHTML);
270         
271         showCanvas();
272
273         holdRecipient = null;
274         holdRequestor = null;
275         currentHoldRecord = null;
276         currentHoldType = null;
277
278         runEvt('common', 'holdUpdated');
279 }
280
281 function holdsCancel(holdid, user) {
282         if(!user) user = G.user;
283         var req = new Request(CANCEL_HOLD, user.session, holdid);
284         req.send(true);
285         return req.result();
286         runEvt('common', 'holdUpdated');
287 }
288
289 function holdsUpdate(hold, user) {
290         if(!user) user = G.user;
291         var req = new Request(UPDATE_HOLD, user.session, hold);
292         req.send(true);
293         var x = req.result(); /* cause an exception if there is one */
294         runEvt('common', 'holdUpdated');
295 }
296
297
298
299