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 var req = new Request(FETCH_ID_TYPES);
42 function uEditFetchStatCats() {
43 var req = new Request(SC_FETCH_ALL, SESSION);
48 function uEditFetchSurveys() {
49 var req = new Request(SV_FETCH_ALL, SESSION);
54 function uEditFetchGroups() {
55 var req = new Request(FETCH_GROUPS);
60 function uEditFetchNetLevels() {
61 var req = new Request(FETCH_NET_LEVELS, SESSION);
66 /* ------------------------------------------------------------------------------ */
70 /* fetches necessary and builds the UI */
71 function uEditBuild() {
72 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
74 uEditBuildLibSelector();
75 patron = fetchFleshedUser(cgi.param('usr'));
76 if(!patron) patron = uEditNewPatron();
80 uEditFetchIdentTypes(),
88 if(clone) uEditClone(clone);
89 else uEditCreateNewAddr();
93 $('ue_barcode').disabled = true;
94 unHideMe($('ue_mark_card_lost'));
95 unHideMe($('ue_reset_pw'));
99 if(PERMS['BAR_PATRON'] == -1)
100 $('ue_barred').disabled = true;
104 /* if this user does not have permission to put users into
105 the edited users group, they do not have permission to
107 function uEditCheckEditPerm() {
109 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
110 _debug("editing user with group app perm "+patron.profile()+' : '+
111 groupsCache[patron.profile()].name() +', and perm = ' + perm);
113 if(PERMS[perm] != -1) return;
115 /* we can edit our own account, but not others in our group */
116 if( patron.id() != USER.id() ){
117 _debug("we are not allowed to edit this user");
119 $('ue_save').disabled = true;
120 $('ue_save_clone').disabled = true;
124 if( f && f.widget && f.widget.node )
125 f.widget.node.disabled = true;
131 var node = $('ue_profile').parentNode;
132 node.removeChild($('ue_profile'));
133 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
135 var field = uEditFindFieldByKey('profile');
136 field.required = false;
141 /* creates a new patron object with card attached */
142 var uEditCardVirtId = -1;
143 function uEditNewPatron() {
144 var patron = new au();
148 card.id(uEditCardVirtId--);
151 patron.cards([card]);
152 patron.stat_cat_entries([]);
153 patron.survey_responses([]);
154 patron.addresses([]);
155 patron.home_ou(USER.ws_ou());
156 uEditMakeRandomPw(patron);
160 function uEditMakeRandomPw(patron) {
161 var rand = Math.random();
162 rand = parseInt(rand * 10000) + '';
163 while(rand.length < 4) rand += '0';
164 appendClear($('ue_password_plain'),text(rand));
165 unHideMe($('ue_password_gen'));
170 function uEditResetPw() {
171 var pw = uEditMakeRandomPw(patron);
172 $('ue_password1').value = pw;
173 $('ue_password2').value = pw;
176 function uEditClone(clone) {
178 var cloneUser = fetchFleshedUser(clone);
179 patron.usrgroup(cloneUser.usrgroup());
181 if( cloneUser.day_phone() )
182 $('ue_day_phone').value = cloneUser.day_phone();
183 if( cloneUser.evening_phone() )
184 $('ue_night_phone').value = cloneUser.evening_phone();
185 if( cloneUser.other_phone() )
186 $('ue_other_phone').value = cloneUser.other_phone();
187 setSelector($('ue_org_selector'), cloneUser.home_ou());
190 setSelector($('ue_profile'), cloneUser.profile());
192 /* force the expire date to be set */
193 $('ue_profile').onchange();
195 for( var a in cloneUser.addresses() ) {
196 var addr = cloneUser.addresses()[a];
197 if( cloneUser.mailing_address &&
198 addr.id() == cloneUser.mailing_address().id() )
199 patron.mailing_address(addr);
200 if( cloneUser.billing_address() &&
201 addr.id() == cloneUser.billing_address().id() )
202 patron.billing_address(addr);
203 patron.addresses().push(addr);
206 uEditBuildAddrs(patron);
210 /* Creates a new blank address,
211 adds it to the user and the fields array */
212 var uEditVirtualAddrId = -1;
213 function uEditCreateNewAddr() {
214 var addr = new aua();
216 addr.id(uEditVirtualAddrId--);
218 addr.usr(patron.id());
219 addr.country(defaultCountry);
221 if(!patron.addresses())
222 patron.addresses([]);
224 if(patron.addresses().length == 0) {
225 patron.mailing_address(addr);
226 patron.billing_address(addr);
230 addr.within_city_limits(1);
232 uEditBuildAddrFields(patron, addr);
233 patron.addresses().push(addr);
234 uEditIterateFields(function(f) { uEditCheckValid(f); });
239 /* kicks off the UI drawing */
240 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
241 hideMe($('uedit_loading'));
242 unHideMe($('ue_maintd'));
245 uEditDrawIDTypes(identTypes);
246 uEditDrawGroups(groups);
247 uEditDrawStatCats(statCats);
248 uEditDrawSurveys(surveys);
249 uEditDrawNetLevels(netLevels);
250 uEditDefineData(patron);
252 uEditIterateFields(function(f) { uEditActivateField(f) });
253 uEditIterateFields(function(f) { uEditCheckValid(f); });
258 /** Applies the event handlers and sets the data for the field */
259 function uEditActivateField(field) {
261 if( field.widget.id ) {
262 field.widget.node = $(field.widget.id);
266 $n(field.widget.base, field.widget.name);
269 uEditSetOnchange(field);
271 if(field.widget.onblur) {
272 field.widget.node.onblur =
273 function() { field.widget.onblur(field); };
276 field.widget.node.disabled = field.widget.disabled;
277 if(field.object == null) return;
278 var val = field.object[field.key]();
279 if(val == null) return;
281 if( field.widget.type == 'input' )
282 field.widget.node.value = val;
284 if( field.widget.type == 'select' )
285 setSelector(field.widget.node, val);
287 if( field.widget.type == 'checkbox' )
288 field.widget.node.checked =
289 (val && val != 'f') ? true : false;
291 if( field.widget.onload )
292 field.widget.onload(val);
296 /* set up the onchange event for the field */
297 function uEditSetOnchange(field) {
298 var func = function() {uEditOnChange( field );}
299 field.widget.node.onchange = func;
301 if(field.widget.type != 'select')
302 field.widget.node.onkeyup = func;
305 /* find the current value of the field object's widget */
306 function uEditNodeVal(field) {
307 if(field.widget.type == 'input')
308 return field.widget.node.value;
310 if(field.widget.type == 'checkbox')
311 return field.widget.node.checked;
313 if(field.widget.type == 'select')
314 return getSelectorVal(field.widget.node);
318 /* update a field value */
319 function uEditOnChange(field) {
321 var newval = uEditNodeVal(field);
322 field.object[field.key](newval);
323 field.object.ischanged(1);
325 if(field.widget.onpostchange)
326 field.widget.onpostchange(field, newval);
329 uEditIterateFields(function(f) { uEditCheckValid(f); });
334 function uEditCheckValid(field) {
335 var newval = uEditNodeVal(field);
339 if(field.widget.regex) {
340 if(newval.match(field.widget.regex))
341 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
343 addCSSClass(field.widget.node, CSS_INVALID_DATA);
346 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
352 addCSSClass(field.widget.node, CSS_INVALID_DATA);
355 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
360 /* find a field object by object key */
361 function uEditFindFieldByKey(key) {
362 var fields = grep( dataFields,
363 function(item) { return (item.key == key); });
364 return (fields) ? fields[0] : null;
367 /* find a list of fields by object key */
368 function uEditFindFieldsByKey(key) {
369 return grep( dataFields,
370 function(item) { return (item.key == key); });
373 /* find a field object by widget id */
374 function uEditFindFieldByWId(id) {
375 var fields = grep( dataFields,
376 function(item) { return (item.widget.id == id); });
377 return (fields) ? fields[0] : null;
381 function uEditIterateFields(callback) {
382 for( var f in dataFields )
383 callback(dataFields[f]);
387 function uEditGetErrorStrings() {
392 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
393 var str = $(field.errkey).innerHTML;
394 if(str) errors.push(str);
400 /* munge up something for all of the required surveys
401 (which are not registered with the fields) */
402 if( patron.isnew() ) {
403 var sel = $('ue_survey_table');
406 var rows = sel.getElementsByTagName('tr');
408 for( var r in rows ) {
411 var sel = $n(row, 'ue_survey_answer');
413 var qstn = row.getAttribute('question');
416 qstn = surveyQuestionsCache[qstn];
417 survey = surveysCache[qstn.survey()];
418 var val = getSelectorVal(sel);
419 if(!val && isTrue(survey.required()))
420 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
426 /* ------------------------------------------------------------ */
428 if(errors[0]) return errors;
432 function uEditAlertErrors() {
433 var errors = uEditGetErrorStrings();
434 if(!errors) return false;
435 alert(errors.join("\n"));
440 /* send the user to the database */
441 function uEditSaveUser(cloneme) {
443 if(uEditGetErrorStrings()) {
448 /* null is unique in the db, but '' is not */
449 if( ! patron.ident_value() ) patron.ident_value(null);
450 if( ! patron.ident_type2() ) patron.ident_type2(null);
451 if( ! patron.ident_value2() ) patron.ident_value2(null);
453 if(! patron.dob() ) patron.dob(null);
455 var req = new Request(UPDATE_PATRON, SESSION, patron);
456 req.alertEvent = false;
458 var newuser = req.result();
461 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
462 if(evt) alert(js2JSON(newuser));
467 /* if it's a new user and a guardian note was created for this user,
468 create the note after we have the new user's id */
470 guardianNote.usr( newuser.id() );
471 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
472 req.alertEvent = false;
474 var resp = req.result();
475 if( checkILSEvent(resp) ) {
477 "Error creating patron guardian/parent note");
482 alert($('ue_success').innerHTML);
486 /* if the user we just created was a clone, and we want to clone it,
487 we really want to clone the original */
488 if( clone ) cloneme = clone;
489 else cloneme = newuser.id();
492 if (window.xulG && typeof window.xulG.on_save == 'function') {
493 window.xulG.on_save(newuser, cloneme);
497 var href = location.href;
499 href = href.replace(/\&?usr=\d+/, '');
500 href = href.replace(/\&?clone=\d+/, '');
502 if( cloneme ) href += '&clone=' + cloneme;
503 location.href = href;
507 function uEditCancel() {
508 var href = location.href;
509 href = href.replace(/\&?usr=\d+/, '');
510 href = href.replace(/\&?clone=\d+/, '');
511 var id = cgi.param('usr')
512 /* reload the current user if available */
513 if( id ) href += "&usr=" + id;
514 location.href = href;
518 var uEditDupHashes = {};
519 var uEditDupTemplate;
521 function uEditRunDupeSearch(type, search_hash) {
523 if(!patron.isnew()) return;
525 _debug('dup search: ' + js2JSON(search_hash));
527 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
529 var container = $('dup_div_container');
530 if(!uEditDupTemplate)
531 uEditDupTemplate = container.removeChild($('dup_div'));
533 /* clear any existing dups for this type */
534 iterate( container.getElementsByTagName('div'),
536 if( d.getAttribute('type') == type ) {
537 container.removeChild(d)
545 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
552 function uEditHandleDupResults(ids, search_hash, type, container) {
554 _debug('dup search results: ' + js2JSON(ids));
556 if(!(ids && ids[0])) /* no results */
557 return uEditDupHashes[type] = null;
559 /* add a dup link to the UI and plug in the data */
560 var node = uEditDupTemplate.cloneNode(true);
561 container.appendChild(node);
562 node.setAttribute('type', type);
564 var link = $n(node, 'link');
565 link.setAttribute('type', type);
567 $n(node,'count').appendChild(text(ids.length));
569 for( var o in search_hash )
570 $n(node, 'data').appendChild(
571 text(search_hash[o].value + ' '));
573 uEditDupHashes[type] = search_hash;
577 if(confirm($('ue_dup_ident1').innerHTML))
578 uEditShowSearch(null, type);
584 function uEditShowSearch(link,type) {
585 if(!type) type = link.getAttribute('type');
587 window.xulG.spawn_search(uEditDupHashes[type]);
588 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
591 function uEditMarkCardLost() {
593 for( var c in patron.cards() ) {
595 var card = patron.cards()[c];
596 if( patron.card().id() == card.id() ) {
598 /* de-activite the current card */
602 /* create a new card for the patron */
603 var newcard = new ac();
604 newcard.id(uEditCardVirtId--);
606 patron.card(newcard);
607 patron.cards().push(newcard);
610 /* reset the widget */
611 var field = uEditFindFieldByWId('ue_barcode');
612 field.widget.node.disabled = false;
613 field.widget.node.value = "";
614 field.widget.node.onchange();
615 field.object = newcard;
621 function compactArray(arr) {
623 for( var i = 0; arr && i < arr.length; i++ ) {