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