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();
85 uEditFetchIdentTypes(),
93 if(clone) uEditClone(clone);
94 else uEditCreateNewAddr();
98 $('ue_barcode').disabled = true;
99 unHideMe($('ue_mark_card_lost'));
100 unHideMe($('ue_reset_pw'));
101 uEditCheckEditPerm();
104 if(PERMS['BAR_PATRON'] == -1)
105 $('ue_barred').disabled = true;
109 /* if this user does not have permission to put users into
110 the edited users group, they do not have permission to
112 function uEditCheckEditPerm() {
114 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
115 _debug("editing user with group app perm "+patron.profile()+' : '+
116 groupsCache[patron.profile()].name() +', and perm = ' + perm);
118 if(PERMS[perm] != -1) return;
120 /* we can edit our own account, but not others in our group */
121 if( patron.id() != USER.id() ){
122 _debug("we are not allowed to edit this user");
124 $('ue_save').disabled = true;
125 $('ue_save_clone').disabled = true;
129 if( f && f.widget && f.widget.node )
130 f.widget.node.disabled = true;
136 var node = $('ue_profile').parentNode;
137 node.removeChild($('ue_profile'));
138 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
140 var field = uEditFindFieldByKey('profile');
141 field.required = false;
142 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
147 /* creates a new patron object with card attached */
148 var uEditCardVirtId = -1;
149 function uEditNewPatron() {
150 var patron = new au();
154 card.id(uEditCardVirtId--);
157 patron.cards([card]);
158 patron.stat_cat_entries([]);
159 patron.survey_responses([]);
160 patron.addresses([]);
161 patron.home_ou(USER.ws_ou());
162 uEditMakeRandomPw(patron);
166 function uEditMakeRandomPw(patron) {
167 var rand = Math.random();
168 rand = parseInt(rand * 10000) + '';
169 while(rand.length < 4) rand += '0';
170 appendClear($('ue_password_plain'),text(rand));
171 unHideMe($('ue_password_gen'));
176 function uEditResetPw() {
177 var pw = uEditMakeRandomPw(patron);
178 $('ue_password1').value = pw;
179 $('ue_password2').value = pw;
182 function uEditClone(clone) {
184 var cloneUser = fetchFleshedUser(clone);
185 patron.usrgroup(cloneUser.usrgroup());
187 if( cloneUser.day_phone() )
188 $('ue_day_phone').value = cloneUser.day_phone();
189 if( cloneUser.evening_phone() )
190 $('ue_night_phone').value = cloneUser.evening_phone();
191 if( cloneUser.other_phone() )
192 $('ue_other_phone').value = cloneUser.other_phone();
193 setSelector($('ue_org_selector'), cloneUser.home_ou());
196 setSelector($('ue_profile'), cloneUser.profile());
198 /* force the expire date to be set */
199 $('ue_profile').onchange();
201 for( var a in cloneUser.addresses() ) {
202 var addr = cloneUser.addresses()[a];
203 if( cloneUser.mailing_address &&
204 addr.id() == cloneUser.mailing_address().id() )
205 patron.mailing_address(addr);
206 if( cloneUser.billing_address() &&
207 addr.id() == cloneUser.billing_address().id() )
208 patron.billing_address(addr);
209 patron.addresses().push(addr);
212 uEditBuildAddrs(patron);
216 /* Creates a new blank address,
217 adds it to the user and the fields array */
218 var uEditVirtualAddrId = -1;
219 function uEditCreateNewAddr() {
220 var addr = new aua();
222 addr.id(uEditVirtualAddrId--);
224 addr.usr(patron.id());
225 addr.country(defaultCountry);
227 if(!patron.addresses())
228 patron.addresses([]);
230 if(patron.addresses().length == 0) {
231 patron.mailing_address(addr);
232 patron.billing_address(addr);
236 addr.within_city_limits(1);
238 uEditBuildAddrFields(patron, addr);
239 patron.addresses().push(addr);
240 uEditIterateFields(function(f) { uEditCheckValid(f); });
245 /* kicks off the UI drawing */
246 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
247 hideMe($('uedit_loading'));
248 unHideMe($('ue_maintd'));
251 uEditDrawIDTypes(identTypes);
252 uEditDrawGroups(groups);
253 uEditDrawStatCats(statCats);
254 uEditDrawSurveys(surveys);
255 uEditDrawNetLevels(netLevels);
256 uEditDefineData(patron);
258 uEditIterateFields(function(f) { uEditActivateField(f) });
259 uEditIterateFields(function(f) { uEditCheckValid(f); });
264 /** Applies the event handlers and sets the data for the field */
265 function uEditActivateField(field) {
267 if( field.widget.id ) {
268 field.widget.node = $(field.widget.id);
272 $n(field.widget.base, field.widget.name);
275 uEditSetOnchange(field);
277 if(field.widget.onblur) {
278 field.widget.node.onblur =
279 function() { field.widget.onblur(field); };
282 field.widget.node.disabled = field.widget.disabled;
283 if(field.object == null) return;
284 var val = field.object[field.key]();
285 if(val == null) return;
287 if( field.widget.type == 'input' )
288 field.widget.node.value = val;
290 if( field.widget.type == 'select' )
291 setSelector(field.widget.node, val);
293 if( field.widget.type == 'checkbox' )
294 field.widget.node.checked =
295 (val && val != 'f') ? true : false;
297 if( field.widget.onload )
298 field.widget.onload(val);
302 /* set up the onchange event for the field */
303 function uEditSetOnchange(field) {
304 var func = function() {uEditOnChange( field );}
305 field.widget.node.onchange = func;
307 if(field.widget.type != 'select')
308 field.widget.node.onkeyup = func;
311 /* find the current value of the field object's widget */
312 function uEditNodeVal(field) {
313 if(field.widget.type == 'input')
314 return field.widget.node.value;
316 if(field.widget.type == 'checkbox')
317 return field.widget.node.checked;
319 if(field.widget.type == 'select')
320 return getSelectorVal(field.widget.node);
324 /* update a field value */
325 function uEditOnChange(field) {
327 var newval = uEditNodeVal(field);
328 field.object[field.key](newval);
329 field.object.ischanged(1);
331 if(field.widget.onpostchange)
332 field.widget.onpostchange(field, newval);
335 uEditIterateFields(function(f) { uEditCheckValid(f); });
340 function uEditCheckValid(field) {
341 var newval = uEditNodeVal(field);
345 if(field.widget.regex) {
346 if(newval.match(field.widget.regex))
347 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
349 addCSSClass(field.widget.node, CSS_INVALID_DATA);
352 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
358 addCSSClass(field.widget.node, CSS_INVALID_DATA);
361 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
366 /* find a field object by object key */
367 function uEditFindFieldByKey(key) {
368 var fields = grep( dataFields,
369 function(item) { return (item.key == key); });
370 return (fields) ? fields[0] : null;
373 /* find a list of fields by object key */
374 function uEditFindFieldsByKey(key) {
375 return grep( dataFields,
376 function(item) { return (item.key == key); });
379 /* find a field object by widget id */
380 function uEditFindFieldByWId(id) {
381 var fields = grep( dataFields,
382 function(item) { return (item.widget.id == id); });
383 return (fields) ? fields[0] : null;
387 function uEditIterateFields(callback) {
388 for( var f in dataFields )
389 callback(dataFields[f]);
393 function uEditGetErrorStrings() {
398 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
399 var str = $(field.errkey).innerHTML;
400 if(str) errors.push(str);
406 /* munge up something for all of the required surveys
407 (which are not registered with the fields) */
408 if( patron.isnew() ) {
409 var sel = $('ue_survey_table');
412 var rows = sel.getElementsByTagName('tr');
414 for( var r in rows ) {
417 var sel = $n(row, 'ue_survey_answer');
419 var qstn = row.getAttribute('question');
422 qstn = surveyQuestionsCache[qstn];
423 survey = surveysCache[qstn.survey()];
424 var val = getSelectorVal(sel);
425 if(!val && isTrue(survey.required()))
426 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
432 /* ------------------------------------------------------------ */
434 if(errors[0]) return errors;
438 function uEditAlertErrors() {
439 var errors = uEditGetErrorStrings();
440 if(!errors) return false;
441 alert(errors.join("\n"));
446 /* send the user to the database */
447 function uEditSaveUser(cloneme) {
449 if(uEditGetErrorStrings()) {
454 /* null is unique in the db, but '' is not */
455 if( ! patron.ident_value() ) patron.ident_value(null);
456 if( ! patron.ident_type2() ) patron.ident_type2(null);
457 if( ! patron.ident_value2() ) patron.ident_value2(null);
459 if(! patron.dob() ) patron.dob(null);
461 var req = new Request(UPDATE_PATRON, SESSION, patron);
462 req.alertEvent = false;
464 var newuser = req.result();
467 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
468 if(evt) alert(js2JSON(newuser));
473 /* if it's a new user and a guardian note was created for this user,
474 create the note after we have the new user's id */
476 guardianNote.usr( newuser.id() );
477 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
478 req.alertEvent = false;
480 var resp = req.result();
481 if( checkILSEvent(resp) ) {
483 "Error creating patron guardian/parent note");
488 alert($('ue_success').innerHTML);
492 /* if the user we just created was a clone, and we want to clone it,
493 we really want to clone the original */
494 if( clone ) cloneme = clone;
495 else cloneme = newuser.id();
498 if (window.xulG && typeof window.xulG.on_save == 'function') {
499 _debug("xulG funcs defined...");
500 if( !patron.isnew() && cloneme ) {
501 _debug("calling spawn_editor to clone user...");
502 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
504 window.xulG.on_save(newuser, cloneme);
508 _debug("xulG funcs not defined, refreshing page..");
509 var href = location.href;
511 href = href.replace(/\&?usr=\d+/, '');
512 href = href.replace(/\&?clone=\d+/, '');
514 if( cloneme ) href += '&clone=' + cloneme;
515 location.href = href;
519 function uEditCancel() {
520 var href = location.href;
521 href = href.replace(/\&?usr=\d+/, '');
522 href = href.replace(/\&?clone=\d+/, '');
523 var id = cgi.param('usr')
524 /* reload the current user if available */
525 if( id ) href += "&usr=" + id;
526 location.href = href;
530 var uEditDupHashes = {};
531 var uEditDupTemplate;
533 function uEditRunDupeSearch(type, search_hash) {
535 if(!patron.isnew()) return;
537 _debug('dup search: ' + js2JSON(search_hash));
539 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
541 var container = $('dup_div_container');
542 if(!uEditDupTemplate)
543 uEditDupTemplate = container.removeChild($('dup_div'));
545 /* clear any existing dups for this type */
546 iterate( container.getElementsByTagName('div'),
548 if( d.getAttribute('type') == type ) {
549 container.removeChild(d)
557 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
564 function uEditHandleDupResults(ids, search_hash, type, container) {
566 _debug('dup search results: ' + js2JSON(ids));
568 if(!(ids && ids[0])) /* no results */
569 return uEditDupHashes[type] = null;
571 /* add a dup link to the UI and plug in the data */
572 var node = uEditDupTemplate.cloneNode(true);
573 container.appendChild(node);
574 node.setAttribute('type', type);
576 var link = $n(node, 'link');
577 link.setAttribute('type', type);
579 $n(node,'count').appendChild(text(ids.length));
581 for( var o in search_hash )
582 $n(node, 'data').appendChild(
583 text(search_hash[o].value + ' '));
585 uEditDupHashes[type] = search_hash;
589 if(confirm($('ue_dup_ident1').innerHTML))
590 uEditShowSearch(null, type);
596 function uEditShowSearch(link,type) {
597 if(!type) type = link.getAttribute('type');
599 window.xulG.spawn_search(uEditDupHashes[type]);
600 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
603 function uEditMarkCardLost() {
605 for( var c in patron.cards() ) {
607 var card = patron.cards()[c];
608 if( patron.card().id() == card.id() ) {
610 /* de-activite the current card */
614 /* create a new card for the patron */
615 var newcard = new ac();
616 newcard.id(uEditCardVirtId--);
618 patron.card(newcard);
619 patron.cards().push(newcard);
622 /* reset the widget */
623 var field = uEditFindFieldByWId('ue_barcode');
624 field.widget.node.disabled = false;
625 field.widget.node.value = "";
626 field.widget.node.onchange();
627 field.object = newcard;
633 function compactArray(arr) {
635 for( var i = 0; arr && i < arr.length; i++ ) {