]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_result.js
treat null result as an empty list
[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
23                 JSAN.use('OpenILS.data'); this.OpenILS = {}; 
24                 obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
25
26                 JSAN.use('util.list'); obj.list = new util.list('patron_list');
27
28                 JSAN.use('patron.util');
29                 var columns = patron.util.columns(
30                         {
31                                 /* 'active' : { 'hidden' : 'false' }, */
32                                 'barred' : { 'hidden' : 'false' },
33                                 'family_name' : { 'hidden' : 'false' },
34                                 'first_given_name' : { 'hidden' : 'false' },
35                                 'second_given_name' : { 'hidden' : 'false' },
36                                 'dob' : { 'hidden' : 'false' }
37                         },
38                         {
39                                 'except_these' : [
40                                         'barcode',
41                                 ]
42                         }
43                 );
44                 obj.list.init(
45                         {
46                                 'columns' : columns,
47                                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
48                                 'retrieve_row' : function(params) {
49                                         var id = params.retrieve_id;
50                                         var au_obj = patron.util.retrieve_au_via_id( ses(), id,
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                                                                 var p = { 
100                                                                         'template' : 'patron'
101                                                                 };
102                                                                 obj.list.print( p );
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 = [ ses(), search_hash, obj.result_cap + 1, [ 'family_name ASC', 'first_given_name ASC', 'second_given_name ASC', 'dob DESC' ] ];
187                         if (inactive) {
188                                 params.push(1);
189                                 if (document.getElementById('active')) {
190                                         document.getElementById('active').setAttribute('hidden','false');
191                                         document.getElementById('active').hidden = false;
192                                 }
193                         } else {
194                 params.push(0);
195             }
196             params.push(search_depth);
197                         if (obj.search_term_count > 0) {
198                                 //alert('search params = ' + obj.error.pretty_print( js2JSON( params ) ) );
199                                 results = this.network.simple_request( 'FM_AU_IDS_RETRIEVE_VIA_HASH', params );
200                 if ( results == null ) results = [];
201                                 if (typeof results.ilsevent != 'undefined') throw(results);
202                                 if (results.length == 0) {
203                                         alert($("patronStrings").getString('staff.patron.search_result.search.no_patrons_found'));
204                                         return;
205                                 }
206                                 if (results.length == obj.result_cap+1) {
207                                         results.pop();
208                                         alert($("patronStrings").getFormattedString('staff.patron.search_result.search.capped_results', [obj.result_cap]));
209                                 }
210                         } else {
211                                 alert($("patronStrings").getString('staff.patron.search_result.search.enter_search_terms'));
212                                 return;
213                         }
214
215             obj.list.clear();
216                         //this.list.append( { 'retrieve_id' : results[i], 'row' : {} } );
217                         var funcs = [];
218
219                                 function gen_func(r) {
220                                         return function() {
221                                                 obj.list.append( { 'retrieve_id' : r, 'row' : {}, 'to_bottom' : true, 'no_auto_select' : true } );
222                                         }
223                                 }
224
225                         for (var i = 0; i < results.length; i++) {
226                                 funcs.push( gen_func(results[i]) );
227                         }
228                         JSAN.use('util.exec'); var exec = new util.exec(4);
229                         exec.chain( funcs );
230
231                 } catch(E) {
232                         this.error.standard_unexpected_error_alert('patron.search_result.search',E);
233                 }
234         }
235
236 }
237
238 dump('exiting patron/search_result.js\n');