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) {
286 if(uEditUsePhonePw) {
287 if( (pw = patron.day_phone()) ||
288 (pw = patron.evening_phone()) || (pw = patron.other_phone()) ) {
289 pw = pw.substring(pw.length - 4); // this is iffy
291 appendClear($('ue_password_plain'), text(pw));
292 unHideMe($('ue_password_gen'));
295 pw = uEditMakeRandomPw(patron);
297 $('ue_password1').value = pw;
298 $('ue_password2').value = pw;
299 $('ue_password1').onchange();
304 function uEditClone(clone) {
306 var cloneUser = fetchFleshedUser(clone);
307 patron.usrgroup(cloneUser.usrgroup());
309 if( cloneUser.day_phone() ) {
310 $('ue_day_phone').value = cloneUser.day_phone();
311 $('ue_day_phone').onchange();
314 if( cloneUser.evening_phone() ) {
315 $('ue_night_phone').value = cloneUser.evening_phone();
316 $('ue_night_phone').onchange();
319 if( cloneUser.other_phone() ) {
320 $('ue_other_phone').value = cloneUser.other_phone();
321 $('ue_other_phone').onchange();
324 setSelector($('ue_org_selector'), cloneUser.home_ou());
325 setSelector($('ue_profile'), cloneUser.profile());
327 /* force the expire date to be set */
328 $('ue_profile').onchange();
329 $('ue_org_selector').onchange();
331 for( var a in cloneUser.addresses() ) {
332 var addr = cloneUser.addresses()[a];
333 if( cloneUser.mailing_address &&
334 addr.id() == cloneUser.mailing_address().id() )
335 patron.mailing_address(addr);
336 if( cloneUser.billing_address() &&
337 addr.id() == cloneUser.billing_address().id() )
338 patron.billing_address(addr);
339 patron.addresses().push(addr);
342 uEditBuildAddrs(patron);
346 /* Creates a new blank address,
347 adds it to the user and the fields array */
348 var uEditVirtualAddrId = -1;
349 function uEditCreateNewAddr() {
350 var addr = new aua();
352 addr.id(uEditVirtualAddrId--);
354 addr.usr(patron.id());
355 addr.country(defaultCountry);
357 if(!patron.addresses())
358 patron.addresses([]);
360 if(patron.addresses().length == 0) {
361 patron.mailing_address(addr);
362 patron.billing_address(addr);
366 addr.within_city_limits(1);
368 uEditBuildAddrFields(patron, addr);
369 patron.addresses().push(addr);
370 uEditIterateFields(function(f) { uEditCheckValid(f); });
375 /* kicks off the UI drawing */
376 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
377 hideMe($('uedit_loading'));
378 unHideMe($('ue_maintd'));
381 uEditDrawIDTypes(identTypes);
382 uEditDrawGroups(groups, null, null, true);
383 uEditDrawStatCats(statCats);
384 uEditDrawSurveys(surveys);
385 uEditDrawNetLevels(netLevels);
386 uEditDefineData(patron);
388 uEditIterateFields(function(f) { uEditActivateField(f) });
389 uEditIterateFields(function(f) { uEditCheckValid(f); });
394 /** Applies the event handlers and sets the data for the field */
395 function uEditActivateField(field) {
397 if( field.widget.id ) {
398 field.widget.node = $(field.widget.id);
402 $n(field.widget.base, field.widget.name);
405 uEditSetOnchange(field);
407 if(field.widget.onblur) {
408 field.widget.node.onblur =
409 function() { field.widget.onblur(field); };
412 field.widget.node.disabled = field.widget.disabled;
413 if(field.object == null) return;
414 var val = field.object[field.key]();
415 if(val == null) return;
417 if( field.widget.type == 'input' )
418 field.widget.node.value = val;
420 if( field.widget.type == 'select' )
421 setSelector(field.widget.node, val);
423 if( field.widget.type == 'checkbox' )
424 field.widget.node.checked =
425 (val && val != 'f') ? true : false;
427 if( field.widget.onload )
428 field.widget.onload(val);
432 /* set up the onchange event for the field */
433 function uEditSetOnchange(field) {
434 var func = function() {uEditOnChange( field );}
435 field.widget.node.onchange = func;
437 if(field.widget.type != 'select')
438 field.widget.node.onkeyup = func;
441 /* find the current value of the field object's widget */
442 function uEditNodeVal(field) {
443 if(field.widget.type == 'input')
444 return field.widget.node.value;
446 if(field.widget.type == 'checkbox')
447 return field.widget.node.checked;
449 if(field.widget.type == 'select')
450 return getSelectorVal(field.widget.node);
454 /* update a field value */
455 function uEditOnChange(field) {
457 var newval = uEditNodeVal(field);
458 field.object[field.key](newval);
459 field.object.ischanged(1);
461 if(field.widget.onpostchange)
462 field.widget.onpostchange(field, newval);
464 //_debug(field.key+' = '+newval);
466 uEditIterateFields(function(f) { uEditCheckValid(f); });
473 function uEditCheckValid(field) {
474 var newval = uEditNodeVal(field);
478 if(field.widget.regex) {
479 if(newval.match(field.widget.regex))
480 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
482 addCSSClass(field.widget.node, CSS_INVALID_DATA);
485 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
491 addCSSClass(field.widget.node, CSS_INVALID_DATA);
494 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
500 /* find a field object by object key */
501 function uEditFindFieldByKey(key) {
502 var fields = grep( dataFields,
503 function(item) { return (item.key == key); });
504 return (fields) ? fields[0] : null;
507 /* find a list of fields by object key */
508 function uEditFindFieldsByKey(key) {
509 return grep( dataFields,
510 function(item) { return (item.key == key); });
513 /* find a field object by widget id */
514 function uEditFindFieldByWId(id) {
515 var fields = grep( dataFields,
516 function(item) { return (item.widget.id == id); });
517 return (fields) ? fields[0] : null;
521 function uEditIterateFields(callback) {
522 for( var f in dataFields )
523 callback(dataFields[f]);
527 function uEditGetErrorStrings() {
532 if( !field.object.isdeleted() ) {
533 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
534 var str = $(field.errkey).innerHTML;
535 if(str) errors.push(str);
542 /* munge up something for all of the required surveys
543 (which are not registered with the fields) */
544 if( patron.isnew() ) {
545 var sel = $('ue_survey_table');
548 var rows = sel.getElementsByTagName('tr');
550 for( var r in rows ) {
553 var sel = $n(row, 'ue_survey_answer');
555 var qstn = row.getAttribute('question');
558 qstn = surveyQuestionsCache[qstn];
559 survey = surveysCache[qstn.survey()];
560 var val = getSelectorVal(sel);
561 if(!val && isTrue(survey.required()))
562 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
568 /* ------------------------------------------------------------ */
570 if(errors[0]) return errors;
574 function uEditAlertErrors() {
575 var errors = uEditGetErrorStrings();
576 if(!errors) return false;
577 alert(errors.join("\n"));
582 /* send the user to the database */
583 function uEditSaveUser(cloneme) {
585 if(uEditGetErrorStrings()) {
590 /* null is unique in the db, but '' is not */
591 if( ! patron.ident_value() ) patron.ident_value(null);
592 //if( ! patron.ident_type2() ) patron.ident_type2(null);
593 if( ! patron.ident_value2() ) patron.ident_value2(null);
594 patron.ident_type2(null);
596 if(! patron.dob() ) patron.dob(null);
598 _debug("Saving patron with card: " + js2JSON(patron.card()));
599 _debug("Saving full patron: " + js2JSON(patron));
601 //for( var c in patron
603 var req = new Request(UPDATE_PATRON, SESSION, patron);
604 req.alertEvent = false;
606 var newuser = req.result();
611 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
614 if( evt.textcode == 'XACT_COLLISION' ) {
615 if( confirmId('ue_xact_collision') )
616 location.href = location.href;
619 var j = js2JSON(evt);
621 _debug("USER UPDATE FAILED:\n" + j);
626 alert($('ue_success').innerHTML);
629 /* if the user we just created was a clone, and we want to clone it,
630 we really want to clone the original */
631 if( clone ) cloneme = clone;
632 else cloneme = newuser.id();
639 typeof window.xulG.spawn_editor == 'function' &&
642 _debug("xulG clone spawning new interface...");
643 var ses = cgi.param('ses');
644 if (xulG) if (xulG.ses) ses = xulG.ses;
645 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
646 window.xulG.spawn_editor({ses:ses,clone:cloneme});
651 var href = location.href;
652 href = href.replace(/\&?usr=\d+/, '');
653 href = href.replace(/\&?clone=\d+/, '');
654 href += (href.match(/\?/) ? "&" : "?") + 'clone=' + cloneme;
655 location.href = href;
663 uEditRefreshXUL(newuser);
667 function uEditRefreshXUL(newuser) {
668 if (window.xulG && typeof window.xulG.on_save == 'function')
669 window.xulG.on_save(newuser);
672 function uEditRefresh() {
673 var href = location.href;
674 href = href.replace(/\&?clone=\d+/, '');
675 location.href = href;
679 function uEditCancel() {
680 var href = location.href;
681 href = href.replace(/\&?usr=\d+/, '');
682 href = href.replace(/\&?clone=\d+/, '');
683 var id = cgi.param('usr');
684 if (xulG) if (xulG.usr) id = xulG.usr;
685 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
686 /* reload the current user if available */
687 if( id ) href += (href.match(/\?/) ? "&" : "?") + "usr=" + id;
688 location.href = href;
692 var uEditDupHashes = {};
693 var uEditDupTemplate;
695 function uEditRunDupeSearch(type, search_hash) {
697 if(!patron.isnew()) return;
699 _debug('dup search: ' + js2JSON(search_hash));
701 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
703 var container = $('dup_div_container');
704 if(!uEditDupTemplate)
705 uEditDupTemplate = container.removeChild($('dup_div'));
707 /* clear any existing dups for this type */
708 iterate( container.getElementsByTagName('div'),
710 if( d.getAttribute('type') == type ) {
711 container.removeChild(d)
719 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
726 function uEditHandleDupResults(ids, search_hash, type, container) {
728 _debug('dup search results: ' + js2JSON(ids));
730 if(!(ids && ids[0])) /* no results */
731 return uEditDupHashes[type] = null;
733 /* add a dup link to the UI and plug in the data */
734 var node = uEditDupTemplate.cloneNode(true);
735 container.appendChild(node);
736 node.setAttribute('type', type);
738 var link = $n(node, 'link');
739 link.setAttribute('type', type);
741 $n(node,'count').appendChild(text(ids.length));
743 for( var o in search_hash )
744 $n(node, 'data').appendChild(
745 text(search_hash[o].value + ' '));
747 uEditDupHashes[type] = search_hash;
751 if(confirm($('ue_dup_ident1').innerHTML))
752 uEditShowSearch(null, type);
758 function uEditShowSearch(link,type) {
759 if(!type) type = link.getAttribute('type');
761 window.xulG.spawn_search(uEditDupHashes[type]);
762 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
765 function uEditMarkCardLost() {
767 for( var c in patron.cards() ) {
769 var card = patron.cards()[c];
770 if( patron.card().id() == card.id() ) {
772 /* de-activite the current card */
776 if( !card.barcode() ) {
777 /* a card exists in the array with no barcode */
778 ueRemoveCard(card.id());
780 } else if( card.isnew() && card.active() == 0 ) {
781 /* a new card was created, then never used, removing.. */
782 _debug("removing new inactive card "+card.barcode());
783 ueRemoveCard(card.id());
786 /* create a new card for the patron */
787 var newcard = new ac();
788 newcard.id(uEditCardVirtId--);
790 patron.card(newcard);
791 patron.cards().push(newcard);
794 /* reset the widget */
795 var field = uEditFindFieldByWId('ue_barcode');
796 field.widget.node.disabled = false;
797 field.widget.node.value = "";
798 field.widget.node.onchange();
799 field.object = newcard;
800 _debug("uEditMarkCardLost(): created new card object for user");
806 function ueRemoveCard(id) {
807 _debug("removing card from cards() array: " + id);
808 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
810 for( var j = 0; j < cds.length; j++ )
811 _debug("patron card array now has : "+cds[j].id());
817 function compactArray(arr) {
819 for( var i = 0; arr && i < arr.length; i++ ) {