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()));
137 /* creates a new patron object with card attached */
138 var uEditCardVirtId = -1;
139 function uEditNewPatron() {
140 var patron = new au();
144 card.id(uEditCardVirtId--);
147 patron.cards([card]);
148 patron.stat_cat_entries([]);
149 patron.survey_responses([]);
150 patron.addresses([]);
151 patron.home_ou(USER.ws_ou());
152 uEditMakeRandomPw(patron);
156 function uEditMakeRandomPw(patron) {
157 var rand = Math.random();
158 rand = parseInt(rand * 10000) + '';
159 while(rand.length < 4) rand += '0';
160 appendClear($('ue_password_plain'),text(rand));
161 unHideMe($('ue_password_gen'));
166 function uEditResetPw() {
167 var pw = uEditMakeRandomPw(patron);
168 $('ue_password1').value = pw;
169 $('ue_password2').value = pw;
172 function uEditClone(clone) {
174 var cloneUser = fetchFleshedUser(clone);
175 patron.usrgroup(cloneUser.usrgroup());
177 if( cloneUser.day_phone() )
178 $('ue_day_phone').value = cloneUser.day_phone();
179 if( cloneUser.evening_phone() )
180 $('ue_night_phone').value = cloneUser.evening_phone();
181 if( cloneUser.other_phone() )
182 $('ue_other_phone').value = cloneUser.other_phone();
183 setSelector($('ue_org_selector'), cloneUser.home_ou());
186 setSelector($('ue_profile'), cloneUser.profile());
188 /* force the expire date to be set */
189 $('ue_profile').onchange();
191 for( var a in cloneUser.addresses() ) {
192 var addr = cloneUser.addresses()[a];
193 if( cloneUser.mailing_address &&
194 addr.id() == cloneUser.mailing_address().id() )
195 patron.mailing_address(addr);
196 if( cloneUser.billing_address() &&
197 addr.id() == cloneUser.billing_address().id() )
198 patron.billing_address(addr);
199 patron.addresses().push(addr);
202 uEditBuildAddrs(patron);
206 /* Creates a new blank address,
207 adds it to the user and the fields array */
208 var uEditVirtualAddrId = -1;
209 function uEditCreateNewAddr() {
210 var addr = new aua();
211 addr.id(uEditVirtualAddrId--);
213 addr.usr(patron.id());
214 addr.country(defaultCountry);
215 if(!patron.addresses())
216 patron.addresses([]);
217 if(patron.addresses().length == 0) {
218 patron.mailing_address(addr);
219 patron.billing_address(addr);
221 uEditBuildAddrFields(patron, addr);
222 patron.addresses().push(addr);
223 uEditIterateFields(function(f) { uEditCheckValid(f); });
228 /* kicks off the UI drawing */
229 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
230 hideMe($('uedit_loading'));
231 unHideMe($('ue_maintd'));
234 uEditDrawIDTypes(identTypes);
235 uEditDrawGroups(groups);
236 uEditDrawStatCats(statCats);
237 uEditDrawSurveys(surveys);
238 uEditDrawNetLevels(netLevels);
239 uEditDefineData(patron);
241 uEditIterateFields(function(f) { uEditActivateField(f) });
242 uEditIterateFields(function(f) { uEditCheckValid(f); });
247 /** Applies the event handlers and sets the data for the field */
248 function uEditActivateField(field) {
250 if( field.widget.id ) {
251 field.widget.node = $(field.widget.id);
255 $n(field.widget.base, field.widget.name);
258 uEditSetOnchange(field);
260 if(field.widget.onblur) {
261 field.widget.node.onblur =
262 function() { field.widget.onblur(field); };
265 field.widget.node.disabled = field.widget.disabled;
266 if(field.object == null) return;
267 var val = field.object[field.key]();
268 if(val == null) return;
270 if( field.widget.type == 'input' )
271 field.widget.node.value = val;
273 if( field.widget.type == 'select' )
274 setSelector(field.widget.node, val);
276 if( field.widget.type == 'checkbox' )
277 field.widget.node.checked =
278 (val && val != 'f') ? true : false;
280 if( field.widget.onload )
281 field.widget.onload(val);
285 /* set up the onchange event for the field */
286 function uEditSetOnchange(field) {
287 var func = function() {uEditOnChange( field );}
288 field.widget.node.onchange = func;
290 if(field.widget.type != 'select')
291 field.widget.node.onkeyup = func;
294 /* find the current value of the field object's widget */
295 function uEditNodeVal(field) {
296 if(field.widget.type == 'input')
297 return field.widget.node.value;
299 if(field.widget.type == 'checkbox')
300 return field.widget.node.checked;
302 if(field.widget.type == 'select')
303 return getSelectorVal(field.widget.node);
307 /* update a field value */
308 function uEditOnChange(field) {
310 var newval = uEditNodeVal(field);
311 field.object[field.key](newval);
312 field.object.ischanged(1);
314 if(field.widget.onpostchange)
315 field.widget.onpostchange(field, newval);
318 uEditIterateFields(function(f) { uEditCheckValid(f); });
323 function uEditCheckValid(field) {
324 var newval = uEditNodeVal(field);
327 if(field.widget.regex) {
328 if(newval.match(field.widget.regex))
329 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
331 addCSSClass(field.widget.node, CSS_INVALID_DATA);
334 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
340 addCSSClass(field.widget.node, CSS_INVALID_DATA);
343 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
348 /* find a field object by object key */
349 function uEditFindFieldByKey(key) {
350 var fields = grep( dataFields,
351 function(item) { return (item.key == key); });
352 return (fields) ? fields[0] : null;
355 /* find a list of fields by object key */
356 function uEditFindFieldsByKey(key) {
357 return grep( dataFields,
358 function(item) { return (item.key == key); });
361 /* find a field object by widget id */
362 function uEditFindFieldByWId(id) {
363 var fields = grep( dataFields,
364 function(item) { return (item.widget.id == id); });
365 return (fields) ? fields[0] : null;
369 function uEditIterateFields(callback) {
370 for( var f in dataFields )
371 callback(dataFields[f]);
375 function uEditGetErrorStrings() {
380 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
381 var str = $(field.errkey).innerHTML;
382 if(str) errors.push(str);
388 /* munge up something for all of the required surveys
389 (which are not registered with the fields) */
390 if( patron.isnew() ) {
391 var sel = $('ue_survey_table');
394 var rows = sel.getElementsByTagName('tr');
396 for( var r in rows ) {
399 var sel = $n(row, 'ue_survey_answer');
401 var qstn = row.getAttribute('question');
404 qstn = surveyQuestionsCache[qstn];
405 survey = surveysCache[qstn.survey()];
406 var val = getSelectorVal(sel);
407 if(!val && isTrue(survey.required()))
408 errors.push($('ue_bad_survey').innerHTML + ' : ' + qstn.question());
414 /* ------------------------------------------------------------ */
416 if(errors[0]) return errors;
420 function uEditAlertErrors() {
421 var errors = uEditGetErrorStrings();
422 if(!errors) return false;
423 alert(errors.join("\n"));
428 /* send the user to the database */
429 function uEditSaveUser(cloneme) {
431 if(uEditGetErrorStrings()) {
436 /* null is unique in the db, but '' is not */
437 if( ! patron.ident_value() ) patron.ident_value(null);
438 if( ! patron.ident_value2() ) patron.ident_value2(null);
440 var req = new Request(UPDATE_PATRON, SESSION, patron);
441 req.alertEvent = false;
443 var newuser = req.result();
446 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
447 if(evt) alert(js2JSON(newuser));
452 /* if it's a new user and a guardian note was created for this user,
453 create the note after we have the new user's id */
455 guardianNote.usr( newuser.id() );
456 var req = new Request(CREATE_USER_NOTE, SESSION, guardianNote);
457 req.alertEvent = false;
459 var resp = req.result();
460 if( checkILSEvent(resp) ) {
462 "Error creating patron guardian/parent note");
467 alert($('ue_success').innerHTML);
471 /* if the user we just created was a clone, and we want to clone it,
472 we really want to clone the original */
473 if( clone ) cloneme = clone;
474 else cloneme = newuser.id();
477 if (window.xulG && typeof window.xulG.on_save == 'function') {
478 window.xulG.on_save(newuser, cloneme);
482 var href = location.href;
484 href = href.replace(/\&?usr=\d+/, '');
485 href = href.replace(/\&?clone=\d+/, '');
487 if( cloneme ) href += '&clone=' + cloneme;
488 location.href = href;
492 function uEditCancel() {
493 var href = location.href;
494 href = href.replace(/\&?usr=\d+/, '');
495 href = href.replace(/\&?clone=\d+/, '');
496 var id = cgi.param('usr')
497 /* reload the current user if available */
498 if( id ) href += "&usr=" + id;
499 location.href = href;
503 var uEditDupHashes = {};
504 var uEditDupTemplate;
506 function uEditRunDupeSearch(type, search_hash) {
508 if(!patron.isnew()) return;
510 _debug('dup search: ' + js2JSON(search_hash));
512 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
514 var container = $('dup_div_container');
515 if(!uEditDupTemplate)
516 uEditDupTemplate = container.removeChild($('dup_div'));
518 /* clear any existing dups for this type */
519 iterate( container.getElementsByTagName('div'),
521 if( d.getAttribute('type') == type ) {
522 container.removeChild(d)
530 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
537 function uEditHandleDupResults(ids, search_hash, type, container) {
539 _debug('dup search results: ' + js2JSON(ids));
541 if(!(ids && ids[0])) /* no results */
542 return uEditDupHashes[type] = null;
544 /* add a dup link to the UI and plug in the data */
545 var node = uEditDupTemplate.cloneNode(true);
546 container.appendChild(node);
547 node.setAttribute('type', type);
549 var link = $n(node, 'link');
550 link.setAttribute('type', type);
552 $n(node,'count').appendChild(text(ids.length));
554 for( var o in search_hash )
555 $n(node, 'data').appendChild(
556 text(search_hash[o].value + ' '));
558 uEditDupHashes[type] = search_hash;
562 if(confirm($('ue_dup_ident1').innerHTML))
563 uEditShowSearch(null, type);
569 function uEditShowSearch(link,type) {
570 if(!type) type = link.getAttribute('type');
572 window.xulG.spawn_search(uEditDupHashes[type]);
573 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
576 function uEditMarkCardLost() {
578 for( var c in patron.cards() ) {
580 var card = patron.cards()[c];
581 if( patron.card().id() == card.id() ) {
583 /* de-activite the current card */
587 /* create a new card for the patron */
588 var newcard = new ac();
589 newcard.id(uEditCardVirtId--);
591 patron.card(newcard);
592 patron.cards().push(newcard);
595 /* reset the widget */
596 var field = uEditFindFieldByWId('ue_barcode');
597 field.widget.node.disabled = false;
598 field.widget.node.value = "";
599 field.widget.node.onchange();
600 field.object = newcard;
606 function compactArray(arr) {
608 for( var i = 0; arr && i < arr.length; i++ ) {