5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 var guardianNote = null;
15 function $(id) { return document.getElementById(id); }
17 /* fetch the necessary data to start off */
18 function uEditInit() {
20 _debug('uEditInit(): ' + location.search);
23 session = cgi.param('ses');
24 if (xulG) if (xulG.ses) session = xulG.ses;
25 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
26 clone = cgi.param('clone');
27 if (xulG) if (xulG.clone) clone = xulG.clone;
28 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
29 if(!session) throw $("patronStrings").getString('staff.patron.ue.uEditInit.session_no_defined');
32 $('uedit_user').appendChild(text(USER.usrname()));
34 setTimeout( function() {
35 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
38 /* ------------------------------------------------------------------------------ */
40 /* ------------------------------------------------------------------------------ */
41 function uEditFetchIdentTypes() {
42 _debug("uEditFetchIdentTypes()");
43 var req = new Request(FETCH_ID_TYPES);
48 function uEditFetchStatCats() {
49 _debug("uEditFetchStatCats()");
50 var req = new Request(SC_FETCH_ALL, SESSION);
55 function uEditFetchSurveys() {
56 _debug("uEditFetchSurveys()");
57 var req = new Request(SV_FETCH_ALL, SESSION);
62 function uEditFetchGroups() {
63 _debug("uEditFetchGroups()");
64 var req = new Request(FETCH_GROUPS);
69 function uEditFetchNetLevels() {
70 _debug("uEditFetchNetLevels()");
71 var req = new Request(FETCH_NET_LEVELS, SESSION);
76 /* ------------------------------------------------------------------------------ */
80 /* fetches necessary and builds the UI */
81 function uEditBuild() {
82 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
84 uEditBuildLibSelector();
85 var usr = cgi.param('usr');
86 if (xulG) if (xulG.usr) usr = xulG.usr;
87 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
88 patron = fetchFleshedUser(usr);
89 if(!patron) patron = uEditNewPatron();
93 uEditFetchIdentTypes(),
101 if(clone) uEditClone(clone);
102 else uEditCreateNewAddr();
106 $('ue_barcode').disabled = true;
107 unHideMe($('ue_mark_card_lost'));
108 unHideMe($('ue_reset_pw'));
109 uEditCheckEditPerm();
112 if(PERMS['BAR_PATRON'] == -1)
113 $('ue_barred').disabled = true;
117 /* if this user does not have permission to put users into
118 the edited users group, they do not have permission to
120 function uEditCheckEditPerm() {
122 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
123 _debug("editing user with group app perm "+patron.profile()+' : '+
124 groupsCache[patron.profile()].name() +', and perm = ' + perm);
126 if(PERMS[perm] != -1) return;
128 /* we can edit our own account, but not others in our group */
129 if( patron.id() != USER.id() ){
130 _debug("we are not allowed to edit this user");
132 $('ue_save').disabled = true;
133 $('ue_save_clone').disabled = true;
137 if( f && f.widget && f.widget.node )
138 f.widget.node.disabled = true;
144 var node = $('ue_profile').parentNode;
145 node.removeChild($('ue_profile'));
146 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
148 var field = uEditFindFieldByKey('profile');
149 field.required = false;
150 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
155 /* creates a new patron object with card attached */
156 var uEditCardVirtId = -1;
157 function uEditNewPatron() {
158 var patron = new au();
162 card.id(uEditCardVirtId--);
165 patron.cards([card]);
166 patron.stat_cat_entries([]);
167 patron.survey_responses([]);
168 patron.addresses([]);
169 patron.home_ou(USER.ws_ou());
170 uEditMakeRandomPw(patron);
174 function uEditMakeRandomPw(patron) {
175 var rand = Math.random();
176 rand = parseInt(rand * 10000) + '';
177 while(rand.length < 4) rand += '0';
178 appendClear($('ue_password_plain'),text(rand));
179 unHideMe($('ue_password_gen'));
184 function uEditResetPw() {
185 var pw = uEditMakeRandomPw(patron);
186 $('ue_password1').value = pw;
187 $('ue_password2').value = pw;
190 function uEditClone(clone) {
192 var cloneUser = fetchFleshedUser(clone);
193 patron.usrgroup(cloneUser.usrgroup());
195 if( cloneUser.day_phone() )
196 $('ue_day_phone').value = cloneUser.day_phone();
197 if( cloneUser.evening_phone() )
198 $('ue_night_phone').value = cloneUser.evening_phone();
199 if( cloneUser.other_phone() )
200 $('ue_other_phone').value = cloneUser.other_phone();
201 setSelector($('ue_org_selector'), cloneUser.home_ou());
204 setSelector($('ue_profile'), cloneUser.profile());
206 /* force the expire date to be set */
207 $('ue_profile').onchange();
209 for( var a in cloneUser.addresses() ) {
210 var addr = cloneUser.addresses()[a];
211 if( cloneUser.mailing_address &&
212 addr.id() == cloneUser.mailing_address().id() )
213 patron.mailing_address(addr);
214 if( cloneUser.billing_address() &&
215 addr.id() == cloneUser.billing_address().id() )
216 patron.billing_address(addr);
217 patron.addresses().push(addr);
220 uEditBuildAddrs(patron);
224 /* Creates a new blank address,
225 adds it to the user and the fields array */
226 var uEditVirtualAddrId = -1;
227 function uEditCreateNewAddr() {
228 var addr = new aua();
230 addr.id(uEditVirtualAddrId--);
232 addr.usr(patron.id());
233 addr.country(defaultCountry);
235 if(!patron.addresses())
236 patron.addresses([]);
238 if(patron.addresses().length == 0) {
239 patron.mailing_address(addr);
240 patron.billing_address(addr);
244 addr.within_city_limits(1);
246 uEditBuildAddrFields(patron, addr);
247 patron.addresses().push(addr);
248 uEditIterateFields(function(f) { uEditCheckValid(f); });
253 /* kicks off the UI drawing */
254 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
255 hideMe($('uedit_loading'));
256 unHideMe($('ue_maintd'));
259 uEditDrawIDTypes(identTypes);
260 uEditDrawGroups(groups);
261 uEditDrawStatCats(statCats);
262 uEditDrawSurveys(surveys);
263 uEditDrawNetLevels(netLevels);
264 uEditDefineData(patron);
266 uEditIterateFields(function(f) { uEditActivateField(f) });
267 uEditIterateFields(function(f) { uEditCheckValid(f); });
272 /** Applies the event handlers and sets the data for the field */
273 function uEditActivateField(field) {
275 if( field.widget.id ) {
276 field.widget.node = $(field.widget.id);
280 $n(field.widget.base, field.widget.name);
283 uEditSetOnchange(field);
285 if(field.widget.onblur) {
286 field.widget.node.onblur =
287 function() { field.widget.onblur(field); };
290 field.widget.node.disabled = field.widget.disabled;
291 if(field.object == null) return;
292 var val = field.object[field.key]();
293 if(val == null) return;
295 if( field.widget.type == 'input' )
296 field.widget.node.value = val;
298 if( field.widget.type == 'select' )
299 setSelector(field.widget.node, val);
301 if( field.widget.type == 'checkbox' )
302 field.widget.node.checked =
303 (val && val != 'f') ? true : false;
305 if( field.widget.onload )
306 field.widget.onload(val);
310 /* set up the onchange event for the field */
311 function uEditSetOnchange(field) {
312 var func = function() {uEditOnChange( field );}
313 field.widget.node.onchange = func;
315 if(field.widget.type != 'select')
316 field.widget.node.onkeyup = func;
319 /* find the current value of the field object's widget */
320 function uEditNodeVal(field) {
321 if(field.widget.type == 'input')
322 return field.widget.node.value;
324 if(field.widget.type == 'checkbox')
325 return field.widget.node.checked;
327 if(field.widget.type == 'select')
328 return getSelectorVal(field.widget.node);
332 /* update a field value */
333 function uEditOnChange(field) {
335 var newval = uEditNodeVal(field);
336 field.object[field.key](newval);
337 field.object.ischanged(1);
339 if(field.widget.onpostchange)
340 field.widget.onpostchange(field, newval);
343 uEditIterateFields(function(f) { uEditCheckValid(f); });
348 function uEditCheckValid(field) {
349 var newval = uEditNodeVal(field);
353 if(field.widget.regex) {
354 if(newval.match(field.widget.regex))
355 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
357 addCSSClass(field.widget.node, CSS_INVALID_DATA);
360 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
366 addCSSClass(field.widget.node, CSS_INVALID_DATA);
369 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
374 /* find a field object by object key */
375 function uEditFindFieldByKey(key) {
376 var fields = grep( dataFields,
377 function(item) { return (item.key == key); });
378 return (fields) ? fields[0] : null;
381 /* find a list of fields by object key */
382 function uEditFindFieldsByKey(key) {
383 return grep( dataFields,
384 function(item) { return (item.key == key); });
387 /* find a field object by widget id */
388 function uEditFindFieldByWId(id) {
389 var fields = grep( dataFields,
390 function(item) { return (item.widget.id == id); });
391 return (fields) ? fields[0] : null;
395 function uEditIterateFields(callback) {
396 for( var f in dataFields )
397 callback(dataFields[f]);
401 function uEditGetErrorStrings() {
406 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
407 var str = $(field.errkey).innerHTML;
408 if(str) errors.push(str);
414 /* munge up something for all of the required surveys
415 (which are not registered with the fields) */
416 if( patron.isnew() ) {
417 var sel = $('ue_survey_table');
420 var rows = sel.getElementsByTagName('tr');
422 for( var r in rows ) {
425 var sel = $n(row, 'ue_survey_answer');
427 var qstn = row.getAttribute('question');
430 qstn = surveyQuestionsCache[qstn];
431 survey = surveysCache[qstn.survey()];
432 var val = getSelectorVal(sel);
433 if(!val && isTrue(survey.required()))
434 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
440 /* ------------------------------------------------------------ */
442 if(errors[0]) return errors;
446 function uEditAlertErrors() {
447 var errors = uEditGetErrorStrings();
448 if(!errors) return false;
449 alert(errors.join("\n"));
454 /* send the user to the database */
455 function uEditSaveUser(cloneme) {
457 if(uEditGetErrorStrings()) {
462 /* null is unique in the db, but '' is not */
463 if( ! patron.ident_value() ) patron.ident_value(null);
464 if( ! patron.ident_type2() ) patron.ident_type2(null);
465 if( ! patron.ident_value2() ) patron.ident_value2(null);
467 if(! patron.dob() ) patron.dob(null);
469 var req = new Request(UPDATE_PATRON, SESSION, patron);
470 req.alertEvent = false;
472 var newuser = req.result();
475 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
476 if(evt) alert(js2JSON(newuser));
481 /* if it's a new user and a guardian note was created for this user,
482 create the note after we have the new user's id */
484 guardianNote.usr( newuser.id() );
485 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
486 req.alertEvent = false;
488 var resp = req.result();
489 if( checkILSEvent(resp) ) {
490 alertILSEvent(resp, $("patronStrings").getString('staff.patron.ue.uEditSaveuser.error_creating_note'));
495 alert($('ue_success').innerHTML);
499 /* if the user we just created was a clone, and we want to clone it,
500 we really want to clone the original */
501 if( clone ) cloneme = clone;
502 else cloneme = newuser.id();
505 if (window.xulG && typeof window.xulG.on_save == 'function') {
506 _debug("xulG funcs defined...");
507 if( !patron.isnew() && cloneme ) {
508 _debug("calling spawn_editor to clone user...");
509 var ses = cgi.param('ses');
510 if (xulG.ses) ses = xulG.ses;
511 if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
512 window.xulG.spawn_editor({ses:ses,clone:cloneme});
514 window.xulG.on_save(newuser, cloneme);
518 _debug("xulG funcs not defined, refreshing page..");
519 var href = location.href;
521 href = href.replace(/\&?usr=\d+/, '');
522 href = href.replace(/\&?clone=\d+/, '');
524 if( cloneme ) href += '&clone=' + cloneme;
525 location.href = href;
529 function uEditCancel() {
530 var href = location.href;
531 href = href.replace(/\&?usr=\d+/, '');
532 href = href.replace(/\&?clone=\d+/, '');
533 var id = cgi.param('usr');
534 if (xulG) if (xulG.usr) id = xulG.usr;
535 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
536 /* reload the current user if available */
537 if( id ) href += "&usr=" + id;
538 location.href = href;
542 var uEditDupHashes = {};
543 var uEditDupTemplate;
545 function uEditRunDupeSearch(type, search_hash) {
547 if(!patron.isnew()) return;
549 _debug('dup search: ' + js2JSON(search_hash));
551 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
553 var container = $('dup_div_container');
554 if(!uEditDupTemplate)
555 uEditDupTemplate = container.removeChild($('dup_div'));
557 /* clear any existing dups for this type */
558 iterate( container.getElementsByTagName('div'),
560 if( d.getAttribute('type') == type ) {
561 container.removeChild(d)
569 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
576 function uEditHandleDupResults(ids, search_hash, type, container) {
578 _debug('dup search results: ' + js2JSON(ids));
580 if(!(ids && ids[0])) /* no results */
581 return uEditDupHashes[type] = null;
583 /* add a dup link to the UI and plug in the data */
584 var node = uEditDupTemplate.cloneNode(true);
585 container.appendChild(node);
586 node.setAttribute('type', type);
588 var link = $n(node, 'link');
589 link.setAttribute('type', type);
591 $n(node,'count').appendChild(text(ids.length));
593 for( var o in search_hash )
594 $n(node, 'data').appendChild(
595 text(search_hash[o].value + ' '));
597 uEditDupHashes[type] = search_hash;
601 if(confirm($('ue_dup_ident1').innerHTML))
602 uEditShowSearch(null, type);
608 function uEditShowSearch(link,type) {
609 if(!type) type = link.getAttribute('type');
611 window.xulG.spawn_search(uEditDupHashes[type]);
613 alert($("patronStrings").getFormattedString('staff.patron.ue.uEditShowSearch.search', [js2JSON(uEditDupHashes[type])]));
617 function uEditMarkCardLost() {
619 for( var c in patron.cards() ) {
621 var card = patron.cards()[c];
622 if( patron.card().id() == card.id() ) {
624 /* de-activite the current card */
628 /* create a new card for the patron */
629 var newcard = new ac();
630 newcard.id(uEditCardVirtId--);
632 patron.card(newcard);
633 patron.cards().push(newcard);
636 /* reset the widget */
637 var field = uEditFindFieldByWId('ue_barcode');
638 field.widget.node.disabled = false;
639 field.widget.node.value = "";
640 field.widget.node.onchange();
641 field.object = newcard;
647 function compactArray(arr) {
649 for( var i = 0; arr && i < arr.length; i++ ) {