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