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