5 var identTypesCache = {};
6 var statCatsCache = {};
8 var surveyQuestionsCache = {};
9 var surveyAnswersCache = {};
12 var netLevelsCache = {};
15 /* fetch the necessary data to start off */
16 function uEditInit() {
19 session = cgi.param('ses');
20 clone = cgi.param('clone');
21 if(!session) throw "User session is not defined";
24 $('uedit_user').appendChild(text(USER.usrname()));
26 setTimeout( function() {
27 uEditBuild(); uEditShowPage('uedit_userid'); }, 20 );
30 /* ------------------------------------------------------------------------------ */
32 /* ------------------------------------------------------------------------------ */
33 function uEditFetchIdentTypes() {
34 var req = new Request(FETCH_ID_TYPES);
39 function uEditFetchStatCats() {
40 var req = new Request(SC_FETCH_ALL, SESSION);
45 function uEditFetchSurveys() {
46 var req = new Request(SV_FETCH_ALL, SESSION);
51 function uEditFetchGroups() {
52 var req = new Request(FETCH_GROUPS);
57 function uEditFetchNetLevels() {
58 var req = new Request(FETCH_NET_LEVELS, SESSION);
62 /* ------------------------------------------------------------------------------ */
66 /* fetches necessary and builds the UI */
67 function uEditBuild() {
68 //fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
70 uEditBuildLibSelector();
71 patron = fetchFleshedUser(cgi.param('usr'));
72 if(!patron) patron = uEditNewPatron();
76 uEditFetchIdentTypes(),
91 $('ue_barcode').disabled = true;
92 unHideMe($('ue_mark_card_lost'));
93 unHideMe($('ue_reset_pw'));
98 /* creates a new patron object with card attached */
99 var uEditCardVirtId = -1;
100 function uEditNewPatron() {
101 var patron = new au();
105 card.id(uEditCardVirtId--);
108 patron.cards([card]);
109 patron.stat_cat_entries([]);
110 patron.survey_responses([]);
111 patron.addresses([]);
112 patron.home_ou(USER.ws_ou());
113 uEditMakeRandomPw(patron);
117 function uEditMakeRandomPw(patron) {
118 var rand = Math.random();
119 rand = parseInt(rand * 10000) + '';
120 while(rand.length < 4) rand += '0';
121 appendClear($('ue_password_plain'),text(rand));
122 unHideMe($('ue_password_gen'));
127 function uEditResetPw() {
128 var pw = uEditMakeRandomPw(patron);
129 $('ue_password1').value = pw;
130 $('ue_password2').value = pw;
133 function uEditClone(clone) {
135 var cloneUser = fetchFleshedUser(clone);
136 patron.usrgroup(cloneUser.usrgroup());
138 if( cloneUser.day_phone() )
139 $('ue_day_phone').value = cloneUser.day_phone();
140 if( cloneUser.evening_phone() )
141 $('ue_night_phone').value = cloneUser.evening_phone();
142 if( cloneUser.other_phone() )
143 $('ue_other_phone').value = cloneUser.other_phone();
144 setSelector($('ue_org_selector'), cloneUser.home_ou());
147 setSelector($('ue_profile'), cloneUser.profile());
149 /* force the expire date to be set */
150 $('ue_profile').onchange();
152 for( var a in cloneUser.addresses() ) {
153 var addr = cloneUser.addresses()[a];
154 if( addr.id() == cloneUser.mailing_address().id() )
155 patron.mailing_address(addr);
156 if( addr.id() == cloneUser.billing_address().id() )
157 patron.billing_address(addr);
158 patron.addresses().push(addr);
161 uEditBuildAddrs(patron);
165 /* Creates a new blank address,
166 adds it to the user and the fields array */
167 var uEditVirtualAddrId = -1;
168 function uEditCreateNewAddr() {
169 var addr = new aua();
170 addr.id(uEditVirtualAddrId--);
172 addr.usr(patron.id());
173 addr.country(defaultCountry);
174 if(!patron.addresses())
175 patron.addresses([]);
176 if(patron.addresses().length == 0) {
177 patron.mailing_address(addr);
178 patron.billing_address(addr);
180 uEditBuildAddrFields(patron, addr);
181 patron.addresses().push(addr);
182 uEditIterateFields(function(f) { uEditCheckValid(f); });
187 /* kicks off the UI drawing */
188 function uEditDraw(identTypes, groups, statCats, surveys, netLevels ) {
189 hideMe($('uedit_loading'));
190 unHideMe($('ue_maintd'));
193 uEditDrawIDTypes(identTypes);
194 uEditDrawGroups(groups);
195 uEditDrawStatCats(statCats);
196 uEditDrawSurveys(surveys);
197 uEditDrawNetLevels(netLevels);
198 uEditDefineData(patron);
200 uEditIterateFields(function(f) { uEditActivateField(f) });
201 uEditIterateFields(function(f) { uEditCheckValid(f); });
206 /** Applies the event handlers and sets the data for the field */
207 function uEditActivateField(field) {
209 if( field.widget.id ) {
210 field.widget.node = $(field.widget.id);
214 $n(field.widget.base, field.widget.name);
217 uEditSetOnchange(field);
219 if(field.widget.onblur) {
220 field.widget.node.onblur =
221 function() { field.widget.onblur(field); };
224 field.widget.node.disabled = field.widget.disabled;
225 var val = field.object[field.key]();
226 if(val == null) return;
228 if( field.widget.type == 'input' )
229 field.widget.node.value = val;
231 if( field.widget.type == 'select' )
232 setSelector(field.widget.node, val);
234 if( field.widget.type == 'checkbox' )
235 field.widget.node.checked =
236 (val && val != 'f') ? true : false;
238 if( field.widget.onload )
239 field.widget.onload(val);
243 if(field.key == 'ident_value') alert(field.widget.onblur);
249 /* set up the onchange event for the field */
250 function uEditSetOnchange(field) {
251 var func = function() {uEditOnChange( field );}
252 field.widget.node.onchange = func;
254 if(field.widget.type != 'select')
255 field.widget.node.onkeyup = func;
258 /* find the current value of the field object's widget */
259 function uEditNodeVal(field) {
260 if(field.widget.type == 'input')
261 return field.widget.node.value;
263 if(field.widget.type == 'checkbox')
264 return field.widget.node.checked;
266 if(field.widget.type == 'select')
267 return getSelectorVal(field.widget.node);
271 /* update a field value */
272 function uEditOnChange(field) {
274 var newval = uEditNodeVal(field);
275 field.object[field.key](newval);
276 field.object.ischanged(1);
278 if(field.widget.onpostchange)
279 field.widget.onpostchange(field, newval);
282 uEditIterateFields(function(f) { uEditCheckValid(f); });
287 function uEditCheckValid(field) {
288 var newval = uEditNodeVal(field);
291 if(field.widget.regex) {
292 if(newval.match(field.widget.regex))
293 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
295 addCSSClass(field.widget.node, CSS_INVALID_DATA);
298 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
304 addCSSClass(field.widget.node, CSS_INVALID_DATA);
307 removeCSSClass(field.widget.node, CSS_INVALID_DATA);
312 /* find a field object by object key */
313 function uEditFindFieldByKey(key) {
314 var fields = grep( dataFields,
315 function(item) { return (item.key == key); });
316 return (fields) ? fields[0] : null;
319 /* find a list of fields by object key */
320 function uEditFindFieldsByKey(key) {
321 return grep( dataFields,
322 function(item) { return (item.key == key); });
325 /* find a field object by widget id */
326 function uEditFindFieldByWId(id) {
327 var fields = grep( dataFields,
328 function(item) { return (item.widget.id == id); });
329 return (fields) ? fields[0] : null;
333 function uEditIterateFields(callback) {
334 for( var f in dataFields )
335 callback(dataFields[f]);
339 function uEditGetErrorStrings() {
344 if( field.widget.node.className.indexOf(CSS_INVALID_DATA) != -1) {
345 var str = $(field.errkey).innerHTML;
346 if(str) errors.push(str);
353 /* munge up something for all of the required surveys
354 (which are not registered with the fields) */
356 /* AWAITS POLICY DECISION */
359 var rows = $('ue_survey_table').getElementsByTagName('tr');
360 for( var r in rows ) {
363 var sel = $n(row, 'ue_survey_answer');
365 var qstn = row.getAttribute('question');
368 qstn = surveyQuestionsCache[qstn];
369 survey = surveysCache[qstn.survey()];
370 var val = getSelectorVal(sel);
371 if(!val && survey.required() && survey.required() != 'f')
372 errors.push($('ue_bad_survey').innerHTML);
377 /* ------------------------------------------------------------ */
379 if(errors[0]) return errors;
383 function uEditAlertErrors() {
384 var errors = uEditGetErrorStrings();
385 if(!errors) return false;
386 alert(errors.join("\n"));
391 /* send the user to the database */
392 function uEditSaveUser(cloneme) {
394 if(uEditGetErrorStrings()) {
399 /* null is unique in the db, but '' is not */
400 if( ! patron.ident_value() ) patron.ident_value(null);
401 if( ! patron.ident_value2() ) patron.ident_value2(null);
403 var req = new Request(UPDATE_PATRON, SESSION, patron);
405 var newuser = req.result();
408 if( (evt = checkILSEvent(newuser)) || ! newuser ) {
409 if(evt) alert(js2JSON(newuser));
413 alert($('ue_success').innerHTML);
417 /* if the user we just created was a clone, and we want to clone it,
418 we really want to clone the original */
419 if( clone ) cloneme = clone;
420 else cloneme = newuser.id();
423 if (window.xulG && typeof window.xulG.on_save == 'function') {
424 window.xulG.on_save(newuser, cloneme);
428 var href = location.href;
430 href = href.replace(/\&?usr=\d+/, '');
431 href = href.replace(/\&?clone=\d+/, '');
433 if( cloneme ) href += '&clone=' + cloneme;
434 location.href = href;
438 function uEditCancel() {
439 var href = location.href;
440 href = href.replace(/\&?usr=\d+/, '');
441 href = href.replace(/\&?clone=\d+/, '');
442 location.href = href;
446 var uEditDupHashes = {};
447 var uEditDupTemplate;
449 function uEditRunDupeSearch(type, search_hash) {
451 if(!patron.isnew()) return;
453 _debug('dup search: ' + js2JSON(search_hash));
455 var req = new Request(PATRON_SEARCH, SESSION, search_hash);
457 var container = $('dup_div_container');
458 if(!uEditDupTemplate)
459 uEditDupTemplate = container.removeChild($('dup_div'));
461 /* clear any existing dups for this type */
462 iterate( container.getElementsByTagName('div'),
464 if( d.getAttribute('type') == type ) {
465 container.removeChild(d)
473 uEditHandleDupResults( r.getResultObject(), search_hash, type, container );
480 function uEditHandleDupResults(ids, search_hash, type, container) {
482 _debug('dup search results: ' + js2JSON(ids));
484 if(!(ids && ids[0])) /* no results */
485 return uEditDupHashes[type] = null;
487 /* add a dup link to the UI and plug in the data */
488 var node = uEditDupTemplate.cloneNode(true);
489 container.appendChild(node);
490 node.setAttribute('type', type);
492 var link = $n(node, 'link');
493 link.setAttribute('type', type);
495 $n(node,'count').appendChild(text(ids.length));
497 for( var o in search_hash )
498 $n(node, 'data').appendChild(
499 text(search_hash[o].value + ' '));
501 uEditDupHashes[type] = search_hash;
505 if(confirm($('ue_dup_ident1').innerHTML))
506 uEditShowSearch(null, type);
512 function uEditShowSearch(link,type) {
513 if(!type) type = link.getAttribute('type');
515 window.xulG.spawn_search(uEditDupHashes[type]);
516 else alert('Search would be:\n' + js2JSON(uEditDupHashes[type]));
519 function uEditMarkCardLost() {
521 for( var c in patron.cards() ) {
523 var card = patron.cards()[c];
524 if( patron.card().id() == card.id() ) {
526 /* de-activite the current card */
530 /* create a new card for the patron */
531 var newcard = new ac();
532 newcard.id(uEditCardVirtId--);
534 patron.card(newcard);
535 patron.cards().push(newcard);
538 /* reset the widget */
539 var field = uEditFindFieldByWId('ue_barcode');
540 field.widget.node.disabled = false;
541 field.widget.node.value = "";
542 field.widget.node.onchange();
543 field.object = newcard;
549 function compactArray(arr) {
551 for( var i = 0; arr && i < arr.length; i++ ) {