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