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