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