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.stat_cat_entries([]);
233 patron.survey_responses([]);
234 patron.addresses([]);
235 patron.home_ou(USER.ws_ou());
236 uEditMakeRandomPw(patron);
240 function uEditMakeRandomPw(patron) {
241 var rand = Math.random();
242 rand = parseInt(rand * 10000) + '';
243 while(rand.length < 4) rand += '0';
244 appendClear($('ue_password_plain'),text(rand));
245 unHideMe($('ue_password_gen'));
250 function uEditResetPw() {
251 var pw = uEditMakeRandomPw(patron);
252 $('ue_password1').value = pw;
253 $('ue_password2').value = pw;
254 $('ue_password1').onchange();
257 function uEditClone(clone) {
259 var cloneUser = fetchFleshedUser(clone);
260 patron.usrgroup(cloneUser.usrgroup());
262 if( cloneUser.day_phone() ) {
263 $('ue_day_phone').value = cloneUser.day_phone();
264 $('ue_day_phone').onchange();
267 if( cloneUser.evening_phone() ) {
268 $('ue_night_phone').value = cloneUser.evening_phone();
269 $('ue_night_phone').onchange();
272 if( cloneUser.other_phone() ) {
273 $('ue_other_phone').value = cloneUser.other_phone();
274 $('ue_other_phone').onchange();
277 setSelector($('ue_org_selector'), cloneUser.home_ou());
278 setSelector($('ue_profile'), cloneUser.profile());
280 /* force the expire date to be set */
281 $('ue_profile').onchange();
282 $('ue_org_selector').onchange();
284 for( var a in cloneUser.addresses() ) {
285 var addr = cloneUser.addresses()[a];
286 if( cloneUser.mailing_address &&
287 addr.id() == cloneUser.mailing_address().id() )
288 patron.mailing_address(addr);
289 if( cloneUser.billing_address() &&
290 addr.id() == cloneUser.billing_address().id() )
291 patron.billing_address(addr);
292 patron.addresses().push(addr);
295 uEditBuildAddrs(patron);
299 /* Creates a new blank address,
300 adds it to the user and the fields array */
301 var uEditVirtualAddrId = -1;
302 function uEditCreateNewAddr() {
303 var addr = new aua();
305 addr.id(uEditVirtualAddrId--);
307 addr.usr(patron.id());
308 addr.country(defaultCountry);
310 if(!patron.addresses())
311 patron.addresses([]);
313 if(patron.addresses().length == 0) {
314 patron.mailing_address(addr);
315 patron.billing_address(addr);
319 addr.within_city_limits(1);
321 uEditBuildAddrFields(patron, addr);
322 patron.addresses().push(addr);
323 uEditIterateFields(function(f) { uEditCheckValid(f); });
328 /* kicks off the UI drawing */
329 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
330 hideMe($('uedit_loading'));
331 unHideMe($('ue_maintd'));
334 uEditDrawIDTypes(identTypes);
335 uEditDrawGroups(groups, null, null, true);
336 uEditDrawStatCats(statCats);
337 uEditDrawSurveys(surveys);
338 uEditDrawNetLevels(netLevels);
339 uEditDefineData(patron);
341 uEditIterateFields(function(f) { uEditActivateField(f) });
342 uEditIterateFields(function(f) { uEditCheckValid(f); });
347 /** Applies the event handlers and sets the data for the field */
348 function uEditActivateField(field) {
350 if( field.widget.id ) {
351 field.widget.node = $(field.widget.id);
355 $n(field.widget.base, field.widget.name);
358 uEditSetOnchange(field);
360 if(field.widget.onblur) {
361 field.widget.node.onblur =
362 function() { field.widget.onblur(field); };
365 field.widget.node.disabled = field.widget.disabled;
366 if(field.object == null) return;
367 var val = field.object[field.key]();
368 if(val == null) return;
370 if( field.widget.type == 'input' )
371 field.widget.node.value = val;
373 if( field.widget.type == 'select' )
374 setSelector(field.widget.node, val);
376 if( field.widget.type == 'checkbox' )
377 field.widget.node.checked =
378 (val && val != 'f') ? true : false;
380 if( field.widget.onload )
381 field.widget.onload(val);
385 /* set up the onchange event for the field */
386 function uEditSetOnchange(field) {
387 var func = function() {uEditOnChange( field );}
388 field.widget.node.onchange = func;
390 if(field.widget.type != 'select')
391 field.widget.node.onkeyup = func;
394 /* find the current value of the field object's widget */
395 function uEditNodeVal(field) {
396 if(field.widget.type == 'input')
397 return field.widget.node.value;
399 if(field.widget.type == 'checkbox')
400 return field.widget.node.checked;
402 if(field.widget.type == 'select')
403 return getSelectorVal(field.widget.node);
407 /* update a field value */
408 function uEditOnChange(field) {
410 var newval = uEditNodeVal(field);
411 field.object[field.key](newval);
412 field.object.ischanged(1);
414 if(field.widget.onpostchange)
415 field.widget.onpostchange(field, newval);
417 //_debug(field.key+' = '+newval);
419 uEditIterateFields(function(f) { uEditCheckValid(f); });
426 function uEditCheckValid(field) {
427 var newval = uEditNodeVal(field);
431 if(field.widget.regex) {
432 if(newval.match(field.widget.regex))
433 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
435 addCSSClass(field.widget.node, CSS_INVALID_DATA);
438 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
444 addCSSClass(field.widget.node, CSS_INVALID_DATA);
447 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
453 /* find a field object by object key */
454 function uEditFindFieldByKey(key) {
455 var fields = grep( dataFields,
456 function(item) { return (item.key == key); });
457 return (fields) ? fields[0] : null;
460 /* find a list of fields by object key */
461 function uEditFindFieldsByKey(key) {
462 return grep( dataFields,
463 function(item) { return (item.key == key); });
466 /* find a field object by widget id */
467 function uEditFindFieldByWId(id) {
468 var fields = grep( dataFields,
469 function(item) { return (item.widget.id == id); });
470 return (fields) ? fields[0] : null;
474 function uEditIterateFields(callback) {
475 for( var f in dataFields )
476 callback(dataFields[f]);
480 function uEditGetErrorStrings() {
485 if( !field.object.isdeleted() ) {
486 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
487 var str = $(field.errkey).innerHTML;
488 if(str) errors.push(str);
495 /* munge up something for all of the required surveys
496 (which are not registered with the fields) */
497 if( patron.isnew() ) {
498 var sel = $('ue_survey_table');
501 var rows = sel.getElementsByTagName('tr');
503 for( var r in rows ) {
506 var sel = $n(row, 'ue_survey_answer');
508 var qstn = row.getAttribute('question');
511 qstn = surveyQuestionsCache[qstn];
512 survey = surveysCache[qstn.survey()];
513 var val = getSelectorVal(sel);
514 if(!val && isTrue(survey.required()))
515 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
521 /* ------------------------------------------------------------ */
523 if(errors[0]) return errors;
527 function uEditAlertErrors() {
528 var errors = uEditGetErrorStrings();
529 if(!errors) return false;
530 alert(errors.join("\n"));
535 /* send the user to the database */
536 function uEditSaveUser(cloneme) {
538 if(uEditGetErrorStrings()) {
543 /* null is unique in the db, but '' is not */
544 if( ! patron.ident_value() ) patron.ident_value(null);
545 //if( ! patron.ident_type2() ) patron.ident_type2(null);
546 if( ! patron.ident_value2() ) patron.ident_value2(null);
547 patron.ident_type2(null);
549 if(! patron.dob() ) patron.dob(null);
551 _debug("Saving patron with card: " + js2JSON(patron.card()));
552 _debug("Saving full patron: " + js2JSON(patron));
554 //for( var c in patron
556 var req = new Request(UPDATE_PATRON, SESSION, patron);
557 req.alertEvent = false;
559 var newuser = req.result();
564 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
567 if( evt.textcode == 'XACT_COLLISION' ) {
568 if( confirmId('ue_xact_collision') )
569 location.href = location.href;
572 var j = js2JSON(evt);
574 _debug("USER UPDATE FAILED:\n" + j);
579 alert($('ue_success').innerHTML);
582 /* if the user we just created was a clone, and we want to clone it,
583 we really want to clone the original */
584 if( clone ) cloneme = clone;
585 else cloneme = newuser.id();
592 typeof window.xulG.spawn_editor == 'function' &&
595 _debug("xulG clone spawning new interface...");
596 var ses = cgi.param('ses');
597 if (xulG) if (xulG.ses) ses = xulG.ses;
598 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
599 window.xulG.spawn_editor({ses:ses,clone:cloneme});
604 var href = location.href;
605 href = href.replace(/\&?usr=\d+/, '');
606 href = href.replace(/\&?clone=\d+/, '');
607 href += '&clone=' + cloneme;
608 location.href = href;
616 uEditRefreshXUL(newuser);
620 function uEditRefreshXUL(newuser) {
621 if (window.xulG && typeof window.xulG.on_save == 'function')
622 window.xulG.on_save(newuser);
625 function uEditRefresh() {
626 var href = location.href;
627 href = href.replace(/\&?clone=\d+/, '');
628 location.href = href;
632 function uEditCancel() {
633 var href = location.href;
634 href = href.replace(/\&?usr=\d+/, '');
635 href = href.replace(/\&?clone=\d+/, '');
636 var id = cgi.param('usr');
637 if (xulG) if (xulG.usr) id = xulG.usr;
638 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
639 /* reload the current user if available */
640 if( id ) href += "&usr=" + id;
641 location.href = href;
645 var uEditDupHashes = {};
646 var uEditDupTemplate;
648 function uEditRunDupeSearch(type, search_hash) {
650 if(!patron.isnew()) return;
652 _debug('dup search: ' + js2JSON(search_hash));
654 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
656 var container = $('dup_div_container');
657 if(!uEditDupTemplate)
658 uEditDupTemplate = container.removeChild($('dup_div'));
660 /* clear any existing dups for this type */
661 iterate( container.getElementsByTagName('div'),
663 if( d.getAttribute('type') == type ) {
664 container.removeChild(d)
672 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
679 function uEditHandleDupResults(ids, search_hash, type, container) {
681 _debug('dup search results: ' + js2JSON(ids));
683 if(!(ids && ids[0])) /* no results */
684 return uEditDupHashes[type] = null;
686 /* add a dup link to the UI and plug in the data */
687 var node = uEditDupTemplate.cloneNode(true);
688 container.appendChild(node);
689 node.setAttribute('type', type);
691 var link = $n(node, 'link');
692 link.setAttribute('type', type);
694 $n(node,'count').appendChild(text(ids.length));
696 for( var o in search_hash )
697 $n(node, 'data').appendChild(
698 text(search_hash[o].value + ' '));
700 uEditDupHashes[type] = search_hash;
704 if(confirm($('ue_dup_ident1').innerHTML))
705 uEditShowSearch(null, type);
711 function uEditShowSearch(link,type) {
712 if(!type) type = link.getAttribute('type');
714 window.xulG.spawn_search(uEditDupHashes[type]);
715 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
718 function uEditMarkCardLost() {
720 for( var c in patron.cards() ) {
722 var card = patron.cards()[c];
723 if( patron.card().id() == card.id() ) {
725 /* de-activite the current card */
729 if( !card.barcode() ) {
730 /* a card exists in the array with no barcode */
731 ueRemoveCard(card.id());
733 } else if( card.isnew() && card.active() == 0 ) {
734 /* a new card was created, then never used, removing.. */
735 _debug("removing new inactive card "+card.barcode());
736 ueRemoveCard(card.id());
739 /* create a new card for the patron */
740 var newcard = new ac();
741 newcard.id(uEditCardVirtId--);
743 patron.card(newcard);
744 patron.cards().push(newcard);
747 /* reset the widget */
748 var field = uEditFindFieldByWId('ue_barcode');
749 field.widget.node.disabled = false;
750 field.widget.node.value = "";
751 field.widget.node.onchange();
752 field.object = newcard;
753 _debug("uEditMarkCardLost(): created new card object for user");
759 function ueRemoveCard(id) {
760 _debug("removing card from cards() array: " + id);
761 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
763 for( var j = 0; j < cds.length; j++ )
764 _debug("patron card array now has : "+cds[j].id());
770 function compactArray(arr) {
772 for( var i = 0; arr && i < arr.length; i++ ) {