5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 //var guardianNote = null;
16 /* fetch the necessary data to start off */
17 function uEditInit() {
19 _debug('uEditInit(): ' + location.search);
22 session = cgi.param('ses');
23 clone = cgi.param('clone');
24 if(!session) throw "User session is not defined";
27 $('uedit_user').appendChild(text(USER.usrname()));
29 setTimeout( function() {
30 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
33 /* ------------------------------------------------------------------------------ */
35 /* ------------------------------------------------------------------------------ */
36 function uEditFetchIdentTypes() {
37 _debug("uEditFetchIdentTypes()");
38 var req = new Request(FETCH_ID_TYPES);
43 function uEditFetchStatCats() {
44 _debug("uEditFetchStatCats()");
45 var req = new Request(SC_FETCH_ALL, SESSION);
50 function uEditFetchSurveys() {
51 _debug("uEditFetchSurveys()");
52 var req = new Request(SV_FETCH_ALL, SESSION);
57 function uEditFetchGroups() {
58 _debug("uEditFetchGroups()");
59 var req = new Request(FETCH_GROUPS);
64 function uEditFetchNetLevels() {
65 _debug("uEditFetchNetLevels()");
66 var req = new Request(FETCH_NET_LEVELS, SESSION);
71 /* ------------------------------------------------------------------------------ */
75 /* fetches necessary and builds the UI */
76 function uEditBuild() {
77 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
79 uEditBuildLibSelector();
80 patron = fetchFleshedUser(cgi.param('usr'));
81 if(!patron) patron = uEditNewPatron();
84 uEditFetchIdentTypes(),
92 if(clone) uEditClone(clone);
93 else uEditCreateNewAddr();
97 /* do we need to display the parent / gurdian field? */
98 uEditCheckDOB(uEditFindFieldByKey('dob'));
100 $('ue_barcode').disabled = true;
101 unHideMe($('ue_mark_card_lost'));
102 unHideMe($('ue_reset_pw'));
103 uEditCheckEditPerm();
106 if(PERMS['BAR_PATRON'] == -1)
107 $('ue_barred').disabled = true;
111 /* if this user does not have permission to put users into
112 the edited users group, they do not have permission to
114 function uEditCheckEditPerm() {
116 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
118 _debug("editing user with group app perm "+patron.profile()+' : '+
119 groupsCache[patron.profile()].name() +', and perm = ' + perm);
122 if(PERMS[perm] != -1) return;
124 /* we can edit our own account, but not others in our group */
125 if( patron.id() != USER.id() ){
126 _debug("we are not allowed to edit this user");
128 $('ue_save').disabled = true;
129 $('ue_save_clone').disabled = true;
130 $('ue_mark_card_lost').disabled = true;
131 $('ue_reset_pw').disabled = true;
135 if( f && f.widget && f.widget.node )
136 f.widget.node.disabled = true;
142 var node = $('ue_profile').parentNode;
143 node.removeChild($('ue_profile'));
144 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
146 var field = uEditFindFieldByKey('profile');
147 field.required = false;
148 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
153 /* creates a new patron object with card attached */
154 var uEditCardVirtId = -1;
155 function uEditNewPatron() {
156 var patron = new au();
160 card.id(uEditCardVirtId--);
163 patron.cards([card]);
164 patron.stat_cat_entries([]);
165 patron.survey_responses([]);
166 patron.addresses([]);
167 patron.home_ou(USER.ws_ou());
168 uEditMakeRandomPw(patron);
172 function uEditMakeRandomPw(patron) {
173 var rand = Math.random();
174 rand = parseInt(rand * 10000) + '';
175 while(rand.length < 4) rand += '0';
176 appendClear($('ue_password_plain'),text(rand));
177 unHideMe($('ue_password_gen'));
182 function uEditResetPw() {
183 var pw = uEditMakeRandomPw(patron);
184 $('ue_password1').value = pw;
185 $('ue_password2').value = pw;
188 function uEditClone(clone) {
190 var cloneUser = fetchFleshedUser(clone);
191 patron.usrgroup(cloneUser.usrgroup());
193 if( cloneUser.day_phone() )
194 $('ue_day_phone').value = cloneUser.day_phone();
195 if( cloneUser.evening_phone() )
196 $('ue_night_phone').value = cloneUser.evening_phone();
197 if( cloneUser.other_phone() )
198 $('ue_other_phone').value = cloneUser.other_phone();
199 setSelector($('ue_org_selector'), cloneUser.home_ou());
202 setSelector($('ue_profile'), cloneUser.profile());
204 /* force the expire date to be set */
205 $('ue_profile').onchange();
207 for( var a in cloneUser.addresses() ) {
208 var addr = cloneUser.addresses()[a];
209 if( cloneUser.mailing_address &&
210 addr.id() == cloneUser.mailing_address().id() )
211 patron.mailing_address(addr);
212 if( cloneUser.billing_address() &&
213 addr.id() == cloneUser.billing_address().id() )
214 patron.billing_address(addr);
215 patron.addresses().push(addr);
218 uEditBuildAddrs(patron);
222 /* Creates a new blank address,
223 adds it to the user and the fields array */
224 var uEditVirtualAddrId = -1;
225 function uEditCreateNewAddr() {
226 var addr = new aua();
228 addr.id(uEditVirtualAddrId--);
230 addr.usr(patron.id());
231 addr.country(defaultCountry);
233 if(!patron.addresses())
234 patron.addresses([]);
236 if(patron.addresses().length == 0) {
237 patron.mailing_address(addr);
238 patron.billing_address(addr);
242 addr.within_city_limits(1);
244 uEditBuildAddrFields(patron, addr);
245 patron.addresses().push(addr);
246 uEditIterateFields(function(f) { uEditCheckValid(f); });
251 /* kicks off the UI drawing */
252 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
253 hideMe($('uedit_loading'));
254 unHideMe($('ue_maintd'));
257 uEditDrawIDTypes(identTypes);
258 uEditDrawGroups(groups, null, null, true);
259 uEditDrawStatCats(statCats);
260 uEditDrawSurveys(surveys);
261 uEditDrawNetLevels(netLevels);
262 uEditDefineData(patron);
264 uEditIterateFields(function(f) { uEditActivateField(f) });
265 uEditIterateFields(function(f) { uEditCheckValid(f); });
270 /** Applies the event handlers and sets the data for the field */
271 function uEditActivateField(field) {
273 if( field.widget.id ) {
274 field.widget.node = $(field.widget.id);
278 $n(field.widget.base, field.widget.name);
281 uEditSetOnchange(field);
283 if(field.widget.onblur) {
284 field.widget.node.onblur =
285 function() { field.widget.onblur(field); };
288 field.widget.node.disabled = field.widget.disabled;
289 if(field.object == null) return;
290 var val = field.object[field.key]();
291 if(val == null) return;
293 if( field.widget.type == 'input' )
294 field.widget.node.value = val;
296 if( field.widget.type == 'select' )
297 setSelector(field.widget.node, val);
299 if( field.widget.type == 'checkbox' )
300 field.widget.node.checked =
301 (val && val != 'f') ? true : false;
303 if( field.widget.onload )
304 field.widget.onload(val);
308 /* set up the onchange event for the field */
309 function uEditSetOnchange(field) {
310 var func = function() {uEditOnChange( field );}
311 field.widget.node.onchange = func;
313 if(field.widget.type != 'select')
314 field.widget.node.onkeyup = func;
317 /* find the current value of the field object's widget */
318 function uEditNodeVal(field) {
319 if(field.widget.type == 'input')
320 return field.widget.node.value;
322 if(field.widget.type == 'checkbox')
323 return field.widget.node.checked;
325 if(field.widget.type == 'select')
326 return getSelectorVal(field.widget.node);
330 /* update a field value */
331 function uEditOnChange(field) {
333 var newval = uEditNodeVal(field);
334 field.object[field.key](newval);
335 field.object.ischanged(1);
337 if(field.widget.onpostchange)
338 field.widget.onpostchange(field, newval);
340 //_debug(field.key+' = '+newval);
342 uEditIterateFields(function(f) { uEditCheckValid(f); });
347 function uEditCheckValid(field) {
348 var newval = uEditNodeVal(field);
352 if(field.widget.regex) {
353 if(newval.match(field.widget.regex))
354 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
356 addCSSClass(field.widget.node, CSS_INVALID_DATA);
359 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
365 addCSSClass(field.widget.node, CSS_INVALID_DATA);
368 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
374 /* find a field object by object key */
375 function uEditFindFieldByKey(key) {
376 var fields = grep( dataFields,
377 function(item) { return (item.key == key); });
378 return (fields) ? fields[0] : null;
381 /* find a list of fields by object key */
382 function uEditFindFieldsByKey(key) {
383 return grep( dataFields,
384 function(item) { return (item.key == key); });
387 /* find a field object by widget id */
388 function uEditFindFieldByWId(id) {
389 var fields = grep( dataFields,
390 function(item) { return (item.widget.id == id); });
391 return (fields) ? fields[0] : null;
395 function uEditIterateFields(callback) {
396 for( var f in dataFields )
397 callback(dataFields[f]);
401 function uEditGetErrorStrings() {
406 if( !field.object.isdeleted() ) {
407 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
408 var str = $(field.errkey).innerHTML;
409 if(str) errors.push(str);
416 /* munge up something for all of the required surveys
417 (which are not registered with the fields) */
418 if( patron.isnew() ) {
419 var sel = $('ue_survey_table');
422 var rows = sel.getElementsByTagName('tr');
424 for( var r in rows ) {
427 var sel = $n(row, 'ue_survey_answer');
429 var qstn = row.getAttribute('question');
432 qstn = surveyQuestionsCache[qstn];
433 survey = surveysCache[qstn.survey()];
434 var val = getSelectorVal(sel);
435 if(!val && isTrue(survey.required()))
436 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
442 /* ------------------------------------------------------------ */
444 if(errors[0]) return errors;
448 function uEditAlertErrors() {
449 var errors = uEditGetErrorStrings();
450 if(!errors) return false;
451 alert(errors.join("\n"));
456 /* send the user to the database */
457 function uEditSaveUser(cloneme) {
459 if(uEditGetErrorStrings()) {
464 /* null is unique in the db, but '' is not */
465 if( ! patron.ident_value() ) patron.ident_value(null);
466 //if( ! patron.ident_type2() ) patron.ident_type2(null);
467 if( ! patron.ident_value2() ) patron.ident_value2(null);
468 patron.ident_type2(null);
470 if(! patron.dob() ) patron.dob(null);
472 _debug("Saving patron with card: " + js2JSON(patron.card()));
473 _debug("Saving full patron: " + js2JSON(patron));
475 //for( var c in patron
477 var req = new Request(UPDATE_PATRON, SESSION, patron);
478 req.alertEvent = false;
480 var newuser = req.result();
483 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
485 var j = js2JSON(newuser);
487 _debug("USER UPDATE FAILED:\n" + j);
492 alert($('ue_success').innerHTML);
495 /* if the user we just created was a clone, and we want to clone it,
496 we really want to clone the original */
497 if( clone ) cloneme = clone;
498 else cloneme = newuser.id();
505 typeof window.xulG.spawn_editor == 'function' &&
508 _debug("xulG clone spawning new interface...");
509 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
514 var href = location.href;
515 href = href.replace(/\&?usr=\d+/, '');
516 href = href.replace(/\&?clone=\d+/, '');
517 href += '&clone=' + cloneme;
518 location.href = href;
526 uEditRefreshXUL(newuser);
530 function uEditRefreshXUL(newuser) {
531 if (window.xulG && typeof window.xulG.on_save == 'function')
532 window.xulG.on_save(newuser);
535 function uEditRefresh() {
536 var href = location.href;
537 href = href.replace(/\&?clone=\d+/, '');
538 location.href = href;
542 function uEditCancel() {
543 var href = location.href;
544 href = href.replace(/\&?usr=\d+/, '');
545 href = href.replace(/\&?clone=\d+/, '');
546 var id = cgi.param('usr')
547 /* reload the current user if available */
548 if( id ) href += "&usr=" + id;
549 location.href = href;
553 var uEditDupHashes = {};
554 var uEditDupTemplate;
556 function uEditRunDupeSearch(type, search_hash) {
558 if(!patron.isnew()) return;
560 _debug('dup search: ' + js2JSON(search_hash));
562 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
564 var container = $('dup_div_container');
565 if(!uEditDupTemplate)
566 uEditDupTemplate = container.removeChild($('dup_div'));
568 /* clear any existing dups for this type */
569 iterate( container.getElementsByTagName('div'),
571 if( d.getAttribute('type') == type ) {
572 container.removeChild(d)
580 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
587 function uEditHandleDupResults(ids, search_hash, type, container) {
589 _debug('dup search results: ' + js2JSON(ids));
591 if(!(ids && ids[0])) /* no results */
592 return uEditDupHashes[type] = null;
594 /* add a dup link to the UI and plug in the data */
595 var node = uEditDupTemplate.cloneNode(true);
596 container.appendChild(node);
597 node.setAttribute('type', type);
599 var link = $n(node, 'link');
600 link.setAttribute('type', type);
602 $n(node,'count').appendChild(text(ids.length));
604 for( var o in search_hash )
605 $n(node, 'data').appendChild(
606 text(search_hash[o].value + ' '));
608 uEditDupHashes[type] = search_hash;
612 if(confirm($('ue_dup_ident1').innerHTML))
613 uEditShowSearch(null, type);
619 function uEditShowSearch(link,type) {
620 if(!type) type = link.getAttribute('type');
622 window.xulG.spawn_search(uEditDupHashes[type]);
623 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
626 function uEditMarkCardLost() {
628 for( var c in patron.cards() ) {
630 var card = patron.cards()[c];
631 if( patron.card().id() == card.id() ) {
633 /* de-activite the current card */
637 if( !card.barcode() ) {
638 /* a card exists in the array with no barcode */
639 ueRemoveCard(card.id());
641 } else if( card.isnew() && card.active() == 0 ) {
642 /* a new card was created, then never used, removing.. */
643 _debug("removing new inactive card "+card.barcode());
644 ueRemoveCard(card.id());
647 /* create a new card for the patron */
648 var newcard = new ac();
649 newcard.id(uEditCardVirtId--);
651 patron.card(newcard);
652 patron.cards().push(newcard);
655 /* reset the widget */
656 var field = uEditFindFieldByWId('ue_barcode');
657 field.widget.node.disabled = false;
658 field.widget.node.value = "";
659 field.widget.node.onchange();
660 field.object = newcard;
661 _debug("uEditMarkCardLost(): created new card object for user");
667 function ueRemoveCard(id) {
668 _debug("removing card from cards() array: " + id);
669 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
671 for( var j = 0; j < cds.length; j++ )
672 _debug("patron card array now has : "+cds[j].id());
678 function compactArray(arr) {
680 for( var i = 0; arr && i < arr.length; i++ ) {