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()));
137 /* creates a new patron object with card attached */
138 var uEditCardVirtId = -1;
139 function uEditNewPatron() {
140 var patron = new au();
144 card.id(uEditCardVirtId--);
147 patron.cards([card]);
148 patron.stat_cat_entries([]);
149 patron.survey_responses([]);
150 patron.addresses([]);
151 patron.home_ou(USER.ws_ou());
152 uEditMakeRandomPw(patron);
156 function uEditMakeRandomPw(patron) {
157 var rand = Math.random();
158 rand = parseInt(rand * 10000) + '';
159 while(rand.length < 4) rand += '0';
160 appendClear($('ue_password_plain'),text(rand));
161 unHideMe($('ue_password_gen'));
166 function uEditResetPw() {
167 var pw = uEditMakeRandomPw(patron);
168 $('ue_password1').value = pw;
169 $('ue_password2').value = pw;
172 function uEditClone(clone) {
174 var cloneUser = fetchFleshedUser(clone);
175 patron.usrgroup(cloneUser.usrgroup());
177 if( cloneUser.day_phone() )
178 $('ue_day_phone').value = cloneUser.day_phone();
179 if( cloneUser.evening_phone() )
180 $('ue_night_phone').value = cloneUser.evening_phone();
181 if( cloneUser.other_phone() )
182 $('ue_other_phone').value = cloneUser.other_phone();
183 setSelector($('ue_org_selector'), cloneUser.home_ou());
186 setSelector($('ue_profile'), cloneUser.profile());
188 /* force the expire date to be set */
189 $('ue_profile').onchange();
191 for( var a in cloneUser.addresses() ) {
192 var addr = cloneUser.addresses()[a];
193 if( cloneUser.mailing_address &&
194 addr.id() == cloneUser.mailing_address().id() )
195 patron.mailing_address(addr);
196 if( cloneUser.billing_address() &&
197 addr.id() == cloneUser.billing_address().id() )
198 patron.billing_address(addr);
199 patron.addresses().push(addr);
202 uEditBuildAddrs(patron);
206 /* Creates a new blank address,
207 adds it to the user and the fields array */
208 var uEditVirtualAddrId = -1;
209 function uEditCreateNewAddr() {
210 var addr = new aua();
212 addr.id(uEditVirtualAddrId--);
214 addr.usr(patron.id());
215 addr.country(defaultCountry);
217 if(!patron.addresses())
218 patron.addresses([]);
220 if(patron.addresses().length == 0) {
221 patron.mailing_address(addr);
222 patron.billing_address(addr);
226 addr.within_city_limits(1);
228 uEditBuildAddrFields(patron, addr);
229 patron.addresses().push(addr);
230 uEditIterateFields(function(f) { uEditCheckValid(f); });
235 /* kicks off the UI drawing */
236 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
237 hideMe($('uedit_loading'));
238 unHideMe($('ue_maintd'));
241 uEditDrawIDTypes(identTypes);
242 uEditDrawGroups(groups);
243 uEditDrawStatCats(statCats);
244 uEditDrawSurveys(surveys);
245 uEditDrawNetLevels(netLevels);
246 uEditDefineData(patron);
248 uEditIterateFields(function(f) { uEditActivateField(f) });
249 uEditIterateFields(function(f) { uEditCheckValid(f); });
254 /** Applies the event handlers and sets the data for the field */
255 function uEditActivateField(field) {
257 if( field.widget.id ) {
258 field.widget.node = $(field.widget.id);
262 $n(field.widget.base, field.widget.name);
265 uEditSetOnchange(field);
267 if(field.widget.onblur) {
268 field.widget.node.onblur =
269 function() { field.widget.onblur(field); };
272 field.widget.node.disabled = field.widget.disabled;
273 if(field.object == null) return;
274 var val = field.object[field.key]();
275 if(val == null) return;
277 if( field.widget.type == 'input' )
278 field.widget.node.value = val;
280 if( field.widget.type == 'select' )
281 setSelector(field.widget.node, val);
283 if( field.widget.type == 'checkbox' )
284 field.widget.node.checked =
285 (val && val != 'f') ? true : false;
287 if( field.widget.onload )
288 field.widget.onload(val);
292 /* set up the onchange event for the field */
293 function uEditSetOnchange(field) {
294 var func = function() {uEditOnChange( field );}
295 field.widget.node.onchange = func;
297 if(field.widget.type != 'select')
298 field.widget.node.onkeyup = func;
301 /* find the current value of the field object's widget */
302 function uEditNodeVal(field) {
303 if(field.widget.type == 'input')
304 return field.widget.node.value;
306 if(field.widget.type == 'checkbox')
307 return field.widget.node.checked;
309 if(field.widget.type == 'select')
310 return getSelectorVal(field.widget.node);
314 /* update a field value */
315 function uEditOnChange(field) {
317 var newval = uEditNodeVal(field);
318 field.object[field.key](newval);
319 field.object.ischanged(1);
321 if(field.widget.onpostchange)
322 field.widget.onpostchange(field, newval);
325 uEditIterateFields(function(f) { uEditCheckValid(f); });
330 function uEditCheckValid(field) {
331 var newval = uEditNodeVal(field);
334 if(field.widget.regex) {
335 if(newval.match(field.widget.regex))
336 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
338 addCSSClass(field.widget.node, CSS_INVALID_DATA);
341 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
347 addCSSClass(field.widget.node, CSS_INVALID_DATA);
350 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
355 /* find a field object by object key */
356 function uEditFindFieldByKey(key) {
357 var fields = grep( dataFields,
358 function(item) { return (item.key == key); });
359 return (fields) ? fields[0] : null;
362 /* find a list of fields by object key */
363 function uEditFindFieldsByKey(key) {
364 return grep( dataFields,
365 function(item) { return (item.key == key); });
368 /* find a field object by widget id */
369 function uEditFindFieldByWId(id) {
370 var fields = grep( dataFields,
371 function(item) { return (item.widget.id == id); });
372 return (fields) ? fields[0] : null;
376 function uEditIterateFields(callback) {
377 for( var f in dataFields )
378 callback(dataFields[f]);
382 function uEditGetErrorStrings() {
387 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
388 var str = $(field.errkey).innerHTML;
389 if(str) errors.push(str);
395 /* munge up something for all of the required surveys
396 (which are not registered with the fields) */
397 if( patron.isnew() ) {
398 var sel = $('ue_survey_table');
401 var rows = sel.getElementsByTagName('tr');
403 for( var r in rows ) {
406 var sel = $n(row, 'ue_survey_answer');
408 var qstn = row.getAttribute('question');
411 qstn = surveyQuestionsCache[qstn];
412 survey = surveysCache[qstn.survey()];
413 var val = getSelectorVal(sel);
414 if(!val && isTrue(survey.required()))
415 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
421 /* ------------------------------------------------------------ */
423 if(errors[0]) return errors;
427 function uEditAlertErrors() {
428 var errors = uEditGetErrorStrings();
429 if(!errors) return false;
430 alert(errors.join("\n"));
435 /* send the user to the database */
436 function uEditSaveUser(cloneme) {
438 if(uEditGetErrorStrings()) {
443 /* null is unique in the db, but '' is not */
444 if( ! patron.ident_value() ) patron.ident_value(null);
445 if( ! patron.ident_type2() ) patron.ident_type2(null);
446 if( ! patron.ident_value2() ) patron.ident_value2(null);
448 if(! patron.dob() ) patron.dob(null);
450 var req = new Request(UPDATE_PATRON, SESSION, patron);
451 req.alertEvent = false;
453 var newuser = req.result();
456 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
457 if(evt) alert(js2JSON(newuser));
462 /* if it's a new user and a guardian note was created for this user,
463 create the note after we have the new user's id */
465 guardianNote.usr( newuser.id() );
466 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
467 req.alertEvent = false;
469 var resp = req.result();
470 if( checkILSEvent(resp) ) {
472 "Error creating patron guardian/parent note");
477 alert($('ue_success').innerHTML);
481 /* if the user we just created was a clone, and we want to clone it,
482 we really want to clone the original */
483 if( clone ) cloneme = clone;
484 else cloneme = newuser.id();
487 if (window.xulG && typeof window.xulG.on_save == 'function') {
488 window.xulG.on_save(newuser, cloneme);
492 var href = location.href;
494 href = href.replace(/\&?usr=\d+/, '');
495 href = href.replace(/\&?clone=\d+/, '');
497 if( cloneme ) href += '&clone=' + cloneme;
498 location.href = href;
502 function uEditCancel() {
503 var href = location.href;
504 href = href.replace(/\&?usr=\d+/, '');
505 href = href.replace(/\&?clone=\d+/, '');
506 var id = cgi.param('usr')
507 /* reload the current user if available */
508 if( id ) href += "&usr=" + id;
509 location.href = href;
513 var uEditDupHashes = {};
514 var uEditDupTemplate;
516 function uEditRunDupeSearch(type, search_hash) {
518 if(!patron.isnew()) return;
520 _debug('dup search: ' + js2JSON(search_hash));
522 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
524 var container = $('dup_div_container');
525 if(!uEditDupTemplate)
526 uEditDupTemplate = container.removeChild($('dup_div'));
528 /* clear any existing dups for this type */
529 iterate( container.getElementsByTagName('div'),
531 if( d.getAttribute('type') == type ) {
532 container.removeChild(d)
540 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
547 function uEditHandleDupResults(ids, search_hash, type, container) {
549 _debug('dup search results: ' + js2JSON(ids));
551 if(!(ids && ids[0])) /* no results */
552 return uEditDupHashes[type] = null;
554 /* add a dup link to the UI and plug in the data */
555 var node = uEditDupTemplate.cloneNode(true);
556 container.appendChild(node);
557 node.setAttribute('type', type);
559 var link = $n(node, 'link');
560 link.setAttribute('type', type);
562 $n(node,'count').appendChild(text(ids.length));
564 for( var o in search_hash )
565 $n(node, 'data').appendChild(
566 text(search_hash[o].value + ' '));
568 uEditDupHashes[type] = search_hash;
572 if(confirm($('ue_dup_ident1').innerHTML))
573 uEditShowSearch(null, type);
579 function uEditShowSearch(link,type) {
580 if(!type) type = link.getAttribute('type');
582 window.xulG.spawn_search(uEditDupHashes[type]);
583 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
586 function uEditMarkCardLost() {
588 for( var c in patron.cards() ) {
590 var card = patron.cards()[c];
591 if( patron.card().id() == card.id() ) {
593 /* de-activite the current card */
597 /* create a new card for the patron */
598 var newcard = new ac();
599 newcard.id(uEditCardVirtId--);
601 patron.card(newcard);
602 patron.cards().push(newcard);
605 /* reset the widget */
606 var field = uEditFindFieldByWId('ue_barcode');
607 field.widget.node.disabled = false;
608 field.widget.node.value = "";
609 field.widget.node.onchange();
610 field.object = newcard;
616 function compactArray(arr) {
618 for( var i = 0; arr && i < arr.length; i++ ) {