5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
14 //var guardianNote = null;
16 if(!window.xulG) var xulG = null;
18 function $(id) { return document.getElementById(id); }
20 /* fetch the necessary data to start off */
21 function uEditInit() {
23 _debug('uEditInit(): ' + location.search);
26 session = cgi.param('ses');
27 if (xulG) if (xulG.ses) session = xulG.ses;
28 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
29 clone = cgi.param('clone');
30 if (xulG) if (xulG.clone) clone = xulG.clone;
31 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
32 if(!session) throw $("patronStrings").getString('web.staff.patron.ue.session_no_defined');
35 $('uedit_user').appendChild(text(USER.usrname()));
37 setTimeout( function() {
38 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
41 function uEditSetUnload() {
42 _debug('setting window unload event');
44 window.onbeforeunload = function(evt) {
45 return $('ue_unsaved_changes').innerHTML;
50 function uEditClearUnload() {
51 _debug('clearing window unload event');
53 window.onbeforeunload = null;
57 /* ------------------------------------------------------------------------------ */
59 /* ------------------------------------------------------------------------------ */
60 function uEditFetchIdentTypes() {
61 _debug("uEditFetchIdentTypes()");
62 var s = fetchXULStash();
63 if (typeof s.list != 'undefined')
64 if (typeof s.list.cit != 'undefined') return s.list.cit;
65 var req = new Request(FETCH_ID_TYPES);
70 function uEditFetchStatCats() {
71 _debug("uEditFetchStatCats()");
72 var s = fetchXULStash();
73 if (typeof s.list != 'undefined')
74 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
75 var req = new Request(SC_FETCH_ALL, SESSION);
80 function uEditFetchSurveys() {
81 _debug("uEditFetchSurveys()");
82 var s = fetchXULStash();
83 if (typeof s.list != 'undefined')
84 if (typeof s.list.asv != 'undefined') return s.list.asv;
85 var req = new Request(SV_FETCH_ALL, SESSION);
90 function uEditFetchGroups() {
91 _debug("uEditFetchGroups()");
92 var s = fetchXULStash();
93 if (typeof s.tree != 'undefined')
94 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
95 var req = new Request(FETCH_GROUPS);
100 function uEditFetchNetLevels() {
101 _debug("uEditFetchNetLevels()");
102 var s = fetchXULStash();
103 if (typeof s.list != 'undefined')
104 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
105 var req = new Request(FETCH_NET_LEVELS, SESSION);
110 /* ------------------------------------------------------------------------------ */
114 * adds all of the group.application_perm's to the list
115 * provided by descending through the group tree
117 function buildAppPermList(list, group) {
119 if(group.application_perm() )
120 list.push(group.application_perm());
121 for(i in group.children()) {
122 buildAppPermList(list, group.children()[i]);
126 /* fetches necessary objects and builds the UI */
127 function uEditBuild() {
129 myPerms = ['BAR_PATRON', 'UNBAR_PATRON'];
131 /* grab the groups before we check perms so we know what
132 application_perms to check */
133 var groups = uEditFetchGroups();
134 buildAppPermList(myPerms, groups);
136 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
138 uEditBuildLibSelector();
139 var usr = cgi.param('usr');
140 if (xulG) if (xulG.usr) usr = xulG.usr;
141 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
142 patron = fetchFleshedUser(usr);
143 if(!patron) patron = uEditNewPatron();
145 orgSettings = fetchBatchOrgSetting(USER.ws_ou(), ['global.juvenile_age_threshold']);
148 uEditFetchIdentTypes(),
150 uEditFetchStatCats(),
152 uEditFetchNetLevels()
156 if(clone) uEditClone(clone);
157 else uEditCreateNewAddr();
161 /* do we need to display the parent / gurdian field? */
162 uEditCheckDOB(uEditFindFieldByKey('dob'));
164 $('ue_barcode').disabled = true;
165 unHideMe($('ue_mark_card_lost'));
166 unHideMe($('ue_reset_pw'));
167 uEditCheckEditPerm();
170 uEditCheckBarredPerm();
173 function uEditCheckBarredPerm() {
174 if(PERMS['BAR_PATRON'] != -1)
177 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
180 $('ue_barred').disabled = true;
184 /* if this user does not have permission to put users into
185 the edited users group, they do not have permission to
187 function uEditCheckEditPerm() {
189 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
191 _debug("editing user with group app perm "+patron.profile()+' : '+
192 groupsCache[patron.profile()].name() +', and perm = ' + perm);
195 if(PERMS[perm] != -1) return;
197 /* we can edit our own account, but not others in our group */
198 if( patron.id() != USER.id() ){
199 _debug("we are not allowed to edit this user");
201 $('ue_save').disabled = true;
202 $('ue_save_clone').disabled = true;
203 $('ue_mark_card_lost').disabled = true;
204 $('ue_reset_pw').disabled = true;
208 if( f && f.widget && f.widget.node )
209 f.widget.node.disabled = true;
215 var node = $('ue_profile').parentNode;
216 node.removeChild($('ue_profile'));
217 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
219 var field = uEditFindFieldByKey('profile');
220 field.required = false;
221 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
226 /* creates a new patron object with card attached */
227 var uEditCardVirtId = -1;
228 function uEditNewPatron() {
229 var patron = new au();
233 card.id(uEditCardVirtId--);
236 patron.cards([card]);
237 patron.net_access_level(defaultNetLevel);
238 patron.stat_cat_entries([]);
239 patron.survey_responses([]);
240 patron.addresses([]);
241 patron.home_ou(USER.ws_ou());
242 uEditMakeRandomPw(patron);
246 function uEditMakeRandomPw(patron) {
247 var rand = Math.random();
248 rand = parseInt(rand * 10000) + '';
249 while(rand.length < 4) rand += '0';
250 appendClear($('ue_password_plain'),text(rand));
251 unHideMe($('ue_password_gen'));
256 function uEditResetPw() {
257 var pw = uEditMakeRandomPw(patron);
258 $('ue_password1').value = pw;
259 $('ue_password2').value = pw;
260 $('ue_password1').onchange();
263 function uEditClone(clone) {
265 var cloneUser = fetchFleshedUser(clone);
266 patron.usrgroup(cloneUser.usrgroup());
268 if( cloneUser.day_phone() ) {
269 $('ue_day_phone').value = cloneUser.day_phone();
270 $('ue_day_phone').onchange();
273 if( cloneUser.evening_phone() ) {
274 $('ue_night_phone').value = cloneUser.evening_phone();
275 $('ue_night_phone').onchange();
278 if( cloneUser.other_phone() ) {
279 $('ue_other_phone').value = cloneUser.other_phone();
280 $('ue_other_phone').onchange();
283 setSelector($('ue_org_selector'), cloneUser.home_ou());
284 setSelector($('ue_profile'), cloneUser.profile());
286 /* force the expire date to be set */
287 $('ue_profile').onchange();
288 $('ue_org_selector').onchange();
290 for( var a in cloneUser.addresses() ) {
291 var addr = cloneUser.addresses()[a];
292 if( cloneUser.mailing_address &&
293 addr.id() == cloneUser.mailing_address().id() )
294 patron.mailing_address(addr);
295 if( cloneUser.billing_address() &&
296 addr.id() == cloneUser.billing_address().id() )
297 patron.billing_address(addr);
298 patron.addresses().push(addr);
301 uEditBuildAddrs(patron);
305 /* Creates a new blank address,
306 adds it to the user and the fields array */
307 var uEditVirtualAddrId = -1;
308 function uEditCreateNewAddr() {
309 var addr = new aua();
311 addr.id(uEditVirtualAddrId--);
313 addr.usr(patron.id());
314 addr.country(defaultCountry);
316 if(!patron.addresses())
317 patron.addresses([]);
319 if(patron.addresses().length == 0) {
320 patron.mailing_address(addr);
321 patron.billing_address(addr);
325 addr.within_city_limits(1);
327 uEditBuildAddrFields(patron, addr);
328 patron.addresses().push(addr);
329 uEditIterateFields(function(f) { uEditCheckValid(f); });
334 /* kicks off the UI drawing */
335 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
336 hideMe($('uedit_loading'));
337 unHideMe($('ue_maintd'));
340 uEditDrawIDTypes(identTypes);
341 uEditDrawGroups(groups, null, null, true);
342 uEditDrawStatCats(statCats);
343 uEditDrawSurveys(surveys);
344 uEditDrawNetLevels(netLevels);
345 uEditDefineData(patron);
347 uEditIterateFields(function(f) { uEditActivateField(f) });
348 uEditIterateFields(function(f) { uEditCheckValid(f); });
353 /** Applies the event handlers and sets the data for the field */
354 function uEditActivateField(field) {
356 if( field.widget.id ) {
357 field.widget.node = $(field.widget.id);
361 $n(field.widget.base, field.widget.name);
364 uEditSetOnchange(field);
366 if(field.widget.onblur) {
367 field.widget.node.onblur =
368 function() { field.widget.onblur(field); };
371 field.widget.node.disabled = field.widget.disabled;
372 if(field.object == null) return;
373 var val = field.object[field.key]();
374 if(val == null) return;
376 if( field.widget.type == 'input' )
377 field.widget.node.value = val;
379 if( field.widget.type == 'select' )
380 setSelector(field.widget.node, val);
382 if( field.widget.type == 'checkbox' )
383 field.widget.node.checked =
384 (val && val != 'f') ? true : false;
386 if( field.widget.onload )
387 field.widget.onload(val);
391 /* set up the onchange event for the field */
392 function uEditSetOnchange(field) {
393 var func = function() {uEditOnChange( field );}
394 field.widget.node.onchange = func;
396 if(field.widget.type != 'select')
397 field.widget.node.onkeyup = func;
400 /* find the current value of the field object's widget */
401 function uEditNodeVal(field) {
402 if(field.widget.type == 'input')
403 return field.widget.node.value;
405 if(field.widget.type == 'checkbox')
406 return field.widget.node.checked;
408 if(field.widget.type == 'select')
409 return getSelectorVal(field.widget.node);
413 /* update a field value */
414 function uEditOnChange(field) {
416 var newval = uEditNodeVal(field);
417 field.object[field.key](newval);
418 field.object.ischanged(1);
420 if(field.widget.onpostchange)
421 field.widget.onpostchange(field, newval);
423 //_debug(field.key+' = '+newval);
425 uEditIterateFields(function(f) { uEditCheckValid(f); });
432 function uEditCheckValid(field) {
433 var newval = uEditNodeVal(field);
437 if(field.widget.regex) {
438 if(newval.match(field.widget.regex))
439 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
441 addCSSClass(field.widget.node, CSS_INVALID_DATA);
444 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
450 addCSSClass(field.widget.node, CSS_INVALID_DATA);
453 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
459 /* find a field object by object key */
460 function uEditFindFieldByKey(key) {
461 var fields = grep( dataFields,
462 function(item) { return (item.key == key); });
463 return (fields) ? fields[0] : null;
466 /* find a list of fields by object key */
467 function uEditFindFieldsByKey(key) {
468 return grep( dataFields,
469 function(item) { return (item.key == key); });
472 /* find a field object by widget id */
473 function uEditFindFieldByWId(id) {
474 var fields = grep( dataFields,
475 function(item) { return (item.widget.id == id); });
476 return (fields) ? fields[0] : null;
480 function uEditIterateFields(callback) {
481 for( var f in dataFields )
482 callback(dataFields[f]);
486 function uEditGetErrorStrings() {
491 if( !field.object.isdeleted() ) {
492 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
493 var str = $(field.errkey).innerHTML;
494 if(str) errors.push(str);
501 /* munge up something for all of the required surveys
502 (which are not registered with the fields) */
503 if( patron.isnew() ) {
504 var sel = $('ue_survey_table');
507 var rows = sel.getElementsByTagName('tr');
509 for( var r in rows ) {
512 var sel = $n(row, 'ue_survey_answer');
514 var qstn = row.getAttribute('question');
517 qstn = surveyQuestionsCache[qstn];
518 survey = surveysCache[qstn.survey()];
519 var val = getSelectorVal(sel);
520 if(!val && isTrue(survey.required()))
521 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
527 /* ------------------------------------------------------------ */
529 if(errors[0]) return errors;
533 function uEditAlertErrors() {
534 var errors = uEditGetErrorStrings();
535 if(!errors) return false;
536 alert(errors.join("\n"));
541 /* send the user to the database */
542 function uEditSaveUser(cloneme) {
544 if(uEditGetErrorStrings()) {
549 /* null is unique in the db, but '' is not */
550 if( ! patron.ident_value() ) patron.ident_value(null);
551 //if( ! patron.ident_type2() ) patron.ident_type2(null);
552 if( ! patron.ident_value2() ) patron.ident_value2(null);
553 patron.ident_type2(null);
555 if(! patron.dob() ) patron.dob(null);
557 _debug("Saving patron with card: " + js2JSON(patron.card()));
558 _debug("Saving full patron: " + js2JSON(patron));
560 //for( var c in patron
562 var req = new Request(UPDATE_PATRON, SESSION, patron);
563 req.alertEvent = false;
565 var newuser = req.result();
570 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
573 if( evt.textcode == 'XACT_COLLISION' ) {
574 if( confirmId('ue_xact_collision') )
575 location.href = location.href;
578 var j = js2JSON(evt);
580 _debug("USER UPDATE FAILED:\n" + j);
585 alert($('ue_success').innerHTML);
588 /* if the user we just created was a clone, and we want to clone it,
589 we really want to clone the original */
590 if( clone ) cloneme = clone;
591 else cloneme = newuser.id();
598 typeof window.xulG.spawn_editor == 'function' &&
601 _debug("xulG clone spawning new interface...");
602 var ses = cgi.param('ses');
603 if (xulG) if (xulG.ses) ses = xulG.ses;
604 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
605 window.xulG.spawn_editor({ses:ses,clone:cloneme});
610 var href = location.href;
611 href = href.replace(/\&?usr=\d+/, '');
612 href = href.replace(/\&?clone=\d+/, '');
613 href += '&clone=' + cloneme;
614 location.href = href;
622 uEditRefreshXUL(newuser);
626 function uEditRefreshXUL(newuser) {
627 if (window.xulG && typeof window.xulG.on_save == 'function')
628 window.xulG.on_save(newuser);
631 function uEditRefresh() {
632 var href = location.href;
633 href = href.replace(/\&?clone=\d+/, '');
634 location.href = href;
638 function uEditCancel() {
639 var href = location.href;
640 href = href.replace(/\&?usr=\d+/, '');
641 href = href.replace(/\&?clone=\d+/, '');
642 var id = cgi.param('usr');
643 if (xulG) if (xulG.usr) id = xulG.usr;
644 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
645 /* reload the current user if available */
646 if( id ) href += "&usr=" + id;
647 location.href = href;
651 var uEditDupHashes = {};
652 var uEditDupTemplate;
654 function uEditRunDupeSearch(type, search_hash) {
656 if(!patron.isnew()) return;
658 _debug('dup search: ' + js2JSON(search_hash));
660 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
662 var container = $('dup_div_container');
663 if(!uEditDupTemplate)
664 uEditDupTemplate = container.removeChild($('dup_div'));
666 /* clear any existing dups for this type */
667 iterate( container.getElementsByTagName('div'),
669 if( d.getAttribute('type') == type ) {
670 container.removeChild(d)
678 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
685 function uEditHandleDupResults(ids, search_hash, type, container) {
687 _debug('dup search results: ' + js2JSON(ids));
689 if(!(ids && ids[0])) /* no results */
690 return uEditDupHashes[type] = null;
692 /* add a dup link to the UI and plug in the data */
693 var node = uEditDupTemplate.cloneNode(true);
694 container.appendChild(node);
695 node.setAttribute('type', type);
697 var link = $n(node, 'link');
698 link.setAttribute('type', type);
700 $n(node,'count').appendChild(text(ids.length));
702 for( var o in search_hash )
703 $n(node, 'data').appendChild(
704 text(search_hash[o].value + ' '));
706 uEditDupHashes[type] = search_hash;
710 if(confirm($('ue_dup_ident1').innerHTML))
711 uEditShowSearch(null, type);
717 function uEditShowSearch(link,type) {
718 if(!type) type = link.getAttribute('type');
720 window.xulG.spawn_search(uEditDupHashes[type]);
721 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
724 function uEditMarkCardLost() {
726 for( var c in patron.cards() ) {
728 var card = patron.cards()[c];
729 if( patron.card().id() == card.id() ) {
731 /* de-activite the current card */
735 if( !card.barcode() ) {
736 /* a card exists in the array with no barcode */
737 ueRemoveCard(card.id());
739 } else if( card.isnew() && card.active() == 0 ) {
740 /* a new card was created, then never used, removing.. */
741 _debug("removing new inactive card "+card.barcode());
742 ueRemoveCard(card.id());
745 /* create a new card for the patron */
746 var newcard = new ac();
747 newcard.id(uEditCardVirtId--);
749 patron.card(newcard);
750 patron.cards().push(newcard);
753 /* reset the widget */
754 var field = uEditFindFieldByWId('ue_barcode');
755 field.widget.node.disabled = false;
756 field.widget.node.value = "";
757 field.widget.node.onchange();
758 field.object = newcard;
759 _debug("uEditMarkCardLost(): created new card object for user");
765 function ueRemoveCard(id) {
766 _debug("removing card from cards() array: " + id);
767 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
769 for( var j = 0; j < cds.length; j++ )
770 _debug("patron card array now has : "+cds[j].id());
776 function compactArray(arr) {
778 for( var i = 0; arr && i < arr.length; i++ ) {