]> git.evergreen-ils.org Git - Evergreen.git/blob - Evergreen/xul/staff_client/server/patron/ue.js
disabling mark-lost and password reset buttons if you can't edit the
[Evergreen.git] / Evergreen / xul / staff_client / server / patron / ue.js
1 var cgi                                                 = null;
2 var clone                                               = false;
3 var patron                                              = null;
4 var counter                                             = 0;
5 var identTypesCache                     = {};
6 var statCatsCache                               = {};
7 var surveysCache                                = {};
8 var surveyQuestionsCache        = {};
9 var surveyAnswersCache          = {};
10 var userCache                                   = {};
11 var groupsCache                         = {};
12 var netLevelsCache                      = {};
13 //var guardianNote                              = null; 
14
15
16 /* fetch the necessary data to start off */
17 function uEditInit() {
18
19         _debug('uEditInit(): ' + location.search);
20
21         cgi             = new CGI();
22         session = cgi.param('ses');
23         clone           = cgi.param('clone');
24         if(!session) throw "User session is not defined";
25
26         fetchUser(session);
27         $('uedit_user').appendChild(text(USER.usrname()));
28
29         setTimeout( function() { 
30                 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
31 }
32
33 /* ------------------------------------------------------------------------------ */
34 /* Fetch code
35 /* ------------------------------------------------------------------------------ */
36 function uEditFetchIdentTypes() {
37         _debug("uEditFetchIdentTypes()");
38         var req = new Request(FETCH_ID_TYPES);
39         req.send(true);
40         return req.result();
41 }
42
43 function uEditFetchStatCats() {
44         _debug("uEditFetchStatCats()");
45         var req = new Request(SC_FETCH_ALL, SESSION);
46         req.send(true);
47         return req.result();
48 }
49
50 function uEditFetchSurveys() {
51         _debug("uEditFetchSurveys()");
52         var req = new Request(SV_FETCH_ALL, SESSION);
53         req.send(true);
54         return req.result();
55 }
56
57 function uEditFetchGroups() {
58         _debug("uEditFetchGroups()");
59         var req = new Request(FETCH_GROUPS);
60         req.send(true);
61         return req.result();
62 }
63
64 function uEditFetchNetLevels() {
65         _debug("uEditFetchNetLevels()");
66         var req = new Request(FETCH_NET_LEVELS, SESSION);
67         req.send(true);
68         return req.result();
69 }
70
71 /* ------------------------------------------------------------------------------ */
72
73
74
75 /* fetches necessary and builds the UI */
76 function uEditBuild() {
77         fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
78
79         uEditBuildLibSelector();
80         patron = fetchFleshedUser(cgi.param('usr'));
81         if(!patron) patron = uEditNewPatron(); 
82         
83         uEditDraw( 
84                 uEditFetchIdentTypes(),
85                 uEditFetchGroups(),
86                 uEditFetchStatCats(),
87                 uEditFetchSurveys(),
88                 uEditFetchNetLevels()
89                 );
90
91         if(patron.isnew()) {
92                 if(clone) uEditClone(clone);
93                 else uEditCreateNewAddr();
94
95         } else {
96
97                 /* do we need to display the parent / gurdian field? */
98                 uEditCheckDOB(uEditFindFieldByKey('dob'));
99
100                 $('ue_barcode').disabled = true;
101                 unHideMe($('ue_mark_card_lost'));
102                 unHideMe($('ue_reset_pw'));
103                 uEditCheckEditPerm();
104         }
105
106         if(PERMS['BAR_PATRON'] == -1) 
107                 $('ue_barred').disabled = true;
108 }
109
110
111 /* if this user does not have permission to put users into
112         the edited users group, they do not have permission to 
113         edit this user */
114 function uEditCheckEditPerm() {
115
116         var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);   
117         /*
118         _debug("editing user with group app perm "+patron.profile()+' : '+
119                 groupsCache[patron.profile()].name() +', and perm = ' + perm);
120                 */
121
122         if(PERMS[perm] != -1) return;
123
124         /* we can edit our own account, but not others in our group */
125         if( patron.id() != USER.id() ){
126                 _debug("we are not allowed to edit this user");
127         
128                 $('ue_save').disabled = true;
129                 $('ue_save_clone').disabled = true;
130                 $('ue_mark_card_lost').disabled = true;
131                 $('ue_reset_pw').disabled = true;
132         
133                 uEditIterateFields(
134                         function(f) {
135                                 if( f && f.widget && f.widget.node )
136                                         f.widget.node.disabled = true;
137                         }       
138                 );      
139
140         }
141
142         var node = $('ue_profile').parentNode;
143         node.removeChild($('ue_profile'));
144         node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
145
146         var field = uEditFindFieldByKey('profile');
147         field.required = false;
148         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
149         uEditCheckErrors();
150 }
151
152
153 /* creates a new patron object with card attached */
154 var uEditCardVirtId = -1;
155 function uEditNewPatron() {
156         var patron = new au(); 
157         patron.isnew(1);
158         patron.id(-1);
159         card = new ac();
160         card.id(uEditCardVirtId--);
161         card.isnew(1);
162         patron.card(card);
163         patron.cards([card]);
164         patron.stat_cat_entries([]);
165         patron.survey_responses([]);
166         patron.addresses([]);
167         patron.home_ou(USER.ws_ou());
168         uEditMakeRandomPw(patron);
169         return patron;
170 }
171
172 function uEditMakeRandomPw(patron) {
173         var rand  = Math.random();
174         rand = parseInt(rand * 10000) + '';
175         while(rand.length < 4) rand += '0';
176         appendClear($('ue_password_plain'),text(rand));
177         unHideMe($('ue_password_gen'));
178         patron.passwd(rand);
179         return rand;
180 }
181
182 function uEditResetPw() { 
183         var pw = uEditMakeRandomPw(patron);     
184         $('ue_password1').value = pw;
185         $('ue_password2').value = pw;
186 }
187
188 function uEditClone(clone) {
189
190         var cloneUser = fetchFleshedUser(clone);
191         patron.usrgroup(cloneUser.usrgroup());
192
193         if( cloneUser.day_phone() )
194                 $('ue_day_phone').value = cloneUser.day_phone();
195         if( cloneUser.evening_phone() )
196                 $('ue_night_phone').value = cloneUser.evening_phone();
197         if( cloneUser.other_phone() )
198                 $('ue_other_phone').value = cloneUser.other_phone();
199         setSelector($('ue_org_selector'), cloneUser.home_ou());
200
201
202         setSelector($('ue_profile'), cloneUser.profile());
203
204         /* force the expire date to be set */
205         $('ue_profile').onchange();
206
207         for( var a in cloneUser.addresses() ) {
208                 var addr = cloneUser.addresses()[a];
209                 if( cloneUser.mailing_address && 
210                                 addr.id() == cloneUser.mailing_address().id() )
211                         patron.mailing_address(addr);
212                 if( cloneUser.billing_address() &&
213                                 addr.id() == cloneUser.billing_address().id() )
214                         patron.billing_address(addr);
215                 patron.addresses().push(addr);
216         }
217
218         uEditBuildAddrs(patron);
219 }
220
221
222 /* Creates a new blank address, 
223         adds it to the user and the fields array */
224 var uEditVirtualAddrId = -1;
225 function uEditCreateNewAddr() {
226         var addr = new aua();
227
228         addr.id(uEditVirtualAddrId--);
229         addr.isnew(1);
230         addr.usr(patron.id());
231         addr.country(defaultCountry);
232
233         if(!patron.addresses()) 
234                 patron.addresses([]);
235
236         if(patron.addresses().length == 0) {
237                 patron.mailing_address(addr);
238                 patron.billing_address(addr);
239         }
240
241         addr.valid(1);
242         addr.within_city_limits(1);
243
244         uEditBuildAddrFields(patron, addr);
245         patron.addresses().push(addr);
246         uEditIterateFields(function(f) { uEditCheckValid(f); });
247         uEditCheckErrors();
248 }
249
250
251 /* kicks off the UI drawing */
252 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
253         hideMe($('uedit_loading'));
254         unHideMe($('ue_maintd'));
255
256         dataFields = [];
257         uEditDrawIDTypes(identTypes);
258         uEditDrawGroups(groups, null, null, true);
259         uEditDrawStatCats(statCats);
260         uEditDrawSurveys(surveys);
261         uEditDrawNetLevels(netLevels);
262         uEditDefineData(patron);
263
264         uEditIterateFields(function(f) { uEditActivateField(f) });
265         uEditIterateFields(function(f) { uEditCheckValid(f); });
266         uEditCheckErrors();
267 }
268
269
270 /** Applies the event handlers and sets the data for the field */
271 function uEditActivateField(field) {
272
273         if( field.widget.id ) {
274                 field.widget.node = $(field.widget.id);
275
276         } else {
277                 field.widget.node = 
278                         $n(field.widget.base, field.widget.name);
279         }
280
281         uEditSetOnchange(field);
282
283         if(field.widget.onblur) {
284                 field.widget.node.onblur = 
285                         function() { field.widget.onblur(field); };
286         }
287
288         field.widget.node.disabled = field.widget.disabled;
289         if(field.object == null) return;
290         var val = field.object[field.key]();
291         if(val == null) return;
292
293         if( field.widget.type == 'input' )
294                 field.widget.node.value = val;
295
296         if( field.widget.type == 'select' )
297                 setSelector(field.widget.node, val);
298
299         if( field.widget.type == 'checkbox' )
300                 field.widget.node.checked = 
301                         (val && val != 'f') ? true : false;
302
303         if( field.widget.onload ) 
304                 field.widget.onload(val);
305 }
306
307
308 /* set up the onchange event for the field */
309 function uEditSetOnchange(field) {
310         var func = function() {uEditOnChange( field );}
311         field.widget.node.onchange = func;
312
313         if(field.widget.type != 'select')
314                 field.widget.node.onkeyup = func;
315 }
316
317 /* find the current value of the field object's widget */
318 function uEditNodeVal(field) {
319         if(field.widget.type == 'input')
320                 return field.widget.node.value;
321
322         if(field.widget.type == 'checkbox')
323                 return field.widget.node.checked;
324
325         if(field.widget.type == 'select')
326                 return getSelectorVal(field.widget.node);
327 }
328
329
330 /* update a field value */
331 function uEditOnChange(field) {
332
333         var newval = uEditNodeVal(field);
334         field.object[field.key](newval);
335         field.object.ischanged(1);
336
337         if(field.widget.onpostchange)
338                 field.widget.onpostchange(field, newval);
339
340         //_debug(field.key+' = '+newval);
341
342         uEditIterateFields(function(f) { uEditCheckValid(f); });
343         uEditCheckErrors();
344 }
345
346
347 function uEditCheckValid(field) {
348         var newval = uEditNodeVal(field);
349
350         if(newval) {
351
352                 if(field.widget.regex) { 
353                         if(newval.match(field.widget.regex)) 
354                                 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
355                         else
356                                 addCSSClass(field.widget.node, CSS_INVALID_DATA);
357
358                 } else {
359                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
360                 }
361
362         } else {
363
364                 if(field.required) {
365                         addCSSClass(field.widget.node, CSS_INVALID_DATA);
366
367                 } else {
368                         removeCSSClass(field.widget.node, CSS_INVALID_DATA);
369                 }
370         }
371
372 }
373
374 /* find a field object by object key */
375 function uEditFindFieldByKey(key) {
376         var fields = grep( dataFields,
377                 function(item) { return (item.key == key); });
378         return (fields) ? fields[0] : null;
379 }
380
381 /* find a list of fields by object key */
382 function uEditFindFieldsByKey(key) {
383         return grep( dataFields,
384                 function(item) { return (item.key == key); });
385 }
386
387 /* find a field object by widget id */
388 function uEditFindFieldByWId(id) {
389         var fields = grep( dataFields,
390                 function(item) { return (item.widget.id == id); });
391         return (fields) ? fields[0] : null;
392 }
393
394
395 function uEditIterateFields(callback) {
396         for( var f in dataFields ) 
397                 callback(dataFields[f]);
398 }
399
400
401 function uEditGetErrorStrings() {
402         var errors = [];
403         uEditIterateFields(
404                 function(field) { 
405                         if(field.errkey) {
406                                 if( !field.object.isdeleted() ) {
407                                         if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
408                                                 var str = $(field.errkey).innerHTML;
409                                                 if(str) errors.push(str);
410                                         }
411                                 }
412                         }
413                 }
414         );
415
416         /* munge up something for all of the required surveys 
417                 (which are not registered with the fields) */
418         if( patron.isnew() ) {
419                 var sel = $('ue_survey_table');
420
421                 if( sel ) {
422                         var rows = sel.getElementsByTagName('tr');
423
424                         for( var r in rows ) {
425                 
426                                 var row = rows[r];
427                                 var sel = $n(row, 'ue_survey_answer');
428                                 if(!sel) continue;
429                                 var qstn = row.getAttribute('question');
430                 
431                                 if(qstn) {
432                                         qstn            = surveyQuestionsCache[qstn];
433                                         survey  = surveysCache[qstn.survey()];
434                                         var val = getSelectorVal(sel);
435                                         if(!val && isTrue(survey.required()))
436                                                 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
437                                 }
438                         }
439                 }
440         }
441
442         /* ------------------------------------------------------------ */
443
444         if(errors[0]) return errors;
445         return null;
446 }
447
448 function uEditAlertErrors() {
449         var errors = uEditGetErrorStrings();
450         if(!errors) return false;
451         alert(errors.join("\n"));
452         return true;
453 }
454
455
456 /* send the user to the database */
457 function uEditSaveUser(cloneme) {
458
459         if(uEditGetErrorStrings()) {
460                 uEditAlertErrors();
461                 return;
462         }
463
464         /* null is unique in the db, but '' is not */
465         if( ! patron.ident_value() ) patron.ident_value(null);
466         //if( ! patron.ident_type2() ) patron.ident_type2(null);
467         if( ! patron.ident_value2() ) patron.ident_value2(null);
468         patron.ident_type2(null);
469
470         if(! patron.dob() ) patron.dob(null);
471
472         _debug("Saving patron with card: " + js2JSON(patron.card()));
473         _debug("Saving full patron: " + js2JSON(patron));
474
475         //for( var c in patron
476
477         var req = new Request(UPDATE_PATRON, SESSION, patron);
478         req.alertEvent = false;
479         req.send(true);
480         var newuser = req.result();
481
482         var evt;
483         if( (evt = checkILSEvent(newuser)) || ! newuser ) {
484                 if(evt) {
485                         var j = js2JSON(newuser); 
486                         alert(j);
487                         _debug("USER UPDATE FAILED:\n" + j);
488                 }
489                 return;
490         } 
491
492         alert($('ue_success').innerHTML);
493
494         if(cloneme) {
495                 /* if the user we just created was a clone, and we want to clone it,
496                 we really want to clone the original */
497                 if( clone ) cloneme = clone;
498                 else cloneme = newuser.id();
499         }
500
501
502         if( cloneme ) {
503
504                 if(window.xulG &&
505                         typeof window.xulG.spawn_editor == 'function' && 
506
507                         !patron.isnew() ) {
508                                 _debug("xulG clone spawning new interface...");
509                                 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
510                                 uEditRefresh();
511
512                 } else {
513
514                         var href = location.href;
515                         href = href.replace(/\&?usr=\d+/, '');
516                         href = href.replace(/\&?clone=\d+/, '');
517                         href += '&clone=' + cloneme;
518                         location.href = href;
519                 }
520
521         } else {
522
523                 uEditRefresh();
524         }
525
526         uEditRefreshXUL(newuser);
527 }
528
529
530 function uEditRefreshXUL(newuser) {
531         if (window.xulG && typeof window.xulG.on_save == 'function') 
532                 window.xulG.on_save(newuser);
533 }
534
535 function uEditRefresh() {
536         var href = location.href;
537         href = href.replace(/\&?clone=\d+/, '');
538         location.href = href;
539 }
540
541
542 function uEditCancel() {
543         var href = location.href;
544         href = href.replace(/\&?usr=\d+/, '');
545         href = href.replace(/\&?clone=\d+/, '');
546         var id = cgi.param('usr')
547         /* reload the current user if available */
548         if( id ) href += "&usr=" + id;
549         location.href = href;
550 }
551
552
553 var uEditDupHashes = {};
554 var uEditDupTemplate;
555
556 function uEditRunDupeSearch(type, search_hash) {
557
558         if(!patron.isnew()) return;
559
560         _debug('dup search: ' + js2JSON(search_hash));
561
562         var req = new Request(PATRON_SEARCH, SESSION, search_hash);
563
564         var container = $('dup_div_container');
565         if(!uEditDupTemplate)
566                 uEditDupTemplate = container.removeChild($('dup_div'));
567
568         /* clear any existing dups for this type */
569         iterate( container.getElementsByTagName('div'),
570                 function(d) {
571                         if( d.getAttribute('type') == type ) {
572                                 container.removeChild(d)
573                                 return;
574                         }
575                 }
576         );
577
578         req.callback(
579                 function(r) {
580                         uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
581                 }
582         );
583         req.send();
584 }
585
586
587 function uEditHandleDupResults(ids, search_hash, type, container) {
588
589         _debug('dup search results: ' + js2JSON(ids));
590
591         if(!(ids && ids[0]))  /* no results */
592                 return uEditDupHashes[type] = null;
593
594         /* add a dup link to the UI and plug in the data */
595         var node = uEditDupTemplate.cloneNode(true);
596         container.appendChild(node);
597         node.setAttribute('type', type);
598
599         var link = $n(node, 'link');
600         link.setAttribute('type', type);
601         unHideMe(link);
602         $n(node,'count').appendChild(text(ids.length));
603
604         for( var o in search_hash ) 
605                 $n(node, 'data').appendChild(
606                         text(search_hash[o].value + ' '));
607
608         uEditDupHashes[type] = search_hash;
609
610         switch(type) {
611                 case 'ident' :
612                         if(confirm($('ue_dup_ident1').innerHTML)) 
613                                 uEditShowSearch(null, type);
614                         break;
615         }
616 }
617
618
619 function uEditShowSearch(link,type) {
620         if(!type) type = link.getAttribute('type');
621         if(window.xulG)
622                 window.xulG.spawn_search(uEditDupHashes[type]); 
623         else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
624 }
625
626 function uEditMarkCardLost() {
627
628         for( var c in patron.cards() ) {
629
630                 var card = patron.cards()[c];
631                 if( patron.card().id() == card.id() ) {
632
633                         /* de-activite the current card */
634                         card.ischanged(1);
635                         card.active(0);
636
637                         if( !card.barcode() ) {
638                                 /* a card exists in the array with no barcode */
639                                 ueRemoveCard(card.id());
640
641                         } else if( card.isnew() && card.active() == 0 ) {
642                                 /* a new card was created, then never used, removing.. */
643                                 _debug("removing new inactive card "+card.barcode());
644                                 ueRemoveCard(card.id());
645                         }
646
647                         /* create a new card for the patron */
648                         var newcard = new ac();
649                         newcard.id(uEditCardVirtId--);
650                         newcard.isnew(1);
651                         patron.card(newcard);
652                         patron.cards().push(newcard);
653
654
655                         /* reset the widget */
656                         var field = uEditFindFieldByWId('ue_barcode');
657                         field.widget.node.disabled = false;
658                         field.widget.node.value = "";
659                         field.widget.node.onchange();
660                         field.object = newcard;
661                         _debug("uEditMarkCardLost(): created new card object for user");
662                 }
663         }
664 }
665
666
667 function ueRemoveCard(id) {
668         _debug("removing card from cards() array: " + id);
669         var cds = grep( patron.cards(), function(c){return (c.id() != id)});
670         if(!cds) cds = [];
671         for( var j = 0; j < cds.length; j++ )
672                 _debug("patron card array now has :  "+cds[j].id());
673         patron.cards(cds);
674 }
675
676
677
678 function compactArray(arr) {
679         var a = [];
680         for( var i = 0; arr && i < arr.length; i++ ) {
681                 if( arr[i] != null )
682                         a.push(arr[i]);
683         }
684         return a;
685 }