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