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 _debug("uEditFetchIdentTypes()");
38 var req = new Request(FETCH_ID_TYPES);
43 function uEditFetchStatCats() {
44 _debug("uEditFetchStatCats()");
45 var req = new Request(SC_FETCH_ALL, SESSION);
50 function uEditFetchSurveys() {
51 _debug("uEditFetchSurveys()");
52 var req = new Request(SV_FETCH_ALL, SESSION);
57 function uEditFetchGroups() {
58 _debug("uEditFetchGroups()");
59 var req = new Request(FETCH_GROUPS);
64 function uEditFetchNetLevels() {
65 _debug("uEditFetchNetLevels()");
66 var req = new Request(FETCH_NET_LEVELS, SESSION);
71 /* ------------------------------------------------------------------------------ */
75 /* fetches necessary and builds the UI */
76 function uEditBuild() {
77 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
79 uEditBuildLibSelector();
80 patron = fetchFleshedUser(cgi.param('usr'));
81 if(!patron) patron = uEditNewPatron();
84 uEditFetchIdentTypes(),
92 if(clone) uEditClone(clone);
93 else uEditCreateNewAddr();
97 /* do we need to display the parent / gurdian field? */
98 uEditCheckDOB(uEditFindFieldByKey('dob'));
100 $('ue_barcode').disabled = true;
101 unHideMe($('ue_mark_card_lost'));
102 unHideMe($('ue_reset_pw'));
103 uEditCheckEditPerm();
106 if(PERMS['BAR_PATRON'] == -1)
107 $('ue_barred').disabled = true;
111 /* if this user does not have permission to put users into
112 the edited users group, they do not have permission to
114 function uEditCheckEditPerm() {
116 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
118 _debug("editing user with group app perm "+patron.profile()+' : '+
119 groupsCache[patron.profile()].name() +', and perm = ' + perm);
122 if(PERMS[perm] != -1) return;
124 /* we can edit our own account, but not others in our group */
125 if( patron.id() != USER.id() ){
126 _debug("we are not allowed to edit this user");
128 $('ue_save').disabled = true;
129 $('ue_save_clone').disabled = true;
133 if( f && f.widget && f.widget.node )
134 f.widget.node.disabled = true;
140 var node = $('ue_profile').parentNode;
141 node.removeChild($('ue_profile'));
142 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
144 var field = uEditFindFieldByKey('profile');
145 field.required = false;
146 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
151 /* creates a new patron object with card attached */
152 var uEditCardVirtId = -1;
153 function uEditNewPatron() {
154 var patron = new au();
158 card.id(uEditCardVirtId--);
161 patron.cards([card]);
162 patron.stat_cat_entries([]);
163 patron.survey_responses([]);
164 patron.addresses([]);
165 patron.home_ou(USER.ws_ou());
166 uEditMakeRandomPw(patron);
170 function uEditMakeRandomPw(patron) {
171 var rand = Math.random();
172 rand = parseInt(rand * 10000) + '';
173 while(rand.length < 4) rand += '0';
174 appendClear($('ue_password_plain'),text(rand));
175 unHideMe($('ue_password_gen'));
180 function uEditResetPw() {
181 var pw = uEditMakeRandomPw(patron);
182 $('ue_password1').value = pw;
183 $('ue_password2').value = pw;
186 function uEditClone(clone) {
188 var cloneUser = fetchFleshedUser(clone);
189 patron.usrgroup(cloneUser.usrgroup());
191 if( cloneUser.day_phone() )
192 $('ue_day_phone').value = cloneUser.day_phone();
193 if( cloneUser.evening_phone() )
194 $('ue_night_phone').value = cloneUser.evening_phone();
195 if( cloneUser.other_phone() )
196 $('ue_other_phone').value = cloneUser.other_phone();
197 setSelector($('ue_org_selector'), cloneUser.home_ou());
200 setSelector($('ue_profile'), cloneUser.profile());
202 /* force the expire date to be set */
203 $('ue_profile').onchange();
205 for( var a in cloneUser.addresses() ) {
206 var addr = cloneUser.addresses()[a];
207 if( cloneUser.mailing_address &&
208 addr.id() == cloneUser.mailing_address().id() )
209 patron.mailing_address(addr);
210 if( cloneUser.billing_address() &&
211 addr.id() == cloneUser.billing_address().id() )
212 patron.billing_address(addr);
213 patron.addresses().push(addr);
216 uEditBuildAddrs(patron);
220 /* Creates a new blank address,
221 adds it to the user and the fields array */
222 var uEditVirtualAddrId = -1;
223 function uEditCreateNewAddr() {
224 var addr = new aua();
226 addr.id(uEditVirtualAddrId--);
228 addr.usr(patron.id());
229 addr.country(defaultCountry);
231 if(!patron.addresses())
232 patron.addresses([]);
234 if(patron.addresses().length == 0) {
235 patron.mailing_address(addr);
236 patron.billing_address(addr);
240 addr.within_city_limits(1);
242 uEditBuildAddrFields(patron, addr);
243 patron.addresses().push(addr);
244 uEditIterateFields(function(f) { uEditCheckValid(f); });
249 /* kicks off the UI drawing */
250 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
251 hideMe($('uedit_loading'));
252 unHideMe($('ue_maintd'));
255 uEditDrawIDTypes(identTypes);
256 uEditDrawGroups(groups, null, null, true);
257 uEditDrawStatCats(statCats);
258 uEditDrawSurveys(surveys);
259 uEditDrawNetLevels(netLevels);
260 uEditDefineData(patron);
262 uEditIterateFields(function(f) { uEditActivateField(f) });
263 uEditIterateFields(function(f) { uEditCheckValid(f); });
268 /** Applies the event handlers and sets the data for the field */
269 function uEditActivateField(field) {
271 if( field.widget.id ) {
272 field.widget.node = $(field.widget.id);
276 $n(field.widget.base, field.widget.name);
279 uEditSetOnchange(field);
281 if(field.widget.onblur) {
282 field.widget.node.onblur =
283 function() { field.widget.onblur(field); };
286 field.widget.node.disabled = field.widget.disabled;
287 if(field.object == null) return;
288 var val = field.object[field.key]();
289 if(val == null) return;
291 if( field.widget.type == 'input' )
292 field.widget.node.value = val;
294 if( field.widget.type == 'select' )
295 setSelector(field.widget.node, val);
297 if( field.widget.type == 'checkbox' )
298 field.widget.node.checked =
299 (val && val != 'f') ? true : false;
301 if( field.widget.onload )
302 field.widget.onload(val);
306 /* set up the onchange event for the field */
307 function uEditSetOnchange(field) {
308 var func = function() {uEditOnChange( field );}
309 field.widget.node.onchange = func;
311 if(field.widget.type != 'select')
312 field.widget.node.onkeyup = func;
315 /* find the current value of the field object's widget */
316 function uEditNodeVal(field) {
317 if(field.widget.type == 'input')
318 return field.widget.node.value;
320 if(field.widget.type == 'checkbox')
321 return field.widget.node.checked;
323 if(field.widget.type == 'select')
324 return getSelectorVal(field.widget.node);
328 /* update a field value */
329 function uEditOnChange(field) {
331 var newval = uEditNodeVal(field);
332 field.object[field.key](newval);
333 field.object.ischanged(1);
335 if(field.widget.onpostchange)
336 field.widget.onpostchange(field, newval);
339 uEditIterateFields(function(f) { uEditCheckValid(f); });
344 function uEditCheckValid(field) {
345 var newval = uEditNodeVal(field);
349 if(field.widget.regex) {
350 if(newval.match(field.widget.regex))
351 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
353 addCSSClass(field.widget.node, CSS_INVALID_DATA);
356 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
362 addCSSClass(field.widget.node, CSS_INVALID_DATA);
365 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
370 /* find a field object by object key */
371 function uEditFindFieldByKey(key) {
372 var fields = grep( dataFields,
373 function(item) { return (item.key == key); });
374 return (fields) ? fields[0] : null;
377 /* find a list of fields by object key */
378 function uEditFindFieldsByKey(key) {
379 return grep( dataFields,
380 function(item) { return (item.key == key); });
383 /* find a field object by widget id */
384 function uEditFindFieldByWId(id) {
385 var fields = grep( dataFields,
386 function(item) { return (item.widget.id == id); });
387 return (fields) ? fields[0] : null;
391 function uEditIterateFields(callback) {
392 for( var f in dataFields )
393 callback(dataFields[f]);
397 function uEditGetErrorStrings() {
402 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
403 var str = $(field.errkey).innerHTML;
404 if(str) errors.push(str);
410 /* munge up something for all of the required surveys
411 (which are not registered with the fields) */
412 if( patron.isnew() ) {
413 var sel = $('ue_survey_table');
416 var rows = sel.getElementsByTagName('tr');
418 for( var r in rows ) {
421 var sel = $n(row, 'ue_survey_answer');
423 var qstn = row.getAttribute('question');
426 qstn = surveyQuestionsCache[qstn];
427 survey = surveysCache[qstn.survey()];
428 var val = getSelectorVal(sel);
429 if(!val && isTrue(survey.required()))
430 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
436 /* ------------------------------------------------------------ */
438 if(errors[0]) return errors;
442 function uEditAlertErrors() {
443 var errors = uEditGetErrorStrings();
444 if(!errors) return false;
445 alert(errors.join("\n"));
450 /* send the user to the database */
451 function uEditSaveUser(cloneme) {
453 if(uEditGetErrorStrings()) {
458 /* null is unique in the db, but '' is not */
459 if( ! patron.ident_value() ) patron.ident_value(null);
460 //if( ! patron.ident_type2() ) patron.ident_type2(null);
461 if( ! patron.ident_value2() ) patron.ident_value2(null);
462 patron.ident_type2(null);
464 if(! patron.dob() ) patron.dob(null);
466 var req = new Request(UPDATE_PATRON, SESSION, patron);
467 req.alertEvent = false;
469 var newuser = req.result();
472 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
473 if(evt) alert(js2JSON(newuser));
477 alert($('ue_success').innerHTML);
480 /* if the user we just created was a clone, and we want to clone it,
481 we really want to clone the original */
482 if( clone ) cloneme = clone;
483 else cloneme = newuser.id();
486 if (window.xulG && typeof window.xulG.on_save == 'function') {
487 _debug("xulG funcs defined...");
488 if( !patron.isnew() && cloneme ) {
489 _debug("calling spawn_editor to clone user...");
490 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
492 window.xulG.on_save(newuser, cloneme);
496 _debug("xulG funcs not defined, refreshing page..");
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++ ) {