5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 //var guardianNote = null;
15 if(!window.xulG) var xulG = null;
17 function $(id) { return document.getElementById(id); }
19 /* fetch the necessary data to start off */
20 function uEditInit() {
22 _debug('uEditInit(): ' + location.search);
25 session = cgi.param('ses');
26 if (xulG) if (xulG.ses) session = xulG.ses;
27 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
28 clone = cgi.param('clone');
29 if (xulG) if (xulG.clone) clone = xulG.clone;
30 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
31 if(!session) throw $("patronStrings").getString('web.staff.patron.ue.session_no_defined');
34 $('uedit_user').appendChild(text(USER.usrname()));
36 setTimeout( function() {
37 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
40 function uEditSetUnload() {
41 _debug('setting window unload event');
43 window.onbeforeunload = function(evt) {
44 return $('ue_unsaved_changes').innerHTML;
49 function uEditClearUnload() {
50 _debug('clearing window unload event');
52 window.onbeforeunload = null;
56 /* ------------------------------------------------------------------------------ */
58 /* ------------------------------------------------------------------------------ */
59 function uEditFetchIdentTypes() {
60 _debug("uEditFetchIdentTypes()");
61 var s = fetchXULStash();
62 if (typeof s.list != 'undefined')
63 if (typeof s.list.cit != 'undefined') return s.list.cit;
64 var req = new Request(FETCH_ID_TYPES);
69 function uEditFetchStatCats() {
70 _debug("uEditFetchStatCats()");
71 var s = fetchXULStash();
72 if (typeof s.list != 'undefined')
73 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
74 var req = new Request(SC_FETCH_ALL, SESSION);
79 function uEditFetchSurveys() {
80 _debug("uEditFetchSurveys()");
81 var s = fetchXULStash();
82 if (typeof s.list != 'undefined')
83 if (typeof s.list.asv != 'undefined') return s.list.asv;
84 var req = new Request(SV_FETCH_ALL, SESSION);
89 function uEditFetchGroups() {
90 _debug("uEditFetchGroups()");
91 var s = fetchXULStash();
92 if (typeof s.tree != 'undefined')
93 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
94 var req = new Request(FETCH_GROUPS);
99 function uEditFetchNetLevels() {
100 _debug("uEditFetchNetLevels()");
101 var s = fetchXULStash();
102 if (typeof s.list != 'undefined')
103 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
104 var req = new Request(FETCH_NET_LEVELS, SESSION);
109 /* ------------------------------------------------------------------------------ */
113 * adds all of the group.application_perm's to the list
114 * provided by descending through the group tree
116 function buildAppPermList(list, group) {
118 if(group.application_perm() )
119 list.push(group.application_perm());
120 for(i in group.children()) {
121 buildAppPermList(list, group.children()[i]);
125 /* fetches necessary objects and builds the UI */
126 function uEditBuild() {
128 myPerms = ['BAR_PATRON', 'UNBAR_PATRON'];
130 /* grab the groups before we check perms so we know what
131 application_perms to check */
132 var groups = uEditFetchGroups();
133 buildAppPermList(myPerms, groups);
135 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
137 uEditBuildLibSelector();
138 var usr = cgi.param('usr');
139 if (xulG) if (xulG.usr) usr = xulG.usr;
140 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
141 patron = fetchFleshedUser(usr);
142 if(!patron) patron = uEditNewPatron();
145 uEditFetchIdentTypes(),
147 uEditFetchStatCats(),
149 uEditFetchNetLevels()
153 if(clone) uEditClone(clone);
154 else uEditCreateNewAddr();
158 /* do we need to display the parent / gurdian field? */
159 uEditCheckDOB(uEditFindFieldByKey('dob'));
161 $('ue_barcode').disabled = true;
162 unHideMe($('ue_mark_card_lost'));
163 unHideMe($('ue_reset_pw'));
164 uEditCheckEditPerm();
167 uEditCheckBarredPerm();
170 function uEditCheckBarredPerm() {
171 if(PERMS['BAR_PATRON'] != -1)
174 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
177 $('ue_barred').disabled = true;
181 /* if this user does not have permission to put users into
182 the edited users group, they do not have permission to
184 function uEditCheckEditPerm() {
186 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
188 _debug("editing user with group app perm "+patron.profile()+' : '+
189 groupsCache[patron.profile()].name() +', and perm = ' + perm);
192 if(PERMS[perm] != -1) return;
194 /* we can edit our own account, but not others in our group */
195 if( patron.id() != USER.id() ){
196 _debug("we are not allowed to edit this user");
198 $('ue_save').disabled = true;
199 $('ue_save_clone').disabled = true;
200 $('ue_mark_card_lost').disabled = true;
201 $('ue_reset_pw').disabled = true;
205 if( f && f.widget && f.widget.node )
206 f.widget.node.disabled = true;
212 var node = $('ue_profile').parentNode;
213 node.removeChild($('ue_profile'));
214 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
216 var field = uEditFindFieldByKey('profile');
217 field.required = false;
218 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
223 /* creates a new patron object with card attached */
224 var uEditCardVirtId = -1;
225 function uEditNewPatron() {
226 var patron = new au();
230 card.id(uEditCardVirtId--);
233 patron.cards([card]);
234 patron.net_access_level(defaultNetLevel);
235 patron.stat_cat_entries([]);
236 patron.survey_responses([]);
237 patron.addresses([]);
238 patron.home_ou(USER.ws_ou());
239 uEditMakeRandomPw(patron);
243 function uEditMakeRandomPw(patron) {
244 var rand = Math.random();
245 rand = parseInt(rand * 10000) + '';
246 while(rand.length < 4) rand += '0';
247 appendClear($('ue_password_plain'),text(rand));
248 unHideMe($('ue_password_gen'));
253 function uEditResetPw() {
254 var pw = uEditMakeRandomPw(patron);
255 $('ue_password1').value = pw;
256 $('ue_password2').value = pw;
257 $('ue_password1').onchange();
260 function uEditClone(clone) {
262 var cloneUser = fetchFleshedUser(clone);
263 patron.usrgroup(cloneUser.usrgroup());
265 if( cloneUser.day_phone() ) {
266 $('ue_day_phone').value = cloneUser.day_phone();
267 $('ue_day_phone').onchange();
270 if( cloneUser.evening_phone() ) {
271 $('ue_night_phone').value = cloneUser.evening_phone();
272 $('ue_night_phone').onchange();
275 if( cloneUser.other_phone() ) {
276 $('ue_other_phone').value = cloneUser.other_phone();
277 $('ue_other_phone').onchange();
280 setSelector($('ue_org_selector'), cloneUser.home_ou());
281 setSelector($('ue_profile'), cloneUser.profile());
283 /* force the expire date to be set */
284 $('ue_profile').onchange();
285 $('ue_org_selector').onchange();
287 for( var a in cloneUser.addresses() ) {
288 var addr = cloneUser.addresses()[a];
289 if( cloneUser.mailing_address &&
290 addr.id() == cloneUser.mailing_address().id() )
291 patron.mailing_address(addr);
292 if( cloneUser.billing_address() &&
293 addr.id() == cloneUser.billing_address().id() )
294 patron.billing_address(addr);
295 patron.addresses().push(addr);
298 uEditBuildAddrs(patron);
302 /* Creates a new blank address,
303 adds it to the user and the fields array */
304 var uEditVirtualAddrId = -1;
305 function uEditCreateNewAddr() {
306 var addr = new aua();
308 addr.id(uEditVirtualAddrId--);
310 addr.usr(patron.id());
311 addr.country(defaultCountry);
313 if(!patron.addresses())
314 patron.addresses([]);
316 if(patron.addresses().length == 0) {
317 patron.mailing_address(addr);
318 patron.billing_address(addr);
322 addr.within_city_limits(1);
324 uEditBuildAddrFields(patron, addr);
325 patron.addresses().push(addr);
326 uEditIterateFields(function(f) { uEditCheckValid(f); });
331 /* kicks off the UI drawing */
332 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
333 hideMe($('uedit_loading'));
334 unHideMe($('ue_maintd'));
337 uEditDrawIDTypes(identTypes);
338 uEditDrawGroups(groups, null, null, true);
339 uEditDrawStatCats(statCats);
340 uEditDrawSurveys(surveys);
341 uEditDrawNetLevels(netLevels);
342 uEditDefineData(patron);
344 uEditIterateFields(function(f) { uEditActivateField(f) });
345 uEditIterateFields(function(f) { uEditCheckValid(f); });
350 /** Applies the event handlers and sets the data for the field */
351 function uEditActivateField(field) {
353 if( field.widget.id ) {
354 field.widget.node = $(field.widget.id);
358 $n(field.widget.base, field.widget.name);
361 uEditSetOnchange(field);
363 if(field.widget.onblur) {
364 field.widget.node.onblur =
365 function() { field.widget.onblur(field); };
368 field.widget.node.disabled = field.widget.disabled;
369 if(field.object == null) return;
370 var val = field.object[field.key]();
371 if(val == null) return;
373 if( field.widget.type == 'input' )
374 field.widget.node.value = val;
376 if( field.widget.type == 'select' )
377 setSelector(field.widget.node, val);
379 if( field.widget.type == 'checkbox' )
380 field.widget.node.checked =
381 (val && val != 'f') ? true : false;
383 if( field.widget.onload )
384 field.widget.onload(val);
388 /* set up the onchange event for the field */
389 function uEditSetOnchange(field) {
390 var func = function() {uEditOnChange( field );}
391 field.widget.node.onchange = func;
393 if(field.widget.type != 'select')
394 field.widget.node.onkeyup = func;
397 /* find the current value of the field object's widget */
398 function uEditNodeVal(field) {
399 if(field.widget.type == 'input')
400 return field.widget.node.value;
402 if(field.widget.type == 'checkbox')
403 return field.widget.node.checked;
405 if(field.widget.type == 'select')
406 return getSelectorVal(field.widget.node);
410 /* update a field value */
411 function uEditOnChange(field) {
413 var newval = uEditNodeVal(field);
414 field.object[field.key](newval);
415 field.object.ischanged(1);
417 if(field.widget.onpostchange)
418 field.widget.onpostchange(field, newval);
420 //_debug(field.key+' = '+newval);
422 uEditIterateFields(function(f) { uEditCheckValid(f); });
429 function uEditCheckValid(field) {
430 var newval = uEditNodeVal(field);
434 if(field.widget.regex) {
435 if(newval.match(field.widget.regex))
436 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
438 addCSSClass(field.widget.node, CSS_INVALID_DATA);
441 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
447 addCSSClass(field.widget.node, CSS_INVALID_DATA);
450 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
456 /* find a field object by object key */
457 function uEditFindFieldByKey(key) {
458 var fields = grep( dataFields,
459 function(item) { return (item.key == key); });
460 return (fields) ? fields[0] : null;
463 /* find a list of fields by object key */
464 function uEditFindFieldsByKey(key) {
465 return grep( dataFields,
466 function(item) { return (item.key == key); });
469 /* find a field object by widget id */
470 function uEditFindFieldByWId(id) {
471 var fields = grep( dataFields,
472 function(item) { return (item.widget.id == id); });
473 return (fields) ? fields[0] : null;
477 function uEditIterateFields(callback) {
478 for( var f in dataFields )
479 callback(dataFields[f]);
483 function uEditGetErrorStrings() {
488 if( !field.object.isdeleted() ) {
489 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
490 var str = $(field.errkey).innerHTML;
491 if(str) errors.push(str);
498 /* munge up something for all of the required surveys
499 (which are not registered with the fields) */
500 if( patron.isnew() ) {
501 var sel = $('ue_survey_table');
504 var rows = sel.getElementsByTagName('tr');
506 for( var r in rows ) {
509 var sel = $n(row, 'ue_survey_answer');
511 var qstn = row.getAttribute('question');
514 qstn = surveyQuestionsCache[qstn];
515 survey = surveysCache[qstn.survey()];
516 var val = getSelectorVal(sel);
517 if(!val && isTrue(survey.required()))
518 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
524 /* ------------------------------------------------------------ */
526 if(errors[0]) return errors;
530 function uEditAlertErrors() {
531 var errors = uEditGetErrorStrings();
532 if(!errors) return false;
533 alert(errors.join("\n"));
538 /* send the user to the database */
539 function uEditSaveUser(cloneme) {
541 if(uEditGetErrorStrings()) {
546 /* null is unique in the db, but '' is not */
547 if( ! patron.ident_value() ) patron.ident_value(null);
548 //if( ! patron.ident_type2() ) patron.ident_type2(null);
549 if( ! patron.ident_value2() ) patron.ident_value2(null);
550 patron.ident_type2(null);
552 if(! patron.dob() ) patron.dob(null);
554 _debug("Saving patron with card: " + js2JSON(patron.card()));
555 _debug("Saving full patron: " + js2JSON(patron));
557 //for( var c in patron
559 var req = new Request(UPDATE_PATRON, SESSION, patron);
560 req.alertEvent = false;
562 var newuser = req.result();
567 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
570 if( evt.textcode == 'XACT_COLLISION' ) {
571 if( confirmId('ue_xact_collision') )
572 location.href = location.href;
575 var j = js2JSON(evt);
577 _debug("USER UPDATE FAILED:\n" + j);
582 alert($('ue_success').innerHTML);
585 /* if the user we just created was a clone, and we want to clone it,
586 we really want to clone the original */
587 if( clone ) cloneme = clone;
588 else cloneme = newuser.id();
595 typeof window.xulG.spawn_editor == 'function' &&
598 _debug("xulG clone spawning new interface...");
599 var ses = cgi.param('ses');
600 if (xulG) if (xulG.ses) ses = xulG.ses;
601 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
602 window.xulG.spawn_editor({ses:ses,clone:cloneme});
607 var href = location.href;
608 href = href.replace(/\&?usr=\d+/, '');
609 href = href.replace(/\&?clone=\d+/, '');
610 href += '&clone=' + cloneme;
611 location.href = href;
619 uEditRefreshXUL(newuser);
623 function uEditRefreshXUL(newuser) {
624 if (window.xulG && typeof window.xulG.on_save == 'function')
625 window.xulG.on_save(newuser);
628 function uEditRefresh() {
629 var href = location.href;
630 href = href.replace(/\&?clone=\d+/, '');
631 location.href = href;
635 function uEditCancel() {
636 var href = location.href;
637 href = href.replace(/\&?usr=\d+/, '');
638 href = href.replace(/\&?clone=\d+/, '');
639 var id = cgi.param('usr');
640 if (xulG) if (xulG.usr) id = xulG.usr;
641 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
642 /* reload the current user if available */
643 if( id ) href += "&usr=" + id;
644 location.href = href;
648 var uEditDupHashes = {};
649 var uEditDupTemplate;
651 function uEditRunDupeSearch(type, search_hash) {
653 if(!patron.isnew()) return;
655 _debug('dup search: ' + js2JSON(search_hash));
657 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
659 var container = $('dup_div_container');
660 if(!uEditDupTemplate)
661 uEditDupTemplate = container.removeChild($('dup_div'));
663 /* clear any existing dups for this type */
664 iterate( container.getElementsByTagName('div'),
666 if( d.getAttribute('type') == type ) {
667 container.removeChild(d)
675 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
682 function uEditHandleDupResults(ids, search_hash, type, container) {
684 _debug('dup search results: ' + js2JSON(ids));
686 if(!(ids && ids[0])) /* no results */
687 return uEditDupHashes[type] = null;
689 /* add a dup link to the UI and plug in the data */
690 var node = uEditDupTemplate.cloneNode(true);
691 container.appendChild(node);
692 node.setAttribute('type', type);
694 var link = $n(node, 'link');
695 link.setAttribute('type', type);
697 $n(node,'count').appendChild(text(ids.length));
699 for( var o in search_hash )
700 $n(node, 'data').appendChild(
701 text(search_hash[o].value + ' '));
703 uEditDupHashes[type] = search_hash;
707 if(confirm($('ue_dup_ident1').innerHTML))
708 uEditShowSearch(null, type);
714 function uEditShowSearch(link,type) {
715 if(!type) type = link.getAttribute('type');
717 window.xulG.spawn_search(uEditDupHashes[type]);
718 else alert($("patronStrings").getString('web.staff.patron.ue.uedit_show_search.search_would_be', js2JSON(uEditDupHashes[type])));
721 function uEditMarkCardLost() {
723 for( var c in patron.cards() ) {
725 var card = patron.cards()[c];
726 if( patron.card().id() == card.id() ) {
728 /* de-activite the current card */
732 if( !card.barcode() ) {
733 /* a card exists in the array with no barcode */
734 ueRemoveCard(card.id());
736 } else if( card.isnew() && card.active() == 0 ) {
737 /* a new card was created, then never used, removing.. */
738 _debug("removing new inactive card "+card.barcode());
739 ueRemoveCard(card.id());
742 /* create a new card for the patron */
743 var newcard = new ac();
744 newcard.id(uEditCardVirtId--);
746 patron.card(newcard);
747 patron.cards().push(newcard);
750 /* reset the widget */
751 var field = uEditFindFieldByWId('ue_barcode');
752 field.widget.node.disabled = false;
753 field.widget.node.value = "";
754 field.widget.node.onchange();
755 field.object = newcard;
756 _debug("uEditMarkCardLost(): created new card object for user");
762 function ueRemoveCard(id) {
763 _debug("removing card from cards() array: " + id);
764 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
766 for( var j = 0; j < cds.length; j++ )
767 _debug("patron card array now has : "+cds[j].id());
773 function compactArray(arr) {
775 for( var i = 0; arr && i < arr.length; i++ ) {