]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/javascript/opac/HoldsWindow.js
more web work
[Evergreen.git] / Open-ILS / src / javascript / opac / HoldsWindow.js
1
2 /* 
3         @param record the id of the target item 
4         @param type is the hold level (M, T, V, C) for metarecord, title,
5                 volume, and copy respectively.
6         @param requestor is the user object (fieldmapper) for the requestor
7         @param recipient is the user object (fieldmapper) for the hold recipient
8                 role in the holds process 
9         @param requestor_login is the login session of the hold requestor
10         */
11 function HoldsWindow(record, type, requestor, recipient, requestor_login) {
12
13         this.record = record;
14         this.div = elem("div");
15         this.requestor = requestor;
16         this.recipient  = recipient;
17         this.type = type;
18         this.session = requestor_login;
19         this.built = false;
20
21         add_css_class(this.div, "holds_window");
22         add_css_class(this.div, "hide_me");
23         getDocument().body.appendChild(this.div);
24 }
25
26
27 HoldsWindow.prototype.process = function() {
28
29         /* collect the checked items */
30         var formats = "";
31         if(this.type == "M") {
32                 for(var idx in resourceFormats) {
33                         var form = resourceFormats[idx];
34                         var item = getById( form + "_hold_checkbox");
35                         if(item.checked) {
36                                 formats += modsFormatToMARC(form);
37                         }
38                 }
39                 
40                 if(formats.length == 0) {
41                         alert("Please select at least one item format");
42                         this.toggle();
43                 }
44         } 
45
46         var sel = getById("holds_org_selector");
47         var orgNode = sel.options[sel.selectedIndex];
48         var org = findOrgUnit(orgNode.value);
49
50         /* for now... */
51         var email = this.recipient.email();
52         var phone = this.recipient.day_phone();
53
54         this.sendHoldsRequest(formats, org, email, phone);
55 }
56
57 /* formats is a string of format characters, org is the 
58         org unit used for delivery */
59 HoldsWindow.prototype.sendHoldsRequest = function(formats, org, email, phone) {
60
61         if(isXUL()) { /* staff member entered barcode, now fetch the user */
62                 var recip_barcode = getById("recipient_barcode").value;
63                 if(!recip_barcode || recip_barcode == "") {
64                         alert("Please enter the user's barcode");
65                 }
66                 try {
67                         var r = grabUserByBarcode(this.session, recip_barcode);
68                         debug("Done grabbing user");
69                         alert(js2JSON(r));
70                         this.recipient = r;
71                 } catch(E) { if(E.err_msg) alert(E.err_msg()); else alert(E); }
72         }
73
74         var hold = new ahr();
75         hold.pickup_lib(org.id());
76         hold.requestor(this.requestor.id());
77         hold.usr(this.recipient.id());
78         hold.hold_type(this.type);
79         hold.email_notify(email);
80         hold.phone_notify(phone);
81
82         if(this.type == "M") hold.holdable_formats(formats);
83         hold.target(this.record);
84
85         var req = new RemoteRequest(
86                 "open-ils.circ",
87                 "open-ils.circ.holds.create",
88                 this.session, hold );
89
90         req.send(true);
91         var res = req.getResultObject();
92         if(res == 1) 
93                 alert("Hold request was succesfully submitted");
94
95 }
96
97 /* returns false if we don't have a recipient for the hold */
98 HoldsWindow.prototype.buildWindow = function(node) {
99
100         if(this.built) return true;
101         var d = elem("div");
102         var id = this.record;
103
104         var usr = this.recipient;
105         var barcodediv = null;
106
107         if(isXUL()) {
108                 /* used by xul to enter the recipient barcode */
109                 
110                 var barcodediv = elem("div",{style:"margin-left: 10px"},null,"2. Enter User Barcode");
111                 var barcodebox = elem("input",{style:"margin-left: 10px;", type:"text",id:"recipient_barcode"});
112                 barcodediv.appendChild(barcodebox);
113                 barcodediv.appendChild(elem("br"));
114
115         } else {
116
117                 if(!usr) {
118                         var obj = this;
119                         var func = function(usr) { 
120                                 obj.recipient = usr.userObject;
121                                 obj.requestor = usr.userObject;
122                                 obj.session = usr.session_id;
123                                 obj.toggle();
124                         }
125                         var diag = new LoginDialog(func);
126                         diag.display(node);
127                         return false;
128                 }
129         }
130
131
132
133         var org = usr.home_ou();
134         d.appendChild(this.buildPickuplibSelector(org));
135
136         if(this.type == "M")
137                 d.appendChild(this.buildResourceSelector());
138
139         if(barcodediv) {
140                 d.appendChild(elem("br"));
141                 d.appendChild(barcodediv);
142         }
143
144         d.appendChild(this.buildSubmit());
145
146         this.div.appendChild(d);
147         this.built = true;
148         return true;
149 }
150
151 HoldsWindow.prototype.toggle = function(node) {
152
153         debug("Building window with node " + node);
154         if(!this.built) 
155                 if(!this.buildWindow(node)) return;
156
157         swapClass( this.div, "hide_me", "show_me" );
158
159
160         /* workaround for IE select box widget bleed through, blegh... */
161         if(IE) {
162
163                 var sels = getDocument().getElementsByTagName("select");
164                 if(sels.length == 0) return true;
165
166                 if(this.div.className.indexOf("hide_me") != -1)  {
167                         for(var i = 0; i!= sels.length; i++) {
168                                 var s = sels[i];
169                                 if(s && s.id != "holds_org_selector") {
170                                         remove_css_class(s, "invisible");
171                                         add_css_class(s, "visible");
172                                 }
173                         }
174                 }
175         
176                 if(this.div.className.indexOf("show_me") != -1)  {
177                         for(var i = 0; i!= sels.length; i++) {
178                                 var s = sels[i];
179                                 if(s && s.id != "holds_org_selector") {
180                                         remove_css_class(s, "visible");
181                                         add_css_class(s, "invisible");
182                                 }
183                         }
184                 }
185         }
186
187         return true;
188 }
189
190
191
192 /*
193 HoldsWindow.prototype.buildHoldsWindowCallback = function(type) {
194
195         var hwindow = this;
196         var func = function() {
197
198                 var wrapper = elem("div");
199                 var id = hwindow.record.doc_id();
200
201                 var org = UserSession.instance().userObject.home_ou();
202                 wrapper.appendChild(hwindow.buildPickuplibSelector(org));
203                 if(type == "M")
204                         wrapper.appendChild(hwindow.buildResourceSelector());
205                 wrapper.appendChild(hwindow.buildSubmit());
206
207                 hwindow.win = window.open(null,"PLACE_HOLD_" + id,
208                         "location=0,menubar=0,status=0,resizeable,resize," +
209                         "outerHeight=500,outerWidth=500,height=500," +
210                         "width=500,scrollbars=1," +
211                         "alwaysraised, chrome" )
212         
213                 hwindow.win.document.write("<html>" + wrapper.innerHTML + "</html>");
214                 hwindow.win.document.close();
215                 hwindow.win.document.title = "View MARC";
216                 hwindow.win.focus();
217         }
218
219         return func;
220 }
221 */
222
223 HoldsWindow.prototype.buildSubmit = function() {
224         var div = elem("div");
225
226
227         /*
228         var bdiv = elem("div",  
229                         {style: 
230                                 "border-top: 1px solid lightgrey;" +
231                                 "border-bottom: 1px solid lightgrey;" +
232                                 "width:100%;text-align:center;"});
233                                 */
234
235         var bdiv = elem("div");  
236         add_css_class(bdiv, "holds_window_buttons");
237
238         var button = elem("input", 
239                 {type:"submit", style:"margin-right: 10px;", value:"Place Hold"});
240
241         var cancel = elem("input", 
242                 {type:"submit", style:"margin-left: 10px;",value:"Cancel"});
243         var obj = this;
244
245         cancel.onclick = function() { obj.toggle(); }
246         button.onclick = function() { obj.toggle(); obj.process(); }
247
248         div.appendChild(elem("br"));
249         bdiv.appendChild(button);
250         bdiv.appendChild(cancel);
251         div.appendChild(bdiv);
252
253         return div;
254 }
255
256 /* builds a selecor where the client can select the location to which
257         the item is sent */
258 HoldsWindow.prototype.buildPickuplibSelector = function(selected_id) {
259
260         var div = elem("div");
261         var tdiv = elem("div",{style:"margin-left:10px"}, null,
262                 "1. Select the location where the item(s) shall be delivered");
263
264         var sdiv = elem("div");
265         var selector = elem("select",{id:"holds_org_selector"});
266
267         /* this is not copied over... XXX fix me */
268         selector.onchange = function() {
269                 var idx = selector.selectedIndex;
270                 var option = selector.options[idx];
271                 var org = findOrgUnit(option.value);
272
273                 var d = getById("selector_error_div");
274                 if(d) div.removeChild(d);
275
276                 if(parseInt(findOrgType(org.ou_type()).depth()) < 2) {
277                         var err = elem("div",
278                                 {id:"selector_error_div", style:"color:red"},null, 
279                                 org.name() + " is a library system, please select a single branch");
280                         div.appendChild(err);
281                 } else {
282                 }
283         }
284
285         var center = elem("center");
286         center.appendChild(selector);
287         sdiv.appendChild(center);
288         _buildOrgList(selector, selected_id, null);
289
290         div.appendChild(elem("br"));
291         div.appendChild(tdiv);
292         div.appendChild(elem("br"));
293         div.appendChild(sdiv);
294         return div;
295 }
296
297 /* utility function for building a org list selector object */
298 function _buildOrgList(selector, selected_id, org) {
299
300         if(selected_id == null) selected_id = -1;
301
302         if(org == null) {
303                 org = globalOrgTree;
304
305         } else { /* add the org to the list */
306                 var index = selector.options.length;
307                 if(IE) {
308                         var node = elem("pre");
309                         for(var x=2; x <= findOrgType(org.ou_type()).depth(); x++) {
310                                 node.appendChild(mktext("    "));
311                         }
312                         node.appendChild(mktext(org.name()));
313                         var select = new Option("", org.id());
314                         selector.options[index] = select;
315                         select.appendChild(node);
316         
317                 } else {
318                         var pad = (findOrgType(org.ou_type()).depth() - 1) * 12;
319                         var select = new Option(org.name(), org.id());
320                         select.setAttribute("style", "padding-left: " + pad);
321                         selector.options[index] = select;
322                 }
323
324                 if(parseInt(org.id()) == parseInt(selected_id)) {
325                         selector.selectedIndex = index;
326                         selector.options[index].selected = true;
327                 }
328         }
329
330         for(var idx in org.children()) 
331                 _buildOrgList(selector, selected_id, org.children()[idx]);
332 }
333
334 HoldsWindow.prototype.buildResourceSelector = function() {
335
336         /* useful message */
337         var big_div = elem('div', {style:"margin-left: 10px;"});
338
339         var desc_div = elem("div",null, null, 
340                 "2. Select all acceptible item formats");
341
342         
343         var table = elem("table");      
344
345         for( var idx in resourceFormats ) {
346                 var row = table.insertRow(table.rows.length)
347         
348                 var pic_cell = row.insertCell(0);
349                 var name_cell = row.insertCell(1);
350                 var box_cell = row.insertCell(2);
351                 var box = elem("input", 
352                         {type:"checkbox", id: resourceFormats[idx] + "_hold_checkbox"}, null);
353
354                 if(idx == 0) { /* select text by default */
355                         box.setAttribute("checked","checked");
356                         box.checked = true;
357                 }
358                 
359                 pic_cell.appendChild(mkResourceImage(resourceFormats[idx]));
360                 name_cell.appendChild(mktext(resourceFormats[idx]));
361                 box_cell.appendChild(mktext(" "));
362                 box_cell.appendChild(box);
363         }
364
365         big_div.appendChild(elem("br"));
366         big_div.appendChild(desc_div);
367         big_div.appendChild(elem("br"));
368         big_div.appendChild(table);
369         return big_div;
370
371 }
372
373
374
375