26f88243d525db6e565d016c08c88d69d1a26c8d
[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         var in_groups = [];
320         for (var i in user_group_objects) {
321                 in_groups.push(user_group_objects[i].grp());
322                 user_groups.push(user_group_objects[i].grp());
323         }
324
325         selectBuilder(
326                 'permgroups',
327                 [group_tree],
328                 in_groups,
329                 { label_field           : 'name',
330                   value_field           : 'id',
331                   clear                 : true,
332                   child_field_name      : 'children' }
333         );
334
335         x['permgroups'].setAttribute( 'onchange', 
336                 'window.user_groups = [];' +
337                 'for (var i = 0; i < this.options.length; i++) {' +
338                 '       if (this.options[i].selected)' +
339                 '               window.user_groups.push(this.options[i].value);' +
340                 '}');
341
342                 display_all_addresses();
343
344         if (cgi.param('adv')) x['permgroups'].parentNode.parentNode.setAttribute('adv', 'false');
345
346         return true;
347 }
348
349 function display_all_addresses () {
350         d = document.getElementById('addresses');
351         while (d.firstChild)
352                 d.removeChild(d.lastChild);
353
354         for (var i in user.addresses())
355                 display_address(document.getElementById('addresses'), user.addresses()[i]);
356 }
357
358 function new_addr () {
359         var x = new aua();
360         x.isnew(1);
361         x.id(fakeid());
362
363         user.addresses().push(x);
364         display_address(document.getElementById('addresses'), x);
365 }
366
367 function display_address (div, adr) {
368
369         var dis = false;
370         if (adr.isdeleted()) dis = true;
371
372         var t = document.getElementById('addr-tmpl').getElementsByTagName('table')[0].cloneNode(true);
373         div.appendChild(t);
374
375         var x;
376
377         x = findNodeByName(t, 'adr.address_type');
378         x.disabled = dis;
379         if (adr.address_type()) x.value = adr.address_type();
380         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').address_type(this.value)');
381
382         x = findNodeByName(t, 'adr.street1');
383         x.disabled = dis;
384         if (adr.street1()) x.value = adr.street1();
385         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').street1(this.value)');
386
387         x = findNodeByName(t, 'adr.street2');
388         x.disabled = dis;
389         if (adr.street2()) x.value = adr.street2();
390         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').street2(this.value)');
391
392         x = findNodeByName(t, 'adr.city');
393         x.disabled = dis;
394         if (adr.city()) x.value = adr.city();
395         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').city(this.value)');
396
397         x = findNodeByName(t, 'adr.state');
398         x.disabled = dis;
399         if (adr.state()) x.value = adr.state();
400         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').state(this.value)');
401
402         x = findNodeByName(t, 'adr.post_code');
403         x.disabled = dis;
404         if (adr.post_code()) x.value = adr.post_code();
405         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').post_code(this.value)');
406
407         x = findNodeByName(t, 'adr.county');
408         x.disabled = dis;
409         if (adr.county()) x.value = adr.county();
410         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').county(this.value)');
411
412         x = findNodeByName(t, 'adr.country');
413         x.disabled = dis;
414         if (adr.country()) x.value = adr.country();
415         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').country(this.value)');
416
417         x = findNodeByName(t, 'adr.valid');
418         x.disabled = dis;
419         if (adr.valid()) x.checked = true;
420         x.setAttribute( 'onchange', 'findAddressById(' + adr.id() + ').valid(this.checked ? "t" : "f")');
421
422         x = findNodeByName(t, 'is_mailing');
423         x.disabled = dis;
424         x.value = adr.id();
425         x.setAttribute( 'onclick', 'user.mailing_address(findAddressById(' + adr.id() + '))');
426         if (adr.id() == user.mailing_address().id()) {
427                 x.checked = true;
428         }
429
430         x = findNodeByName(t, 'is_billing');
431         x.disabled = dis;
432         x.value = adr.id();
433         x.setAttribute( 'onclick', 'user.billing_address(findAddressById(' + adr.id() + '))');
434         if (adr.id() == user.billing_address().id()) {
435                 x.checked = true;
436         }
437
438         x = findNodeByName(t, 'remove');
439         if (dis) {
440                 x.setAttribute('onclick', 'unremove_adr(' + adr.id() + '); return false;');
441                 x.innerHTML = "Undelete this address";
442         } else {
443                 x.setAttribute('onclick', 'remove_adr(' + adr.id() + '); return false;');
444         }
445
446
447         return true;
448 }
449
450 function unremove_adr (id) {
451         findAddressById(id).isdeleted(0);
452         display_all_addresses();
453 }       
454
455 function remove_adr (id) {
456         findAddressById(id).isdeleted(1);
457         display_all_addresses();
458 }       
459
460 function findAddressById (id) {
461         for (var i in user.addresses())
462                 if (user.addresses()[i].id() == id) return user.addresses()[i];
463 }
464
465
466 function selectBuilder (id, objects, def, args) {
467         var label_field = args['label_field'];
468         var value_field = args['value_field'];
469         var depth = args['depth'];
470
471         if (!depth) depth = 0;
472
473         args['depth'] = parseInt(depth) + 1;
474
475         var child_field_name = args['child_field_name'];
476
477         var sel = document.getElementById(id);
478
479         if (args['clear']) {
480                 for (var o in sel.options) {
481                         sel.options[o] = null;
482                 }
483                 args['clear'] = false;
484                 if (args['empty_label']) {
485                         sel.options[0] = new Option( args['empty_label'], args['empty_value'] );
486                         sel.selectedIndex = 0;
487                 }
488         }
489
490         for (var i in objects) {
491                 var l = objects[i][label_field];
492                 var v = objects[i][value_field];
493
494                 if (typeof l == 'function')
495                         l = objects[i][label_field]();
496
497                 if (typeof v == 'function')
498                         v = objects[i][value_field]();
499
500                 var opt = new Option( l, v );
501
502                 if (depth) {
503                         var d = 10 * depth;
504                         opt.style.paddingLeft = '' + d + 'px';
505                 }
506
507                 sel.options[sel.options.length] = opt;
508
509
510                 if (typeof def == 'object') {
511                         for (var j in def) {
512                                 if (v == def[j]) opt.selected = true;
513                         }
514                 } else {
515                         if (v == def) opt.selected = true;
516                 }
517
518                 if (child_field_name) {
519                         var c = objects[i][child_field_name];
520                         if (typeof c == 'function')
521                                 c = objects[i][child_field_name]();
522
523                         selectBuilder(
524                                 id,
525                                 c,
526                                 def,
527                                 { label_field           : args['label_field'],
528                                   value_field           : args['value_field'],
529                                   depth                 : args['depth'],
530                                   child_field_name      : args['child_field_name'] }
531                         );
532                 }
533
534         }
535 }       
536