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 if (xulG) if (xulG.ses) session = xulG.ses;
23 if (xulG) if (xulG.params) if (xulG.params.ses) session = xulG.params.ses;
24 clone = cgi.param('clone');
25 if (xulG) if (xulG.clone) clone = xulG.clone;
26 if (xulG) if (xulG.params) if (xulG.params.clone) clone = xulG.params.clone;
27 if(!session) throw "User session is not defined";
30 $('uedit_user').appendChild(text(USER.usrname()));
32 setTimeout( function() {
33 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
36 function uEditSetUnload() {
37 _debug('setting window unload event');
39 window.onbeforeunload = function(evt) {
40 return $('ue_unsaved_changes').innerHTML;
45 function uEditClearUnload() {
46 _debug('clearing window unload event');
48 window.onbeforeunload = null;
52 /* ------------------------------------------------------------------------------ */
54 /* ------------------------------------------------------------------------------ */
55 function uEditFetchIdentTypes() {
56 _debug("uEditFetchIdentTypes()");
57 var s = fetchXULStash();
58 if (typeof s.list != 'undefined')
59 if (typeof s.list.cit != 'undefined') return s.list.cit;
60 var req = new Request(FETCH_ID_TYPES);
65 function uEditFetchStatCats() {
66 _debug("uEditFetchStatCats()");
67 var s = fetchXULStash();
68 if (typeof s.list != 'undefined')
69 if (typeof s.list.my_actsc != 'undefined') return s.list.my_actsc;
70 var req = new Request(SC_FETCH_ALL, SESSION);
75 function uEditFetchSurveys() {
76 _debug("uEditFetchSurveys()");
77 var s = fetchXULStash();
78 if (typeof s.list != 'undefined')
79 if (typeof s.list.asv != 'undefined') return s.list.asv;
80 var req = new Request(SV_FETCH_ALL, SESSION);
85 function uEditFetchGroups() {
86 _debug("uEditFetchGroups()");
87 var s = fetchXULStash();
88 if (typeof s.tree != 'undefined')
89 if (typeof s.tree.pgt != 'undefined') return s.tree.pgt;
90 var req = new Request(FETCH_GROUPS);
95 function uEditFetchNetLevels() {
96 _debug("uEditFetchNetLevels()");
97 var s = fetchXULStash();
98 if (typeof s.list != 'undefined')
99 if (typeof s.list.cnal != 'undefined') return s.list.cnal;
100 var req = new Request(FETCH_NET_LEVELS, SESSION);
105 /* ------------------------------------------------------------------------------ */
109 * adds all of the group.application_perm's to the list
110 * provided by descending through the group tree
112 function buildAppPermList(list, group) {
114 if(group.application_perm() )
115 list.push(group.application_perm());
116 for(i in group.children()) {
117 buildAppPermList(list, group.children()[i]);
121 /* fetches necessary objects and builds the UI */
122 function uEditBuild() {
124 myPerms = ['BAR_PATRON', 'UNBAR_PATRON'];
126 /* grab the groups before we check perms so we know what
127 application_perms to check */
128 var groups = uEditFetchGroups();
129 buildAppPermList(myPerms, groups);
131 fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
133 uEditBuildLibSelector();
134 var usr = cgi.param('usr');
135 if (xulG) if (xulG.usr) usr = xulG.usr;
136 if (xulG) if (xulG.params) if (xulG.params.usr) usr = xulG.params.usr;
137 patron = fetchFleshedUser(usr);
138 if(!patron) patron = uEditNewPatron();
141 uEditFetchIdentTypes(),
143 uEditFetchStatCats(),
145 uEditFetchNetLevels()
149 if(clone) uEditClone(clone);
150 else uEditCreateNewAddr();
154 /* do we need to display the parent / gurdian field? */
155 uEditCheckDOB(uEditFindFieldByKey('dob'));
157 $('ue_barcode').disabled = true;
158 unHideMe($('ue_mark_card_lost'));
159 unHideMe($('ue_reset_pw'));
160 uEditCheckEditPerm();
163 uEditCheckBarredPerm();
166 function uEditCheckBarredPerm() {
167 if(PERMS['BAR_PATRON'] != -1)
170 if(isTrue(patron.barred()) && PERMS['UNBAR_PATRON'] != -1)
173 $('ue_barred').disabled = true;
177 /* if this user does not have permission to put users into
178 the edited users group, they do not have permission to
180 function uEditCheckEditPerm() {
182 var perm = uEditFindGroupPerm(groupsCache[patron.profile()]);
184 _debug("editing user with group app perm "+patron.profile()+' : '+
185 groupsCache[patron.profile()].name() +', and perm = ' + perm);
188 if(PERMS[perm] != -1) return;
190 /* we can edit our own account, but not others in our group */
191 if( patron.id() != USER.id() ){
192 _debug("we are not allowed to edit this user");
194 $('ue_save').disabled = true;
195 $('ue_save_clone').disabled = true;
196 $('ue_mark_card_lost').disabled = true;
197 $('ue_reset_pw').disabled = true;
201 if( f && f.widget && f.widget.node )
202 f.widget.node.disabled = true;
208 var node = $('ue_profile').parentNode;
209 node.removeChild($('ue_profile'));
210 node.appendChild(elem('span',null,groupsCache[patron.profile()].name()));
212 var field = uEditFindFieldByKey('profile');
213 field.required = false;
214 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
219 /* creates a new patron object with card attached */
220 var uEditCardVirtId = -1;
221 function uEditNewPatron() {
222 var patron = new au();
226 card.id(uEditCardVirtId--);
229 patron.cards([card]);
230 patron.stat_cat_entries([]);
231 patron.survey_responses([]);
232 patron.addresses([]);
233 patron.home_ou(USER.ws_ou());
234 uEditMakeRandomPw(patron);
238 function uEditMakeRandomPw(patron) {
239 var rand = Math.random();
240 rand = parseInt(rand * 10000) + '';
241 while(rand.length < 4) rand += '0';
242 appendClear($('ue_password_plain'),text(rand));
243 unHideMe($('ue_password_gen'));
248 function uEditResetPw() {
249 var pw = uEditMakeRandomPw(patron);
250 $('ue_password1').value = pw;
251 $('ue_password2').value = pw;
252 $('ue_password1').onchange();
255 function uEditClone(clone) {
257 var cloneUser = fetchFleshedUser(clone);
258 patron.usrgroup(cloneUser.usrgroup());
260 if( cloneUser.day_phone() ) {
261 $('ue_day_phone').value = cloneUser.day_phone();
262 $('ue_day_phone').onchange();
265 if( cloneUser.evening_phone() ) {
266 $('ue_night_phone').value = cloneUser.evening_phone();
267 $('ue_night_phone').onchange();
270 if( cloneUser.other_phone() ) {
271 $('ue_other_phone').value = cloneUser.other_phone();
272 $('ue_other_phone').onchange();
275 setSelector($('ue_org_selector'), cloneUser.home_ou());
276 setSelector($('ue_profile'), cloneUser.profile());
278 /* force the expire date to be set */
279 $('ue_profile').onchange();
280 $('ue_org_selector').onchange();
282 for( var a in cloneUser.addresses() ) {
283 var addr = cloneUser.addresses()[a];
284 if( cloneUser.mailing_address &&
285 addr.id() == cloneUser.mailing_address().id() )
286 patron.mailing_address(addr);
287 if( cloneUser.billing_address() &&
288 addr.id() == cloneUser.billing_address().id() )
289 patron.billing_address(addr);
290 patron.addresses().push(addr);
293 uEditBuildAddrs(patron);
297 /* Creates a new blank address,
298 adds it to the user and the fields array */
299 var uEditVirtualAddrId = -1;
300 function uEditCreateNewAddr() {
301 var addr = new aua();
303 addr.id(uEditVirtualAddrId--);
305 addr.usr(patron.id());
306 addr.country(defaultCountry);
308 if(!patron.addresses())
309 patron.addresses([]);
311 if(patron.addresses().length == 0) {
312 patron.mailing_address(addr);
313 patron.billing_address(addr);
317 addr.within_city_limits(1);
319 uEditBuildAddrFields(patron, addr);
320 patron.addresses().push(addr);
321 uEditIterateFields(function(f) { uEditCheckValid(f); });
326 /* kicks off the UI drawing */
327 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
328 hideMe($('uedit_loading'));
329 unHideMe($('ue_maintd'));
332 uEditDrawIDTypes(identTypes);
333 uEditDrawGroups(groups, null, null, true);
334 uEditDrawStatCats(statCats);
335 uEditDrawSurveys(surveys);
336 uEditDrawNetLevels(netLevels);
337 uEditDefineData(patron);
339 uEditIterateFields(function(f) { uEditActivateField(f) });
340 uEditIterateFields(function(f) { uEditCheckValid(f); });
345 /** Applies the event handlers and sets the data for the field */
346 function uEditActivateField(field) {
348 if( field.widget.id ) {
349 field.widget.node = $(field.widget.id);
353 $n(field.widget.base, field.widget.name);
356 uEditSetOnchange(field);
358 if(field.widget.onblur) {
359 field.widget.node.onblur =
360 function() { field.widget.onblur(field); };
363 field.widget.node.disabled = field.widget.disabled;
364 if(field.object == null) return;
365 var val = field.object[field.key]();
366 if(val == null) return;
368 if( field.widget.type == 'input' )
369 field.widget.node.value = val;
371 if( field.widget.type == 'select' )
372 setSelector(field.widget.node, val);
374 if( field.widget.type == 'checkbox' )
375 field.widget.node.checked =
376 (val && val != 'f') ? true : false;
378 if( field.widget.onload )
379 field.widget.onload(val);
383 /* set up the onchange event for the field */
384 function uEditSetOnchange(field) {
385 var func = function() {uEditOnChange( field );}
386 field.widget.node.onchange = func;
388 if(field.widget.type != 'select')
389 field.widget.node.onkeyup = func;
392 /* find the current value of the field object's widget */
393 function uEditNodeVal(field) {
394 if(field.widget.type == 'input')
395 return field.widget.node.value;
397 if(field.widget.type == 'checkbox')
398 return field.widget.node.checked;
400 if(field.widget.type == 'select')
401 return getSelectorVal(field.widget.node);
405 /* update a field value */
406 function uEditOnChange(field) {
408 var newval = uEditNodeVal(field);
409 field.object[field.key](newval);
410 field.object.ischanged(1);
412 if(field.widget.onpostchange)
413 field.widget.onpostchange(field, newval);
415 //_debug(field.key+' = '+newval);
417 uEditIterateFields(function(f) { uEditCheckValid(f); });
424 function uEditCheckValid(field) {
425 var newval = uEditNodeVal(field);
429 if(field.widget.regex) {
430 if(newval.match(field.widget.regex))
431 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
433 addCSSClass(field.widget.node, CSS_INVALID_DATA);
436 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
442 addCSSClass(field.widget.node, CSS_INVALID_DATA);
445 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
451 /* find a field object by object key */
452 function uEditFindFieldByKey(key) {
453 var fields = grep( dataFields,
454 function(item) { return (item.key == key); });
455 return (fields) ? fields[0] : null;
458 /* find a list of fields by object key */
459 function uEditFindFieldsByKey(key) {
460 return grep( dataFields,
461 function(item) { return (item.key == key); });
464 /* find a field object by widget id */
465 function uEditFindFieldByWId(id) {
466 var fields = grep( dataFields,
467 function(item) { return (item.widget.id == id); });
468 return (fields) ? fields[0] : null;
472 function uEditIterateFields(callback) {
473 for( var f in dataFields )
474 callback(dataFields[f]);
478 function uEditGetErrorStrings() {
483 if( !field.object.isdeleted() ) {
484 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
485 var str = $(field.errkey).innerHTML;
486 if(str) errors.push(str);
493 /* munge up something for all of the required surveys
494 (which are not registered with the fields) */
495 if( patron.isnew() ) {
496 var sel = $('ue_survey_table');
499 var rows = sel.getElementsByTagName('tr');
501 for( var r in rows ) {
504 var sel = $n(row, 'ue_survey_answer');
506 var qstn = row.getAttribute('question');
509 qstn = surveyQuestionsCache[qstn];
510 survey = surveysCache[qstn.survey()];
511 var val = getSelectorVal(sel);
512 if(!val && isTrue(survey.required()))
513 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
519 /* ------------------------------------------------------------ */
521 if(errors[0]) return errors;
525 function uEditAlertErrors() {
526 var errors = uEditGetErrorStrings();
527 if(!errors) return false;
528 alert(errors.join("\n"));
533 /* send the user to the database */
534 function uEditSaveUser(cloneme) {
536 if(uEditGetErrorStrings()) {
541 /* null is unique in the db, but '' is not */
542 if( ! patron.ident_value() ) patron.ident_value(null);
543 //if( ! patron.ident_type2() ) patron.ident_type2(null);
544 if( ! patron.ident_value2() ) patron.ident_value2(null);
545 patron.ident_type2(null);
547 if(! patron.dob() ) patron.dob(null);
549 _debug("Saving patron with card: " + js2JSON(patron.card()));
550 _debug("Saving full patron: " + js2JSON(patron));
552 //for( var c in patron
554 var req = new Request(UPDATE_PATRON, SESSION, patron);
555 req.alertEvent = false;
557 var newuser = req.result();
562 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
565 if( evt.textcode == 'XACT_COLLISION' ) {
566 if( confirmId('ue_xact_collision') )
567 location.href = location.href;
570 var j = js2JSON(evt);
572 _debug("USER UPDATE FAILED:\n" + j);
577 alert($('ue_success').innerHTML);
580 /* if the user we just created was a clone, and we want to clone it,
581 we really want to clone the original */
582 if( clone ) cloneme = clone;
583 else cloneme = newuser.id();
590 typeof window.xulG.spawn_editor == 'function' &&
593 _debug("xulG clone spawning new interface...");
594 var ses = cgi.param('ses');
595 if (xulG) if (xulG.ses) ses = xulG.ses;
596 if (xulG) if (xulG.params) if (xulG.params.ses) ses = xulG.params.ses;
597 window.xulG.spawn_editor({ses:ses,clone:cloneme});
602 var href = location.href;
603 href = href.replace(/\&?usr=\d+/, '');
604 href = href.replace(/\&?clone=\d+/, '');
605 href += '&clone=' + cloneme;
606 location.href = href;
614 uEditRefreshXUL(newuser);
618 function uEditRefreshXUL(newuser) {
619 if (window.xulG && typeof window.xulG.on_save == 'function')
620 window.xulG.on_save(newuser);
623 function uEditRefresh() {
624 var href = location.href;
625 href = href.replace(/\&?clone=\d+/, '');
626 location.href = href;
630 function uEditCancel() {
631 var href = location.href;
632 href = href.replace(/\&?usr=\d+/, '');
633 href = href.replace(/\&?clone=\d+/, '');
634 var id = cgi.param('usr');
635 if (xulG) if (xulG.usr) id = xulG.usr;
636 if (xulG) if (xulG.params) if (xulG.params.usr) id = xulG.params.usr;
637 /* reload the current user if available */
638 if( id ) href += "&usr=" + id;
639 location.href = href;
643 var uEditDupHashes = {};
644 var uEditDupTemplate;
646 function uEditRunDupeSearch(type, search_hash) {
648 if(!patron.isnew()) return;
650 _debug('dup search: ' + js2JSON(search_hash));
652 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
654 var container = $('dup_div_container');
655 if(!uEditDupTemplate)
656 uEditDupTemplate = container.removeChild($('dup_div'));
658 /* clear any existing dups for this type */
659 iterate( container.getElementsByTagName('div'),
661 if( d.getAttribute('type') == type ) {
662 container.removeChild(d)
670 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
677 function uEditHandleDupResults(ids, search_hash, type, container) {
679 _debug('dup search results: ' + js2JSON(ids));
681 if(!(ids && ids[0])) /* no results */
682 return uEditDupHashes[type] = null;
684 /* add a dup link to the UI and plug in the data */
685 var node = uEditDupTemplate.cloneNode(true);
686 container.appendChild(node);
687 node.setAttribute('type', type);
689 var link = $n(node, 'link');
690 link.setAttribute('type', type);
692 $n(node,'count').appendChild(text(ids.length));
694 for( var o in search_hash )
695 $n(node, 'data').appendChild(
696 text(search_hash[o].value + ' '));
698 uEditDupHashes[type] = search_hash;
702 if(confirm($('ue_dup_ident1').innerHTML))
703 uEditShowSearch(null, type);
709 function uEditShowSearch(link,type) {
710 if(!type) type = link.getAttribute('type');
712 window.xulG.spawn_search(uEditDupHashes[type]);
713 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
716 function uEditMarkCardLost() {
718 for( var c in patron.cards() ) {
720 var card = patron.cards()[c];
721 if( patron.card().id() == card.id() ) {
723 /* de-activite the current card */
727 if( !card.barcode() ) {
728 /* a card exists in the array with no barcode */
729 ueRemoveCard(card.id());
731 } else if( card.isnew() && card.active() == 0 ) {
732 /* a new card was created, then never used, removing.. */
733 _debug("removing new inactive card "+card.barcode());
734 ueRemoveCard(card.id());
737 /* create a new card for the patron */
738 var newcard = new ac();
739 newcard.id(uEditCardVirtId--);
741 patron.card(newcard);
742 patron.cards().push(newcard);
745 /* reset the widget */
746 var field = uEditFindFieldByWId('ue_barcode');
747 field.widget.node.disabled = false;
748 field.widget.node.value = "";
749 field.widget.node.onchange();
750 field.object = newcard;
751 _debug("uEditMarkCardLost(): created new card object for user");
757 function ueRemoveCard(id) {
758 _debug("removing card from cards() array: " + id);
759 var cds = grep( patron.cards(), function(c){return (c.id() != id)});
761 for( var j = 0; j < cds.length; j++ )
762 _debug("patron card array now has : "+cds[j].id());
768 function compactArray(arr) {
770 for( var i = 0; arr && i < arr.length; i++ ) {