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