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 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
139 uEditBuildLibSelector();
140 var usr = cgi.param('usr');
141 if (xulG) if (xulG.usr) usr = xulG.usr;
142 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
144 orgSettings = fetchBatchOrgSetting(USER.ws_ou(), [
145 'global.juvenile_age_threshold',
146 'patron.password.use_phone'
149 uEditUsePhonePw = (orgSettings['patron.password.use_phone'] &&
150 orgSettings['patron.password.use_phone'].value);
152 patron = fetchFleshedUser(usr);
153 if(!patron) patron = uEditNewPatron();
156 uEditFetchIdentTypes(),
158 uEditFetchStatCats(),
160 uEditFetchNetLevels()
164 if(clone) uEditClone(clone);
165 else uEditCreateNewAddr();
169 /* do we need to display the parent / gurdian field? */
170 uEditCheckDOB(uEditFindFieldByKey('dob'));
172 $('ue_barcode').disabled = true;
173 unHideMe($('ue_mark_card_lost'));
174 unHideMe($('ue_reset_pw'));
175 uEditCheckEditPerm();
178 uEditCheckBarredPerm();
181 function uEditCheckBarredPerm() {
182 if(PERMS['BAR_PATRON'] != -1)
185 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
188 $('ue_barred').disabled = true;
192 /* if this user does not have permission to put users into
193 the edited users group, they do not have permission to
195 function uEditCheckEditPerm() {
197 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
199 _debug("editing user with group app perm "+patron.profile()+' : '+
200 groupsCache[patron.profile()].name() +', and perm = ' + perm);
203 if(PERMS[perm] != -1) return;
205 /* we can edit our own account, but not others in our group */
206 if( patron.id() != USER.id() ){
207 _debug("we are not allowed to edit this user");
209 $('ue_save').disabled = true;
210 $('ue_save_clone').disabled = true;
211 $('ue_mark_card_lost').disabled = true;
212 $('ue_reset_pw').disabled = true;
216 if( f && f.widget && f.widget.node )
217 f.widget.node.disabled = true;
223 var node = $('ue_profile').parentNode;
224 node.removeChild($('ue_profile'));
225 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
227 var field = uEditFindFieldByKey('profile');
228 field.required = false;
229 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
234 /* creates a new patron object with card attached */
235 var uEditCardVirtId = -1;
236 function uEditNewPatron() {
237 var patron = new au();
241 card.id(uEditCardVirtId--);
244 patron.cards([card]);
245 patron.net_access_level(defaultNetLevel);
246 patron.stat_cat_entries([]);
247 patron.survey_responses([]);
248 patron.addresses([]);
249 patron.home_ou(USER.ws_ou());
250 uEditMakeRandomPw(patron);
254 function uEditMakeRandomPw(patron) {
255 if(uEditUsePhonePw) return;
256 var rand = Math.random();
257 rand = parseInt(rand * 10000) + '';
258 while(rand.length < 4) rand += '0';
259 appendClear($('ue_password_plain'),text(rand));
260 unHideMe($('ue_password_gen'));
265 function uEditMakePhonePw() {
266 if(patron.passwd()) return;
267 if( (pw = patron.day_phone()) ||
268 (pw = patron.evening_phone()) || (pw = patron.other_phone()) ) {
269 pw = pw.substring(pw.length - 4); // this is iffy
271 appendClear($('ue_password_plain'), text(pw));
272 unHideMe($('ue_password_gen'));
277 function uEditResetPw(pw) {
278 if(!pw) pw = uEditMakeRandomPw(patron);
279 $('ue_password1').value = pw;
280 $('ue_password2').value = pw;
281 $('ue_password1').onchange();
284 function uEditClone(clone) {
286 var cloneUser = fetchFleshedUser(clone);
287 patron.usrgroup(cloneUser.usrgroup());
289 if( cloneUser.day_phone() ) {
290 $('ue_day_phone').value = cloneUser.day_phone();
291 $('ue_day_phone').onchange();
294 if( cloneUser.evening_phone() ) {
295 $('ue_night_phone').value = cloneUser.evening_phone();
296 $('ue_night_phone').onchange();
299 if( cloneUser.other_phone() ) {
300 $('ue_other_phone').value = cloneUser.other_phone();
301 $('ue_other_phone').onchange();
304 setSelector($('ue_org_selector'), cloneUser.home_ou());
305 setSelector($('ue_profile'), cloneUser.profile());
307 /* force the expire date to be set */
308 $('ue_profile').onchange();
309 $('ue_org_selector').onchange();
311 for( var a in cloneUser.addresses() ) {
312 var addr = cloneUser.addresses()[a];
313 if( cloneUser.mailing_address &&
314 addr.id() == cloneUser.mailing_address().id() )
315 patron.mailing_address(addr);
316 if( cloneUser.billing_address() &&
317 addr.id() == cloneUser.billing_address().id() )
318 patron.billing_address(addr);
319 patron.addresses().push(addr);
322 uEditBuildAddrs(patron);
326 /* Creates a new blank address,
327 adds it to the user and the fields array */
328 var uEditVirtualAddrId = -1;
329 function uEditCreateNewAddr() {
330 var addr = new aua();
332 addr.id(uEditVirtualAddrId--);
334 addr.usr(patron.id());
335 addr.country(defaultCountry);
337 if(!patron.addresses())
338 patron.addresses([]);
340 if(patron.addresses().length == 0) {
341 patron.mailing_address(addr);
342 patron.billing_address(addr);
346 addr.within_city_limits(1);
348 uEditBuildAddrFields(patron, addr);
349 patron.addresses().push(addr);
350 uEditIterateFields(function(f) { uEditCheckValid(f); });
355 /* kicks off the UI drawing */
356 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
357 hideMe($('uedit_loading'));
358 unHideMe($('ue_maintd'));
361 uEditDrawIDTypes(identTypes);
362 uEditDrawGroups(groups, null, null, true);
363 uEditDrawStatCats(statCats);
364 uEditDrawSurveys(surveys);
365 uEditDrawNetLevels(netLevels);
366 uEditDefineData(patron);
368 uEditIterateFields(function(f) { uEditActivateField(f) });
369 uEditIterateFields(function(f) { uEditCheckValid(f); });
374 /** Applies the event handlers and sets the data for the field */
375 function uEditActivateField(field) {
377 if( field.widget.id ) {
378 field.widget.node = $(field.widget.id);
382 $n(field.widget.base, field.widget.name);
385 uEditSetOnchange(field);
387 if(field.widget.onblur) {
388 field.widget.node.onblur =
389 function() { field.widget.onblur(field); };
392 field.widget.node.disabled = field.widget.disabled;
393 if(field.object == null) return;
394 var val = field.object[field.key]();
395 if(val == null) return;
397 if( field.widget.type == 'input' )
398 field.widget.node.value = val;
400 if( field.widget.type == 'select' )
401 setSelector(field.widget.node, val);
403 if( field.widget.type == 'checkbox' )
404 field.widget.node.checked =
405 (val && val != 'f') ? true : false;
407 if( field.widget.onload )
408 field.widget.onload(val);
412 /* set up the onchange event for the field */
413 function uEditSetOnchange(field) {
414 var func = function() {uEditOnChange( field );}
415 field.widget.node.onchange = func;
417 if(field.widget.type != 'select')
418 field.widget.node.onkeyup = func;
421 /* find the current value of the field object's widget */
422 function uEditNodeVal(field) {
423 if(field.widget.type == 'input')
424 return field.widget.node.value;
426 if(field.widget.type == 'checkbox')
427 return field.widget.node.checked;
429 if(field.widget.type == 'select')
430 return getSelectorVal(field.widget.node);
434 /* update a field value */
435 function uEditOnChange(field) {
437 var newval = uEditNodeVal(field);
438 field.object[field.key](newval);
439 field.object.ischanged(1);
441 if(field.widget.onpostchange)
442 field.widget.onpostchange(field, newval);
444 //_debug(field.key+' = '+newval);
446 uEditIterateFields(function(f) { uEditCheckValid(f); });
453 function uEditCheckValid(field) {
454 var newval = uEditNodeVal(field);
458 if(field.widget.regex) {
459 if(newval.match(field.widget.regex))
460 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
462 addCSSClass(field.widget.node, CSS_INVALID_DATA);
465 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
471 addCSSClass(field.widget.node, CSS_INVALID_DATA);
474 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
480 /* find a field object by object key */
481 function uEditFindFieldByKey(key) {
482 var fields = grep( dataFields,
483 function(item) { return (item.key == key); });
484 return (fields) ? fields[0] : null;
487 /* find a list of fields by object key */
488 function uEditFindFieldsByKey(key) {
489 return grep( dataFields,
490 function(item) { return (item.key == key); });
493 /* find a field object by widget id */
494 function uEditFindFieldByWId(id) {
495 var fields = grep( dataFields,
496 function(item) { return (item.widget.id == id); });
497 return (fields) ? fields[0] : null;
501 function uEditIterateFields(callback) {
502 for( var f in dataFields )
503 callback(dataFields[f]);
507 function uEditGetErrorStrings() {
512 if( !field.object.isdeleted() ) {
513 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
514 var str = $(field.errkey).innerHTML;
515 if(str) errors.push(str);
522 /* munge up something for all of the required surveys
523 (which are not registered with the fields) */
524 if( patron.isnew() ) {
525 var sel = $('ue_survey_table');
528 var rows = sel.getElementsByTagName('tr');
530 for( var r in rows ) {
533 var sel = $n(row, 'ue_survey_answer');
535 var qstn = row.getAttribute('question');
538 qstn = surveyQuestionsCache[qstn];
539 survey = surveysCache[qstn.survey()];
540 var val = getSelectorVal(sel);
541 if(!val && isTrue(survey.required()))
542 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
548 /* ------------------------------------------------------------ */
550 if(errors[0]) return errors;
554 function uEditAlertErrors() {
555 var errors = uEditGetErrorStrings();
556 if(!errors) return false;
557 alert(errors.join("\n"));
562 /* send the user to the database */
563 function uEditSaveUser(cloneme) {
565 if(uEditGetErrorStrings()) {
570 /* null is unique in the db, but '' is not */
571 if( ! patron.ident_value() ) patron.ident_value(null);
572 //if( ! patron.ident_type2() ) patron.ident_type2(null);
573 if( ! patron.ident_value2() ) patron.ident_value2(null);
574 patron.ident_type2(null);
576 if(! patron.dob() ) patron.dob(null);
578 _debug("Saving patron with card: " + js2JSON(patron.card()));
579 _debug("Saving full patron: " + js2JSON(patron));
581 //for( var c in patron
583 var req = new Request(UPDATE_PATRON, SESSION, patron);
584 req.alertEvent = false;
586 var newuser = req.result();
591 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
594 if( evt.textcode == 'XACT_COLLISION' ) {
595 if( confirmId('ue_xact_collision') )
596 location.href = location.href;
599 var j = js2JSON(evt);
601 _debug("USER UPDATE FAILED:\n" + j);
606 alert($('ue_success').innerHTML);
609 /* if the user we just created was a clone, and we want to clone it,
610 we really want to clone the original */
611 if( clone ) cloneme = clone;
612 else cloneme = newuser.id();
619 typeof window.xulG.spawn_editor == 'function' &&
622 _debug("xulG clone spawning new interface...");
623 var ses = cgi.param('ses');
624 if (xulG) if (xulG.ses) ses = xulG.ses;
625 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
626 window.xulG.spawn_editor({ses:ses,clone:cloneme});
631 var href = location.href;
632 href = href.replace(/\&?usr=\d+/, '');
633 href = href.replace(/\&?clone=\d+/, '');
634 href += '&clone=' + cloneme;
635 location.href = href;
643 uEditRefreshXUL(newuser);
647 function uEditRefreshXUL(newuser) {
648 if (window.xulG && typeof window.xulG.on_save == 'function')
649 window.xulG.on_save(newuser);
652 function uEditRefresh() {
653 var href = location.href;
654 href = href.replace(/\&?clone=\d+/, '');
655 location.href = href;
659 function uEditCancel() {
660 var href = location.href;
661 href = href.replace(/\&?usr=\d+/, '');
662 href = href.replace(/\&?clone=\d+/, '');
663 var id = cgi.param('usr');
664 if (xulG) if (xulG.usr) id = xulG.usr;
665 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
666 /* reload the current user if available */
667 if( id ) href += (href.match(/\?/) ? "&" : "?") + "usr=" + id;
668 location.href = href;
672 var uEditDupHashes = {};
673 var uEditDupTemplate;
675 function uEditRunDupeSearch(type, search_hash) {
677 if(!patron.isnew()) return;
679 _debug('dup search: ' + js2JSON(search_hash));
681 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
683 var container = $('dup_div_container');
684 if(!uEditDupTemplate)
685 uEditDupTemplate = container.removeChild($('dup_div'));
687 /* clear any existing dups for this type */
688 iterate( container.getElementsByTagName('div'),
690 if( d.getAttribute('type') == type ) {
691 container.removeChild(d)
699 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
706 function uEditHandleDupResults(ids, search_hash, type, container) {
708 _debug('dup search results: ' + js2JSON(ids));
710 if(!(ids && ids[0])) /* no results */
711 return uEditDupHashes[type] = null;
713 /* add a dup link to the UI and plug in the data */
714 var node = uEditDupTemplate.cloneNode(true);
715 container.appendChild(node);
716 node.setAttribute('type', type);
718 var link = $n(node, 'link');
719 link.setAttribute('type', type);
721 $n(node,'count').appendChild(text(ids.length));
723 for( var o in search_hash )
724 $n(node, 'data').appendChild(
725 text(search_hash[o].value + ' '));
727 uEditDupHashes[type] = search_hash;
731 if(confirm($('ue_dup_ident1').innerHTML))
732 uEditShowSearch(null, type);
738 function uEditShowSearch(link,type) {
739 if(!type) type = link.getAttribute('type');
741 window.xulG.spawn_search(uEditDupHashes[type]);
742 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
745 function uEditMarkCardLost() {
747 for( var c in patron.cards() ) {
749 var card = patron.cards()[c];
750 if( patron.card().id() == card.id() ) {
752 /* de-activite the current card */
756 if( !card.barcode() ) {
757 /* a card exists in the array with no barcode */
758 ueRemoveCard(card.id());
760 } else if( card.isnew() && card.active() == 0 ) {
761 /* a new card was created, then never used, removing.. */
762 _debug("removing new inactive card "+card.barcode());
763 ueRemoveCard(card.id());
766 /* create a new card for the patron */
767 var newcard = new ac();
768 newcard.id(uEditCardVirtId--);
770 patron.card(newcard);
771 patron.cards().push(newcard);
774 /* reset the widget */
775 var field = uEditFindFieldByWId('ue_barcode');
776 field.widget.node.disabled = false;
777 field.widget.node.value = "";
778 field.widget.node.onchange();
779 field.object = newcard;
780 _debug("uEditMarkCardLost(): created new card object for user");
786 function ueRemoveCard(id) {
787 _debug("removing card from cards() array: " + id);
788 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
790 for( var j = 0; j < cds.length; j++ )
791 _debug("patron card array now has : "+cds[j].id());
797 function compactArray(arr) {
799 for( var i = 0; arr && i < arr.length; i++ ) {