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');
34 window.onbeforeunload = function(evt) {
35 //evt.returnValue = ('ue_unsaved_changes').innerHTML;
36 return $('ue_unsaved_changes').innerHTML;
40 function uEditClearUnload() {
41 _debug('clearing window unload event');
42 window.onbeforeunload = null;
45 /* ------------------------------------------------------------------------------ */
47 /* ------------------------------------------------------------------------------ */
48 function uEditFetchIdentTypes() {
49 _debug("uEditFetchIdentTypes()");
50 var req = new Request(FETCH_ID_TYPES);
55 function uEditFetchStatCats() {
56 _debug("uEditFetchStatCats()");
57 var req = new Request(SC_FETCH_ALL, SESSION);
62 function uEditFetchSurveys() {
63 _debug("uEditFetchSurveys()");
64 var req = new Request(SV_FETCH_ALL, SESSION);
69 function uEditFetchGroups() {
70 _debug("uEditFetchGroups()");
71 var req = new Request(FETCH_GROUPS);
76 function uEditFetchNetLevels() {
77 _debug("uEditFetchNetLevels()");
78 var req = new Request(FETCH_NET_LEVELS, SESSION);
83 /* ------------------------------------------------------------------------------ */
87 /* fetches necessary and builds the UI */
88 function uEditBuild() {
89 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
91 uEditBuildLibSelector();
92 patron = fetchFleshedUser(cgi.param('usr'));
93 if(!patron) patron = uEditNewPatron();
96 uEditFetchIdentTypes(),
100 uEditFetchNetLevels()
104 if(clone) uEditClone(clone);
105 else uEditCreateNewAddr();
109 /* do we need to display the parent / gurdian field? */
110 uEditCheckDOB(uEditFindFieldByKey('dob'));
112 $('ue_barcode').disabled = true;
113 unHideMe($('ue_mark_card_lost'));
114 unHideMe($('ue_reset_pw'));
115 uEditCheckEditPerm();
118 if(PERMS['BAR_PATRON'] == -1)
119 $('ue_barred').disabled = true;
123 /* if this user does not have permission to put users into
124 the edited users group, they do not have permission to
126 function uEditCheckEditPerm() {
128 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
130 _debug("editing user with group app perm "+patron.profile()+' : '+
131 groupsCache[patron.profile()].name() +', and perm = ' + perm);
134 if(PERMS[perm] != -1) return;
136 /* we can edit our own account, but not others in our group */
137 if( patron.id() != USER.id() ){
138 _debug("we are not allowed to edit this user");
140 $('ue_save').disabled = true;
141 $('ue_save_clone').disabled = true;
142 $('ue_mark_card_lost').disabled = true;
143 $('ue_reset_pw').disabled = true;
147 if( f && f.widget && f.widget.node )
148 f.widget.node.disabled = true;
154 var node = $('ue_profile').parentNode;
155 node.removeChild($('ue_profile'));
156 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
158 var field = uEditFindFieldByKey('profile');
159 field.required = false;
160 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
165 /* creates a new patron object with card attached */
166 var uEditCardVirtId = -1;
167 function uEditNewPatron() {
168 var patron = new au();
172 card.id(uEditCardVirtId--);
175 patron.cards([card]);
176 patron.stat_cat_entries([]);
177 patron.survey_responses([]);
178 patron.addresses([]);
179 patron.home_ou(USER.ws_ou());
180 uEditMakeRandomPw(patron);
184 function uEditMakeRandomPw(patron) {
185 var rand = Math.random();
186 rand = parseInt(rand * 10000) + '';
187 while(rand.length < 4) rand += '0';
188 appendClear($('ue_password_plain'),text(rand));
189 unHideMe($('ue_password_gen'));
194 function uEditResetPw() {
195 var pw = uEditMakeRandomPw(patron);
196 $('ue_password1').value = pw;
197 $('ue_password2').value = pw;
200 function uEditClone(clone) {
202 var cloneUser = fetchFleshedUser(clone);
203 patron.usrgroup(cloneUser.usrgroup());
205 if( cloneUser.day_phone() )
206 $('ue_day_phone').value = cloneUser.day_phone();
207 if( cloneUser.evening_phone() )
208 $('ue_night_phone').value = cloneUser.evening_phone();
209 if( cloneUser.other_phone() )
210 $('ue_other_phone').value = cloneUser.other_phone();
211 setSelector($('ue_org_selector'), cloneUser.home_ou());
214 setSelector($('ue_profile'), cloneUser.profile());
216 /* force the expire date to be set */
217 $('ue_profile').onchange();
219 for( var a in cloneUser.addresses() ) {
220 var addr = cloneUser.addresses()[a];
221 if( cloneUser.mailing_address &&
222 addr.id() == cloneUser.mailing_address().id() )
223 patron.mailing_address(addr);
224 if( cloneUser.billing_address() &&
225 addr.id() == cloneUser.billing_address().id() )
226 patron.billing_address(addr);
227 patron.addresses().push(addr);
230 uEditBuildAddrs(patron);
234 /* Creates a new blank address,
235 adds it to the user and the fields array */
236 var uEditVirtualAddrId = -1;
237 function uEditCreateNewAddr() {
238 var addr = new aua();
240 addr.id(uEditVirtualAddrId--);
242 addr.usr(patron.id());
243 addr.country(defaultCountry);
245 if(!patron.addresses())
246 patron.addresses([]);
248 if(patron.addresses().length == 0) {
249 patron.mailing_address(addr);
250 patron.billing_address(addr);
254 addr.within_city_limits(1);
256 uEditBuildAddrFields(patron, addr);
257 patron.addresses().push(addr);
258 uEditIterateFields(function(f) { uEditCheckValid(f); });
263 /* kicks off the UI drawing */
264 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
265 hideMe($('uedit_loading'));
266 unHideMe($('ue_maintd'));
269 uEditDrawIDTypes(identTypes);
270 uEditDrawGroups(groups, null, null, true);
271 uEditDrawStatCats(statCats);
272 uEditDrawSurveys(surveys);
273 uEditDrawNetLevels(netLevels);
274 uEditDefineData(patron);
276 uEditIterateFields(function(f) { uEditActivateField(f) });
277 uEditIterateFields(function(f) { uEditCheckValid(f); });
282 /** Applies the event handlers and sets the data for the field */
283 function uEditActivateField(field) {
285 if( field.widget.id ) {
286 field.widget.node = $(field.widget.id);
290 $n(field.widget.base, field.widget.name);
293 uEditSetOnchange(field);
295 if(field.widget.onblur) {
296 field.widget.node.onblur =
297 function() { field.widget.onblur(field); };
300 field.widget.node.disabled = field.widget.disabled;
301 if(field.object == null) return;
302 var val = field.object[field.key]();
303 if(val == null) return;
305 if( field.widget.type == 'input' )
306 field.widget.node.value = val;
308 if( field.widget.type == 'select' )
309 setSelector(field.widget.node, val);
311 if( field.widget.type == 'checkbox' )
312 field.widget.node.checked =
313 (val && val != 'f') ? true : false;
315 if( field.widget.onload )
316 field.widget.onload(val);
320 /* set up the onchange event for the field */
321 function uEditSetOnchange(field) {
322 var func = function() {uEditOnChange( field );}
323 field.widget.node.onchange = func;
325 if(field.widget.type != 'select')
326 field.widget.node.onkeyup = func;
329 /* find the current value of the field object's widget */
330 function uEditNodeVal(field) {
331 if(field.widget.type == 'input')
332 return field.widget.node.value;
334 if(field.widget.type == 'checkbox')
335 return field.widget.node.checked;
337 if(field.widget.type == 'select')
338 return getSelectorVal(field.widget.node);
342 /* update a field value */
343 function uEditOnChange(field) {
345 var newval = uEditNodeVal(field);
346 field.object[field.key](newval);
347 field.object.ischanged(1);
349 if(field.widget.onpostchange)
350 field.widget.onpostchange(field, newval);
352 //_debug(field.key+' = '+newval);
354 uEditIterateFields(function(f) { uEditCheckValid(f); });
361 function uEditCheckValid(field) {
362 var newval = uEditNodeVal(field);
366 if(field.widget.regex) {
367 if(newval.match(field.widget.regex))
368 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
370 addCSSClass(field.widget.node, CSS_INVALID_DATA);
373 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
379 addCSSClass(field.widget.node, CSS_INVALID_DATA);
382 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
388 /* find a field object by object key */
389 function uEditFindFieldByKey(key) {
390 var fields = grep( dataFields,
391 function(item) { return (item.key == key); });
392 return (fields) ? fields[0] : null;
395 /* find a list of fields by object key */
396 function uEditFindFieldsByKey(key) {
397 return grep( dataFields,
398 function(item) { return (item.key == key); });
401 /* find a field object by widget id */
402 function uEditFindFieldByWId(id) {
403 var fields = grep( dataFields,
404 function(item) { return (item.widget.id == id); });
405 return (fields) ? fields[0] : null;
409 function uEditIterateFields(callback) {
410 for( var f in dataFields )
411 callback(dataFields[f]);
415 function uEditGetErrorStrings() {
420 if( !field.object.isdeleted() ) {
421 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
422 var str = $(field.errkey).innerHTML;
423 if(str) errors.push(str);
430 /* munge up something for all of the required surveys
431 (which are not registered with the fields) */
432 if( patron.isnew() ) {
433 var sel = $('ue_survey_table');
436 var rows = sel.getElementsByTagName('tr');
438 for( var r in rows ) {
441 var sel = $n(row, 'ue_survey_answer');
443 var qstn = row.getAttribute('question');
446 qstn = surveyQuestionsCache[qstn];
447 survey = surveysCache[qstn.survey()];
448 var val = getSelectorVal(sel);
449 if(!val && isTrue(survey.required()))
450 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
456 /* ------------------------------------------------------------ */
458 if(errors[0]) return errors;
462 function uEditAlertErrors() {
463 var errors = uEditGetErrorStrings();
464 if(!errors) return false;
465 alert(errors.join("\n"));
470 /* send the user to the database */
471 function uEditSaveUser(cloneme) {
473 if(uEditGetErrorStrings()) {
478 /* null is unique in the db, but '' is not */
479 if( ! patron.ident_value() ) patron.ident_value(null);
480 //if( ! patron.ident_type2() ) patron.ident_type2(null);
481 if( ! patron.ident_value2() ) patron.ident_value2(null);
482 patron.ident_type2(null);
484 if(! patron.dob() ) patron.dob(null);
486 _debug("Saving patron with card: " + js2JSON(patron.card()));
487 _debug("Saving full patron: " + js2JSON(patron));
489 //for( var c in patron
491 var req = new Request(UPDATE_PATRON, SESSION, patron);
492 req.alertEvent = false;
494 var newuser = req.result();
499 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
501 var j = js2JSON(newuser);
503 _debug("USER UPDATE FAILED:\n" + j);
508 alert($('ue_success').innerHTML);
511 /* if the user we just created was a clone, and we want to clone it,
512 we really want to clone the original */
513 if( clone ) cloneme = clone;
514 else cloneme = newuser.id();
521 typeof window.xulG.spawn_editor == 'function' &&
524 _debug("xulG clone spawning new interface...");
525 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
530 var href = location.href;
531 href = href.replace(/\&?usr=\d+/, '');
532 href = href.replace(/\&?clone=\d+/, '');
533 href += '&clone=' + cloneme;
534 location.href = href;
542 uEditRefreshXUL(newuser);
546 function uEditRefreshXUL(newuser) {
547 if (window.xulG && typeof window.xulG.on_save == 'function')
548 window.xulG.on_save(newuser);
551 function uEditRefresh() {
552 var href = location.href;
553 href = href.replace(/\&?clone=\d+/, '');
554 location.href = href;
558 function uEditCancel() {
559 var href = location.href;
560 href = href.replace(/\&?usr=\d+/, '');
561 href = href.replace(/\&?clone=\d+/, '');
562 var id = cgi.param('usr')
563 /* reload the current user if available */
564 if( id ) href += "&usr=" + id;
565 location.href = href;
569 var uEditDupHashes = {};
570 var uEditDupTemplate;
572 function uEditRunDupeSearch(type, search_hash) {
574 if(!patron.isnew()) return;
576 _debug('dup search: ' + js2JSON(search_hash));
578 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
580 var container = $('dup_div_container');
581 if(!uEditDupTemplate)
582 uEditDupTemplate = container.removeChild($('dup_div'));
584 /* clear any existing dups for this type */
585 iterate( container.getElementsByTagName('div'),
587 if( d.getAttribute('type') == type ) {
588 container.removeChild(d)
596 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
603 function uEditHandleDupResults(ids, search_hash, type, container) {
605 _debug('dup search results: ' + js2JSON(ids));
607 if(!(ids && ids[0])) /* no results */
608 return uEditDupHashes[type] = null;
610 /* add a dup link to the UI and plug in the data */
611 var node = uEditDupTemplate.cloneNode(true);
612 container.appendChild(node);
613 node.setAttribute('type', type);
615 var link = $n(node, 'link');
616 link.setAttribute('type', type);
618 $n(node,'count').appendChild(text(ids.length));
620 for( var o in search_hash )
621 $n(node, 'data').appendChild(
622 text(search_hash[o].value + ' '));
624 uEditDupHashes[type] = search_hash;
628 if(confirm($('ue_dup_ident1').innerHTML))
629 uEditShowSearch(null, type);
635 function uEditShowSearch(link,type) {
636 if(!type) type = link.getAttribute('type');
638 window.xulG.spawn_search(uEditDupHashes[type]);
639 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
642 function uEditMarkCardLost() {
644 for( var c in patron.cards() ) {
646 var card = patron.cards()[c];
647 if( patron.card().id() == card.id() ) {
649 /* de-activite the current card */
653 if( !card.barcode() ) {
654 /* a card exists in the array with no barcode */
655 ueRemoveCard(card.id());
657 } else if( card.isnew() && card.active() == 0 ) {
658 /* a new card was created, then never used, removing.. */
659 _debug("removing new inactive card "+card.barcode());
660 ueRemoveCard(card.id());
663 /* create a new card for the patron */
664 var newcard = new ac();
665 newcard.id(uEditCardVirtId--);
667 patron.card(newcard);
668 patron.cards().push(newcard);
671 /* reset the widget */
672 var field = uEditFindFieldByWId('ue_barcode');
673 field.widget.node.disabled = false;
674 field.widget.node.value = "";
675 field.widget.node.onchange();
676 field.object = newcard;
677 _debug("uEditMarkCardLost(): created new card object for user");
683 function ueRemoveCard(id) {
684 _debug("removing card from cards() array: " + id);
685 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
687 for( var j = 0; j < cds.length; j++ )
688 _debug("patron card array now has : "+cds[j].id());
694 function compactArray(arr) {
696 for( var i = 0; arr && i < arr.length; i++ ) {