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 /* fetches necessary and builds the UI */
110 function uEditBuild() {
111 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
113 uEditBuildLibSelector();
114 var usr = cgi.param('usr');
115 if (xulG) if (xulG.usr) usr = xulG.usr;
116 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
117 patron = fetchFleshedUser(usr);
118 if(!patron) patron = uEditNewPatron();
121 uEditFetchIdentTypes(),
123 uEditFetchStatCats(),
125 uEditFetchNetLevels()
129 if(clone) uEditClone(clone);
130 else uEditCreateNewAddr();
134 /* do we need to display the parent / gurdian field? */
135 uEditCheckDOB(uEditFindFieldByKey('dob'));
137 $('ue_barcode').disabled = true;
138 unHideMe($('ue_mark_card_lost'));
139 unHideMe($('ue_reset_pw'));
140 uEditCheckEditPerm();
143 if(PERMS['BAR_PATRON'] == -1)
144 $('ue_barred').disabled = true;
148 /* if this user does not have permission to put users into
149 the edited users group, they do not have permission to
151 function uEditCheckEditPerm() {
153 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
155 _debug("editing user with group app perm "+patron.profile()+' : '+
156 groupsCache[patron.profile()].name() +', and perm = ' + perm);
159 if(PERMS[perm] != -1) return;
161 /* we can edit our own account, but not others in our group */
162 if( patron.id() != USER.id() ){
163 _debug("we are not allowed to edit this user");
165 $('ue_save').disabled = true;
166 $('ue_save_clone').disabled = true;
167 $('ue_mark_card_lost').disabled = true;
168 $('ue_reset_pw').disabled = true;
172 if( f && f.widget && f.widget.node )
173 f.widget.node.disabled = true;
179 var node = $('ue_profile').parentNode;
180 node.removeChild($('ue_profile'));
181 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
183 var field = uEditFindFieldByKey('profile');
184 field.required = false;
185 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
190 /* creates a new patron object with card attached */
191 var uEditCardVirtId = -1;
192 function uEditNewPatron() {
193 var patron = new au();
197 card.id(uEditCardVirtId--);
200 patron.cards([card]);
201 patron.stat_cat_entries([]);
202 patron.survey_responses([]);
203 patron.addresses([]);
204 patron.home_ou(USER.ws_ou());
205 uEditMakeRandomPw(patron);
209 function uEditMakeRandomPw(patron) {
210 var rand = Math.random();
211 rand = parseInt(rand * 10000) + '';
212 while(rand.length < 4) rand += '0';
213 appendClear($('ue_password_plain'),text(rand));
214 unHideMe($('ue_password_gen'));
219 function uEditResetPw() {
220 var pw = uEditMakeRandomPw(patron);
221 $('ue_password1').value = pw;
222 $('ue_password2').value = pw;
223 $('ue_password1').onchange();
226 function uEditClone(clone) {
228 var cloneUser = fetchFleshedUser(clone);
229 patron.usrgroup(cloneUser.usrgroup());
231 if( cloneUser.day_phone() ) {
232 $('ue_day_phone').value = cloneUser.day_phone();
233 $('ue_day_phone').onchange();
236 if( cloneUser.evening_phone() ) {
237 $('ue_night_phone').value = cloneUser.evening_phone();
238 $('ue_night_phone').onchange();
241 if( cloneUser.other_phone() ) {
242 $('ue_other_phone').value = cloneUser.other_phone();
243 $('ue_other_phone').onchange();
246 setSelector($('ue_org_selector'), cloneUser.home_ou());
247 setSelector($('ue_profile'), cloneUser.profile());
249 /* force the expire date to be set */
250 $('ue_profile').onchange();
251 $('ue_org_selector').onchange();
253 for( var a in cloneUser.addresses() ) {
254 var addr = cloneUser.addresses()[a];
255 if( cloneUser.mailing_address &&
256 addr.id() == cloneUser.mailing_address().id() )
257 patron.mailing_address(addr);
258 if( cloneUser.billing_address() &&
259 addr.id() == cloneUser.billing_address().id() )
260 patron.billing_address(addr);
261 patron.addresses().push(addr);
264 uEditBuildAddrs(patron);
268 /* Creates a new blank address,
269 adds it to the user and the fields array */
270 var uEditVirtualAddrId = -1;
271 function uEditCreateNewAddr() {
272 var addr = new aua();
274 addr.id(uEditVirtualAddrId--);
276 addr.usr(patron.id());
277 addr.country(defaultCountry);
279 if(!patron.addresses())
280 patron.addresses([]);
282 if(patron.addresses().length == 0) {
283 patron.mailing_address(addr);
284 patron.billing_address(addr);
288 addr.within_city_limits(1);
290 uEditBuildAddrFields(patron, addr);
291 patron.addresses().push(addr);
292 uEditIterateFields(function(f) { uEditCheckValid(f); });
297 /* kicks off the UI drawing */
298 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
299 hideMe($('uedit_loading'));
300 unHideMe($('ue_maintd'));
303 uEditDrawIDTypes(identTypes);
304 uEditDrawGroups(groups, null, null, true);
305 uEditDrawStatCats(statCats);
306 uEditDrawSurveys(surveys);
307 uEditDrawNetLevels(netLevels);
308 uEditDefineData(patron);
310 uEditIterateFields(function(f) { uEditActivateField(f) });
311 uEditIterateFields(function(f) { uEditCheckValid(f); });
316 /** Applies the event handlers and sets the data for the field */
317 function uEditActivateField(field) {
319 if( field.widget.id ) {
320 field.widget.node = $(field.widget.id);
324 $n(field.widget.base, field.widget.name);
327 uEditSetOnchange(field);
329 if(field.widget.onblur) {
330 field.widget.node.onblur =
331 function() { field.widget.onblur(field); };
334 field.widget.node.disabled = field.widget.disabled;
335 if(field.object == null) return;
336 var val = field.object[field.key]();
337 if(val == null) return;
339 if( field.widget.type == 'input' )
340 field.widget.node.value = val;
342 if( field.widget.type == 'select' )
343 setSelector(field.widget.node, val);
345 if( field.widget.type == 'checkbox' )
346 field.widget.node.checked =
347 (val && val != 'f') ? true : false;
349 if( field.widget.onload )
350 field.widget.onload(val);
354 /* set up the onchange event for the field */
355 function uEditSetOnchange(field) {
356 var func = function() {uEditOnChange( field );}
357 field.widget.node.onchange = func;
359 if(field.widget.type != 'select')
360 field.widget.node.onkeyup = func;
363 /* find the current value of the field object's widget */
364 function uEditNodeVal(field) {
365 if(field.widget.type == 'input')
366 return field.widget.node.value;
368 if(field.widget.type == 'checkbox')
369 return field.widget.node.checked;
371 if(field.widget.type == 'select')
372 return getSelectorVal(field.widget.node);
376 /* update a field value */
377 function uEditOnChange(field) {
379 var newval = uEditNodeVal(field);
380 field.object[field.key](newval);
381 field.object.ischanged(1);
383 if(field.widget.onpostchange)
384 field.widget.onpostchange(field, newval);
386 //_debug(field.key+' = '+newval);
388 uEditIterateFields(function(f) { uEditCheckValid(f); });
395 function uEditCheckValid(field) {
396 var newval = uEditNodeVal(field);
400 if(field.widget.regex) {
401 if(newval.match(field.widget.regex))
402 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
404 addCSSClass(field.widget.node, CSS_INVALID_DATA);
407 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
413 addCSSClass(field.widget.node, CSS_INVALID_DATA);
416 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
422 /* find a field object by object key */
423 function uEditFindFieldByKey(key) {
424 var fields = grep( dataFields,
425 function(item) { return (item.key == key); });
426 return (fields) ? fields[0] : null;
429 /* find a list of fields by object key */
430 function uEditFindFieldsByKey(key) {
431 return grep( dataFields,
432 function(item) { return (item.key == key); });
435 /* find a field object by widget id */
436 function uEditFindFieldByWId(id) {
437 var fields = grep( dataFields,
438 function(item) { return (item.widget.id == id); });
439 return (fields) ? fields[0] : null;
443 function uEditIterateFields(callback) {
444 for( var f in dataFields )
445 callback(dataFields[f]);
449 function uEditGetErrorStrings() {
454 if( !field.object.isdeleted() ) {
455 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
456 var str = $(field.errkey).innerHTML;
457 if(str) errors.push(str);
464 /* munge up something for all of the required surveys
465 (which are not registered with the fields) */
466 if( patron.isnew() ) {
467 var sel = $('ue_survey_table');
470 var rows = sel.getElementsByTagName('tr');
472 for( var r in rows ) {
475 var sel = $n(row, 'ue_survey_answer');
477 var qstn = row.getAttribute('question');
480 qstn = surveyQuestionsCache[qstn];
481 survey = surveysCache[qstn.survey()];
482 var val = getSelectorVal(sel);
483 if(!val && isTrue(survey.required()))
484 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
490 /* ------------------------------------------------------------ */
492 if(errors[0]) return errors;
496 function uEditAlertErrors() {
497 var errors = uEditGetErrorStrings();
498 if(!errors) return false;
499 alert(errors.join("\n"));
504 /* send the user to the database */
505 function uEditSaveUser(cloneme) {
507 if(uEditGetErrorStrings()) {
512 /* null is unique in the db, but '' is not */
513 if( ! patron.ident_value() ) patron.ident_value(null);
514 //if( ! patron.ident_type2() ) patron.ident_type2(null);
515 if( ! patron.ident_value2() ) patron.ident_value2(null);
516 patron.ident_type2(null);
518 if(! patron.dob() ) patron.dob(null);
520 _debug("Saving patron with card: " + js2JSON(patron.card()));
521 _debug("Saving full patron: " + js2JSON(patron));
523 //for( var c in patron
525 var req = new Request(UPDATE_PATRON, SESSION, patron);
526 req.alertEvent = false;
528 var newuser = req.result();
533 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
536 if( evt.textcode == 'XACT_COLLISION' ) {
537 if( confirmId('ue_xact_collision') )
538 location.href = location.href;
541 var j = js2JSON(evt);
543 _debug("USER UPDATE FAILED:\n" + j);
548 alert($('ue_success').innerHTML);
551 /* if the user we just created was a clone, and we want to clone it,
552 we really want to clone the original */
553 if( clone ) cloneme = clone;
554 else cloneme = newuser.id();
561 typeof window.xulG.spawn_editor == 'function' &&
564 _debug("xulG clone spawning new interface...");
565 var ses = cgi.param('ses');
566 if (xulG) if (xulG.ses) ses = xulG.ses;
567 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
568 window.xulG.spawn_editor({ses:ses,clone:cloneme});
573 var href = location.href;
574 href = href.replace(/\&?usr=\d+/, '');
575 href = href.replace(/\&?clone=\d+/, '');
576 href += '&clone=' + cloneme;
577 location.href = href;
585 uEditRefreshXUL(newuser);
589 function uEditRefreshXUL(newuser) {
590 if (window.xulG && typeof window.xulG.on_save == 'function')
591 window.xulG.on_save(newuser);
594 function uEditRefresh() {
595 var href = location.href;
596 href = href.replace(/\&?clone=\d+/, '');
597 location.href = href;
601 function uEditCancel() {
602 var href = location.href;
603 href = href.replace(/\&?usr=\d+/, '');
604 href = href.replace(/\&?clone=\d+/, '');
605 var id = cgi.param('usr');
606 if (xulG) if (xulG.usr) id = xulG.usr;
607 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
608 /* reload the current user if available */
609 if( id ) href += "&usr=" + id;
610 location.href = href;
614 var uEditDupHashes = {};
615 var uEditDupTemplate;
617 function uEditRunDupeSearch(type, search_hash) {
619 if(!patron.isnew()) return;
621 _debug('dup search: ' + js2JSON(search_hash));
623 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
625 var container = $('dup_div_container');
626 if(!uEditDupTemplate)
627 uEditDupTemplate = container.removeChild($('dup_div'));
629 /* clear any existing dups for this type */
630 iterate( container.getElementsByTagName('div'),
632 if( d.getAttribute('type') == type ) {
633 container.removeChild(d)
641 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
648 function uEditHandleDupResults(ids, search_hash, type, container) {
650 _debug('dup search results: ' + js2JSON(ids));
652 if(!(ids && ids[0])) /* no results */
653 return uEditDupHashes[type] = null;
655 /* add a dup link to the UI and plug in the data */
656 var node = uEditDupTemplate.cloneNode(true);
657 container.appendChild(node);
658 node.setAttribute('type', type);
660 var link = $n(node, 'link');
661 link.setAttribute('type', type);
663 $n(node,'count').appendChild(text(ids.length));
665 for( var o in search_hash )
666 $n(node, 'data').appendChild(
667 text(search_hash[o].value + ' '));
669 uEditDupHashes[type] = search_hash;
673 if(confirm($('ue_dup_ident1').innerHTML))
674 uEditShowSearch(null, type);
680 function uEditShowSearch(link,type) {
681 if(!type) type = link.getAttribute('type');
683 window.xulG.spawn_search(uEditDupHashes[type]);
684 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
687 function uEditMarkCardLost() {
689 for( var c in patron.cards() ) {
691 var card = patron.cards()[c];
692 if( patron.card().id() == card.id() ) {
694 /* de-activite the current card */
698 if( !card.barcode() ) {
699 /* a card exists in the array with no barcode */
700 ueRemoveCard(card.id());
702 } else if( card.isnew() && card.active() == 0 ) {
703 /* a new card was created, then never used, removing.. */
704 _debug("removing new inactive card "+card.barcode());
705 ueRemoveCard(card.id());
708 /* create a new card for the patron */
709 var newcard = new ac();
710 newcard.id(uEditCardVirtId--);
712 patron.card(newcard);
713 patron.cards().push(newcard);
716 /* reset the widget */
717 var field = uEditFindFieldByWId('ue_barcode');
718 field.widget.node.disabled = false;
719 field.widget.node.value = "";
720 field.widget.node.onchange();
721 field.object = newcard;
722 _debug("uEditMarkCardLost(): created new card object for user");
728 function ueRemoveCard(id) {
729 _debug("removing card from cards() array: " + id);
730 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
732 for( var j = 0; j < cds.length; j++ )
733 _debug("patron card array now has : "+cds[j].id());
739 function compactArray(arr) {
741 for( var i = 0; arr && i < arr.length; i++ ) {