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