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