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;
137 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
142 /* creates a new patron object with card attached */
143 var uEditCardVirtId = -1;
144 function uEditNewPatron() {
145 var patron = new au();
149 card.id(uEditCardVirtId--);
152 patron.cards([card]);
153 patron.stat_cat_entries([]);
154 patron.survey_responses([]);
155 patron.addresses([]);
156 patron.home_ou(USER.ws_ou());
157 uEditMakeRandomPw(patron);
161 function uEditMakeRandomPw(patron) {
162 var rand = Math.random();
163 rand = parseInt(rand * 10000) + '';
164 while(rand.length < 4) rand += '0';
165 appendClear($('ue_password_plain'),text(rand));
166 unHideMe($('ue_password_gen'));
171 function uEditResetPw() {
172 var pw = uEditMakeRandomPw(patron);
173 $('ue_password1').value = pw;
174 $('ue_password2').value = pw;
177 function uEditClone(clone) {
179 var cloneUser = fetchFleshedUser(clone);
180 patron.usrgroup(cloneUser.usrgroup());
182 if( cloneUser.day_phone() )
183 $('ue_day_phone').value = cloneUser.day_phone();
184 if( cloneUser.evening_phone() )
185 $('ue_night_phone').value = cloneUser.evening_phone();
186 if( cloneUser.other_phone() )
187 $('ue_other_phone').value = cloneUser.other_phone();
188 setSelector($('ue_org_selector'), cloneUser.home_ou());
191 setSelector($('ue_profile'), cloneUser.profile());
193 /* force the expire date to be set */
194 $('ue_profile').onchange();
196 for( var a in cloneUser.addresses() ) {
197 var addr = cloneUser.addresses()[a];
198 if( cloneUser.mailing_address &&
199 addr.id() == cloneUser.mailing_address().id() )
200 patron.mailing_address(addr);
201 if( cloneUser.billing_address() &&
202 addr.id() == cloneUser.billing_address().id() )
203 patron.billing_address(addr);
204 patron.addresses().push(addr);
207 uEditBuildAddrs(patron);
211 /* Creates a new blank address,
212 adds it to the user and the fields array */
213 var uEditVirtualAddrId = -1;
214 function uEditCreateNewAddr() {
215 var addr = new aua();
217 addr.id(uEditVirtualAddrId--);
219 addr.usr(patron.id());
220 addr.country(defaultCountry);
222 if(!patron.addresses())
223 patron.addresses([]);
225 if(patron.addresses().length == 0) {
226 patron.mailing_address(addr);
227 patron.billing_address(addr);
231 addr.within_city_limits(1);
233 uEditBuildAddrFields(patron, addr);
234 patron.addresses().push(addr);
235 uEditIterateFields(function(f) { uEditCheckValid(f); });
240 /* kicks off the UI drawing */
241 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
242 hideMe($('uedit_loading'));
243 unHideMe($('ue_maintd'));
246 uEditDrawIDTypes(identTypes);
247 uEditDrawGroups(groups);
248 uEditDrawStatCats(statCats);
249 uEditDrawSurveys(surveys);
250 uEditDrawNetLevels(netLevels);
251 uEditDefineData(patron);
253 uEditIterateFields(function(f) { uEditActivateField(f) });
254 uEditIterateFields(function(f) { uEditCheckValid(f); });
259 /** Applies the event handlers and sets the data for the field */
260 function uEditActivateField(field) {
262 if( field.widget.id ) {
263 field.widget.node = $(field.widget.id);
267 $n(field.widget.base, field.widget.name);
270 uEditSetOnchange(field);
272 if(field.widget.onblur) {
273 field.widget.node.onblur =
274 function() { field.widget.onblur(field); };
277 field.widget.node.disabled = field.widget.disabled;
278 if(field.object == null) return;
279 var val = field.object[field.key]();
280 if(val == null) return;
282 if( field.widget.type == 'input' )
283 field.widget.node.value = val;
285 if( field.widget.type == 'select' )
286 setSelector(field.widget.node, val);
288 if( field.widget.type == 'checkbox' )
289 field.widget.node.checked =
290 (val && val != 'f') ? true : false;
292 if( field.widget.onload )
293 field.widget.onload(val);
297 /* set up the onchange event for the field */
298 function uEditSetOnchange(field) {
299 var func = function() {uEditOnChange( field );}
300 field.widget.node.onchange = func;
302 if(field.widget.type != 'select')
303 field.widget.node.onkeyup = func;
306 /* find the current value of the field object's widget */
307 function uEditNodeVal(field) {
308 if(field.widget.type == 'input')
309 return field.widget.node.value;
311 if(field.widget.type == 'checkbox')
312 return field.widget.node.checked;
314 if(field.widget.type == 'select')
315 return getSelectorVal(field.widget.node);
319 /* update a field value */
320 function uEditOnChange(field) {
322 var newval = uEditNodeVal(field);
323 field.object[field.key](newval);
324 field.object.ischanged(1);
326 if(field.widget.onpostchange)
327 field.widget.onpostchange(field, newval);
330 uEditIterateFields(function(f) { uEditCheckValid(f); });
335 function uEditCheckValid(field) {
336 var newval = uEditNodeVal(field);
340 if(field.widget.regex) {
341 if(newval.match(field.widget.regex))
342 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
344 addCSSClass(field.widget.node, CSS_INVALID_DATA);
347 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
353 addCSSClass(field.widget.node, CSS_INVALID_DATA);
356 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
361 /* find a field object by object key */
362 function uEditFindFieldByKey(key) {
363 var fields = grep( dataFields,
364 function(item) { return (item.key == key); });
365 return (fields) ? fields[0] : null;
368 /* find a list of fields by object key */
369 function uEditFindFieldsByKey(key) {
370 return grep( dataFields,
371 function(item) { return (item.key == key); });
374 /* find a field object by widget id */
375 function uEditFindFieldByWId(id) {
376 var fields = grep( dataFields,
377 function(item) { return (item.widget.id == id); });
378 return (fields) ? fields[0] : null;
382 function uEditIterateFields(callback) {
383 for( var f in dataFields )
384 callback(dataFields[f]);
388 function uEditGetErrorStrings() {
393 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
394 var str = $(field.errkey).innerHTML;
395 if(str) errors.push(str);
401 /* munge up something for all of the required surveys
402 (which are not registered with the fields) */
403 if( patron.isnew() ) {
404 var sel = $('ue_survey_table');
407 var rows = sel.getElementsByTagName('tr');
409 for( var r in rows ) {
412 var sel = $n(row, 'ue_survey_answer');
414 var qstn = row.getAttribute('question');
417 qstn = surveyQuestionsCache[qstn];
418 survey = surveysCache[qstn.survey()];
419 var val = getSelectorVal(sel);
420 if(!val && isTrue(survey.required()))
421 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
427 /* ------------------------------------------------------------ */
429 if(errors[0]) return errors;
433 function uEditAlertErrors() {
434 var errors = uEditGetErrorStrings();
435 if(!errors) return false;
436 alert(errors.join("\n"));
441 /* send the user to the database */
442 function uEditSaveUser(cloneme) {
444 if(uEditGetErrorStrings()) {
449 /* null is unique in the db, but '' is not */
450 if( ! patron.ident_value() ) patron.ident_value(null);
451 if( ! patron.ident_type2() ) patron.ident_type2(null);
452 if( ! patron.ident_value2() ) patron.ident_value2(null);
454 if(! patron.dob() ) patron.dob(null);
456 var req = new Request(UPDATE_PATRON, SESSION, patron);
457 req.alertEvent = false;
459 var newuser = req.result();
462 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
463 if(evt) alert(js2JSON(newuser));
468 /* if it's a new user and a guardian note was created for this user,
469 create the note after we have the new user's id */
471 guardianNote.usr( newuser.id() );
472 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
473 req.alertEvent = false;
475 var resp = req.result();
476 if( checkILSEvent(resp) ) {
478 "Error creating patron guardian/parent note");
483 alert($('ue_success').innerHTML);
487 /* if the user we just created was a clone, and we want to clone it,
488 we really want to clone the original */
489 if( clone ) cloneme = clone;
490 else cloneme = newuser.id();
493 if (window.xulG && typeof window.xulG.on_save == 'function') {
494 window.xulG.on_save(newuser, cloneme);
498 var href = location.href;
500 href = href.replace(/\&?usr=\d+/, '');
501 href = href.replace(/\&?clone=\d+/, '');
503 if( cloneme ) href += '&clone=' + cloneme;
504 location.href = href;
508 function uEditCancel() {
509 var href = location.href;
510 href = href.replace(/\&?usr=\d+/, '');
511 href = href.replace(/\&?clone=\d+/, '');
512 var id = cgi.param('usr')
513 /* reload the current user if available */
514 if( id ) href += "&usr=" + id;
515 location.href = href;
519 var uEditDupHashes = {};
520 var uEditDupTemplate;
522 function uEditRunDupeSearch(type, search_hash) {
524 if(!patron.isnew()) return;
526 _debug('dup search: ' + js2JSON(search_hash));
528 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
530 var container = $('dup_div_container');
531 if(!uEditDupTemplate)
532 uEditDupTemplate = container.removeChild($('dup_div'));
534 /* clear any existing dups for this type */
535 iterate( container.getElementsByTagName('div'),
537 if( d.getAttribute('type') == type ) {
538 container.removeChild(d)
546 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
553 function uEditHandleDupResults(ids, search_hash, type, container) {
555 _debug('dup search results: ' + js2JSON(ids));
557 if(!(ids && ids[0])) /* no results */
558 return uEditDupHashes[type] = null;
560 /* add a dup link to the UI and plug in the data */
561 var node = uEditDupTemplate.cloneNode(true);
562 container.appendChild(node);
563 node.setAttribute('type', type);
565 var link = $n(node, 'link');
566 link.setAttribute('type', type);
568 $n(node,'count').appendChild(text(ids.length));
570 for( var o in search_hash )
571 $n(node, 'data').appendChild(
572 text(search_hash[o].value + ' '));
574 uEditDupHashes[type] = search_hash;
578 if(confirm($('ue_dup_ident1').innerHTML))
579 uEditShowSearch(null, type);
585 function uEditShowSearch(link,type) {
586 if(!type) type = link.getAttribute('type');
588 window.xulG.spawn_search(uEditDupHashes[type]);
589 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
592 function uEditMarkCardLost() {
594 for( var c in patron.cards() ) {
596 var card = patron.cards()[c];
597 if( patron.card().id() == card.id() ) {
599 /* de-activite the current card */
603 /* create a new card for the patron */
604 var newcard = new ac();
605 newcard.id(uEditCardVirtId--);
607 patron.card(newcard);
608 patron.cards().push(newcard);
611 /* reset the widget */
612 var field = uEditFindFieldByWId('ue_barcode');
613 field.widget.node.disabled = false;
614 field.widget.node.value = "";
615 field.widget.node.onchange();
616 field.object = newcard;
622 function compactArray(arr) {
624 for( var i = 0; arr && i < arr.length; i++ ) {