]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/circ/selfcheck/selfcheck.js
Started porting the self-check web interface over to the new hotness that is dojo...
[working/Evergreen.git] / Open-ILS / web / js / ui / default / circ / selfcheck / selfcheck.js
1 dojo.require('openils.CGI');
2 dojo.require('openils.Util');
3 dojo.require('openils.User');
4 dojo.require('openils.Event');
5
6 const SET_BARCODE_REGEX = 'opac.barcode_regex';
7 const SET_PATRON_TIMEOUT = 'circ.selfcheck.patron_login_timeout';
8 const SET_ALERT_ON_CHECKOUT_EVENT = 'circ.selfcheck.alert_on_checkout_event';
9 const SET_AUTO_OVERRIDE_EVENTS = 'circ.selfcheck.auto_override_checkout_events';
10 const SET_PATRON_PASSWORD_REQUIRED = 'circ.selfcheck.patron_password_required';
11
12 function SelfCheckManager() {
13
14     this.cgi = new openils.CGI();
15     this.staff = null; 
16     this.workstation = null;
17     this.authtoken = null;
18
19     this.patron = null; 
20     this.patronBarcodeRegex = null;
21
22     // current item barcode
23     this.itemBarcode = null; 
24
25     // are we currently performing a renewal?
26     this.isRenewal = false; 
27
28     // is a transaction pending?
29     this.pendingXact = false; 
30
31     // dict of org unit settings for "here"
32     this.orgSettings = {};
33 }
34
35 /**
36  * Fetch the org-unit settings, initialize the display, etc.
37  */
38 SelfCheckManager.prototype.init = function() {
39
40     this.staff = openils.User.user;
41     this.workstation = openils.User.workstation;
42     this.authtoken = openils.User.authtoken;
43     this.loadOrgSettings();
44
45     if(this.cgi.param('patron')) {
46         // Patron barcode via cgi param.  Mainly used for debugging.
47         this.loginPatron(this.cgi.param('patron'));
48     } else {
49         this.drawLoginPage();
50     }
51 }
52
53 /**
54  * Loads the org unit settings
55  */
56 SelfCheckManager.prototype.loadOrgSettings = function() {
57
58     var settings = fieldmapper.aou.fetchOrgSettingBatch(
59         this.staff.ws_ou(), [
60             SET_BARCODE_REGEX,
61             SET_PATRON_TIMEOUT,
62             SET_ALERT_ON_CHECKOUT_EVENT,
63             SET_AUTO_OVERRIDE_EVENTS,
64         ]
65     );
66
67     for(k in settings) {
68         if(settings[k])
69             this.orgSettings[k] = settings[k].value;
70     }
71
72     if(settings[SET_BARCODE_REGEX]) 
73         this.patronBarcodeRegex = new RegExp(settings[SET_BARCODE_REGEX].value);
74 }
75
76 SelfCheckManager.prototype.drawLoginPage = function() {
77     var self = this;
78
79     var bcHandler = function(barcode) {
80         // handle patron barcode entry
81
82         if(self.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
83             
84             // password is required.  wire up the scan box to read it
85             self.updateScanBox(
86                 'Please enter your password', // TODO i18n 
87                 false,
88                 function(pw) { self.loginPatron(barcode, ps); }
89             );
90
91             dojo.connect(selfckScanBox, 'onKeyDown', pwHandler);
92
93         } else {
94             // password is not required, go ahead and login
95             self.loginPatron(barcode);
96         }
97     };
98
99     this.updateScanBox(
100         'Please log in with your library barcode.', // TODO
101         false,
102         bcHandler
103     );
104 }
105
106 /**
107  * Login the patron.  
108  */
109 SelfCheckManager.prototype.loginPatron = function(barcode, passwd) {
110
111     if(this.orgSettings[SET_PATRON_PASSWORD_REQUIRED]) {
112
113         // patron password is required.  Verify it.
114
115         var res = fieldmapper.standardRequest(
116             ['open-ils.actor', 'open-ils.actor.verify_user_password'],
117             {params : [this.authtoken, barcode, null, hex_md5(passwd)]}
118         );
119
120         if(res == 0) {
121             return alert('login failed'); // TODO
122         }
123     } 
124
125     // retrieve the fleshed user by barcode
126     this.patron = fieldmapper.standardRequest(
127         ['open-ils.actor', 'open-ils.actor.user.fleshed.retrieve_by_barcode'],
128         {params : [this.authtoken, barcode]}
129     );
130
131     var evt = openils.Event.parse(this.patron);
132     if(evt) {
133
134         // User login failed, why?
135         
136         switch(evt.textcode) {
137
138             case 'ACTOR_USER_NOT_FOUND':
139                 return alert('user not found'); // TODO
140
141             case 'NO_SESSION':
142                 return alert('staff login timed out'); // TODO
143
144             default:
145                 return alert('unexpected patron login error occured: ' + evt.textcode); // TODO
146         }
147     }
148
149     // patron login succeeded
150     dojo.byId('oils-selfck-user-banner').innerHTML = 'Welcome, ' + this.patron.usrname(); // TODO i18n
151     this.drawCircPage();
152 }
153
154
155 /**
156  * Manages the main input box
157  * @param str The context message to display with the box
158  * @param clearOnly Don't update the context message, just clear the value and re-focus
159  * @param handler Optional "on-enter" handler.  
160  */
161 SelfCheckManager.prototype.updateScanBox = function(str, clearOnly, handler) {
162
163     if(!clearOnly)
164         dojo.byId('oils-selfck-scan-text').innerHTML = str;
165     selfckScanBox.attr('value', '');
166     selfckScanBox.focus();
167
168     if(handler) {
169         dojo.connect(selfckScanBox, 'onKeyDown', 
170             function(e) {
171                 if(e.keyCode != dojo.keys.ENTER) 
172                     return;
173                 handler(selfckScanBox.attr('value'));
174             }
175         );
176     }
177 }
178
179 /**
180  *  Sets up the checkout/renewal interface
181  */
182 SelfCheckManager.prototype.drawCircPage = function() {
183
184     var self = this;
185     this.updateScanBox(
186         'Please enter an item barcode', // TODO i18n
187         false,
188         function(barcode) { self.checkout(barcode); }
189     );
190
191     openils.Util.show('oils-selfck-circ-page');
192
193     this.circTbody = dojo.byId('oils-selfck-circ-tbody');
194     if(!this.circTemplate)
195         this.circTemplate = this.circTbody.removeChild(dojo.byId('oils-selfck-circ-row'));
196 }
197
198
199
200 /**
201  * Check out a single item.  If the item is already checked 
202  * out to the patron, redirect to renew()
203  */
204 SelfCheckManager.prototype.checkout = function(barcode, override) {
205
206     if(!barcode) {
207         this.updateScanbox(null, true);
208         return;
209     }
210
211     // TODO see if it's a patron barcode
212     // TODO see if this item has already been checked out in this session
213
214     var method = 'open-ils.circ.checkout.full';
215     if(override) method += '.override';
216
217     var result = fieldmapper.standardRequest(
218         ['open-ils.circ', 'open-ils.circ.checkout.full'],
219         {params: [
220             this.authtoken, {
221                 patron_id : this.patron.id(),
222                 copy_barcode : barcode
223             }
224         ]}
225     );
226
227
228     if(dojo.isArray(result)) {
229         // list of results.  See if we can override all of them.
230
231     } else {
232         var evt = openils.Event.parse(result);
233
234         switch(evt.textcode) {
235             // standard result events
236             
237             case 'SUCCESS':
238                 this.displayCheckout(evt);
239                 break;
240
241             case 'OPEN_CIRCULATION_EXISTS':
242                 // TODO renewal
243                 break;
244
245             case 'NO_SESSION':
246                 // TODO logout staff
247                 break;
248         }
249     }
250
251     console.log("Circ resulted in " + js2JSON(result));
252 }
253
254 /**
255  * Renew an item
256  */
257 SelfCheckManager.prototype.renew = function() {
258 }
259
260 /**
261  * Display the result of a checkout or renewal in the items out table
262  */
263 SelfCheckManager.prototype.displayCheckout = function(evt) {
264     var copy = evt.payload.copy;
265     var record = evt.payload.record;
266     var circ = evt.payload.circ;
267     var row = this.circTemplate.cloneNode(true);
268
269     /*
270     if(record.isbn()) {
271             var pic = $n(template, 'jacket');
272             pic.setAttribute('src', '/opac/ac/jacket/small/' + cleanISBN(record.isbn()));
273     }
274     */
275
276     this.byName('barcode', row).innerHTML = copy.barcode();
277     this.byName('title', row).innerHTML = record.title();
278     this.byName('author', row).innerHTML = record.author();
279     this.circTbody.appendChild(row);
280 }
281
282
283 SelfCheckManager.prototype.byName = function(node, name) {
284     return dojo.query('[name=' + name+']', node)[0];
285 }
286
287 /**
288  * Print a receipt
289  */
290 SelfCheckManager.prototype.printReceipt = function() {
291 }
292
293 /**
294  * Build the patron holds table
295  */
296 SelfCheckManager.prototype.displayHolds = function() {
297 }
298
299
300 /**
301  * Logout the patron and return to the login page
302  */
303 SelfCheckManager.prototype.logoutPatron = function() {
304 }
305
306
307 /**
308  * Fire up the manager on page load
309  */
310 openils.Util.addOnLoad(
311     function() {
312         new SelfCheckManager().init();
313     }
314 );