0bf2139fb9915f25485ecbeaeea8ef5971c40fee
[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 }
20
21 function _holdsHandleStaff() {
22         var barcode = $('xul_recipient_barcode').value;
23         var user = grabUserByBarcode( G.user.session, barcode );
24         var code = checkILSEvent(user);
25         if(code || !user) {
26                 alertILSEvent(code, barcode);
27                 showCanvas();
28                 return;
29         }
30         holdRecipient = user;
31         holdsDrawWindow( currentHoldRecord, null );
32 }
33
34 function holdsDrawWindow(recid, type, edithold, done_callback) {
35
36         if(recid == null) {
37                 recid = currentHoldRecord;
38                 if(recid == null) return;
39         }       
40         currentHoldRecord = recid;
41         holdEditHold = edithold;
42         holdEditCallback = done_callback;
43         
44         if(isXUL() && holdRecipient == null ) { 
45                 holdsHandleStaff();
46                 return;
47         }
48
49         if( holdRecipient == null ) holdRecipient = G.user;
50         if( holdRequestor == null ) holdRequestor = G.user;
51
52         if(!(holdRequestor && holdRequestor.session)) {
53
54                 detachAllEvt('common','locationChanged');
55                 attachEvt('common','loggedIn', holdsDrawWindow)
56                 initLogin();
57                 return;
58         }
59
60
61         swapCanvas($('check_holds_box'));
62
63         if(!edithold) {
64                 setTimeout( function() { holdsCheckPossibility(recid, type); }, 10 );
65
66         } else {
67                 _holdsDrawWindow(recid, type);
68                 _holdsUpdateEditHold(edithold);
69         }
70 }
71
72 function _holdsUpdateEditHold(hold) {
73
74         if( hold.capture_time() )
75                 $('holds_org_selector').disabled = true;
76         else
77                 setSelector($('holds_org_selector'), hold.pickup_lib());
78
79         $('holds_submit').onclick = holdsEditHold;
80
81
82         if(hold.phone_notify()) {
83                 $('holds_enable_phone').checked = true;
84                 $('holds_phone').value = hold.phone_notify();
85
86         } else {
87                 $('holds_phone').disabled = true;
88                 $('holds_enable_phone').checked = false;
89         }
90
91         if(hold.email_notify()) {
92                 $('holds_enable_email').checked = true;
93
94         } else {
95                 $('holds_enable_email').checked = false;
96         }
97 }
98
99 function holdsEditHold() {
100         var hold = holdsBuildHoldFromWindow();
101         hold.id( holdEditHold.id() );
102         holdsUpdate(hold);
103         showCanvas();
104         if(holdEditCallback) holdEditCallback(hold);
105 }
106
107
108 function _holdsDrawWindow(recid, type) {
109
110         swapCanvas($('holds_box'));
111
112         var rec = findRecord( recid, type );
113         currentHoldsRecordObj = rec;
114
115         if(!holdsOrgSelectorBuilt) {
116                 holdsBuildOrgSelector(null,0);
117                 holdsOrgSelectorBuilt = true;
118         }
119
120         appendClear($('holds_recipient'), text(
121                 holdRecipient.family_name() + ', ' +  
122                         holdRecipient.first_given_name()));
123         appendClear($('holds_title'), text(rec.title()));
124         appendClear($('holds_author'), text(rec.author()));
125
126         removeChildren($('holds_format'));
127         for( var i in rec.types_of_resource() ) {
128                 var res = rec.types_of_resource()[i];
129                 var img = elem("img");
130                 setResourcePic(img, res);
131                 $('holds_format').appendChild(text(' '+res+' '));
132                 $('holds_format').appendChild(img);
133                 $('holds_format').appendChild(text(' '));
134         }
135
136
137         $('holds_phone').value = holdRecipient.day_phone();
138         appendClear( $('holds_email'), text(holdRecipient.email()));
139
140         var pref = G.user.prefs[PREF_HOLD_NOTIFY];
141
142         if(pref) {
143                 if( ! pref.match(/email/i) ) 
144                         $('holds_enable_email').checked = false;
145
146                 if( ! pref.match(/phone/i) ) {
147                         $('holds_phone').disabled = true;
148                         $('holds_enable_phone').checked = false;
149                 }
150         }
151
152         $('holds_cancel').onclick = showCanvas;
153         $('holds_submit').onclick = function(){holdsPlaceHold(holdsBuildHoldFromWindow())};
154         appendClear($('holds_physical_desc'), text(rec.physical_description()));
155         if(type == 'M') hideMe($('hold_physical_desc_row'));
156 }
157
158
159 function holdsCheckPossibility(recid, type) {
160         var req = new Request(CHECK_HOLD_POSSIBLE, G.user.session, 
161                         { titleid : recid, patronid : G.user.id(), depth : 0 } );
162         req.send(true);
163         var res = req.result();
164
165         if(res) _holdsDrawWindow(recid, type);
166         else drawCanvas();
167 }
168
169
170 function holdsBuildOrgSelector(node) {
171
172         if(!node) node = globalOrgTree;
173
174         var selector = $('holds_org_selector');
175         var index = selector.options.length;
176
177         var type = findOrgType(node.ou_type());
178         var indent = type.depth() - 1;
179         var opt = setSelectorVal( selector, index, node.name(), node.id(), null, indent );
180         if(!type.can_have_vols()) opt.disabled = true;
181         
182         if( node.id() == holdRecipient.home_ou() ) {
183                 selector.selectedIndex = index;
184                 selector.options[index].selected = true;        
185         }
186
187         for( var i in node.children() ) {
188                 var child = node.children()[i];
189                 if(child) holdsBuildOrgSelector(child);
190         }
191 }
192
193 function holdsBuildHoldFromWindow() {
194
195         var org = $('holds_org_selector').options[
196                 $('holds_org_selector').selectedIndex].value;
197
198         var hold = new ahr();
199
200
201         if( $('holds_enable_phone').checked ) {
202                 var phone = $('holds_phone').value;
203                 if( !phone.match(REGEX_PHONE) ) {
204                         alert($('holds_bad_phone').innerHTML);
205                         return;
206                 }
207                 hold.phone_notify(phone);
208
209         } else {
210                 hold.phone_notify("");
211         }
212
213         if( $('holds_enable_email').checked ) 
214                 hold.email_notify(1);
215         else
216                 hold.email_notify(0);
217
218
219         hold.pickup_lib(org); 
220         hold.request_lib(org); 
221         hold.requestor(holdRequestor.id());
222         hold.usr(holdRecipient.id());
223         hold.hold_type('T');
224         hold.target(currentHoldRecord);
225         return hold;
226 }
227         
228 function holdsPlaceHold(hold) {
229
230         var req = new Request( CREATE_HOLD, holdRequestor.session, hold );
231         req.send(true);
232         var res = req.result();
233
234         if( res == '1' ) alert($('holds_success').innerHTML);
235         else alert($('holds_failure').innerHTML);
236         
237         showCanvas();
238         holdRecipient = null;
239         holdRequestor = null;
240 }
241
242 function holdsCancel(holdid, user) {
243         if(!user) user = G.user;
244         var req = new Request(CANCEL_HOLD, user.session, holdid);
245         req.send(true);
246         return req.result();
247 }
248
249 function holdsUpdate(hold, user) {
250         if(!user) user = G.user;
251         var req = new Request(UPDATE_HOLD, user.session, hold);
252         req.send(true);
253         var x = req.result(); /* cause an exception if there is one */
254 }
255
256
257
258