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