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