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() {
20 session = cgi.param('ses');
21 clone = cgi.param('clone');
22 if(!session) throw "User session is not defined";
25 $('uedit_user').appendChild(text(USER.usrname()));
27 setTimeout( function() {
28 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
31 /* ------------------------------------------------------------------------------ */
33 /* ------------------------------------------------------------------------------ */
34 function uEditFetchIdentTypes() {
35 var req = new Request(FETCH_ID_TYPES);
40 function uEditFetchStatCats() {
41 var req = new Request(SC_FETCH_ALL, SESSION);
46 function uEditFetchSurveys() {
47 var req = new Request(SV_FETCH_ALL, SESSION);
52 function uEditFetchGroups() {
53 var req = new Request(FETCH_GROUPS);
58 function uEditFetchNetLevels() {
59 var req = new Request(FETCH_NET_LEVELS, SESSION);
64 /* ------------------------------------------------------------------------------ */
68 /* fetches necessary and builds the UI */
69 function uEditBuild() {
70 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
72 uEditBuildLibSelector();
73 patron = fetchFleshedUser(cgi.param('usr'));
74 if(!patron) patron = uEditNewPatron();
78 uEditFetchIdentTypes(),
86 if(clone) uEditClone(clone);
87 else uEditCreateNewAddr();
91 $('ue_barcode').disabled = true;
92 unHideMe($('ue_mark_card_lost'));
93 unHideMe($('ue_reset_pw'));
97 if(PERMS['BAR_PATRON'] == -1)
98 $('ue_barred').disabled = true;
102 /* if this user does not have permission to put users into
103 the edited users group, they do not have permission to
105 function uEditCheckEditPerm() {
107 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
108 _debug("editing user with group app perm "+patron.profile()+' : '+
109 groupsCache[patron.profile()].name() +', and perm = ' + perm);
111 if(PERMS[perm] != -1) return;
113 /* we can edit our own account, but not others in our group */
114 if( patron.id() != USER.id() ){
115 _debug("we are not allowed to edit this user");
117 $('ue_save').disabled = true;
118 $('ue_save_clone').disabled = true;
122 if( f && f.widget && f.widget.node )
123 f.widget.node.disabled = true;
129 var node = $('ue_profile').parentNode;
130 node.removeChild($('ue_profile'));
131 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
135 /* creates a new patron object with card attached */
136 var uEditCardVirtId = -1;
137 function uEditNewPatron() {
138 var patron = new au();
142 card.id(uEditCardVirtId--);
145 patron.cards([card]);
146 patron.stat_cat_entries([]);
147 patron.survey_responses([]);
148 patron.addresses([]);
149 patron.home_ou(USER.ws_ou());
150 uEditMakeRandomPw(patron);
154 function uEditMakeRandomPw(patron) {
155 var rand = Math.random();
156 rand = parseInt(rand * 10000) + '';
157 while(rand.length < 4) rand += '0';
158 appendClear($('ue_password_plain'),text(rand));
159 unHideMe($('ue_password_gen'));
164 function uEditResetPw() {
165 var pw = uEditMakeRandomPw(patron);
166 $('ue_password1').value = pw;
167 $('ue_password2').value = pw;
170 function uEditClone(clone) {
172 var cloneUser = fetchFleshedUser(clone);
173 patron.usrgroup(cloneUser.usrgroup());
175 if( cloneUser.day_phone() )
176 $('ue_day_phone').value = cloneUser.day_phone();
177 if( cloneUser.evening_phone() )
178 $('ue_night_phone').value = cloneUser.evening_phone();
179 if( cloneUser.other_phone() )
180 $('ue_other_phone').value = cloneUser.other_phone();
181 setSelector($('ue_org_selector'), cloneUser.home_ou());
184 setSelector($('ue_profile'), cloneUser.profile());
186 /* force the expire date to be set */
187 $('ue_profile').onchange();
189 for( var a in cloneUser.addresses() ) {
190 var addr = cloneUser.addresses()[a];
191 if( cloneUser.mailing_address &&
192 addr.id() == cloneUser.mailing_address().id() )
193 patron.mailing_address(addr);
194 if( cloneUser.billing_address() &&
195 addr.id() == cloneUser.billing_address().id() )
196 patron.billing_address(addr);
197 patron.addresses().push(addr);
200 uEditBuildAddrs(patron);
204 /* Creates a new blank address,
205 adds it to the user and the fields array */
206 var uEditVirtualAddrId = -1;
207 function uEditCreateNewAddr() {
208 var addr = new aua();
209 addr.id(uEditVirtualAddrId--);
211 addr.usr(patron.id());
212 addr.country(defaultCountry);
213 if(!patron.addresses())
214 patron.addresses([]);
215 if(patron.addresses().length == 0) {
216 patron.mailing_address(addr);
217 patron.billing_address(addr);
219 uEditBuildAddrFields(patron, addr);
220 patron.addresses().push(addr);
221 uEditIterateFields(function(f) { uEditCheckValid(f); });
226 /* kicks off the UI drawing */
227 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
228 hideMe($('uedit_loading'));
229 unHideMe($('ue_maintd'));
232 uEditDrawIDTypes(identTypes);
233 uEditDrawGroups(groups);
234 uEditDrawStatCats(statCats);
235 uEditDrawSurveys(surveys);
236 uEditDrawNetLevels(netLevels);
237 uEditDefineData(patron);
239 uEditIterateFields(function(f) { uEditActivateField(f) });
240 uEditIterateFields(function(f) { uEditCheckValid(f); });
245 /** Applies the event handlers and sets the data for the field */
246 function uEditActivateField(field) {
248 if( field.widget.id ) {
249 field.widget.node = $(field.widget.id);
253 $n(field.widget.base, field.widget.name);
256 uEditSetOnchange(field);
258 if(field.widget.onblur) {
259 field.widget.node.onblur =
260 function() { field.widget.onblur(field); };
263 field.widget.node.disabled = field.widget.disabled;
264 if(field.object == null) return;
265 var val = field.object[field.key]();
266 if(val == null) return;
268 if( field.widget.type == 'input' )
269 field.widget.node.value = val;
271 if( field.widget.type == 'select' )
272 setSelector(field.widget.node, val);
274 if( field.widget.type == 'checkbox' )
275 field.widget.node.checked =
276 (val && val != 'f') ? true : false;
278 if( field.widget.onload )
279 field.widget.onload(val);
283 /* set up the onchange event for the field */
284 function uEditSetOnchange(field) {
285 var func = function() {uEditOnChange( field );}
286 field.widget.node.onchange = func;
288 if(field.widget.type != 'select')
289 field.widget.node.onkeyup = func;
292 /* find the current value of the field object's widget */
293 function uEditNodeVal(field) {
294 if(field.widget.type == 'input')
295 return field.widget.node.value;
297 if(field.widget.type == 'checkbox')
298 return field.widget.node.checked;
300 if(field.widget.type == 'select')
301 return getSelectorVal(field.widget.node);
305 /* update a field value */
306 function uEditOnChange(field) {
308 var newval = uEditNodeVal(field);
309 field.object[field.key](newval);
310 field.object.ischanged(1);
312 if(field.widget.onpostchange)
313 field.widget.onpostchange(field, newval);
316 uEditIterateFields(function(f) { uEditCheckValid(f); });
321 function uEditCheckValid(field) {
322 var newval = uEditNodeVal(field);
325 if(field.widget.regex) {
326 if(newval.match(field.widget.regex))
327 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
329 addCSSClass(field.widget.node, CSS_INVALID_DATA);
332 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
338 addCSSClass(field.widget.node, CSS_INVALID_DATA);
341 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
346 /* find a field object by object key */
347 function uEditFindFieldByKey(key) {
348 var fields = grep( dataFields,
349 function(item) { return (item.key == key); });
350 return (fields) ? fields[0] : null;
353 /* find a list of fields by object key */
354 function uEditFindFieldsByKey(key) {
355 return grep( dataFields,
356 function(item) { return (item.key == key); });
359 /* find a field object by widget id */
360 function uEditFindFieldByWId(id) {
361 var fields = grep( dataFields,
362 function(item) { return (item.widget.id == id); });
363 return (fields) ? fields[0] : null;
367 function uEditIterateFields(callback) {
368 for( var f in dataFields )
369 callback(dataFields[f]);
373 function uEditGetErrorStrings() {
378 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
379 var str = $(field.errkey).innerHTML;
380 if(str) errors.push(str);
386 /* munge up something for all of the required surveys
387 (which are not registered with the fields) */
388 var rows = $('ue_survey_table').getElementsByTagName('tr');
389 if( patron.isnew() ) {
390 for( var r in rows ) {
393 var sel = $n(row, 'ue_survey_answer');
395 var qstn = row.getAttribute('question');
398 qstn = surveyQuestionsCache[qstn];
399 survey = surveysCache[qstn.survey()];
400 var val = getSelectorVal(sel);
401 if(!val && isTrue(survey.required()))
402 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
407 /* ------------------------------------------------------------ */
409 if(errors[0]) return errors;
413 function uEditAlertErrors() {
414 var errors = uEditGetErrorStrings();
415 if(!errors) return false;
416 alert(errors.join("\n"));
421 /* send the user to the database */
422 function uEditSaveUser(cloneme) {
424 if(uEditGetErrorStrings()) {
429 /* null is unique in the db, but '' is not */
430 if( ! patron.ident_value() ) patron.ident_value(null);
431 if( ! patron.ident_value2() ) patron.ident_value2(null);
433 var req = new Request(UPDATE_PATRON, SESSION, patron);
434 req.alertEvent = false;
436 var newuser = req.result();
439 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
440 if(evt) alert(js2JSON(newuser));
445 /* if it's a new user and a guardian note was created for this user,
446 create the note after we have the new user's id */
448 guardianNote.usr( newuser.id() );
449 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
450 req.alertEvent = false;
452 var resp = req.result();
453 if( checkILSEvent(resp) ) {
455 "Error creating patron guardian/parent note");
460 alert($('ue_success').innerHTML);
464 /* if the user we just created was a clone, and we want to clone it,
465 we really want to clone the original */
466 if( clone ) cloneme = clone;
467 else cloneme = newuser.id();
470 if (window.xulG && typeof window.xulG.on_save == 'function') {
471 window.xulG.on_save(newuser, cloneme);
475 var href = location.href;
477 href = href.replace(/\&?usr=\d+/, '');
478 href = href.replace(/\&?clone=\d+/, '');
480 if( cloneme ) href += '&clone=' + cloneme;
481 location.href = href;
485 function uEditCancel() {
486 var href = location.href;
487 href = href.replace(/\&?usr=\d+/, '');
488 href = href.replace(/\&?clone=\d+/, '');
489 var id = cgi.param('usr')
490 /* reload the current user if available */
491 if( id ) href += "&usr=" + id;
492 location.href = href;
496 var uEditDupHashes = {};
497 var uEditDupTemplate;
499 function uEditRunDupeSearch(type, search_hash) {
501 if(!patron.isnew()) return;
503 _debug('dup search: ' + js2JSON(search_hash));
505 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
507 var container = $('dup_div_container');
508 if(!uEditDupTemplate)
509 uEditDupTemplate = container.removeChild($('dup_div'));
511 /* clear any existing dups for this type */
512 iterate( container.getElementsByTagName('div'),
514 if( d.getAttribute('type') == type ) {
515 container.removeChild(d)
523 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
530 function uEditHandleDupResults(ids, search_hash, type, container) {
532 _debug('dup search results: ' + js2JSON(ids));
534 if(!(ids && ids[0])) /* no results */
535 return uEditDupHashes[type] = null;
537 /* add a dup link to the UI and plug in the data */
538 var node = uEditDupTemplate.cloneNode(true);
539 container.appendChild(node);
540 node.setAttribute('type', type);
542 var link = $n(node, 'link');
543 link.setAttribute('type', type);
545 $n(node,'count').appendChild(text(ids.length));
547 for( var o in search_hash )
548 $n(node, 'data').appendChild(
549 text(search_hash[o].value + ' '));
551 uEditDupHashes[type] = search_hash;
555 if(confirm($('ue_dup_ident1').innerHTML))
556 uEditShowSearch(null, type);
562 function uEditShowSearch(link,type) {
563 if(!type) type = link.getAttribute('type');
565 window.xulG.spawn_search(uEditDupHashes[type]);
566 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
569 function uEditMarkCardLost() {
571 for( var c in patron.cards() ) {
573 var card = patron.cards()[c];
574 if( patron.card().id() == card.id() ) {
576 /* de-activite the current card */
580 /* create a new card for the patron */
581 var newcard = new ac();
582 newcard.id(uEditCardVirtId--);
584 patron.card(newcard);
585 patron.cards().push(newcard);
588 /* reset the widget */
589 var field = uEditFindFieldByWId('ue_barcode');
590 field.widget.node.disabled = false;
591 field.widget.node.value = "";
592 field.widget.node.onchange();
593 field.object = newcard;
599 function compactArray(arr) {
601 for( var i = 0; arr && i < arr.length; i++ ) {