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