5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 //var guardianNote = null;
15 function $(id) { return document.getElementById(id); }
17 /* fetch the necessary data to start off */
18 function uEditInit() {
20 _debug('uEditInit(): ' + location.search);
23 session = cgi.param('ses');
24 if (xulG) if (xulG.ses) session = xulG.ses;
25 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
26 clone = cgi.param('clone');
27 if (xulG) if (xulG.clone) clone = xulG.clone;
28 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
29 if(!session) throw $("patronStrings").getString('web.staff.patron.ue.session_no_defined');
32 $('uedit_user').appendChild(text(USER.usrname()));
34 setTimeout( function() {
35 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
38 function uEditSetUnload() {
39 _debug('setting window unload event');
41 window.onbeforeunload = function(evt) {
42 return $('ue_unsaved_changes').innerHTML;
47 function uEditClearUnload() {
48 _debug('clearing window unload event');
50 window.onbeforeunload = null;
54 /* ------------------------------------------------------------------------------ */
56 /* ------------------------------------------------------------------------------ */
57 function uEditFetchIdentTypes() {
58 _debug("uEditFetchIdentTypes()");
59 var s = fetchXULStash();
60 if (typeof s.list != 'undefined')
61 if (typeof s.list.cit != 'undefined') return s.list.cit;
62 var req = new Request(FETCH_ID_TYPES);
67 function uEditFetchStatCats() {
68 _debug("uEditFetchStatCats()");
69 var s = fetchXULStash();
70 if (typeof s.list != 'undefined')
71 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
72 var req = new Request(SC_FETCH_ALL, SESSION);
77 function uEditFetchSurveys() {
78 _debug("uEditFetchSurveys()");
79 var s = fetchXULStash();
80 if (typeof s.list != 'undefined')
81 if (typeof s.list.asv != 'undefined') return s.list.asv;
82 var req = new Request(SV_FETCH_ALL, SESSION);
87 function uEditFetchGroups() {
88 _debug("uEditFetchGroups()");
89 var s = fetchXULStash();
90 if (typeof s.tree != 'undefined')
91 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
92 var req = new Request(FETCH_GROUPS);
97 function uEditFetchNetLevels() {
98 _debug("uEditFetchNetLevels()");
99 var s = fetchXULStash();
100 if (typeof s.list != 'undefined')
101 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
102 var req = new Request(FETCH_NET_LEVELS, SESSION);
107 /* ------------------------------------------------------------------------------ */
111 * adds all of the group.application_perm's to the list
112 * provided by descending through the group tree
114 function buildAppPermList(list, group) {
116 if(group.application_perm() )
117 list.push(group.application_perm());
118 for(i in group.children()) {
119 buildAppPermList(list, group.children()[i]);
123 /* fetches necessary objects and builds the UI */
124 function uEditBuild() {
126 myPerms = ['BAR_PATRON', 'UNBAR_PATRON'];
128 /* grab the groups before we check perms so we know what
129 application_perms to check */
130 var groups = uEditFetchGroups();
131 buildAppPermList(myPerms, groups);
133 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
135 uEditBuildLibSelector();
136 var usr = cgi.param('usr');
137 if (xulG) if (xulG.usr) usr = xulG.usr;
138 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
139 patron = fetchFleshedUser(usr);
140 if(!patron) patron = uEditNewPatron();
143 uEditFetchIdentTypes(),
145 uEditFetchStatCats(),
147 uEditFetchNetLevels()
151 if(clone) uEditClone(clone);
152 else uEditCreateNewAddr();
156 /* do we need to display the parent / gurdian field? */
157 uEditCheckDOB(uEditFindFieldByKey('dob'));
159 $('ue_barcode').disabled = true;
160 unHideMe($('ue_mark_card_lost'));
161 unHideMe($('ue_reset_pw'));
162 uEditCheckEditPerm();
165 uEditCheckBarredPerm();
168 function uEditCheckBarredPerm() {
169 if(PERMS['BAR_PATRON'] != -1)
172 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
175 $('ue_barred').disabled = true;
179 /* if this user does not have permission to put users into
180 the edited users group, they do not have permission to
182 function uEditCheckEditPerm() {
184 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
186 _debug("editing user with group app perm "+patron.profile()+' : '+
187 groupsCache[patron.profile()].name() +', and perm = ' + perm);
190 if(PERMS[perm] != -1) return;
192 /* we can edit our own account, but not others in our group */
193 if( patron.id() != USER.id() ){
194 _debug("we are not allowed to edit this user");
196 $('ue_save').disabled = true;
197 $('ue_save_clone').disabled = true;
198 $('ue_mark_card_lost').disabled = true;
199 $('ue_reset_pw').disabled = true;
203 if( f && f.widget && f.widget.node )
204 f.widget.node.disabled = true;
210 var node = $('ue_profile').parentNode;
211 node.removeChild($('ue_profile'));
212 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
214 var field = uEditFindFieldByKey('profile');
215 field.required = false;
216 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
221 /* creates a new patron object with card attached */
222 var uEditCardVirtId = -1;
223 function uEditNewPatron() {
224 var patron = new au();
228 card.id(uEditCardVirtId--);
231 patron.cards([card]);
232 patron.net_access_level(defaultNetLevel);
233 patron.stat_cat_entries([]);
234 patron.survey_responses([]);
235 patron.addresses([]);
236 patron.home_ou(USER.ws_ou());
237 uEditMakeRandomPw(patron);
241 function uEditMakeRandomPw(patron) {
242 var rand = Math.random();
243 rand = parseInt(rand * 10000) + '';
244 while(rand.length < 4) rand += '0';
245 appendClear($('ue_password_plain'),text(rand));
246 unHideMe($('ue_password_gen'));
251 function uEditResetPw() {
252 var pw = uEditMakeRandomPw(patron);
253 $('ue_password1').value = pw;
254 $('ue_password2').value = pw;
255 $('ue_password1').onchange();
258 function uEditClone(clone) {
260 var cloneUser = fetchFleshedUser(clone);
261 patron.usrgroup(cloneUser.usrgroup());
263 if( cloneUser.day_phone() ) {
264 $('ue_day_phone').value = cloneUser.day_phone();
265 $('ue_day_phone').onchange();
268 if( cloneUser.evening_phone() ) {
269 $('ue_night_phone').value = cloneUser.evening_phone();
270 $('ue_night_phone').onchange();
273 if( cloneUser.other_phone() ) {
274 $('ue_other_phone').value = cloneUser.other_phone();
275 $('ue_other_phone').onchange();
278 setSelector($('ue_org_selector'), cloneUser.home_ou());
279 setSelector($('ue_profile'), cloneUser.profile());
281 /* force the expire date to be set */
282 $('ue_profile').onchange();
283 $('ue_org_selector').onchange();
285 for( var a in cloneUser.addresses() ) {
286 var addr = cloneUser.addresses()[a];
287 if( cloneUser.mailing_address &&
288 addr.id() == cloneUser.mailing_address().id() )
289 patron.mailing_address(addr);
290 if( cloneUser.billing_address() &&
291 addr.id() == cloneUser.billing_address().id() )
292 patron.billing_address(addr);
293 patron.addresses().push(addr);
296 uEditBuildAddrs(patron);
300 /* Creates a new blank address,
301 adds it to the user and the fields array */
302 var uEditVirtualAddrId = -1;
303 function uEditCreateNewAddr() {
304 var addr = new aua();
306 addr.id(uEditVirtualAddrId--);
308 addr.usr(patron.id());
309 addr.country(defaultCountry);
311 if(!patron.addresses())
312 patron.addresses([]);
314 if(patron.addresses().length == 0) {
315 patron.mailing_address(addr);
316 patron.billing_address(addr);
320 addr.within_city_limits(1);
322 uEditBuildAddrFields(patron, addr);
323 patron.addresses().push(addr);
324 uEditIterateFields(function(f) { uEditCheckValid(f); });
329 /* kicks off the UI drawing */
330 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
331 hideMe($('uedit_loading'));
332 unHideMe($('ue_maintd'));
335 uEditDrawIDTypes(identTypes);
336 uEditDrawGroups(groups, null, null, true);
337 uEditDrawStatCats(statCats);
338 uEditDrawSurveys(surveys);
339 uEditDrawNetLevels(netLevels);
340 uEditDefineData(patron);
342 uEditIterateFields(function(f) { uEditActivateField(f) });
343 uEditIterateFields(function(f) { uEditCheckValid(f); });
348 /** Applies the event handlers and sets the data for the field */
349 function uEditActivateField(field) {
351 if( field.widget.id ) {
352 field.widget.node = $(field.widget.id);
356 $n(field.widget.base, field.widget.name);
359 uEditSetOnchange(field);
361 if(field.widget.onblur) {
362 field.widget.node.onblur =
363 function() { field.widget.onblur(field); };
366 field.widget.node.disabled = field.widget.disabled;
367 if(field.object == null) return;
368 var val = field.object[field.key]();
369 if(val == null) return;
371 if( field.widget.type == 'input' )
372 field.widget.node.value = val;
374 if( field.widget.type == 'select' )
375 setSelector(field.widget.node, val);
377 if( field.widget.type == 'checkbox' )
378 field.widget.node.checked =
379 (val && val != 'f') ? true : false;
381 if( field.widget.onload )
382 field.widget.onload(val);
386 /* set up the onchange event for the field */
387 function uEditSetOnchange(field) {
388 var func = function() {uEditOnChange( field );}
389 field.widget.node.onchange = func;
391 if(field.widget.type != 'select')
392 field.widget.node.onkeyup = func;
395 /* find the current value of the field object's widget */
396 function uEditNodeVal(field) {
397 if(field.widget.type == 'input')
398 return field.widget.node.value;
400 if(field.widget.type == 'checkbox')
401 return field.widget.node.checked;
403 if(field.widget.type == 'select')
404 return getSelectorVal(field.widget.node);
408 /* update a field value */
409 function uEditOnChange(field) {
411 var newval = uEditNodeVal(field);
412 field.object[field.key](newval);
413 field.object.ischanged(1);
415 if(field.widget.onpostchange)
416 field.widget.onpostchange(field, newval);
418 //_debug(field.key+' = '+newval);
420 uEditIterateFields(function(f) { uEditCheckValid(f); });
427 function uEditCheckValid(field) {
428 var newval = uEditNodeVal(field);
432 if(field.widget.regex) {
433 if(newval.match(field.widget.regex))
434 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
436 addCSSClass(field.widget.node, CSS_INVALID_DATA);
439 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
445 addCSSClass(field.widget.node, CSS_INVALID_DATA);
448 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
454 /* find a field object by object key */
455 function uEditFindFieldByKey(key) {
456 var fields = grep( dataFields,
457 function(item) { return (item.key == key); });
458 return (fields) ? fields[0] : null;
461 /* find a list of fields by object key */
462 function uEditFindFieldsByKey(key) {
463 return grep( dataFields,
464 function(item) { return (item.key == key); });
467 /* find a field object by widget id */
468 function uEditFindFieldByWId(id) {
469 var fields = grep( dataFields,
470 function(item) { return (item.widget.id == id); });
471 return (fields) ? fields[0] : null;
475 function uEditIterateFields(callback) {
476 for( var f in dataFields )
477 callback(dataFields[f]);
481 function uEditGetErrorStrings() {
486 if( !field.object.isdeleted() ) {
487 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
488 var str = $(field.errkey).innerHTML;
489 if(str) errors.push(str);
496 /* munge up something for all of the required surveys
497 (which are not registered with the fields) */
498 if( patron.isnew() ) {
499 var sel = $('ue_survey_table');
502 var rows = sel.getElementsByTagName('tr');
504 for( var r in rows ) {
507 var sel = $n(row, 'ue_survey_answer');
509 var qstn = row.getAttribute('question');
512 qstn = surveyQuestionsCache[qstn];
513 survey = surveysCache[qstn.survey()];
514 var val = getSelectorVal(sel);
515 if(!val && isTrue(survey.required()))
516 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
522 /* ------------------------------------------------------------ */
524 if(errors[0]) return errors;
528 function uEditAlertErrors() {
529 var errors = uEditGetErrorStrings();
530 if(!errors) return false;
531 alert(errors.join("\n"));
536 /* send the user to the database */
537 function uEditSaveUser(cloneme) {
539 if(uEditGetErrorStrings()) {
544 /* null is unique in the db, but '' is not */
545 if( ! patron.ident_value() ) patron.ident_value(null);
546 //if( ! patron.ident_type2() ) patron.ident_type2(null);
547 if( ! patron.ident_value2() ) patron.ident_value2(null);
548 patron.ident_type2(null);
550 if(! patron.dob() ) patron.dob(null);
552 _debug("Saving patron with card: " + js2JSON(patron.card()));
553 _debug("Saving full patron: " + js2JSON(patron));
555 //for( var c in patron
557 var req = new Request(UPDATE_PATRON, SESSION, patron);
558 req.alertEvent = false;
560 var newuser = req.result();
565 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
568 if( evt.textcode == 'XACT_COLLISION' ) {
569 if( confirmId('ue_xact_collision') )
570 location.href = location.href;
573 var j = js2JSON(evt);
575 _debug("USER UPDATE FAILED:\n" + j);
580 alert($('ue_success').innerHTML);
583 /* if the user we just created was a clone, and we want to clone it,
584 we really want to clone the original */
585 if( clone ) cloneme = clone;
586 else cloneme = newuser.id();
593 typeof window.xulG.spawn_editor == 'function' &&
596 _debug("xulG clone spawning new interface...");
597 var ses = cgi.param('ses');
598 if (xulG) if (xulG.ses) ses = xulG.ses;
599 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
600 window.xulG.spawn_editor({ses:ses,clone:cloneme});
605 var href = location.href;
606 href = href.replace(/\&?usr=\d+/, '');
607 href = href.replace(/\&?clone=\d+/, '');
608 href += '&clone=' + cloneme;
609 location.href = href;
617 uEditRefreshXUL(newuser);
621 function uEditRefreshXUL(newuser) {
622 if (window.xulG && typeof window.xulG.on_save == 'function')
623 window.xulG.on_save(newuser);
626 function uEditRefresh() {
627 var href = location.href;
628 href = href.replace(/\&?clone=\d+/, '');
629 location.href = href;
633 function uEditCancel() {
634 var href = location.href;
635 href = href.replace(/\&?usr=\d+/, '');
636 href = href.replace(/\&?clone=\d+/, '');
637 var id = cgi.param('usr');
638 if (xulG) if (xulG.usr) id = xulG.usr;
639 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
640 /* reload the current user if available */
641 if( id ) href += "&usr=" + id;
642 location.href = href;
646 var uEditDupHashes = {};
647 var uEditDupTemplate;
649 function uEditRunDupeSearch(type, search_hash) {
651 if(!patron.isnew()) return;
653 _debug('dup search: ' + js2JSON(search_hash));
655 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
657 var container = $('dup_div_container');
658 if(!uEditDupTemplate)
659 uEditDupTemplate = container.removeChild($('dup_div'));
661 /* clear any existing dups for this type */
662 iterate( container.getElementsByTagName('div'),
664 if( d.getAttribute('type') == type ) {
665 container.removeChild(d)
673 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
680 function uEditHandleDupResults(ids, search_hash, type, container) {
682 _debug('dup search results: ' + js2JSON(ids));
684 if(!(ids && ids[0])) /* no results */
685 return uEditDupHashes[type] = null;
687 /* add a dup link to the UI and plug in the data */
688 var node = uEditDupTemplate.cloneNode(true);
689 container.appendChild(node);
690 node.setAttribute('type', type);
692 var link = $n(node, 'link');
693 link.setAttribute('type', type);
695 $n(node,'count').appendChild(text(ids.length));
697 for( var o in search_hash )
698 $n(node, 'data').appendChild(
699 text(search_hash[o].value + ' '));
701 uEditDupHashes[type] = search_hash;
705 if(confirm($('ue_dup_ident1').innerHTML))
706 uEditShowSearch(null, type);
712 function uEditShowSearch(link,type) {
713 if(!type) type = link.getAttribute('type');
715 window.xulG.spawn_search(uEditDupHashes[type]);
716 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
719 function uEditMarkCardLost() {
721 for( var c in patron.cards() ) {
723 var card = patron.cards()[c];
724 if( patron.card().id() == card.id() ) {
726 /* de-activite the current card */
730 if( !card.barcode() ) {
731 /* a card exists in the array with no barcode */
732 ueRemoveCard(card.id());
734 } else if( card.isnew() && card.active() == 0 ) {
735 /* a new card was created, then never used, removing.. */
736 _debug("removing new inactive card "+card.barcode());
737 ueRemoveCard(card.id());
740 /* create a new card for the patron */
741 var newcard = new ac();
742 newcard.id(uEditCardVirtId--);
744 patron.card(newcard);
745 patron.cards().push(newcard);
748 /* reset the widget */
749 var field = uEditFindFieldByWId('ue_barcode');
750 field.widget.node.disabled = false;
751 field.widget.node.value = "";
752 field.widget.node.onchange();
753 field.object = newcard;
754 _debug("uEditMarkCardLost(): created new card object for user");
760 function ueRemoveCard(id) {
761 _debug("removing card from cards() array: " + id);
762 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
764 for( var j = 0; j < cds.length; j++ )
765 _debug("patron card array now has : "+cds[j].id());
771 function compactArray(arr) {
773 for( var i = 0; arr && i < arr.length; i++ ) {