adding required surveys to the user editor
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / user_edit.js
1 var cgi;
2 var orgTree;
3 var user;
4 var ses_id;
5 var user_groups = [];
6
7 var required_user_parts = {
8         usrname:'User Name',
9         first_given_name:'First Name',
10         family_name:'Last Name',
11         dob:'Date of Birth',
12         ident_type:'Primary Identification Type',
13         ident_value:'Primary Identification',
14         day_phone:'Daytime Phone',
15         home_ou:'Home Library',
16         profile:'Profile Group',
17         standing:'Standing',
18 };
19
20 var required_addr_parts = {
21         street1:'Street 1',
22         city:'City',
23         state:'State',
24         post_code:'ZIP',
25         country:'Country',
26         address_type:'Address Label',
27 };
28
29 function reset_crc () {
30         document.forms.editor.elements["user.claims_returned_count"].value = '0';
31         user.claims_returned_count(0);
32 }
33
34 function clear_alert_message () {
35         document.forms.editor.elements["user.alert_message"].value = ' ';
36         user.alert_message('');
37 }
38
39 function save_user () {
40         user.ischanged(1);
41
42         //alert(        js2JSON(user));
43         //return false;
44
45         try {
46
47                 for (var i in required_user_parts) {
48                         if (!user[i]()) {
49                                 throw required_user_parts[i] + " is required.";
50                         }
51                 }
52
53                 for (var j in user.addresses()) {
54                         if (user.addresses()[j].isdeleted()) continue;
55
56                         for (var i in required_addr_parts) {
57                                 if (!user.addresses()[j][i]()) {
58                                         throw required_addr_parts[i] + " is required.";
59                                 }
60                         }
61                 }
62
63                 var res = [];
64                 for (var i in responses) {
65                         if (!i) continue;
66                         for (var j in responses[i]) {
67                                 if (!j) continue;
68                                 var r = new asvr();
69                                 r.usr(survey_user);
70                                 r.survey(i);
71                                 r.question(j);
72                                 r.answer(responses[i][j]);
73                                 r.answer_date( document.getElementById('e_date_'+i).value );
74                                 res.push(r);
75                         }
76                 }
77
78                 user.survey_responses( res );
79                 responses = {};
80
81
82                 if (user.billing_address().isdeleted()) 
83                         throw "Please select a valid Billing Address";
84
85                 if (user.mailing_address().isdeleted()) 
86                         throw "Please select a valid Mailing Address";
87
88                 var req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.patron.update', ses_id, user );
89                 req.send(true);
90                 var ok = req.getResultObject();
91
92                 req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.user.set_groups', ses_id, ok.id(), user_groups );
93                 req.send(true);
94                 req.getResultObject();
95
96                 if (ok) {
97                         alert(  'User ' + ok.usrname() +
98                                 ' [' + ok.card().barcode() + '] ' +
99                                 ' successfully saved!');
100                 }
101
102                 init_editor(ok);
103
104         } catch (e) {
105                 alert(e)
106         };
107
108         return false;
109 }
110
111 var _fake_id = 0;
112 function fakeid () {
113         return --_fake_id;
114 };
115
116 function init_editor (u) {
117         
118         var x = document.getElementById('editor').elements;
119
120         if (!u) {
121                 cgi = new CGI();
122                 ses_id = cgi.param('ses');
123
124                 var usr_id = cgi.param('usr');
125                 if (!usr_id) {
126
127                         user = new au();
128                         user.isnew(1);
129
130                         user.mailing_address( new aua() );
131                         user.mailing_address().isnew(1);
132                         user.mailing_address().id(fakeid());
133
134                         user.billing_address( user.mailing_address() );
135
136                         user.card( new ac() );
137                         user.card().isnew(1);
138                         user.card().id(fakeid());
139
140                         user.addresses([]);
141                         user.addresses().push(user.mailing_address());
142                         
143                         user.cards([]);
144                         user.cards().push(user.card());
145
146                 } else {
147                         var req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.user.fleshed.retrieve', ses_id, usr_id );
148                         req.send(true);
149                         user = req.getResultObject();
150                 }
151
152         } else {
153                 user = u;
154         }
155
156         if (user.id()) x['user.id'].value = user.id();
157         if (cgi.param('adv')) x['user.id'].parentNode.parentNode.setAttribute('adv', 'false');
158
159         if (user.create_date()) x['user.create_date'].value = user.create_date();
160
161         if (user.usrname()) x['user.usrname'].value = user.usrname();
162         x['user.usrname'].setAttribute('onchange','user.usrname(this.value)');
163
164         if (user.card() && user.card().barcode()) x['user.card.barcode'].value = user.card().barcode();
165         x['user.card.barcode'].setAttribute('onchange','user.card().barcode(this.value)');
166         if (user.isnew()) {
167                 x['user.card.barcode'].disabled = false;
168         } else {
169                 x['replace_card'].className = '';
170                 x['replace_card'].setAttribute('onclick',
171                         'user.card(new ac()); ' +
172                         'user.card().isnew(1); ' +
173                         'user.card().id(fakeid()); ' +
174                         'user.cards().push(user.card()); ' +
175                         'this.parentNode.firstChild.disabled = false; ' +
176                         'this.parentNode.firstChild.value = ""; ' +
177                         'this.parentNode.firstChild.focus(); ' +
178                         'return false;'
179                 );
180         }
181
182         if (user.passwd()) x['user.passwd'].value = user.passwd();
183         x['user.passwd'].setAttribute('onchange','user.passwd(this.value)');
184
185         if (user.prefix()) x['user.prefix'].value = user.prefix();
186         x['user.prefix'].setAttribute('onchange','user.prefix(this.value)');
187
188         if (user.first_given_name()) x['user.first_given_name'].value = user.first_given_name();
189         x['user.first_given_name'].setAttribute('onchange','user.first_given_name(this.value)');
190
191         if (user.second_given_name()) x['user.second_given_name'].value = user.second_given_name();
192         x['user.second_given_name'].setAttribute('onchange','user.second_given_name(this.value);');
193
194         if (user.family_name()) x['user.family_name'].value = user.family_name();
195         x['user.family_name'].setAttribute('onchange','user.family_name(this.value)');
196
197         if (user.suffix()) x['user.suffix'].value = user.suffix();
198         x['user.suffix'].setAttribute('onchange','user.suffix(this.value)');
199
200         if (user.dob()) x['user.dob'].value = user.dob();
201         x['user.dob'].setAttribute('onchange','user.dob(this.value)');
202
203         if (user.ident_value()) x['user.ident_value'].value = user.ident_value();
204         x['user.ident_value'].setAttribute('onchange','user.ident_value(this.value)');
205
206         if (user.ident_value2()) x['user.ident_value2'].value = user.ident_value2();
207         x['user.ident_value2'].setAttribute('onchange','user.ident_value2(this.value)');
208
209         if (user.email()) x['user.email'].value = user.email();
210         x['user.email'].setAttribute('onchange','user.email(this.value)');
211
212         if (user.day_phone()) x['user.day_phone'].value = user.day_phone();
213         x['user.day_phone'].setAttribute('onchange','user.day_phone(this.value)');
214
215         if (user.evening_phone()) x['user.evening_phone'].value = user.evening_phone();
216         x['user.evening_phone'].setAttribute('onchange','user.evening_phone(this.value)');
217
218         if (user.other_phone()) x['user.other_phone'].value = user.other_phone();
219         x['user.other_phone'].setAttribute('onchange','user.other_phone(this.value)');
220
221         if (user.expire_date()) x['user.expire_date'].value = user.expire_date();
222         x['user.expire_date'].setAttribute('onchange','user.expire_date(this.value)');
223
224         if (user.active()) x['user.active'].checked = true;
225         x['user.active'].setAttribute('onchange','user.active(this.checked ? "t" : "f" )');
226
227         if (user.master_account()) x['user.master_account'].checked = true;
228         x['user.master_account'].setAttribute('onchange','user.master_account(this.checked ? "t" : "f" )');
229
230         if (user.super_user()) x['user.super_user'].checked = true;
231         x['user.super_user'].setAttribute('onchange','user.super_user(this.checked ? "t" : "f" )');
232         if (cgi.param('adv')) x['user.super_user'].parentNode.parentNode.setAttribute('adv', 'false');
233
234         if (user.claims_returned_count()) x['user.claims_returned_count'].value = user.claims_returned_count();
235         // onchange handled by func above
236
237         if (user.alert_message()) x['user.alert_message'].value = user.alert_message();
238         // onchange handled by func above
239
240
241         // set up the home_ou selector
242         req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.org_tree.retrieve' );
243         req.send(true);
244         orgTree = req.getResultObject();
245
246         selectBuilder(
247                 'user.home_ou',
248                 [orgTree],
249                 user.home_ou(),
250                 { label_field           : 'name',
251                   value_field           : 'id',
252                   empty_label           : '-- Required --',
253                   empty_value           : '',
254                   clear                 : true,
255                   child_field_name      : 'children' }
256         );
257
258         x['user.home_ou'].setAttribute('onchange','user.home_ou(this.options[this.selectedIndex].value)');
259
260         // set up the ident_type selector
261         req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.user.ident_types.retrieve' );
262         req.send(true);
263         ident_type_list = req.getResultObject();
264
265         selectBuilder(
266                 'user.ident_type',
267                 ident_type_list,
268                 user.ident_type(),
269                 { label_field           : 'name',
270                   value_field           : 'id',
271                   empty_label           : '-- Required --',
272                   empty_value           : '',
273                   clear                 : true }
274         );
275
276         x['user.ident_type'].setAttribute('onchange','user.ident_type(this.options[this.selectedIndex].value)');
277
278         selectBuilder(
279                 'user.ident_type2',
280                 ident_type_list,
281                 (user.ident_value2 == '' ? user.ident_type2() : ''),
282                 { label_field           : 'name',
283                   value_field           : 'id',
284                   empty_label           : '-- Optional --',
285                   empty_value           : '',
286                   clear                 : true }
287         );
288
289         x['user.ident_type2'].setAttribute('onchange','var x = this.options[this.selectedIndex].value; x ? user.ident_type2(x) : user.ident_type2(null);');
290
291
292         // set up the standing selector
293         req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.standings.retrieve' );
294         req.send(true);
295         standing_list = req.getResultObject();
296
297         selectBuilder(
298                 'user.standing',
299                 standing_list,
300                 user.standing(),
301                 { label_field           : 'value',
302                   value_field           : 'id',
303                   empty_label           : '-- Required --',
304                   empty_value           : '',
305                   clear                 : true }
306         );
307
308         x['user.standing'].setAttribute('onchange','user.standing(this.options[this.selectedIndex].value)');
309
310         // set up the profile selector
311         req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.groups.tree.retrieve' );
312         req.send(true);
313         group_tree = req.getResultObject();
314
315         selectBuilder(
316                 'user.profile',
317                 [group_tree],
318                 user.profile(),
319                 { label_field           : 'name',
320                   value_field           : 'id',
321                   empty_label           : '-- Required --',
322                   empty_value           : '',
323                   clear                 : true,
324                   child_field_name      : 'children' }
325         );
326
327         x['user.profile'].setAttribute('onchange','user.profile(this.options[this.selectedIndex].value)');
328
329         // set up the profile selector
330         var user_group_objects = [];
331         if (user.id() > 0) {
332                 req = new RemoteRequest( 'open-ils.actor', 'open-ils.actor.user.get_groups', ses_id, user.id() );
333                 req.send(true);
334                 user_group_objects = req.getResultObject();
335         }
336
337         user_groups = [];
338         for (var i in user_group_objects) {
339                 user_groups.push(user_group_objects[i].grp());
340         }
341
342         selectBuilder(
343                 'permgroups',
344                 [group_tree],
345                 user_groups,
346                 { label_field           : 'name',
347                   value_field           : 'id',
348                   clear                 : true,
349                   child_field_name      : 'children' }
350         );
351
352         x['permgroups'].setAttribute( 'onchange', 
353                 'window.user_groups = [];' +
354                 'for (var i = 0; i < this.options.length; i++) {' +
355                 '       if (this.options[i].selected)' +
356                 '               window.user_groups.push(this.options[i].value);' +
357                 '}');
358
359                 display_all_addresses();
360
361         if (cgi.param('adv')) x['permgroups'].parentNode.parentNode.setAttribute('adv', 'false');
362
363         req = new RemoteRequest( 'open-ils.circ', 'open-ils.circ.survey.retrieve.required', ses_id );
364         req.send(true);
365         surveys = req.getResultObject();
366
367         var f = document.getElementById('surveys');
368         for ( var i in surveys )
369                 display_survey( f, surveys[i].id(), user.id() );
370
371         return true;
372 }
373
374 function display_all_addresses () {
375         d = document.getElementById('addresses');
376         while (d.firstChild)
377                 d.removeChild(d.lastChild);
378
379         for (var i in user.addresses())
380                 display_address(document.getElementById('addresses'), user.addresses()[i]);
381 }
382
383 function new_addr () {
384         var x = new aua();
385         x.isnew(1);
386         x.id(fakeid());
387
388         user.addresses().push(x);
389         display_address(document.getElementById('addresses'), x);
390 }
391
392 function display_survey (div, sid, uid) {
393
394         var t = document.getElementById('survey-tmpl').firstChild.cloneNode(true);
395         div.appendChild(t);
396         
397         init_survey(t,sid,uid);
398 }
399
400 function display_address (div, adr) {
401
402         var dis = false;
403         if (adr.isdeleted()) dis = true;
404
405         var t = document.getElementById('addr-tmpl').getElementsByTagName('table')[0].cloneNode(true);
406         div.appendChild(t);
407
408         var x;
409
410         x = findNodeByName(t, 'adr.address_type');
411         x.disabled = dis;
412         if (adr.address_type()) x.value = adr.address_type();
413         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').address_type(this.value)');
414
415         x = findNodeByName(t, 'adr.street1');
416         x.disabled = dis;
417         if (adr.street1()) x.value = adr.street1();
418         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').street1(this.value)');
419
420         x = findNodeByName(t, 'adr.street2');
421         x.disabled = dis;
422         if (adr.street2()) x.value = adr.street2();
423         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').street2(this.value)');
424
425         x = findNodeByName(t, 'adr.city');
426         x.disabled = dis;
427         if (adr.city()) x.value = adr.city();
428         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').city(this.value)');
429
430         x = findNodeByName(t, 'adr.state');
431         x.disabled = dis;
432         if (adr.state()) x.value = adr.state();
433         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').state(this.value)');
434
435         x = findNodeByName(t, 'adr.post_code');
436         x.disabled = dis;
437         if (adr.post_code()) x.value = adr.post_code();
438         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').post_code(this.value)');
439
440         x = findNodeByName(t, 'adr.county');
441         x.disabled = dis;
442         if (adr.county()) x.value = adr.county();
443         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').county(this.value)');
444
445         x = findNodeByName(t, 'adr.country');
446         x.disabled = dis;
447         if (adr.country()) x.value = adr.country();
448         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').country(this.value)');
449
450         x = findNodeByName(t, 'adr.valid');
451         x.disabled = dis;
452         if (adr.valid()) x.checked = true;
453         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').valid(this.checked ? "t" : "f")');
454
455         x = findNodeByName(t, 'is_mailing');
456         x.disabled = dis;
457         x.value = adr.id();
458         x.setAttribute( 'onclick', 'user.mailing_address(findAddressById(' + adr.id() + '))');
459         if (adr.id() == user.mailing_address().id()) {
460                 x.checked = true;
461         }
462
463         x = findNodeByName(t, 'is_billing');
464         x.disabled = dis;
465         x.value = adr.id();
466         x.setAttribute( 'onclick', 'user.billing_address(findAddressById(' + adr.id() + '))');
467         if (adr.id() == user.billing_address().id()) {
468                 x.checked = true;
469         }
470
471         x = findNodeByName(t, 'remove');
472         if (dis) {
473                 x.setAttribute('onclick', 'unremove_adr(' + adr.id() + '); return false;');
474                 x.innerHTML = "Undelete this address";
475         } else {
476                 x.setAttribute('onclick', 'remove_adr(' + adr.id() + '); return false;');
477         }
478
479
480         return true;
481 }
482
483 function unremove_adr (id) {
484         findAddressById(id).isdeleted(0);
485         display_all_addresses();
486 }       
487
488 function remove_adr (id) {
489         findAddressById(id).isdeleted(1);
490         display_all_addresses();
491 }       
492
493 function findAddressById (id) {
494         for (var i in user.addresses())
495                 if (user.addresses()[i].id() == id) return user.addresses()[i];
496 }
497
498
499 function selectBuilder (id, objects, def, args) {
500         var label_field = args['label_field'];
501         var value_field = args['value_field'];
502         var depth = args['depth'];
503
504         if (!depth) depth = 0;
505
506         args['depth'] = parseInt(depth) + 1;
507
508         var child_field_name = args['child_field_name'];
509
510         var sel = document.getElementById(id);
511
512         if (args['clear']) {
513                 for (var o in sel.options) {
514                         sel.options[o] = null;
515                 }
516                 args['clear'] = false;
517                 if (args['empty_label']) {
518                         sel.options[0] = new Option( args['empty_label'], args['empty_value'] );
519                         sel.selectedIndex = 0;
520                 }
521         }
522
523         for (var i in objects) {
524                 var l = objects[i][label_field];
525                 var v = objects[i][value_field];
526
527                 if (typeof l == 'function')
528                         l = objects[i][label_field]();
529
530                 if (typeof v == 'function')
531                         v = objects[i][value_field]();
532
533                 var opt = new Option( l, v );
534
535                 if (depth) {
536                         var d = 10 * depth;
537                         opt.style.paddingLeft = '' + d + 'px';
538                 }
539
540                 sel.options[sel.options.length] = opt;
541
542
543                 if (typeof def == 'object') {
544                         for (var j in def) {
545                                 if (v == def[j]) opt.selected = true;
546                         }
547                 } else {
548                         if (v == def) opt.selected = true;
549                 }
550
551                 if (child_field_name) {
552                         var c = objects[i][child_field_name];
553                         if (typeof c == 'function')
554                                 c = objects[i][child_field_name]();
555
556                         selectBuilder(
557                                 id,
558                                 c,
559                                 def,
560                                 { label_field           : args['label_field'],
561                                   value_field           : args['value_field'],
562                                   depth                 : args['depth'],
563                                   child_field_name      : args['child_field_name'] }
564                         );
565                 }
566
567         }
568 }       
569