5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 //var guardianNote = null;
15 /* fetch the necessary data to start off */
16 function uEditInit() {
18 _debug('uEditInit(): ' + location.search);
21 session = cgi.param('ses');
22 if (xulG) if (xulG.ses) session = xulG.ses;
23 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
24 clone = cgi.param('clone');
25 if (xulG) if (xulG.clone) clone = xulG.clone;
26 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
27 if(!session) throw "User session is not defined";
30 $('uedit_user').appendChild(text(USER.usrname()));
32 setTimeout( function() {
33 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
36 function uEditSetUnload() {
37 _debug('setting window unload event');
39 window.onbeforeunload = function(evt) {
40 return $('ue_unsaved_changes').innerHTML;
45 function uEditClearUnload() {
46 _debug('clearing window unload event');
48 window.onbeforeunload = null;
52 /* ------------------------------------------------------------------------------ */
54 /* ------------------------------------------------------------------------------ */
55 function uEditFetchIdentTypes() {
56 _debug("uEditFetchIdentTypes()");
57 var s = fetchXULStash();
58 if (typeof s.list != 'undefined')
59 if (typeof s.list.cit != 'undefined') return s.list.cit;
60 var req = new Request(FETCH_ID_TYPES);
65 function uEditFetchStatCats() {
66 _debug("uEditFetchStatCats()");
67 var s = fetchXULStash();
68 if (typeof s.list != 'undefined')
69 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
70 var req = new Request(SC_FETCH_ALL, SESSION);
75 function uEditFetchSurveys() {
76 _debug("uEditFetchSurveys()");
77 var s = fetchXULStash();
78 if (typeof s.list != 'undefined')
79 if (typeof s.list.asv != 'undefined') return s.list.asv;
80 var req = new Request(SV_FETCH_ALL, SESSION);
85 function uEditFetchGroups() {
86 _debug("uEditFetchGroups()");
87 var s = fetchXULStash();
88 if (typeof s.tree != 'undefined')
89 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
90 var req = new Request(FETCH_GROUPS);
95 function uEditFetchNetLevels() {
96 _debug("uEditFetchNetLevels()");
97 var s = fetchXULStash();
98 if (typeof s.list != 'undefined')
99 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
100 var req = new Request(FETCH_NET_LEVELS, SESSION);
105 /* ------------------------------------------------------------------------------ */
109 * adds all of the group.application_perm's to the list
110 * provided by descending through the group tree
112 function buildAppPermList(list, group) {
114 if(group.application_perm() )
115 list.push(group.application_perm());
116 for(i in group.children()) {
117 buildAppPermList(list, group.children()[i]);
121 /* fetches necessary objects and builds the UI */
122 function uEditBuild() {
124 myPerms = ['BAR_PATRON'];
126 /* grab the groups before we check perms so we know what
127 application_perms to check */
128 var groups = uEditFetchGroups();
129 buildAppPermList(myPerms, groups);
131 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
133 uEditBuildLibSelector();
134 var usr = cgi.param('usr');
135 if (xulG) if (xulG.usr) usr = xulG.usr;
136 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
137 patron = fetchFleshedUser(usr);
138 if(!patron) patron = uEditNewPatron();
141 uEditFetchIdentTypes(),
143 uEditFetchStatCats(),
145 uEditFetchNetLevels()
149 if(clone) uEditClone(clone);
150 else uEditCreateNewAddr();
154 /* do we need to display the parent / gurdian field? */
155 uEditCheckDOB(uEditFindFieldByKey('dob'));
157 $('ue_barcode').disabled = true;
158 unHideMe($('ue_mark_card_lost'));
159 unHideMe($('ue_reset_pw'));
160 uEditCheckEditPerm();
163 if(PERMS['BAR_PATRON'] == -1)
164 $('ue_barred').disabled = true;
168 /* if this user does not have permission to put users into
169 the edited users group, they do not have permission to
171 function uEditCheckEditPerm() {
173 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
175 _debug("editing user with group app perm "+patron.profile()+' : '+
176 groupsCache[patron.profile()].name() +', and perm = ' + perm);
179 if(PERMS[perm] != -1) return;
181 /* we can edit our own account, but not others in our group */
182 if( patron.id() != USER.id() ){
183 _debug("we are not allowed to edit this user");
185 $('ue_save').disabled = true;
186 $('ue_save_clone').disabled = true;
187 $('ue_mark_card_lost').disabled = true;
188 $('ue_reset_pw').disabled = true;
192 if( f && f.widget && f.widget.node )
193 f.widget.node.disabled = true;
199 var node = $('ue_profile').parentNode;
200 node.removeChild($('ue_profile'));
201 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
203 var field = uEditFindFieldByKey('profile');
204 field.required = false;
205 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
210 /* creates a new patron object with card attached */
211 var uEditCardVirtId = -1;
212 function uEditNewPatron() {
213 var patron = new au();
217 card.id(uEditCardVirtId--);
220 patron.cards([card]);
221 patron.stat_cat_entries([]);
222 patron.survey_responses([]);
223 patron.addresses([]);
224 patron.home_ou(USER.ws_ou());
225 uEditMakeRandomPw(patron);
229 function uEditMakeRandomPw(patron) {
230 var rand = Math.random();
231 rand = parseInt(rand * 10000) + '';
232 while(rand.length < 4) rand += '0';
233 appendClear($('ue_password_plain'),text(rand));
234 unHideMe($('ue_password_gen'));
239 function uEditResetPw() {
240 var pw = uEditMakeRandomPw(patron);
241 $('ue_password1').value = pw;
242 $('ue_password2').value = pw;
243 $('ue_password1').onchange();
246 function uEditClone(clone) {
248 var cloneUser = fetchFleshedUser(clone);
249 patron.usrgroup(cloneUser.usrgroup());
251 if( cloneUser.day_phone() ) {
252 $('ue_day_phone').value = cloneUser.day_phone();
253 $('ue_day_phone').onchange();
256 if( cloneUser.evening_phone() ) {
257 $('ue_night_phone').value = cloneUser.evening_phone();
258 $('ue_night_phone').onchange();
261 if( cloneUser.other_phone() ) {
262 $('ue_other_phone').value = cloneUser.other_phone();
263 $('ue_other_phone').onchange();
266 setSelector($('ue_org_selector'), cloneUser.home_ou());
267 setSelector($('ue_profile'), cloneUser.profile());
269 /* force the expire date to be set */
270 $('ue_profile').onchange();
271 $('ue_org_selector').onchange();
273 for( var a in cloneUser.addresses() ) {
274 var addr = cloneUser.addresses()[a];
275 if( cloneUser.mailing_address &&
276 addr.id() == cloneUser.mailing_address().id() )
277 patron.mailing_address(addr);
278 if( cloneUser.billing_address() &&
279 addr.id() == cloneUser.billing_address().id() )
280 patron.billing_address(addr);
281 patron.addresses().push(addr);
284 uEditBuildAddrs(patron);
288 /* Creates a new blank address,
289 adds it to the user and the fields array */
290 var uEditVirtualAddrId = -1;
291 function uEditCreateNewAddr() {
292 var addr = new aua();
294 addr.id(uEditVirtualAddrId--);
296 addr.usr(patron.id());
297 addr.country(defaultCountry);
299 if(!patron.addresses())
300 patron.addresses([]);
302 if(patron.addresses().length == 0) {
303 patron.mailing_address(addr);
304 patron.billing_address(addr);
308 addr.within_city_limits(1);
310 uEditBuildAddrFields(patron, addr);
311 patron.addresses().push(addr);
312 uEditIterateFields(function(f) { uEditCheckValid(f); });
317 /* kicks off the UI drawing */
318 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
319 hideMe($('uedit_loading'));
320 unHideMe($('ue_maintd'));
323 uEditDrawIDTypes(identTypes);
324 uEditDrawGroups(groups, null, null, true);
325 uEditDrawStatCats(statCats);
326 uEditDrawSurveys(surveys);
327 uEditDrawNetLevels(netLevels);
328 uEditDefineData(patron);
330 uEditIterateFields(function(f) { uEditActivateField(f) });
331 uEditIterateFields(function(f) { uEditCheckValid(f); });
336 /** Applies the event handlers and sets the data for the field */
337 function uEditActivateField(field) {
339 if( field.widget.id ) {
340 field.widget.node = $(field.widget.id);
344 $n(field.widget.base, field.widget.name);
347 uEditSetOnchange(field);
349 if(field.widget.onblur) {
350 field.widget.node.onblur =
351 function() { field.widget.onblur(field); };
354 field.widget.node.disabled = field.widget.disabled;
355 if(field.object == null) return;
356 var val = field.object[field.key]();
357 if(val == null) return;
359 if( field.widget.type == 'input' )
360 field.widget.node.value = val;
362 if( field.widget.type == 'select' )
363 setSelector(field.widget.node, val);
365 if( field.widget.type == 'checkbox' )
366 field.widget.node.checked =
367 (val && val != 'f') ? true : false;
369 if( field.widget.onload )
370 field.widget.onload(val);
374 /* set up the onchange event for the field */
375 function uEditSetOnchange(field) {
376 var func = function() {uEditOnChange( field );}
377 field.widget.node.onchange = func;
379 if(field.widget.type != 'select')
380 field.widget.node.onkeyup = func;
383 /* find the current value of the field object's widget */
384 function uEditNodeVal(field) {
385 if(field.widget.type == 'input')
386 return field.widget.node.value;
388 if(field.widget.type == 'checkbox')
389 return field.widget.node.checked;
391 if(field.widget.type == 'select')
392 return getSelectorVal(field.widget.node);
396 /* update a field value */
397 function uEditOnChange(field) {
399 var newval = uEditNodeVal(field);
400 field.object[field.key](newval);
401 field.object.ischanged(1);
403 if(field.widget.onpostchange)
404 field.widget.onpostchange(field, newval);
406 //_debug(field.key+' = '+newval);
408 uEditIterateFields(function(f) { uEditCheckValid(f); });
415 function uEditCheckValid(field) {
416 var newval = uEditNodeVal(field);
420 if(field.widget.regex) {
421 if(newval.match(field.widget.regex))
422 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
424 addCSSClass(field.widget.node, CSS_INVALID_DATA);
427 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
433 addCSSClass(field.widget.node, CSS_INVALID_DATA);
436 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
442 /* find a field object by object key */
443 function uEditFindFieldByKey(key) {
444 var fields = grep( dataFields,
445 function(item) { return (item.key == key); });
446 return (fields) ? fields[0] : null;
449 /* find a list of fields by object key */
450 function uEditFindFieldsByKey(key) {
451 return grep( dataFields,
452 function(item) { return (item.key == key); });
455 /* find a field object by widget id */
456 function uEditFindFieldByWId(id) {
457 var fields = grep( dataFields,
458 function(item) { return (item.widget.id == id); });
459 return (fields) ? fields[0] : null;
463 function uEditIterateFields(callback) {
464 for( var f in dataFields )
465 callback(dataFields[f]);
469 function uEditGetErrorStrings() {
474 if( !field.object.isdeleted() ) {
475 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
476 var str = $(field.errkey).innerHTML;
477 if(str) errors.push(str);
484 /* munge up something for all of the required surveys
485 (which are not registered with the fields) */
486 if( patron.isnew() ) {
487 var sel = $('ue_survey_table');
490 var rows = sel.getElementsByTagName('tr');
492 for( var r in rows ) {
495 var sel = $n(row, 'ue_survey_answer');
497 var qstn = row.getAttribute('question');
500 qstn = surveyQuestionsCache[qstn];
501 survey = surveysCache[qstn.survey()];
502 var val = getSelectorVal(sel);
503 if(!val && isTrue(survey.required()))
504 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
510 /* ------------------------------------------------------------ */
512 if(errors[0]) return errors;
516 function uEditAlertErrors() {
517 var errors = uEditGetErrorStrings();
518 if(!errors) return false;
519 alert(errors.join("\n"));
524 /* send the user to the database */
525 function uEditSaveUser(cloneme) {
527 if(uEditGetErrorStrings()) {
532 /* null is unique in the db, but '' is not */
533 if( ! patron.ident_value() ) patron.ident_value(null);
534 //if( ! patron.ident_type2() ) patron.ident_type2(null);
535 if( ! patron.ident_value2() ) patron.ident_value2(null);
536 patron.ident_type2(null);
538 if(! patron.dob() ) patron.dob(null);
540 _debug("Saving patron with card: " + js2JSON(patron.card()));
541 _debug("Saving full patron: " + js2JSON(patron));
543 //for( var c in patron
545 var req = new Request(UPDATE_PATRON, SESSION, patron);
546 req.alertEvent = false;
548 var newuser = req.result();
553 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
556 if( evt.textcode == 'XACT_COLLISION' ) {
557 if( confirmId('ue_xact_collision') )
558 location.href = location.href;
561 var j = js2JSON(evt);
563 _debug("USER UPDATE FAILED:\n" + j);
568 alert($('ue_success').innerHTML);
571 /* if the user we just created was a clone, and we want to clone it,
572 we really want to clone the original */
573 if( clone ) cloneme = clone;
574 else cloneme = newuser.id();
581 typeof window.xulG.spawn_editor == 'function' &&
584 _debug("xulG clone spawning new interface...");
585 var ses = cgi.param('ses');
586 if (xulG) if (xulG.ses) ses = xulG.ses;
587 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
588 window.xulG.spawn_editor({ses:ses,clone:cloneme});
593 var href = location.href;
594 href = href.replace(/\&?usr=\d+/, '');
595 href = href.replace(/\&?clone=\d+/, '');
596 href += '&clone=' + cloneme;
597 location.href = href;
605 uEditRefreshXUL(newuser);
609 function uEditRefreshXUL(newuser) {
610 if (window.xulG && typeof window.xulG.on_save == 'function')
611 window.xulG.on_save(newuser);
614 function uEditRefresh() {
615 var href = location.href;
616 href = href.replace(/\&?clone=\d+/, '');
617 location.href = href;
621 function uEditCancel() {
622 var href = location.href;
623 href = href.replace(/\&?usr=\d+/, '');
624 href = href.replace(/\&?clone=\d+/, '');
625 var id = cgi.param('usr');
626 if (xulG) if (xulG.usr) id = xulG.usr;
627 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
628 /* reload the current user if available */
629 if( id ) href += "&usr=" + id;
630 location.href = href;
634 var uEditDupHashes = {};
635 var uEditDupTemplate;
637 function uEditRunDupeSearch(type, search_hash) {
639 if(!patron.isnew()) return;
641 _debug('dup search: ' + js2JSON(search_hash));
643 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
645 var container = $('dup_div_container');
646 if(!uEditDupTemplate)
647 uEditDupTemplate = container.removeChild($('dup_div'));
649 /* clear any existing dups for this type */
650 iterate( container.getElementsByTagName('div'),
652 if( d.getAttribute('type') == type ) {
653 container.removeChild(d)
661 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
668 function uEditHandleDupResults(ids, search_hash, type, container) {
670 _debug('dup search results: ' + js2JSON(ids));
672 if(!(ids && ids[0])) /* no results */
673 return uEditDupHashes[type] = null;
675 /* add a dup link to the UI and plug in the data */
676 var node = uEditDupTemplate.cloneNode(true);
677 container.appendChild(node);
678 node.setAttribute('type', type);
680 var link = $n(node, 'link');
681 link.setAttribute('type', type);
683 $n(node,'count').appendChild(text(ids.length));
685 for( var o in search_hash )
686 $n(node, 'data').appendChild(
687 text(search_hash[o].value + ' '));
689 uEditDupHashes[type] = search_hash;
693 if(confirm($('ue_dup_ident1').innerHTML))
694 uEditShowSearch(null, type);
700 function uEditShowSearch(link,type) {
701 if(!type) type = link.getAttribute('type');
703 window.xulG.spawn_search(uEditDupHashes[type]);
704 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
707 function uEditMarkCardLost() {
709 for( var c in patron.cards() ) {
711 var card = patron.cards()[c];
712 if( patron.card().id() == card.id() ) {
714 /* de-activite the current card */
718 if( !card.barcode() ) {
719 /* a card exists in the array with no barcode */
720 ueRemoveCard(card.id());
722 } else if( card.isnew() && card.active() == 0 ) {
723 /* a new card was created, then never used, removing.. */
724 _debug("removing new inactive card "+card.barcode());
725 ueRemoveCard(card.id());
728 /* create a new card for the patron */
729 var newcard = new ac();
730 newcard.id(uEditCardVirtId--);
732 patron.card(newcard);
733 patron.cards().push(newcard);
736 /* reset the widget */
737 var field = uEditFindFieldByWId('ue_barcode');
738 field.widget.node.disabled = false;
739 field.widget.node.value = "";
740 field.widget.node.onchange();
741 field.object = newcard;
742 _debug("uEditMarkCardLost(): created new card object for user");
748 function ueRemoveCard(id) {
749 _debug("removing card from cards() array: " + id);
750 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
752 for( var j = 0; j < cds.length; j++ )
753 _debug("patron card array now has : "+cds[j].id());
759 function compactArray(arr) {
761 for( var i = 0; arr && i < arr.length; i++ ) {