Show human label for Internet Access Level in Patron Search
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / search_result.js
1 dump('entering patron/search_result.js\n');
2
3 function $(id) { return document.getElementById(id); }
4
5 if (typeof patron == 'undefined') patron = {};
6 patron.search_result = function (params) {
7
8     JSAN.use('util.error'); this.error = new util.error();
9     JSAN.use('util.network'); this.network = new util.network();
10     this.w = window;
11 }
12
13 patron.search_result.prototype = {
14
15     'result_cap' : 50,
16
17     'init' : function( params ) {
18
19         var obj = this;
20
21         obj.query = params['query'];
22         obj.search_limit = params['search_limit'];
23         obj.search_sort = params['search_sort'];
24
25         JSAN.use('OpenILS.data'); this.OpenILS = {}; 
26         obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
27         var obscure_dob = String( obj.OpenILS.data.hash.aous['circ.obscure_dob'] ) == 'true';
28
29         JSAN.use('util.list'); obj.list = new util.list('patron_list');
30
31         JSAN.use('patron.util');
32         var columns = obj.list.fm_columns('au',{
33             '*' : { 'remove_virtual' : true, 'expanded_label' : false, 'hidden' : true },
34             'au_barcode' : { 'hidden' : false },
35             'au_barred' : { 'hidden' : false },
36             'au_family_name' : { 'hidden' : false },
37             'au_first_given_name' : { 'hidden' : false },
38             'au_second_given_name' : { 'hidden' : false },
39             'au_dob' : { 'hidden' : false },
40             'au_profile' : { 'fleshed_display_field' : 'name' },
41             'au_ident_type' : { 'fleshed_display_field' : 'name' },
42             'au_ident_type2' : { 'fleshed_display_field' : 'name' },
43             'au_net_access_level' : { 'fleshed_display_field' : 'name' }
44         }).concat(
45             obj.list.fm_columns('ac',{
46                 '*' : { 'remove_virtual' : true, 'expanded_label' : true, 'hidden' : true },
47                 'ac_barcode' : { 'hidden' : false }
48             })
49         ).concat(
50             obj.list.fm_columns('aua',{
51                 '*' : {
52                     'dataobj' : 'billing_aua',
53                     'remove_virtual' : true,
54                     'label_prefix' : $('patronStrings').getString('staff.patron.search_result.billing_address_column_label_prefix'),
55                     'hidden' : true
56                 }
57             },'billing_')
58         ).concat(
59             obj.list.fm_columns('aua',{
60                 '*' : {
61                     'dataobj' : 'mailing_aua',
62                     'remove_virtual' : true,
63                     'label_prefix' : $('patronStrings').getString('staff.patron.search_result.mailing_address_column_label_prefix'),
64                     'hidden' : true
65                 }
66             },'mailing_')
67         );
68
69         obj.list.init(
70             {
71                 'columns' : columns,
72                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
73                 'retrieve_row' : function(params) {
74                     var id = params.retrieve_id;
75                     var au_obj = patron.util.retrieve_fleshed_au_via_id(
76                         ses(),
77                         id,
78                         ["card","billing_address","mailing_address"],
79                         function(req) {
80                             try {
81                                 var row = params.row;
82                                 if (typeof row.my == 'undefined') row.my = {};
83                                 row.my.au = req.getResultObject();
84                                 row.my.ac = row.my.au.card();
85                                 row.my.billing_aua = row.my.au.billing_address();
86                                 row.my.mailing_aua = row.my.au.mailing_address();
87                                 if (typeof params.on_retrieve == 'function') {
88                                     params.on_retrieve(row);
89                                 } else {
90                                     alert($("patronStrings").getFormattedString('staff.patron.search_result.init.typeof_params', [typeof params.on_retrieve]));
91                                 }
92                             } catch(E) {
93                                 alert('error: ' + E);
94                             }
95                         }
96                     );
97                 },
98                 'on_select' : function(ev) {
99                     JSAN.use('util.functional');
100                     var sel = obj.list.retrieve_selection();
101                     var list = util.functional.map_list(
102                         sel,
103                         function(o) { return o.getAttribute('retrieve_id'); }
104                     );
105                     obj.controller.view.cmd_sel_clip.setAttribute('disabled', list.length < 1 );
106                     if (typeof obj.on_select == 'function') {
107                         obj.on_select(list);
108                     }
109                     if (typeof window.xulG == 'object' && typeof window.xulG.on_select == 'function') {
110                         obj.error.sdump('D_PATRON','patron.search_result: Calling external .on_select()\n');
111                         window.xulG.on_select(list);
112                     } else {
113                         obj.error.sdump('D_PATRON','patron.search_result: No external .on_select()\n');
114                     }
115                 }
116             }
117         );
118         JSAN.use('util.controller'); obj.controller = new util.controller();
119         obj.controller.init(
120             {
121                 control_map : {
122                     'cmd_broken' : [
123                         ['command'],
124                         function() { alert($("commonStrings").getString('common.unimplemented')); }
125                     ],
126                     'cmd_search_print' : [
127                         ['command'],
128                         function() {
129                             try {
130                                 obj.list.dump_csv_to_printer();
131                             } catch(E) {
132                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_print'),E);
133                             }
134                         }
135                     ],
136                     'cmd_sel_clip' : [
137                         ['command'],
138                         function() {
139                             try {
140                                 obj.list.clipboard();
141                             } catch(E) {
142                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_clipboard'),E);
143                             }
144                         }
145                     ],
146                     'cmd_save_cols' : [
147                         ['command'],
148                         function() {
149                             try {
150                                 obj.list.save_columns();
151                             } catch(E) {
152                                 obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.search_result.init.search_saving_columns'),E);
153                             }
154                         }
155                     ],
156                 }
157             }
158         );
159
160         if (obj.query) obj.search(obj.query);
161     },
162
163     'search' : function(query) {
164         var obj = this;
165         var search_hash = {};
166         obj.search_term_count = 0;
167         var inactive = false;
168         var search_depth = 0;
169         for (var i in query) {
170             switch( i ) {
171                 case 'card':
172                     search_hash[ i ] = {};
173                     search_hash[ i ].value = query[i];
174                     search_hash[i].group = 3; 
175                     obj.search_term_count++;
176                 break;
177
178                 case 'phone': case 'ident': 
179                 
180                     search_hash[ i ] = {};
181                     search_hash[ i ].value = query[i];
182                     search_hash[i].group = 2; 
183                     obj.search_term_count++;
184                 break;
185
186                 case 'street1': case 'street2': case 'city': case 'state': case 'post_code': 
187                 
188                     search_hash[ i ] = {};
189                     search_hash[ i ].value = query[i];
190                     search_hash[i].group = 1; 
191                     obj.search_term_count++;
192                 break;
193
194                 case 'family_name': case 'first_given_name': case 'second_given_name': case 'email': case 'alias': case 'usrname': case 'profile':
195
196                     search_hash[ i ] = {};
197                     search_hash[ i ].value = query[i];
198                     search_hash[i].group = 0; 
199                     obj.search_term_count++;
200                 break;
201
202                 case 'inactive':
203                     if (query[i] == 'checked' || query[i] == 'true') inactive = true;
204                 break;
205
206                 case 'search_depth':
207                     search_depth = function(a){return a;}(query[i]);
208                 break;
209             }
210         }
211         try {
212             var results = [];
213
214             var params = [ 
215                 ses(), 
216                 search_hash, 
217                 typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap + 1, 
218                 typeof obj.search_sort != 'undefined' ? obj.search_sort : [ 'family_name ASC', 'first_given_name ASC', 'second_given_name ASC', 'dob DESC' ] 
219             ];
220             if (inactive) {
221                 params.push(1);
222                 if (document.getElementById('active')) {
223                     document.getElementById('active').setAttribute('hidden','false');
224                     document.getElementById('active').hidden = false;
225                 }
226             } else {
227                 params.push(0);
228             }
229             params.push(search_depth);
230             if (obj.search_term_count > 0) {
231                 //alert('search params = ' + obj.error.pretty_print( js2JSON( params ) ) );
232                 results = this.network.simple_request( 'FM_AU_IDS_RETRIEVE_VIA_HASH', params );
233                 if ( results == null ) results = [];
234                 if (typeof results.ilsevent != 'undefined') throw(results);
235                 if (results.length == 0) {
236                     alert($("patronStrings").getString('staff.patron.search_result.search.no_patrons_found'));
237                     return;
238                 }
239                 if (results.length == typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap+1) {
240                     results.pop();
241                     alert($("patronStrings").getFormattedString('staff.patron.search_result.search.capped_results', [typeof obj.search_limit != 'undefined' && typeof obj.search_limit != 'null' ? obj.search_limit : obj.result_cap]));
242                 }
243             } else {
244                 alert($("patronStrings").getString('staff.patron.search_result.search.enter_search_terms'));
245                 return;
246             }
247
248             obj.list.clear();
249             //this.list.append( { 'retrieve_id' : results[i], 'row' : {} } );
250             var funcs = [];
251
252                 function gen_func(r) {
253                     return function() {
254                         obj.list.append( { 'retrieve_id' : r, 'row' : {}, 'to_bottom' : true, 'no_auto_select' : true } );
255                     }
256                 }
257
258             for (var i = 0; i < results.length; i++) {
259                 funcs.push( gen_func(results[i]) );
260             }
261             JSAN.use('util.exec'); var exec = new util.exec(4);
262             exec.chain( funcs );
263
264         } catch(E) {
265             this.error.standard_unexpected_error_alert('patron.search_result.search',E);
266         }
267     }
268
269 }
270
271 dump('exiting patron/search_result.js\n');