5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
14 //var guardianNote = null;
15 var uEditUsePhonePw = false;
17 if(!window.xulG) var xulG = null;
19 function $(id) { return document.getElementById(id); }
21 /* fetch the necessary data to start off */
22 function uEditInit() {
24 _debug('uEditInit(): ' + location.search);
27 session = cgi.param('ses');
28 if (xulG) if (xulG.ses) session = xulG.ses;
29 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
30 clone = cgi.param('clone');
31 if (xulG) if (xulG.clone) clone = xulG.clone;
32 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
33 if(!session) throw $("patronStrings").getString('web.staff.patron.ue.session_no_defined');
36 $('uedit_user').appendChild(text(USER.usrname()));
38 setTimeout( function() {
39 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
42 function uEditSetUnload() {
43 _debug('setting window unload event');
45 window.onbeforeunload = function(evt) {
46 return $('ue_unsaved_changes').innerHTML;
51 function uEditClearUnload() {
52 _debug('clearing window unload event');
54 window.onbeforeunload = null;
58 /* ------------------------------------------------------------------------------ */
60 /* ------------------------------------------------------------------------------ */
61 function uEditFetchIdentTypes() {
62 _debug("uEditFetchIdentTypes()");
63 var s = fetchXULStash();
64 if (typeof s.list != 'undefined')
65 if (typeof s.list.cit != 'undefined') return s.list.cit;
66 var req = new Request(FETCH_ID_TYPES);
71 function uEditFetchStatCats() {
72 _debug("uEditFetchStatCats()");
73 var s = fetchXULStash();
74 if (typeof s.list != 'undefined')
75 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
76 var req = new Request(SC_FETCH_ALL, SESSION);
81 function uEditFetchSurveys() {
82 _debug("uEditFetchSurveys()");
83 var s = fetchXULStash();
84 if (typeof s.list != 'undefined')
85 if (typeof s.list.asv != 'undefined') return s.list.asv;
86 var req = new Request(SV_FETCH_ALL, SESSION);
91 function uEditFetchGroups() {
92 _debug("uEditFetchGroups()");
93 var s = fetchXULStash();
94 if (typeof s.tree != 'undefined')
95 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
96 var req = new Request(FETCH_GROUPS);
101 function uEditFetchNetLevels() {
102 _debug("uEditFetchNetLevels()");
103 var s = fetchXULStash();
104 if (typeof s.list != 'undefined')
105 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
106 var req = new Request(FETCH_NET_LEVELS, SESSION);
111 /* ------------------------------------------------------------------------------ */
115 * adds all of the group.application_perm's to the list
116 * provided by descending through the group tree
118 function buildAppPermList(list, group) {
120 if(group.application_perm() )
121 list.push(group.application_perm());
122 for(i in group.children()) {
123 buildAppPermList(list, group.children()[i]);
127 /* fetches necessary objects and builds the UI */
128 function uEditBuild() {
130 myPerms = ['BAR_PATRON', 'UNBAR_PATRON'];
132 /* grab the groups before we check perms so we know what
133 application_perms to check */
134 var groups = uEditFetchGroups();
135 buildAppPermList(myPerms, groups);
137 // de-dupe the permission list
139 for(var p in myPerms)
140 if(perms.indexOf(myPerms[p]) == -1)
141 perms.push(myPerms[p]);
144 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
146 uEditBuildLibSelector();
147 var usr = cgi.param('usr');
148 if (xulG) if (xulG.usr) usr = xulG.usr;
149 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
151 orgSettings = fetchBatchOrgSetting(USER.ws_ou(), [
152 'global.juvenile_age_threshold',
153 'patron.password.use_phone'
156 uEditUsePhonePw = (orgSettings['patron.password.use_phone'] &&
157 orgSettings['patron.password.use_phone'].value);
159 patron = fetchFleshedUser(usr);
160 if(!patron) patron = uEditNewPatron();
163 uEditFetchIdentTypes(),
165 uEditFetchStatCats(),
167 uEditFetchNetLevels()
171 if(clone) uEditClone(clone);
172 else uEditCreateNewAddr();
176 /* do we need to display the parent / gurdian field? */
177 uEditCheckDOB(uEditFindFieldByKey('dob'));
179 $('ue_barcode').disabled = true;
180 unHideMe($('ue_mark_card_lost'));
181 unHideMe($('ue_reset_pw'));
182 uEditCheckEditPerm();
185 uEditCheckBarredPerm();
188 function uEditCheckBarredPerm() {
189 if(PERMS['BAR_PATRON'] != -1)
192 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
195 $('ue_barred').disabled = true;
199 /* if this user does not have permission to put users into
200 the edited users group, they do not have permission to
202 function uEditCheckEditPerm() {
204 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
206 _debug("editing user with group app perm "+patron.profile()+' : '+
207 groupsCache[patron.profile()].name() +', and perm = ' + perm);
210 if(PERMS[perm] != -1) return;
212 /* we can edit our own account, but not others in our group */
213 if( patron.id() != USER.id() ){
214 _debug("we are not allowed to edit this user");
216 $('ue_save').disabled = true;
217 $('ue_save_clone').disabled = true;
218 $('ue_mark_card_lost').disabled = true;
219 $('ue_reset_pw').disabled = true;
223 if( f && f.widget && f.widget.node )
224 f.widget.node.disabled = true;
230 var node = $('ue_profile').parentNode;
231 node.removeChild($('ue_profile'));
232 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
234 var field = uEditFindFieldByKey('profile');
235 field.required = false;
236 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
241 /* creates a new patron object with card attached */
242 var uEditCardVirtId = -1;
243 function uEditNewPatron() {
244 var patron = new au();
248 card.id(uEditCardVirtId--);
251 patron.cards([card]);
252 patron.net_access_level(defaultNetLevel);
253 patron.stat_cat_entries([]);
254 patron.survey_responses([]);
255 patron.addresses([]);
256 patron.home_ou(USER.ws_ou());
257 uEditMakeRandomPw(patron);
261 function uEditMakeRandomPw(patron) {
262 if(uEditUsePhonePw) return;
263 var rand = Math.random();
264 rand = parseInt(rand * 10000) + '';
265 while(rand.length < 4) rand += '0';
266 appendClear($('ue_password_plain'),text(rand));
267 unHideMe($('ue_password_gen'));
272 function uEditMakePhonePw() {
273 if(patron.passwd()) return;
274 if( (pw = patron.day_phone()) ||
275 (pw = patron.evening_phone()) || (pw = patron.other_phone()) ) {
276 pw = pw.substring(pw.length - 4); // this is iffy
278 appendClear($('ue_password_plain'), text(pw));
279 unHideMe($('ue_password_gen'));
284 function uEditResetPw(pw) {
285 if(!pw) pw = uEditMakeRandomPw(patron);
286 $('ue_password1').value = pw;
287 $('ue_password2').value = pw;
288 $('ue_password1').onchange();
291 function uEditClone(clone) {
293 var cloneUser = fetchFleshedUser(clone);
294 patron.usrgroup(cloneUser.usrgroup());
296 if( cloneUser.day_phone() ) {
297 $('ue_day_phone').value = cloneUser.day_phone();
298 $('ue_day_phone').onchange();
301 if( cloneUser.evening_phone() ) {
302 $('ue_night_phone').value = cloneUser.evening_phone();
303 $('ue_night_phone').onchange();
306 if( cloneUser.other_phone() ) {
307 $('ue_other_phone').value = cloneUser.other_phone();
308 $('ue_other_phone').onchange();
311 setSelector($('ue_org_selector'), cloneUser.home_ou());
312 setSelector($('ue_profile'), cloneUser.profile());
314 /* force the expire date to be set */
315 $('ue_profile').onchange();
316 $('ue_org_selector').onchange();
318 for( var a in cloneUser.addresses() ) {
319 var addr = cloneUser.addresses()[a];
320 if( cloneUser.mailing_address &&
321 addr.id() == cloneUser.mailing_address().id() )
322 patron.mailing_address(addr);
323 if( cloneUser.billing_address() &&
324 addr.id() == cloneUser.billing_address().id() )
325 patron.billing_address(addr);
326 patron.addresses().push(addr);
329 uEditBuildAddrs(patron);
333 /* Creates a new blank address,
334 adds it to the user and the fields array */
335 var uEditVirtualAddrId = -1;
336 function uEditCreateNewAddr() {
337 var addr = new aua();
339 addr.id(uEditVirtualAddrId--);
341 addr.usr(patron.id());
342 addr.country(defaultCountry);
344 if(!patron.addresses())
345 patron.addresses([]);
347 if(patron.addresses().length == 0) {
348 patron.mailing_address(addr);
349 patron.billing_address(addr);
353 addr.within_city_limits(1);
355 uEditBuildAddrFields(patron, addr);
356 patron.addresses().push(addr);
357 uEditIterateFields(function(f) { uEditCheckValid(f); });
362 /* kicks off the UI drawing */
363 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
364 hideMe($('uedit_loading'));
365 unHideMe($('ue_maintd'));
368 uEditDrawIDTypes(identTypes);
369 uEditDrawGroups(groups, null, null, true);
370 uEditDrawStatCats(statCats);
371 uEditDrawSurveys(surveys);
372 uEditDrawNetLevels(netLevels);
373 uEditDefineData(patron);
375 uEditIterateFields(function(f) { uEditActivateField(f) });
376 uEditIterateFields(function(f) { uEditCheckValid(f); });
381 /** Applies the event handlers and sets the data for the field */
382 function uEditActivateField(field) {
384 if( field.widget.id ) {
385 field.widget.node = $(field.widget.id);
389 $n(field.widget.base, field.widget.name);
392 uEditSetOnchange(field);
394 if(field.widget.onblur) {
395 field.widget.node.onblur =
396 function() { field.widget.onblur(field); };
399 field.widget.node.disabled = field.widget.disabled;
400 if(field.object == null) return;
401 var val = field.object[field.key]();
402 if(val == null) return;
404 if( field.widget.type == 'input' )
405 field.widget.node.value = val;
407 if( field.widget.type == 'select' )
408 setSelector(field.widget.node, val);
410 if( field.widget.type == 'checkbox' )
411 field.widget.node.checked =
412 (val && val != 'f') ? true : false;
414 if( field.widget.onload )
415 field.widget.onload(val);
419 /* set up the onchange event for the field */
420 function uEditSetOnchange(field) {
421 var func = function() {uEditOnChange( field );}
422 field.widget.node.onchange = func;
424 if(field.widget.type != 'select')
425 field.widget.node.onkeyup = func;
428 /* find the current value of the field object's widget */
429 function uEditNodeVal(field) {
430 if(field.widget.type == 'input')
431 return field.widget.node.value;
433 if(field.widget.type == 'checkbox')
434 return field.widget.node.checked;
436 if(field.widget.type == 'select')
437 return getSelectorVal(field.widget.node);
441 /* update a field value */
442 function uEditOnChange(field) {
444 var newval = uEditNodeVal(field);
445 field.object[field.key](newval);
446 field.object.ischanged(1);
448 if(field.widget.onpostchange)
449 field.widget.onpostchange(field, newval);
451 //_debug(field.key+' = '+newval);
453 uEditIterateFields(function(f) { uEditCheckValid(f); });
460 function uEditCheckValid(field) {
461 var newval = uEditNodeVal(field);
465 if(field.widget.regex) {
466 if(newval.match(field.widget.regex))
467 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
469 addCSSClass(field.widget.node, CSS_INVALID_DATA);
472 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
478 addCSSClass(field.widget.node, CSS_INVALID_DATA);
481 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
487 /* find a field object by object key */
488 function uEditFindFieldByKey(key) {
489 var fields = grep( dataFields,
490 function(item) { return (item.key == key); });
491 return (fields) ? fields[0] : null;
494 /* find a list of fields by object key */
495 function uEditFindFieldsByKey(key) {
496 return grep( dataFields,
497 function(item) { return (item.key == key); });
500 /* find a field object by widget id */
501 function uEditFindFieldByWId(id) {
502 var fields = grep( dataFields,
503 function(item) { return (item.widget.id == id); });
504 return (fields) ? fields[0] : null;
508 function uEditIterateFields(callback) {
509 for( var f in dataFields )
510 callback(dataFields[f]);
514 function uEditGetErrorStrings() {
519 if( !field.object.isdeleted() ) {
520 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
521 var str = $(field.errkey).innerHTML;
522 if(str) errors.push(str);
529 /* munge up something for all of the required surveys
530 (which are not registered with the fields) */
531 if( patron.isnew() ) {
532 var sel = $('ue_survey_table');
535 var rows = sel.getElementsByTagName('tr');
537 for( var r in rows ) {
540 var sel = $n(row, 'ue_survey_answer');
542 var qstn = row.getAttribute('question');
545 qstn = surveyQuestionsCache[qstn];
546 survey = surveysCache[qstn.survey()];
547 var val = getSelectorVal(sel);
548 if(!val && isTrue(survey.required()))
549 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
555 /* ------------------------------------------------------------ */
557 if(errors[0]) return errors;
561 function uEditAlertErrors() {
562 var errors = uEditGetErrorStrings();
563 if(!errors) return false;
564 alert(errors.join("\n"));
569 /* send the user to the database */
570 function uEditSaveUser(cloneme) {
572 if(uEditGetErrorStrings()) {
577 /* null is unique in the db, but '' is not */
578 if( ! patron.ident_value() ) patron.ident_value(null);
579 //if( ! patron.ident_type2() ) patron.ident_type2(null);
580 if( ! patron.ident_value2() ) patron.ident_value2(null);
581 patron.ident_type2(null);
583 if(! patron.dob() ) patron.dob(null);
585 _debug("Saving patron with card: " + js2JSON(patron.card()));
586 _debug("Saving full patron: " + js2JSON(patron));
588 //for( var c in patron
590 var req = new Request(UPDATE_PATRON, SESSION, patron);
591 req.alertEvent = false;
593 var newuser = req.result();
598 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
601 if( evt.textcode == 'XACT_COLLISION' ) {
602 if( confirmId('ue_xact_collision') )
603 location.href = location.href;
606 var j = js2JSON(evt);
608 _debug("USER UPDATE FAILED:\n" + j);
613 alert($('ue_success').innerHTML);
616 /* if the user we just created was a clone, and we want to clone it,
617 we really want to clone the original */
618 if( clone ) cloneme = clone;
619 else cloneme = newuser.id();
626 typeof window.xulG.spawn_editor == 'function' &&
629 _debug("xulG clone spawning new interface...");
630 var ses = cgi.param('ses');
631 if (xulG) if (xulG.ses) ses = xulG.ses;
632 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
633 window.xulG.spawn_editor({ses:ses,clone:cloneme});
638 var href = location.href;
639 href = href.replace(/\&?usr=\d+/, '');
640 href = href.replace(/\&?clone=\d+/, '');
641 href += (href.match(/\?/) ? "&" : "?") + 'clone=' + cloneme;
642 location.href = href;
650 uEditRefreshXUL(newuser);
654 function uEditRefreshXUL(newuser) {
655 if (window.xulG && typeof window.xulG.on_save == 'function')
656 window.xulG.on_save(newuser);
659 function uEditRefresh() {
660 var href = location.href;
661 href = href.replace(/\&?clone=\d+/, '');
662 location.href = href;
666 function uEditCancel() {
667 var href = location.href;
668 href = href.replace(/\&?usr=\d+/, '');
669 href = href.replace(/\&?clone=\d+/, '');
670 var id = cgi.param('usr');
671 if (xulG) if (xulG.usr) id = xulG.usr;
672 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
673 /* reload the current user if available */
674 if( id ) href += (href.match(/\?/) ? "&" : "?") + "usr=" + id;
675 location.href = href;
679 var uEditDupHashes = {};
680 var uEditDupTemplate;
682 function uEditRunDupeSearch(type, search_hash) {
684 if(!patron.isnew()) return;
686 _debug('dup search: ' + js2JSON(search_hash));
688 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
690 var container = $('dup_div_container');
691 if(!uEditDupTemplate)
692 uEditDupTemplate = container.removeChild($('dup_div'));
694 /* clear any existing dups for this type */
695 iterate( container.getElementsByTagName('div'),
697 if( d.getAttribute('type') == type ) {
698 container.removeChild(d)
706 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
713 function uEditHandleDupResults(ids, search_hash, type, container) {
715 _debug('dup search results: ' + js2JSON(ids));
717 if(!(ids && ids[0])) /* no results */
718 return uEditDupHashes[type] = null;
720 /* add a dup link to the UI and plug in the data */
721 var node = uEditDupTemplate.cloneNode(true);
722 container.appendChild(node);
723 node.setAttribute('type', type);
725 var link = $n(node, 'link');
726 link.setAttribute('type', type);
728 $n(node,'count').appendChild(text(ids.length));
730 for( var o in search_hash )
731 $n(node, 'data').appendChild(
732 text(search_hash[o].value + ' '));
734 uEditDupHashes[type] = search_hash;
738 if(confirm($('ue_dup_ident1').innerHTML))
739 uEditShowSearch(null, type);
745 function uEditShowSearch(link,type) {
746 if(!type) type = link.getAttribute('type');
748 window.xulG.spawn_search(uEditDupHashes[type]);
749 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
752 function uEditMarkCardLost() {
754 for( var c in patron.cards() ) {
756 var card = patron.cards()[c];
757 if( patron.card().id() == card.id() ) {
759 /* de-activite the current card */
763 if( !card.barcode() ) {
764 /* a card exists in the array with no barcode */
765 ueRemoveCard(card.id());
767 } else if( card.isnew() && card.active() == 0 ) {
768 /* a new card was created, then never used, removing.. */
769 _debug("removing new inactive card "+card.barcode());
770 ueRemoveCard(card.id());
773 /* create a new card for the patron */
774 var newcard = new ac();
775 newcard.id(uEditCardVirtId--);
777 patron.card(newcard);
778 patron.cards().push(newcard);
781 /* reset the widget */
782 var field = uEditFindFieldByWId('ue_barcode');
783 field.widget.node.disabled = false;
784 field.widget.node.value = "";
785 field.widget.node.onchange();
786 field.object = newcard;
787 _debug("uEditMarkCardLost(): created new card object for user");
793 function ueRemoveCard(id) {
794 _debug("removing card from cards() array: " + id);
795 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
797 for( var j = 0; j < cds.length; j++ )
798 _debug("patron card array now has : "+cds[j].id());
804 function compactArray(arr) {
806 for( var i = 0; arr && i < arr.length; i++ ) {