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