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 clone = cgi.param('clone');
23 if(!session) throw "User session is not defined";
26 $('uedit_user').appendChild(text(USER.usrname()));
28 setTimeout( function() {
29 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
32 function uEditSetUnload() {
33 _debug('setting window unload event');
35 window.onbeforeunload = function(evt) {
36 return $('ue_unsaved_changes').innerHTML;
41 function uEditClearUnload() {
42 _debug('clearing window unload event');
44 window.onbeforeunload = null;
48 /* ------------------------------------------------------------------------------ */
50 /* ------------------------------------------------------------------------------ */
51 function uEditFetchIdentTypes() {
52 _debug("uEditFetchIdentTypes()");
53 var s = fetchXULStash();
54 if (typeof s.list != 'undefined')
55 if (typeof s.list.cit != 'undefined') return s.list.cit;
56 var req = new Request(FETCH_ID_TYPES);
61 function uEditFetchStatCats() {
62 _debug("uEditFetchStatCats()");
63 var s = fetchXULStash();
64 if (typeof s.list != 'undefined')
65 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
66 var req = new Request(SC_FETCH_ALL, SESSION);
71 function uEditFetchSurveys() {
72 _debug("uEditFetchSurveys()");
73 var s = fetchXULStash();
74 if (typeof s.list != 'undefined')
75 if (typeof s.list.asv != 'undefined') return s.list.asv;
76 var req = new Request(SV_FETCH_ALL, SESSION);
81 function uEditFetchGroups() {
82 _debug("uEditFetchGroups()");
83 var s = fetchXULStash();
84 if (typeof s.tree != 'undefined')
85 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
86 var req = new Request(FETCH_GROUPS);
91 function uEditFetchNetLevels() {
92 _debug("uEditFetchNetLevels()");
93 var s = fetchXULStash();
94 if (typeof s.list != 'undefined')
95 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
96 var req = new Request(FETCH_NET_LEVELS, SESSION);
101 /* ------------------------------------------------------------------------------ */
105 /* fetches necessary and builds the UI */
106 function uEditBuild() {
107 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
109 uEditBuildLibSelector();
110 patron = fetchFleshedUser(cgi.param('usr'));
111 if(!patron) patron = uEditNewPatron();
114 uEditFetchIdentTypes(),
116 uEditFetchStatCats(),
118 uEditFetchNetLevels()
122 if(clone) uEditClone(clone);
123 else uEditCreateNewAddr();
127 /* do we need to display the parent / gurdian field? */
128 uEditCheckDOB(uEditFindFieldByKey('dob'));
130 $('ue_barcode').disabled = true;
131 unHideMe($('ue_mark_card_lost'));
132 unHideMe($('ue_reset_pw'));
133 uEditCheckEditPerm();
136 if(PERMS['BAR_PATRON'] == -1)
137 $('ue_barred').disabled = true;
141 /* if this user does not have permission to put users into
142 the edited users group, they do not have permission to
144 function uEditCheckEditPerm() {
146 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
148 _debug("editing user with group app perm "+patron.profile()+' : '+
149 groupsCache[patron.profile()].name() +', and perm = ' + perm);
152 if(PERMS[perm] != -1) return;
154 /* we can edit our own account, but not others in our group */
155 if( patron.id() != USER.id() ){
156 _debug("we are not allowed to edit this user");
158 $('ue_save').disabled = true;
159 $('ue_save_clone').disabled = true;
160 $('ue_mark_card_lost').disabled = true;
161 $('ue_reset_pw').disabled = true;
165 if( f && f.widget && f.widget.node )
166 f.widget.node.disabled = true;
172 var node = $('ue_profile').parentNode;
173 node.removeChild($('ue_profile'));
174 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
176 var field = uEditFindFieldByKey('profile');
177 field.required = false;
178 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
183 /* creates a new patron object with card attached */
184 var uEditCardVirtId = -1;
185 function uEditNewPatron() {
186 var patron = new au();
190 card.id(uEditCardVirtId--);
193 patron.cards([card]);
194 patron.stat_cat_entries([]);
195 patron.survey_responses([]);
196 patron.addresses([]);
197 patron.home_ou(USER.ws_ou());
198 uEditMakeRandomPw(patron);
202 function uEditMakeRandomPw(patron) {
203 var rand = Math.random();
204 rand = parseInt(rand * 10000) + '';
205 while(rand.length < 4) rand += '0';
206 appendClear($('ue_password_plain'),text(rand));
207 unHideMe($('ue_password_gen'));
212 function uEditResetPw() {
213 var pw = uEditMakeRandomPw(patron);
214 $('ue_password1').value = pw;
215 $('ue_password2').value = pw;
218 function uEditClone(clone) {
220 var cloneUser = fetchFleshedUser(clone);
221 patron.usrgroup(cloneUser.usrgroup());
223 if( cloneUser.day_phone() ) {
224 $('ue_day_phone').value = cloneUser.day_phone();
225 $('ue_day_phone').onchange();
228 if( cloneUser.evening_phone() ) {
229 $('ue_night_phone').value = cloneUser.evening_phone();
230 $('ue_night_phone').onchange();
233 if( cloneUser.other_phone() ) {
234 $('ue_other_phone').value = cloneUser.other_phone();
235 $('ue_other_phone').onchange();
238 setSelector($('ue_org_selector'), cloneUser.home_ou());
239 setSelector($('ue_profile'), cloneUser.profile());
241 /* force the expire date to be set */
242 $('ue_profile').onchange();
243 $('ue_org_selector').onchange();
245 for( var a in cloneUser.addresses() ) {
246 var addr = cloneUser.addresses()[a];
247 if( cloneUser.mailing_address &&
248 addr.id() == cloneUser.mailing_address().id() )
249 patron.mailing_address(addr);
250 if( cloneUser.billing_address() &&
251 addr.id() == cloneUser.billing_address().id() )
252 patron.billing_address(addr);
253 patron.addresses().push(addr);
256 uEditBuildAddrs(patron);
260 /* Creates a new blank address,
261 adds it to the user and the fields array */
262 var uEditVirtualAddrId = -1;
263 function uEditCreateNewAddr() {
264 var addr = new aua();
266 addr.id(uEditVirtualAddrId--);
268 addr.usr(patron.id());
269 addr.country(defaultCountry);
271 if(!patron.addresses())
272 patron.addresses([]);
274 if(patron.addresses().length == 0) {
275 patron.mailing_address(addr);
276 patron.billing_address(addr);
280 addr.within_city_limits(1);
282 uEditBuildAddrFields(patron, addr);
283 patron.addresses().push(addr);
284 uEditIterateFields(function(f) { uEditCheckValid(f); });
289 /* kicks off the UI drawing */
290 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
291 hideMe($('uedit_loading'));
292 unHideMe($('ue_maintd'));
295 uEditDrawIDTypes(identTypes);
296 uEditDrawGroups(groups, null, null, true);
297 uEditDrawStatCats(statCats);
298 uEditDrawSurveys(surveys);
299 uEditDrawNetLevels(netLevels);
300 uEditDefineData(patron);
302 uEditIterateFields(function(f) { uEditActivateField(f) });
303 uEditIterateFields(function(f) { uEditCheckValid(f); });
308 /** Applies the event handlers and sets the data for the field */
309 function uEditActivateField(field) {
311 if( field.widget.id ) {
312 field.widget.node = $(field.widget.id);
316 $n(field.widget.base, field.widget.name);
319 uEditSetOnchange(field);
321 if(field.widget.onblur) {
322 field.widget.node.onblur =
323 function() { field.widget.onblur(field); };
326 field.widget.node.disabled = field.widget.disabled;
327 if(field.object == null) return;
328 var val = field.object[field.key]();
329 if(val == null) return;
331 if( field.widget.type == 'input' )
332 field.widget.node.value = val;
334 if( field.widget.type == 'select' )
335 setSelector(field.widget.node, val);
337 if( field.widget.type == 'checkbox' )
338 field.widget.node.checked =
339 (val && val != 'f') ? true : false;
341 if( field.widget.onload )
342 field.widget.onload(val);
346 /* set up the onchange event for the field */
347 function uEditSetOnchange(field) {
348 var func = function() {uEditOnChange( field );}
349 field.widget.node.onchange = func;
351 if(field.widget.type != 'select')
352 field.widget.node.onkeyup = func;
355 /* find the current value of the field object's widget */
356 function uEditNodeVal(field) {
357 if(field.widget.type == 'input')
358 return field.widget.node.value;
360 if(field.widget.type == 'checkbox')
361 return field.widget.node.checked;
363 if(field.widget.type == 'select')
364 return getSelectorVal(field.widget.node);
368 /* update a field value */
369 function uEditOnChange(field) {
371 var newval = uEditNodeVal(field);
372 field.object[field.key](newval);
373 field.object.ischanged(1);
375 if(field.widget.onpostchange)
376 field.widget.onpostchange(field, newval);
378 //_debug(field.key+' = '+newval);
380 uEditIterateFields(function(f) { uEditCheckValid(f); });
387 function uEditCheckValid(field) {
388 var newval = uEditNodeVal(field);
392 if(field.widget.regex) {
393 if(newval.match(field.widget.regex))
394 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
396 addCSSClass(field.widget.node, CSS_INVALID_DATA);
399 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
405 addCSSClass(field.widget.node, CSS_INVALID_DATA);
408 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
414 /* find a field object by object key */
415 function uEditFindFieldByKey(key) {
416 var fields = grep( dataFields,
417 function(item) { return (item.key == key); });
418 return (fields) ? fields[0] : null;
421 /* find a list of fields by object key */
422 function uEditFindFieldsByKey(key) {
423 return grep( dataFields,
424 function(item) { return (item.key == key); });
427 /* find a field object by widget id */
428 function uEditFindFieldByWId(id) {
429 var fields = grep( dataFields,
430 function(item) { return (item.widget.id == id); });
431 return (fields) ? fields[0] : null;
435 function uEditIterateFields(callback) {
436 for( var f in dataFields )
437 callback(dataFields[f]);
441 function uEditGetErrorStrings() {
446 if( !field.object.isdeleted() ) {
447 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
448 var str = $(field.errkey).innerHTML;
449 if(str) errors.push(str);
456 /* munge up something for all of the required surveys
457 (which are not registered with the fields) */
458 if( patron.isnew() ) {
459 var sel = $('ue_survey_table');
462 var rows = sel.getElementsByTagName('tr');
464 for( var r in rows ) {
467 var sel = $n(row, 'ue_survey_answer');
469 var qstn = row.getAttribute('question');
472 qstn = surveyQuestionsCache[qstn];
473 survey = surveysCache[qstn.survey()];
474 var val = getSelectorVal(sel);
475 if(!val && isTrue(survey.required()))
476 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
482 /* ------------------------------------------------------------ */
484 if(errors[0]) return errors;
488 function uEditAlertErrors() {
489 var errors = uEditGetErrorStrings();
490 if(!errors) return false;
491 alert(errors.join("\n"));
496 /* send the user to the database */
497 function uEditSaveUser(cloneme) {
499 if(uEditGetErrorStrings()) {
504 /* null is unique in the db, but '' is not */
505 if( ! patron.ident_value() ) patron.ident_value(null);
506 //if( ! patron.ident_type2() ) patron.ident_type2(null);
507 if( ! patron.ident_value2() ) patron.ident_value2(null);
508 patron.ident_type2(null);
510 if(! patron.dob() ) patron.dob(null);
512 _debug("Saving patron with card: " + js2JSON(patron.card()));
513 _debug("Saving full patron: " + js2JSON(patron));
515 //for( var c in patron
517 var req = new Request(UPDATE_PATRON, SESSION, patron);
518 req.alertEvent = false;
520 var newuser = req.result();
525 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
528 if( evt.textcode == 'XACT_COLLISION' ) {
529 if( confirmId('ue_xact_collision') )
530 location.href = location.href;
533 var j = js2JSON(evt);
535 _debug("USER UPDATE FAILED:\n" + j);
540 alert($('ue_success').innerHTML);
543 /* if the user we just created was a clone, and we want to clone it,
544 we really want to clone the original */
545 if( clone ) cloneme = clone;
546 else cloneme = newuser.id();
553 typeof window.xulG.spawn_editor == 'function' &&
556 _debug("xulG clone spawning new interface...");
557 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
562 var href = location.href;
563 href = href.replace(/\&?usr=\d+/, '');
564 href = href.replace(/\&?clone=\d+/, '');
565 href += '&clone=' + cloneme;
566 location.href = href;
574 uEditRefreshXUL(newuser);
578 function uEditRefreshXUL(newuser) {
579 if (window.xulG && typeof window.xulG.on_save == 'function')
580 window.xulG.on_save(newuser);
583 function uEditRefresh() {
584 var href = location.href;
585 href = href.replace(/\&?clone=\d+/, '');
586 location.href = href;
590 function uEditCancel() {
591 var href = location.href;
592 href = href.replace(/\&?usr=\d+/, '');
593 href = href.replace(/\&?clone=\d+/, '');
594 var id = cgi.param('usr')
595 /* reload the current user if available */
596 if( id ) href += "&usr=" + id;
597 location.href = href;
601 var uEditDupHashes = {};
602 var uEditDupTemplate;
604 function uEditRunDupeSearch(type, search_hash) {
606 if(!patron.isnew()) return;
608 _debug('dup search: ' + js2JSON(search_hash));
610 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
612 var container = $('dup_div_container');
613 if(!uEditDupTemplate)
614 uEditDupTemplate = container.removeChild($('dup_div'));
616 /* clear any existing dups for this type */
617 iterate( container.getElementsByTagName('div'),
619 if( d.getAttribute('type') == type ) {
620 container.removeChild(d)
628 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
635 function uEditHandleDupResults(ids, search_hash, type, container) {
637 _debug('dup search results: ' + js2JSON(ids));
639 if(!(ids && ids[0])) /* no results */
640 return uEditDupHashes[type] = null;
642 /* add a dup link to the UI and plug in the data */
643 var node = uEditDupTemplate.cloneNode(true);
644 container.appendChild(node);
645 node.setAttribute('type', type);
647 var link = $n(node, 'link');
648 link.setAttribute('type', type);
650 $n(node,'count').appendChild(text(ids.length));
652 for( var o in search_hash )
653 $n(node, 'data').appendChild(
654 text(search_hash[o].value + ' '));
656 uEditDupHashes[type] = search_hash;
660 if(confirm($('ue_dup_ident1').innerHTML))
661 uEditShowSearch(null, type);
667 function uEditShowSearch(link,type) {
668 if(!type) type = link.getAttribute('type');
670 window.xulG.spawn_search(uEditDupHashes[type]);
671 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
674 function uEditMarkCardLost() {
676 for( var c in patron.cards() ) {
678 var card = patron.cards()[c];
679 if( patron.card().id() == card.id() ) {
681 /* de-activite the current card */
685 if( !card.barcode() ) {
686 /* a card exists in the array with no barcode */
687 ueRemoveCard(card.id());
689 } else if( card.isnew() && card.active() == 0 ) {
690 /* a new card was created, then never used, removing.. */
691 _debug("removing new inactive card "+card.barcode());
692 ueRemoveCard(card.id());
695 /* create a new card for the patron */
696 var newcard = new ac();
697 newcard.id(uEditCardVirtId--);
699 patron.card(newcard);
700 patron.cards().push(newcard);
703 /* reset the widget */
704 var field = uEditFindFieldByWId('ue_barcode');
705 field.widget.node.disabled = false;
706 field.widget.node.value = "";
707 field.widget.node.onchange();
708 field.object = newcard;
709 _debug("uEditMarkCardLost(): created new card object for user");
715 function ueRemoveCard(id) {
716 _debug("removing card from cards() array: " + id);
717 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
719 for( var j = 0; j < cds.length; j++ )
720 _debug("patron card array now has : "+cds[j].id());
726 function compactArray(arr) {
728 for( var i = 0; arr && i < arr.length; i++ ) {