]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_result.js
have alias passed to search
[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 'phone': case 'ident': 
144                                 
145                                         search_hash[ i ] = {};
146                                         search_hash[ i ].value = query[i];
147                                         search_hash[i].group = 2; 
148                                         obj.search_term_count++;
149                                 break;
150
151                                 case 'street1': case 'street2': case 'city': case 'state': case 'post_code': 
152                                 
153                                         search_hash[ i ] = {};
154                                         search_hash[ i ].value = query[i];
155                                         search_hash[i].group = 1; 
156                                         obj.search_term_count++;
157                                 break;
158
159                                 case 'family_name': case 'first_given_name': case 'second_given_name': case 'email': case 'alias':
160
161                                         search_hash[ i ] = {};
162                                         search_hash[ i ].value = query[i];
163                                         search_hash[i].group = 0; 
164                                         obj.search_term_count++;
165                                 break;
166
167                                 case 'inactive':
168                                         if (query[i] == 'checked' || query[i] == 'true') inactive = true;
169                                 break;
170
171                 case 'search_depth':
172                     search_depth = function(a){return a;}(query[i]);
173                 break;
174                         }
175                 }
176                 try {
177                         var results = [];
178
179                         var params = [ ses(), search_hash, obj.result_cap + 1, [ 'family_name ASC', 'first_given_name ASC', 'second_given_name ASC', 'dob DESC' ] ];
180                         if (inactive) {
181                                 params.push(1);
182                                 if (document.getElementById('active')) {
183                                         document.getElementById('active').setAttribute('hidden','false');
184                                         document.getElementById('active').hidden = false;
185                                 }
186                         } else {
187                 params.push(0);
188             }
189             params.push(search_depth);
190                         if (obj.search_term_count > 0) {
191                                 //alert('search params = ' + obj.error.pretty_print( js2JSON( params ) ) );
192                                 results = this.network.simple_request( 'FM_AU_IDS_RETRIEVE_VIA_HASH', params );
193                                 if ( (results == null) || (typeof results.ilsevent != 'undefined') ) throw(results);
194                                 if (results.length == 0) {
195                                         alert($("patronStrings").getString('staff.patron.search_result.search.no_patrons_found'));
196                                         return;
197                                 }
198                                 if (results.length == obj.result_cap+1) {
199                                         results.pop();
200                                         alert($("patronStrings").getFormattedString('staff.patron.search_result.search.capped_results', [obj.result_cap]));
201                                 }
202                         } else {
203                                 alert($("patronStrings").getString('staff.patron.search_result.search.enter_search_terms'));
204                                 return;
205                         }
206
207                         //this.list.append( { 'retrieve_id' : results[i], 'row' : {} } );
208                         var funcs = [];
209
210                                 function gen_func(r) {
211                                         return function() {
212                                                 obj.list.append( { 'retrieve_id' : r, 'row' : {}, 'to_bottom' : true, 'no_auto_select' : true } );
213                                         }
214                                 }
215
216                         for (var i = 0; i < results.length; i++) {
217                                 funcs.push( gen_func(results[i]) );
218                         }
219                         JSAN.use('util.exec'); var exec = new util.exec(4);
220                         exec.chain( funcs );
221
222                 } catch(E) {
223                         this.error.standard_unexpected_error_alert('patron.search_result.search',E);
224                 }
225         }
226
227 }
228
229 dump('exiting patron/search_result.js\n');