5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
13 //var guardianNote = null;
15 /* fetch the necessary data to start off */
16 function uEditInit() {
18 _debug('uEditInit(): ' + location.search);
21 session = cgi.param('ses');
22 clone = cgi.param('clone');
23 if(!session) throw "User session is not defined";
26 $('uedit_user').appendChild(text(USER.usrname()));
28 setTimeout( function() {
29 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
32 function uEditSetUnload() {
33 _debug('setting window unload event');
35 window.onbeforeunload = function(evt) {
36 return $('ue_unsaved_changes').innerHTML;
41 function uEditClearUnload() {
42 _debug('clearing window unload event');
44 window.onbeforeunload = null;
48 /* ------------------------------------------------------------------------------ */
50 /* ------------------------------------------------------------------------------ */
51 function uEditFetchIdentTypes() {
52 _debug("uEditFetchIdentTypes()");
53 var req = new Request(FETCH_ID_TYPES);
58 function uEditFetchStatCats() {
59 _debug("uEditFetchStatCats()");
60 var req = new Request(SC_FETCH_ALL, SESSION);
65 function uEditFetchSurveys() {
66 _debug("uEditFetchSurveys()");
67 var req = new Request(SV_FETCH_ALL, SESSION);
72 function uEditFetchGroups() {
73 _debug("uEditFetchGroups()");
74 var req = new Request(FETCH_GROUPS);
79 function uEditFetchNetLevels() {
80 _debug("uEditFetchNetLevels()");
81 var req = new Request(FETCH_NET_LEVELS, SESSION);
86 /* ------------------------------------------------------------------------------ */
90 /* fetches necessary and builds the UI */
91 function uEditBuild() {
92 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
94 uEditBuildLibSelector();
95 patron = fetchFleshedUser(cgi.param('usr'));
96 if(!patron) patron = uEditNewPatron();
99 uEditFetchIdentTypes(),
101 uEditFetchStatCats(),
103 uEditFetchNetLevels()
107 if(clone) uEditClone(clone);
108 else uEditCreateNewAddr();
112 /* do we need to display the parent / gurdian field? */
113 uEditCheckDOB(uEditFindFieldByKey('dob'));
115 $('ue_barcode').disabled = true;
116 unHideMe($('ue_mark_card_lost'));
117 unHideMe($('ue_reset_pw'));
118 uEditCheckEditPerm();
121 if(PERMS['BAR_PATRON'] == -1)
122 $('ue_barred').disabled = true;
126 /* if this user does not have permission to put users into
127 the edited users group, they do not have permission to
129 function uEditCheckEditPerm() {
131 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
133 _debug("editing user with group app perm "+patron.profile()+' : '+
134 groupsCache[patron.profile()].name() +', and perm = ' + perm);
137 if(PERMS[perm] != -1) return;
139 /* we can edit our own account, but not others in our group */
140 if( patron.id() != USER.id() ){
141 _debug("we are not allowed to edit this user");
143 $('ue_save').disabled = true;
144 $('ue_save_clone').disabled = true;
145 $('ue_mark_card_lost').disabled = true;
146 $('ue_reset_pw').disabled = true;
150 if( f && f.widget && f.widget.node )
151 f.widget.node.disabled = true;
157 var node = $('ue_profile').parentNode;
158 node.removeChild($('ue_profile'));
159 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
161 var field = uEditFindFieldByKey('profile');
162 field.required = false;
163 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
168 /* creates a new patron object with card attached */
169 var uEditCardVirtId = -1;
170 function uEditNewPatron() {
171 var patron = new au();
175 card.id(uEditCardVirtId--);
178 patron.cards([card]);
179 patron.stat_cat_entries([]);
180 patron.survey_responses([]);
181 patron.addresses([]);
182 patron.home_ou(USER.ws_ou());
183 uEditMakeRandomPw(patron);
187 function uEditMakeRandomPw(patron) {
188 var rand = Math.random();
189 rand = parseInt(rand * 10000) + '';
190 while(rand.length < 4) rand += '0';
191 appendClear($('ue_password_plain'),text(rand));
192 unHideMe($('ue_password_gen'));
197 function uEditResetPw() {
198 var pw = uEditMakeRandomPw(patron);
199 $('ue_password1').value = pw;
200 $('ue_password2').value = pw;
203 function uEditClone(clone) {
205 var cloneUser = fetchFleshedUser(clone);
206 patron.usrgroup(cloneUser.usrgroup());
208 if( cloneUser.day_phone() ) {
209 $('ue_day_phone').value = cloneUser.day_phone();
210 $('ue_day_phone').onchange();
213 if( cloneUser.evening_phone() ) {
214 $('ue_night_phone').value = cloneUser.evening_phone();
215 $('ue_night_phone').onchange();
218 if( cloneUser.other_phone() ) {
219 $('ue_other_phone').value = cloneUser.other_phone();
220 $('ue_other_phone').onchange();
223 setSelector($('ue_org_selector'), cloneUser.home_ou());
224 setSelector($('ue_profile'), cloneUser.profile());
226 /* force the expire date to be set */
227 $('ue_profile').onchange();
228 $('ue_org_selector').onchange();
230 for( var a in cloneUser.addresses() ) {
231 var addr = cloneUser.addresses()[a];
232 if( cloneUser.mailing_address &&
233 addr.id() == cloneUser.mailing_address().id() )
234 patron.mailing_address(addr);
235 if( cloneUser.billing_address() &&
236 addr.id() == cloneUser.billing_address().id() )
237 patron.billing_address(addr);
238 patron.addresses().push(addr);
241 uEditBuildAddrs(patron);
245 /* Creates a new blank address,
246 adds it to the user and the fields array */
247 var uEditVirtualAddrId = -1;
248 function uEditCreateNewAddr() {
249 var addr = new aua();
251 addr.id(uEditVirtualAddrId--);
253 addr.usr(patron.id());
254 addr.country(defaultCountry);
256 if(!patron.addresses())
257 patron.addresses([]);
259 if(patron.addresses().length == 0) {
260 patron.mailing_address(addr);
261 patron.billing_address(addr);
265 addr.within_city_limits(1);
267 uEditBuildAddrFields(patron, addr);
268 patron.addresses().push(addr);
269 uEditIterateFields(function(f) { uEditCheckValid(f); });
274 /* kicks off the UI drawing */
275 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
276 hideMe($('uedit_loading'));
277 unHideMe($('ue_maintd'));
280 uEditDrawIDTypes(identTypes);
281 uEditDrawGroups(groups, null, null, true);
282 uEditDrawStatCats(statCats);
283 uEditDrawSurveys(surveys);
284 uEditDrawNetLevels(netLevels);
285 uEditDefineData(patron);
287 uEditIterateFields(function(f) { uEditActivateField(f) });
288 uEditIterateFields(function(f) { uEditCheckValid(f); });
293 /** Applies the event handlers and sets the data for the field */
294 function uEditActivateField(field) {
296 if( field.widget.id ) {
297 field.widget.node = $(field.widget.id);
301 $n(field.widget.base, field.widget.name);
304 uEditSetOnchange(field);
306 if(field.widget.onblur) {
307 field.widget.node.onblur =
308 function() { field.widget.onblur(field); };
311 field.widget.node.disabled = field.widget.disabled;
312 if(field.object == null) return;
313 var val = field.object[field.key]();
314 if(val == null) return;
316 if( field.widget.type == 'input' )
317 field.widget.node.value = val;
319 if( field.widget.type == 'select' )
320 setSelector(field.widget.node, val);
322 if( field.widget.type == 'checkbox' )
323 field.widget.node.checked =
324 (val && val != 'f') ? true : false;
326 if( field.widget.onload )
327 field.widget.onload(val);
331 /* set up the onchange event for the field */
332 function uEditSetOnchange(field) {
333 var func = function() {uEditOnChange( field );}
334 field.widget.node.onchange = func;
336 if(field.widget.type != 'select')
337 field.widget.node.onkeyup = func;
340 /* find the current value of the field object's widget */
341 function uEditNodeVal(field) {
342 if(field.widget.type == 'input')
343 return field.widget.node.value;
345 if(field.widget.type == 'checkbox')
346 return field.widget.node.checked;
348 if(field.widget.type == 'select')
349 return getSelectorVal(field.widget.node);
353 /* update a field value */
354 function uEditOnChange(field) {
356 var newval = uEditNodeVal(field);
357 field.object[field.key](newval);
358 field.object.ischanged(1);
360 if(field.widget.onpostchange)
361 field.widget.onpostchange(field, newval);
363 //_debug(field.key+' = '+newval);
365 uEditIterateFields(function(f) { uEditCheckValid(f); });
372 function uEditCheckValid(field) {
373 var newval = uEditNodeVal(field);
377 if(field.widget.regex) {
378 if(newval.match(field.widget.regex))
379 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
381 addCSSClass(field.widget.node, CSS_INVALID_DATA);
384 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
390 addCSSClass(field.widget.node, CSS_INVALID_DATA);
393 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
399 /* find a field object by object key */
400 function uEditFindFieldByKey(key) {
401 var fields = grep( dataFields,
402 function(item) { return (item.key == key); });
403 return (fields) ? fields[0] : null;
406 /* find a list of fields by object key */
407 function uEditFindFieldsByKey(key) {
408 return grep( dataFields,
409 function(item) { return (item.key == key); });
412 /* find a field object by widget id */
413 function uEditFindFieldByWId(id) {
414 var fields = grep( dataFields,
415 function(item) { return (item.widget.id == id); });
416 return (fields) ? fields[0] : null;
420 function uEditIterateFields(callback) {
421 for( var f in dataFields )
422 callback(dataFields[f]);
426 function uEditGetErrorStrings() {
431 if( !field.object.isdeleted() ) {
432 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
433 var str = $(field.errkey).innerHTML;
434 if(str) errors.push(str);
441 /* munge up something for all of the required surveys
442 (which are not registered with the fields) */
443 if( patron.isnew() ) {
444 var sel = $('ue_survey_table');
447 var rows = sel.getElementsByTagName('tr');
449 for( var r in rows ) {
452 var sel = $n(row, 'ue_survey_answer');
454 var qstn = row.getAttribute('question');
457 qstn = surveyQuestionsCache[qstn];
458 survey = surveysCache[qstn.survey()];
459 var val = getSelectorVal(sel);
460 if(!val && isTrue(survey.required()))
461 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
467 /* ------------------------------------------------------------ */
469 if(errors[0]) return errors;
473 function uEditAlertErrors() {
474 var errors = uEditGetErrorStrings();
475 if(!errors) return false;
476 alert(errors.join("\n"));
481 /* send the user to the database */
482 function uEditSaveUser(cloneme) {
484 if(uEditGetErrorStrings()) {
489 /* null is unique in the db, but '' is not */
490 if( ! patron.ident_value() ) patron.ident_value(null);
491 //if( ! patron.ident_type2() ) patron.ident_type2(null);
492 if( ! patron.ident_value2() ) patron.ident_value2(null);
493 patron.ident_type2(null);
495 if(! patron.dob() ) patron.dob(null);
497 _debug("Saving patron with card: " + js2JSON(patron.card()));
498 _debug("Saving full patron: " + js2JSON(patron));
500 //for( var c in patron
502 var req = new Request(UPDATE_PATRON, SESSION, patron);
503 req.alertEvent = false;
505 var newuser = req.result();
510 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
512 var j = js2JSON(newuser);
514 _debug("USER UPDATE FAILED:\n" + j);
519 alert($('ue_success').innerHTML);
522 /* if the user we just created was a clone, and we want to clone it,
523 we really want to clone the original */
524 if( clone ) cloneme = clone;
525 else cloneme = newuser.id();
532 typeof window.xulG.spawn_editor == 'function' &&
535 _debug("xulG clone spawning new interface...");
536 window.xulG.spawn_editor({ses:cgi.param('ses'),clone:cloneme});
541 var href = location.href;
542 href = href.replace(/\&?usr=\d+/, '');
543 href = href.replace(/\&?clone=\d+/, '');
544 href += '&clone=' + cloneme;
545 location.href = href;
553 uEditRefreshXUL(newuser);
557 function uEditRefreshXUL(newuser) {
558 if (window.xulG && typeof window.xulG.on_save == 'function')
559 window.xulG.on_save(newuser);
562 function uEditRefresh() {
563 var href = location.href;
564 href = href.replace(/\&?clone=\d+/, '');
565 location.href = href;
569 function uEditCancel() {
570 var href = location.href;
571 href = href.replace(/\&?usr=\d+/, '');
572 href = href.replace(/\&?clone=\d+/, '');
573 var id = cgi.param('usr')
574 /* reload the current user if available */
575 if( id ) href += "&usr=" + id;
576 location.href = href;
580 var uEditDupHashes = {};
581 var uEditDupTemplate;
583 function uEditRunDupeSearch(type, search_hash) {
585 if(!patron.isnew()) return;
587 _debug('dup search: ' + js2JSON(search_hash));
589 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
591 var container = $('dup_div_container');
592 if(!uEditDupTemplate)
593 uEditDupTemplate = container.removeChild($('dup_div'));
595 /* clear any existing dups for this type */
596 iterate( container.getElementsByTagName('div'),
598 if( d.getAttribute('type') == type ) {
599 container.removeChild(d)
607 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
614 function uEditHandleDupResults(ids, search_hash, type, container) {
616 _debug('dup search results: ' + js2JSON(ids));
618 if(!(ids && ids[0])) /* no results */
619 return uEditDupHashes[type] = null;
621 /* add a dup link to the UI and plug in the data */
622 var node = uEditDupTemplate.cloneNode(true);
623 container.appendChild(node);
624 node.setAttribute('type', type);
626 var link = $n(node, 'link');
627 link.setAttribute('type', type);
629 $n(node,'count').appendChild(text(ids.length));
631 for( var o in search_hash )
632 $n(node, 'data').appendChild(
633 text(search_hash[o].value + ' '));
635 uEditDupHashes[type] = search_hash;
639 if(confirm($('ue_dup_ident1').innerHTML))
640 uEditShowSearch(null, type);
646 function uEditShowSearch(link,type) {
647 if(!type) type = link.getAttribute('type');
649 window.xulG.spawn_search(uEditDupHashes[type]);
650 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
653 function uEditMarkCardLost() {
655 for( var c in patron.cards() ) {
657 var card = patron.cards()[c];
658 if( patron.card().id() == card.id() ) {
660 /* de-activite the current card */
664 if( !card.barcode() ) {
665 /* a card exists in the array with no barcode */
666 ueRemoveCard(card.id());
668 } else if( card.isnew() && card.active() == 0 ) {
669 /* a new card was created, then never used, removing.. */
670 _debug("removing new inactive card "+card.barcode());
671 ueRemoveCard(card.id());
674 /* create a new card for the patron */
675 var newcard = new ac();
676 newcard.id(uEditCardVirtId--);
678 patron.card(newcard);
679 patron.cards().push(newcard);
682 /* reset the widget */
683 var field = uEditFindFieldByWId('ue_barcode');
684 field.widget.node.disabled = false;
685 field.widget.node.value = "";
686 field.widget.node.onchange();
687 field.object = newcard;
688 _debug("uEditMarkCardLost(): created new card object for user");
694 function ueRemoveCard(id) {
695 _debug("removing card from cards() array: " + id);
696 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
698 for( var j = 0; j < cds.length; j++ )
699 _debug("patron card array now has : "+cds[j].id());
705 function compactArray(arr) {
707 for( var i = 0; arr && i < arr.length; i++ ) {