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()]);
117 _debug("editing user with group app perm "+patron.profile()+' : '+
118 groupsCache[patron.profile()].name() +', and perm = ' + perm);
120 if(PERMS[perm] != -1) return;
122 /* we can edit our own account, but not others in our group */
123 if( patron.id() != USER.id() ){
124 _debug("we are not allowed to edit this user");
126 $('ue_save').disabled = true;
127 $('ue_save_clone').disabled = true;
131 if( f && f.widget && f.widget.node )
132 f.widget.node.disabled = true;
138 var node = $('ue_profile').parentNode;
139 node.removeChild($('ue_profile'));
140 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
142 var field = uEditFindFieldByKey('profile');
143 field.required = false;
144 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
149 /* creates a new patron object with card attached */
150 var uEditCardVirtId = -1;
151 function uEditNewPatron() {
152 var patron = new au();
156 card.id(uEditCardVirtId--);
159 patron.cards([card]);
160 patron.stat_cat_entries([]);
161 patron.survey_responses([]);
162 patron.addresses([]);
163 patron.home_ou(USER.ws_ou());
164 uEditMakeRandomPw(patron);
168 function uEditMakeRandomPw(patron) {
169 var rand = Math.random();
170 rand = parseInt(rand * 10000) + '';
171 while(rand.length < 4) rand += '0';
172 appendClear($('ue_password_plain'),text(rand));
173 unHideMe($('ue_password_gen'));
178 function uEditResetPw() {
179 var pw = uEditMakeRandomPw(patron);
180 $('ue_password1').value = pw;
181 $('ue_password2').value = pw;
184 function uEditClone(clone) {
186 var cloneUser = fetchFleshedUser(clone);
187 patron.usrgroup(cloneUser.usrgroup());
189 if( cloneUser.day_phone() )
190 $('ue_day_phone').value = cloneUser.day_phone();
191 if( cloneUser.evening_phone() )
192 $('ue_night_phone').value = cloneUser.evening_phone();
193 if( cloneUser.other_phone() )
194 $('ue_other_phone').value = cloneUser.other_phone();
195 setSelector($('ue_org_selector'), cloneUser.home_ou());
198 setSelector($('ue_profile'), cloneUser.profile());
200 /* force the expire date to be set */
201 $('ue_profile').onchange();
203 for( var a in cloneUser.addresses() ) {
204 var addr = cloneUser.addresses()[a];
205 if( cloneUser.mailing_address &&
206 addr.id() == cloneUser.mailing_address().id() )
207 patron.mailing_address(addr);
208 if( cloneUser.billing_address() &&
209 addr.id() == cloneUser.billing_address().id() )
210 patron.billing_address(addr);
211 patron.addresses().push(addr);
214 uEditBuildAddrs(patron);
218 /* Creates a new blank address,
219 adds it to the user and the fields array */
220 var uEditVirtualAddrId = -1;
221 function uEditCreateNewAddr() {
222 var addr = new aua();
224 addr.id(uEditVirtualAddrId--);
226 addr.usr(patron.id());
227 addr.country(defaultCountry);
229 if(!patron.addresses())
230 patron.addresses([]);
232 if(patron.addresses().length == 0) {
233 patron.mailing_address(addr);
234 patron.billing_address(addr);
238 addr.within_city_limits(1);
240 uEditBuildAddrFields(patron, addr);
241 patron.addresses().push(addr);
242 uEditIterateFields(function(f) { uEditCheckValid(f); });
247 /* kicks off the UI drawing */
248 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
249 hideMe($('uedit_loading'));
250 unHideMe($('ue_maintd'));
253 uEditDrawIDTypes(identTypes);
254 uEditDrawGroups(groups);
255 uEditDrawStatCats(statCats);
256 uEditDrawSurveys(surveys);
257 uEditDrawNetLevels(netLevels);
258 uEditDefineData(patron);
260 uEditIterateFields(function(f) { uEditActivateField(f) });
261 uEditIterateFields(function(f) { uEditCheckValid(f); });
266 /** Applies the event handlers and sets the data for the field */
267 function uEditActivateField(field) {
269 if( field.widget.id ) {
270 field.widget.node = $(field.widget.id);
274 $n(field.widget.base, field.widget.name);
277 uEditSetOnchange(field);
279 if(field.widget.onblur) {
280 field.widget.node.onblur =
281 function() { field.widget.onblur(field); };
284 field.widget.node.disabled = field.widget.disabled;
285 if(field.object == null) return;
286 var val = field.object[field.key]();
287 if(val == null) return;
289 if( field.widget.type == 'input' )
290 field.widget.node.value = val;
292 if( field.widget.type == 'select' )
293 setSelector(field.widget.node, val);
295 if( field.widget.type == 'checkbox' )
296 field.widget.node.checked =
297 (val && val != 'f') ? true : false;
299 if( field.widget.onload )
300 field.widget.onload(val);
304 /* set up the onchange event for the field */
305 function uEditSetOnchange(field) {
306 var func = function() {uEditOnChange( field );}
307 field.widget.node.onchange = func;
309 if(field.widget.type != 'select')
310 field.widget.node.onkeyup = func;
313 /* find the current value of the field object's widget */
314 function uEditNodeVal(field) {
315 if(field.widget.type == 'input')
316 return field.widget.node.value;
318 if(field.widget.type == 'checkbox')
319 return field.widget.node.checked;
321 if(field.widget.type == 'select')
322 return getSelectorVal(field.widget.node);
326 /* update a field value */
327 function uEditOnChange(field) {
329 var newval = uEditNodeVal(field);
330 field.object[field.key](newval);
331 field.object.ischanged(1);
333 if(field.widget.onpostchange)
334 field.widget.onpostchange(field, newval);
337 uEditIterateFields(function(f) { uEditCheckValid(f); });
342 function uEditCheckValid(field) {
343 var newval = uEditNodeVal(field);
347 if(field.widget.regex) {
348 if(newval.match(field.widget.regex))
349 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
351 addCSSClass(field.widget.node, CSS_INVALID_DATA);
354 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
360 addCSSClass(field.widget.node, CSS_INVALID_DATA);
363 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
368 /* find a field object by object key */
369 function uEditFindFieldByKey(key) {
370 var fields = grep( dataFields,
371 function(item) { return (item.key == key); });
372 return (fields) ? fields[0] : null;
375 /* find a list of fields by object key */
376 function uEditFindFieldsByKey(key) {
377 return grep( dataFields,
378 function(item) { return (item.key == key); });
381 /* find a field object by widget id */
382 function uEditFindFieldByWId(id) {
383 var fields = grep( dataFields,
384 function(item) { return (item.widget.id == id); });
385 return (fields) ? fields[0] : null;
389 function uEditIterateFields(callback) {
390 for( var f in dataFields )
391 callback(dataFields[f]);
395 function uEditGetErrorStrings() {
400 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
401 var str = $(field.errkey).innerHTML;
402 if(str) errors.push(str);
408 /* munge up something for all of the required surveys
409 (which are not registered with the fields) */
410 if( patron.isnew() ) {
411 var sel = $('ue_survey_table');
414 var rows = sel.getElementsByTagName('tr');
416 for( var r in rows ) {
419 var sel = $n(row, 'ue_survey_answer');
421 var qstn = row.getAttribute('question');
424 qstn = surveyQuestionsCache[qstn];
425 survey = surveysCache[qstn.survey()];
426 var val = getSelectorVal(sel);
427 if(!val && isTrue(survey.required()))
428 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
434 /* ------------------------------------------------------------ */
436 if(errors[0]) return errors;
440 function uEditAlertErrors() {
441 var errors = uEditGetErrorStrings();
442 if(!errors) return false;
443 alert(errors.join("\n"));
448 /* send the user to the database */
449 function uEditSaveUser(cloneme) {
451 if(uEditGetErrorStrings()) {
456 /* null is unique in the db, but '' is not */
457 if( ! patron.ident_value() ) patron.ident_value(null);
458 //if( ! patron.ident_type2() ) patron.ident_type2(null);
459 if( ! patron.ident_value2() ) patron.ident_value2(null);
460 patron.ident_type2(null);
462 if(! patron.dob() ) patron.dob(null);
464 var req = new Request(UPDATE_PATRON, SESSION, patron);
465 req.alertEvent = false;
467 var newuser = req.result();
470 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
471 if(evt) alert(js2JSON(newuser));
475 alert($('ue_success').innerHTML);
478 /* if the user we just created was a clone, and we want to clone it,
479 we really want to clone the original */
480 if( clone ) cloneme = clone;
481 else cloneme = newuser.id();
484 if (window.xulG && typeof window.xulG.on_save == 'function') {
485 _debug("xulG funcs defined...");
486 if( !patron.isnew() && cloneme ) {
487 _debug("calling spawn_editor to clone user...");
488 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
490 window.xulG.on_save(newuser, cloneme);
494 _debug("xulG funcs not defined, refreshing page..");
495 var href = location.href;
497 href = href.replace(/\&?usr=\d+/, '');
498 href = href.replace(/\&?clone=\d+/, '');
500 if( cloneme ) href += '&clone=' + cloneme;
501 location.href = href;
505 function uEditCancel() {
506 var href = location.href;
507 href = href.replace(/\&?usr=\d+/, '');
508 href = href.replace(/\&?clone=\d+/, '');
509 var id = cgi.param('usr')
510 /* reload the current user if available */
511 if( id ) href += "&usr=" + id;
512 location.href = href;
516 var uEditDupHashes = {};
517 var uEditDupTemplate;
519 function uEditRunDupeSearch(type, search_hash) {
521 if(!patron.isnew()) return;
523 _debug('dup search: ' + js2JSON(search_hash));
525 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
527 var container = $('dup_div_container');
528 if(!uEditDupTemplate)
529 uEditDupTemplate = container.removeChild($('dup_div'));
531 /* clear any existing dups for this type */
532 iterate( container.getElementsByTagName('div'),
534 if( d.getAttribute('type') == type ) {
535 container.removeChild(d)
543 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
550 function uEditHandleDupResults(ids, search_hash, type, container) {
552 _debug('dup search results: ' + js2JSON(ids));
554 if(!(ids && ids[0])) /* no results */
555 return uEditDupHashes[type] = null;
557 /* add a dup link to the UI and plug in the data */
558 var node = uEditDupTemplate.cloneNode(true);
559 container.appendChild(node);
560 node.setAttribute('type', type);
562 var link = $n(node, 'link');
563 link.setAttribute('type', type);
565 $n(node,'count').appendChild(text(ids.length));
567 for( var o in search_hash )
568 $n(node, 'data').appendChild(
569 text(search_hash[o].value + ' '));
571 uEditDupHashes[type] = search_hash;
575 if(confirm($('ue_dup_ident1').innerHTML))
576 uEditShowSearch(null, type);
582 function uEditShowSearch(link,type) {
583 if(!type) type = link.getAttribute('type');
585 window.xulG.spawn_search(uEditDupHashes[type]);
586 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
589 function uEditMarkCardLost() {
591 for( var c in patron.cards() ) {
593 var card = patron.cards()[c];
594 if( patron.card().id() == card.id() ) {
596 /* de-activite the current card */
600 /* create a new card for the patron */
601 var newcard = new ac();
602 newcard.id(uEditCardVirtId--);
604 patron.card(newcard);
605 patron.cards().push(newcard);
608 /* reset the widget */
609 var field = uEditFindFieldByWId('ue_barcode');
610 field.widget.node.disabled = false;
611 field.widget.node.value = "";
612 field.widget.node.onchange();
613 field.object = newcard;
619 function compactArray(arr) {
621 for( var i = 0; arr && i < arr.length; i++ ) {