]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/user_editor.js
trudging forward. added more sanity checks and more info is added in the saveuser...
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / user_editor.js
1
2 var cgi                                 = null;
3 var patron                              = null;
4 var advanced                    = false;
5 var SC_FETCH_ALL     = 'open-ils.circ:open-ils.circ.stat_cat.actor.retrieve.all';
6 var SC_CREATE_MAP               = 'open-ils.circ:open-ils.circ.stat_cat.actor.user_map.create';
7 var SV_FETCH_ALL                = 'open-ils.circ:open-ils.circ.survey.retrieve.all';
8 var FETCH_ID_TYPES      = 'open-ils.actor:open-ils.actor.user.ident_types.retrieve';
9 var identTypes                  = null;
10
11 var myPerms             = [ 'CREATE_USER', 'UPDATE_USER', 'CREATE_PATRON_STAT_CAT_ENTRY_MAP' ];
12
13 var pages               = [ 
14         'uedit_userid', 
15         'uedit_contact_info', 
16         'uedit_addresses', 
17         'uedit_groups', 
18         'uedit_stat_cats', 
19         'uedit_surveys',
20         'uedit_finalize',
21         ];
22
23 /* ID's of objects that should be focused when their page is made visible */
24 var pageFocus   = [
25         'ue_barcode',
26         'ue_email1',
27         'ue_addr_label',
28         'ue_profile',
29         'ue_stat_cat_selector_1',
30         'ue_survey_selector_1',
31         'ue_view_summary'
32 ];
33
34 /* fetch the necessary data to start off */
35 function uEditInit() {
36
37         cgi             = new CGI();
38         session = cgi.param('ses');
39         if(cgi.param('adv')) advanced = true 
40         if(!session) throw "User session is not defined";
41
42         fetchUser(session);
43         $('uedit_user').appendChild(text(USER.usrname()));
44         uEditShowPage('uedit_userid');
45
46         setTimeout( 
47                 function() { 
48                         fetchHighestPermOrgs( SESSION, USER.id(), myPerms );
49                         uEditDrawUser(fetchFleshedUser(cgi.param('usr')));
50                         uEditFetchIDTypes();
51                         uEditFetchStatCats();
52                         uEditFetchSurveys();
53                 }, 20 
54         );
55 }
56
57 function uEditNext() {
58         var i = _findActive();
59         if( i < (pages.length - 1)) uEditShowPage(pages[i+1]);
60 }
61
62 function uEditPrev() {
63         var i = _findActive();
64         if( i > 0 ) uEditShowPage(pages[i-1]);
65 }
66
67
68 function uEditShowPage(id) {
69         if( id == null ) return;
70
71         for( var p in pages ) {
72                 var page = pages[p];
73                 hideMe($(page));
74                 removeCSSClass($(page+'_label'), 'label_active');
75         }
76
77         unHideMe($(id));
78         addCSSClass($(id+'_label'), 'label_active');
79         var idx = _findPageIdx(id);
80         var fpage = pageFocus[idx];
81         if(fpage) { $(fpage).focus(); try{$(fpage).select()}catch(e){} }
82
83         unHideMe($('ue_back'));
84         unHideMe($('ue_fwd'));
85
86         if(idx == 0) hideMe($('ue_back'));
87         if(idx == (pages.length-1)) hideMe($('ue_fwd'));
88 }
89
90
91 function _findActive() {
92         for( var p in pages ) {
93                 if(! $(pages[p]).className.match(/hide_me/) )
94                         return parseInt(p);
95         }
96         return null;
97 }
98 function _findPageIdx(name) {
99         for( var i in pages ) {
100                 var page = pages[i];
101                 if( page == name ) return i;
102         }
103         return -1;
104 }
105
106 function uEditAddrHighlight( node, type ) {
107         var tbody = $('ue_address_tbody');
108         for( var c in tbody.childNodes ) {
109                 var row = tbody.childNodes[c];
110                 if(row.nodeType != XML_ELEMENT_NODE ) continue;
111                 var div = $n(row,'ue_addr_'+type+'_yes').parentNode;
112                 removeCSSClass(div, 'addr_info_checked');
113         }
114         addCSSClass(node.parentNode, 'addr_info_checked');
115 }
116
117
118
119 function uEditDrawUser(patron) {
120         if(!patron) return 0;
121 }
122
123 function uEditFetchIDTypes() {
124         var req = new Request(FETCH_ID_TYPES);
125         req.callback(uEditDrawIDTypes);
126         req.send();
127 }
128
129 function uEditDrawIDTypes(r) {
130
131         var types = r.getResultObject();
132         var pri_sel = $('ue_primary_ident_type');
133         var sec_sel = $('ue_secondary_ident_type');
134
135         var idx = 1;
136         for( var t in types ) {
137                 var type = types[t];
138                 setSelectorVal( pri_sel, idx, type.name(), type.id() );
139                 setSelectorVal( sec_sel, idx++, type.name(), type.id() );
140         }
141 }
142
143 function uEditFetchStatCats() {
144         var req = new Request(SC_FETCH_ALL, SESSION);
145         req.callback(uEditDrawStatCats);
146         req.send();
147 }
148
149 function uEditDrawStatCats(r) {
150         var cats = r.getResultObject();
151         var tbody = $('ue_stat_cat_tbody');
152         var templ = tbody.removeChild($('ue_stat_cat_row'));
153
154         for( var c in cats ) {
155                 var row = templ.cloneNode(true);
156                 uEditInsertCat( tbody, row, cats[c], c );
157                 tbody.appendChild(row);
158         }
159 }
160
161 function uEditInsertCat( tbody, row, cat, idx ) {
162
163         cat.entries().sort(  /* sort the entries by value */
164                 function( a, b ) { 
165                         if( a.value().toLowerCase() > b.value().toLowerCase()) return 1;
166                         if( a.value().toLowerCase() < b.value().toLowerCase()) return -1;
167                         return 0;
168                 }
169         );
170
171         var selector = $n(row, 'ue_stat_cat_selector');
172         if( idx == 0 ) selector.id = 'ue_stat_cat_selector_1'; 
173         $n(row, 'ue_stat_cat_name').appendChild(text(cat.name()));
174         $n(row, 'ue_stat_cat_owner').appendChild(text(fetchOrgUnit(cat.owner()).shortname()));
175
176         var idx = 1;
177         for( var e in cat.entries() ) {
178                 var entry = cat.entries()[e];
179                 setSelectorVal( selector, idx++, entry.value(), entry.id() );
180         }
181 }
182
183 function uEditFetchSurveys() {
184         var req = new Request(SV_FETCH_ALL, SESSION);
185         req.callback(uEditDrawSurveys);
186         req.send();
187 }
188
189 function uEditDrawSurveys(r) {
190
191         var surveys = r.getResultObject();
192         var div = $('uedit_surveys');
193         var table = div.removeChild($('ue_survey_table'));
194
195         var x = 0;
196         for( var s in surveys ) {
197                 var survey = surveys[s];
198                 var clone = table.cloneNode(true);
199                 uEditInsertSurvey( div, clone, survey, x++ );
200                 div.appendChild(clone);
201         }
202 }
203
204 function uEditInsertSurvey( div, table, survey, sidx ) {
205         $n(table, 'ue_survey_name').appendChild(text(survey.name()));
206         $n(table, 'ue_survey_desc').appendChild(text(survey.description()));
207         var tbody = $n(table, 'ue_survey_tbody');
208         var templ = tbody.removeChild($n(table, 'ue_survey_row'));
209
210         var polldiv             = $('ue_survey_answer_poll');
211
212         var idx = 1;
213         for( var q in survey.questions() ) {
214                 var row = templ.cloneNode(true);
215                 uEditInsertSurveyQuestion( div, table, tbody, row, survey, survey.questions()[q], sidx );
216                 tbody.appendChild(row);
217         }
218 }
219
220 function uEditInsertSurveyQuestion( div, table, tbody, row, survey, question, sidx ) {
221
222         $n(row, 'ue_survey_question').appendChild(text(question.question()));
223
224         var selector    = $n(row, 'ue_survey_answer');
225         var polldiv             = $n(row, 'ue_survey_answer_poll');
226         var idx                 = 1;
227         var polltbody   = $n(row, 'ue_survey_answer_poll_tbody');
228         var pollrow             = polltbody.removeChild($n(polltbody, 'ue_survey_answer_poll_row'));
229
230         if( sidx == 0 ) selector.id = 'ue_survey_selector_1'; 
231
232         for( var a in question.answers() ) {
233
234                 var answer = question.answers()[a];
235
236                 if( survey.poll() ) {
237
238                         unHideMe(polldiv);
239                         var prow = pollrow.cloneNode(true);
240                         $n(prow, 'ue_survey_answer_poll_answer').appendChild(text(answer.answer()));
241
242                         $n(prow, 'ue_survey_answer_poll_radio').appendChild(
243                                 elem('input', { 
244                                         type    : 'radio', 
245                                         name    : 'survey_poll_answer_'+survey.id(),
246                                         id              :  answer.id()
247                                 }));
248
249                         polltbody.appendChild(prow);
250
251                 } else {
252
253                         unHideMe(selector.parentNode);
254                         setSelectorVal( selector, idx++, answer.answer(), answer.id() );
255                 }
256         }
257 }
258
259
260 function uEditFetchError(id) { return $(id).innerHTML + "\n"; }
261
262
263 function uEditSaveUser() {
264
265         var card                = null;
266         var errors      = "";
267
268         if(patron == null) { 
269                 patron = new au(); 
270                 patron.isnew(1);
271                 card = new ac();
272                 patron.card(-1); /* attach to the virtual id of the card */
273                 patron.cards([card]);
274
275         } else { 
276                 patron.ischanged(1); 
277                 patron.isnew(0);
278         }
279
280         errors += uEditFleshCard(card);
281         errors += uEditAddBasicPatronInfo(patron);
282         errors += uEditAddIdents(patron);
283
284         if(errors) alert(errors);
285         else alert(js2JSON(patron));
286 }
287
288 function uEditSetVal( obj, func, val, regex ) {
289
290         if( val == null ) return false;
291         if(!instanceOf(val, String)) {
292                 try {
293                         val = val.value;
294                 } catch(e) { return false; }
295         }
296
297         if(val == "" ) return false;
298         if(regex && !val.match(regex) ) return false;
299
300         obj[func](val);
301         alert("Setting val: "+val);
302
303         return true;
304 }
305
306
307 function uEditAddBasicPatronInfo(patron) {
308
309         var errors = "";
310
311         if(!uEditSetVal(patron, "usrname", $('ue_username'), /.+/ ))
312                 errors += uEditFetchError('ue_bad_username');
313
314         /* make sure passwords match */
315         var p1 = $('ue_password1').value;
316         var p2 = $('ue_password1').value;
317         if( p1 != p2 || !uEditSetVal( patron, "passwd", p1 )) 
318                 errors += uEditFetchError('ue_bad_password');
319
320         if(!uEditSetVal(patron, "first_given_name", $('ue_firstname'), /.+/)) 
321                 errors += uEditFetchError('ue_bad_firstname');
322
323         if(!uEditSetVal(patron, "second_given_name", $('ue_middlename'), /.+/)) 
324                 errors += uEditFetchError('ue_bad_middlename');
325
326         if(!uEditSetVal(patron, "family_name", $('ue_lastname'), /.+/)) 
327                 errors += uEditFetchError('ue_bad_lastname');
328
329         patron.suffix($('ue_suffix').value); /* suffis isn't required */
330
331         if(!uEditSetVal(patron, "dob", $('ue_dob'), /^\d{4}-\d{2}-\d{2}/ ) )
332                 errors += uEditFetchError('ue_bad_dob');
333
334         /* make sure emails match */
335         var email       = $('ue_email1').value;
336         var email2      = $('ue_email2').value;
337         if( email != email2 || !uEditSetVal(patron, "email", email, /.+\@.+\..+/ ))
338                 errors += uEditFetchError('ue_bad_email');
339
340         return errors;
341 }
342
343 function uEditFleshCard(card) {
344         if(!card) return "";
345
346         if(!uEditSetVal( card, "barcode", $('ue_barcode') ))
347                 return uEditFetchError('ue_bad_barcode');
348
349         card.id(-1);
350         card.active(1);
351         return "";
352 }
353
354 function uEditAddIdents(patron) {
355
356         var ptype1 = 
357         var ptype2 = getSelectorVal($('ue_secondary_ident_type'));
358         var val1 = $('ue_primary_ident').value;
359         var val2 = $('ue_secondary_ident').value;
360         
361         if( !uEditSetVal( patron, 
362                 "ident_type", getSelectorVal($('ue_primary_ident_type'), /^[0-9]+$/ );
363                 return uEditFetchError('ue_no_ident');
364
365         if( !uEditSetVal( patron, 
366                 "ident_type2", getSelectorVal($('ue_secondary_ident_type'), /^[0-9]+$/ );
367                 return uEditFetchError('ue_no_ident');
368
369         if( !uEditSetVal( patron, "ident_value", $('ue_primary_ident') ))
370                 return uEditFetchError('ue_no_ident');
371
372         if( !uEditSetVal( patron, "ident_value2", $('ue_secondary_ident') ))
373                 return uEditFetchError('ue_no_ident');
374
375         return "";
376 }
377