]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/search_form.js
LP#1576435: Force "Include inactive patrons?" on patron reg duplicate search
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / search_form.js
1 dump('entering patron/search_form.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.search_form = function (params) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7     JSAN.use('util.network'); this.network = new util.network();
8     this.w = window;
9 }
10
11 patron.search_form.prototype = {
12
13     'init' : function( params ) {
14
15         var obj = this;
16         obj.event_listeners = new EventListenerList();
17
18         // The bulk of params.query is getting parsed/rendered by obj.controller.init below, and will be reconstituted from possibly modified XUL elements upon Submit.
19         // But we're going to let search_limit and search_sort be configurable now by those spawning this interface, and let's assume there are no corresponding widgets for now.  
20         // I'm going to place them into the "obj" scope for this instance.
21         obj.search_limit = params.query.search_limit;
22         obj.search_sort = (typeof params.query.search_sort === 'undefined') ?
23                             null :
24                             JSON2js( params.query.search_sort ); // Let's assume this is encoded as JSON
25
26         obj.include_inactive = (typeof params.query.include_inactive !== 'undefined') ? params.query.include_inactive : null;
27
28         JSAN.use('OpenILS.data'); this.OpenILS = {}; 
29         obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'});
30
31         JSAN.use('util.controller'); obj.controller = new util.controller();
32         obj.controller.init(
33             {
34                 control_map : {
35                     'cmd_broken' : [
36                         ['command'],
37                         function() { alert($("commonStrings").getString('common.unimplemented')); }
38                     ],
39                     'cmd_patron_search_submit' : [
40                         ['command'],
41                         function() {
42                             obj.submit();
43                         }
44                     ],
45                     'cmd_patron_search_clear' : [
46                         ['command'],
47                         function() { 
48                             obj.controller.render(); 
49                             window.xulG.clear_left_deck();
50                         }
51                     ],
52                     'family_name' : [
53                         ['render'],
54                         function(e) {
55                             return function() {
56                                 if (params.query&&params.query.family_name) {
57                                     e.setAttribute('value',params.query.family_name);
58                                     e.value = params.query.family_name;
59                                 } else {
60                                     e.value = '';
61                                 }
62                             };
63                         }
64                     ],
65                     'first_given_name' : [
66                         ['render'],
67                         function(e) {
68                             return function() {
69                                 if (params.query&&params.query.first_given_name) {
70                                     e.setAttribute('value',params.query.first_given_name);
71                                     e.value = params.query.first_given_name;
72                                 } else {
73                                     e.value = '';
74                                 }
75                             };
76                         }
77                     ],
78                     'second_given_name' : [
79                         ['render'],
80                         function(e) {
81                             return function() {
82                                 if (params.query&&params.query.second_given_name) {
83                                     e.setAttribute('value',params.query.second_given_name);
84                                     e.value = params.query.second_given_name;
85                                 } else {
86                                     e.value = '';
87                                 }
88                             };
89                         }
90                     ],
91                     'alias' : [
92                         ['render'],
93                         function(e) {
94                             return function() {
95                                 if (params.query&&params.query.alias) {
96                                     e.setAttribute('value',params.query.alias);
97                                     e.value = params.query.alias;
98                                 } else {
99                                     e.value = '';
100                                 }
101                             };
102                         }
103                     ],
104                     'usrname' : [
105                         ['render'],
106                         function(e) {
107                             return function() {
108                                 if (params.query&&params.query.usrname) {
109                                     e.setAttribute('value',params.query.usrname);
110                                     e.value = params.query.usrname;
111                                 } else {
112                                     e.value = '';
113                                 }
114                             };
115                         }
116                     ],
117                     'card' : [
118                         ['render'],
119                         function(e) {
120                             return function() {
121                                 if (params.query&&params.query.card) {
122                                     e.setAttribute('value',params.query.card);
123                                     e.value = params.query.card;
124                                 } else {
125                                     e.value = '';
126                                 }
127                             };
128                         }
129                     ],
130                     'email' : [
131                         ['render'],
132                         function(e) {
133                             return function() {
134                                 if (params.query&&params.query.email) {
135                                     e.setAttribute('value',params.query.email);
136                                     e.value = params.query.email;
137                                 } else {
138                                     e.value = '';
139                                 }
140                             };
141                         }
142                     ],
143                     'phone' : [
144                         ['render'],
145                         function(e) {
146                             return function() {
147                                 if (params.query&&params.query.phone) {
148                                     e.setAttribute('value',params.query.phone);
149                                     e.value = params.query.phone;
150                                 } else {
151                                     e.value = '';
152                                 }
153                             };
154                         }
155                     ],
156                     'ident' : [
157                         ['render'],
158                         function(e) {
159                             return function() {
160                                 if (params.query&&params.query.ident) {
161                                     e.setAttribute('value',params.query.ident);
162                                     e.value = params.query.ident;
163                                 } else if (params.query&&params.query.ident_value) {
164                                     e.setAttribute('value',params.query.ident_value);
165                                     e.value = params.query.ident_value;
166                                 } else if (params.query&&params.query.ident_value2) {
167                                     e.setAttribute('value',params.query.ident_value2);
168                                     e.value = params.query.ident_value2;
169                                 } else {
170                                     e.value = '';
171                                 }
172                             };
173                         }
174                     ],
175                     'street1' : [
176                         ['render'],
177                         function(e) {
178                             return function() {
179                                 if (params.query&&params.query.street1) {
180                                     e.setAttribute('value',params.query.street1);
181                                     e.value = params.query.street1;
182                                 } else {
183                                     e.value = '';
184                                 }
185                             };
186                         }
187                     ],
188                     'street2' : [
189                         ['render'],
190                         function(e) {
191                             return function() {
192                                 if (params.query&&params.query.street2) {
193                                     e.setAttribute('value',params.query.street2);
194                                     e.value = params.query.street2;
195                                 } else {
196                                     e.value = '';
197                                 }
198                             };
199                         }
200                     ],
201                     'city' : [
202                         ['render'],
203                         function(e) {
204                             return function() {
205                                 if (params.query&&params.query.city) {
206                                     e.setAttribute('value',params.query.city);
207                                     e.value = params.query.city;
208                                 } else {
209                                     e.value = '';
210                                 }
211                             };
212                         }
213                     ],
214                     'state' : [
215                         ['render'],
216                         function(e) {
217                             return function() {
218                                 if (params.query&&params.query.state) {
219                                     e.setAttribute('value',params.query.state);
220                                     e.value = params.query.state;
221                                 } else {
222                                     e.value = '';
223                                 }
224                             };
225                         }
226                     ],
227                     'post_code' : [
228                         ['render'],
229                         function(e) {
230                             return function() {
231                                 if (params.query&&params.query.post_code) {
232                                     e.setAttribute('value',params.query.post_code);
233                                     e.value = params.query.post_code;
234                                 } else {
235                                     e.value = '';
236                                 }
237                             };
238                         }
239                     ],
240                     'profile' : [ ['render'], function(e) {
241                             return function() {};
242                         } 
243                     ],
244                     'inactive' : [ ['render'], function(e) { 
245                             return function() {}; 
246                         } 
247                     ],
248                     'search_depth' : [ ['render'],function(e) {
249                             return function() {};
250                         }
251                     ]
252                 }
253             }
254         );
255
256         obj.controller.render();
257         var nl = document.getElementsByTagName('textbox');
258         for (var i = 0; i < nl.length; i++) {
259             obj.event_listeners.add(nl[i], 'keypress', function(ev) {
260                 if (ev.target.tagName != 'textbox') return;
261                 if (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* enter on a mac */) setTimeout( function() { obj.submit(); }, 0);
262             },false);
263         }
264         document.getElementById('family_name').focus();
265
266         JSAN.use('util.file'); JSAN.use('util.widgets'); JSAN.use('util.functional');
267         util.widgets.remove_children(obj.controller.view.search_depth);
268         var ml = util.widgets.make_menulist(
269             util.functional.map_list( obj.OpenILS.data.list.my_aou,
270                 function(el,idx) {
271                     return [ el.name(), el.id() ]
272                 }
273             ).sort(
274                 function(a,b) {
275                     if (a[1] < b[1]) return -1;
276                     if (a[1] > b[1]) return 1;
277                     return 0;
278                 }
279             )
280         );
281         obj.event_listeners.add(ml, 'command', function() {
282                 ml.parentNode.setAttribute('value',ml.value);
283                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
284                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
285             }, false
286         );
287         ml.setAttribute('id','search_depth_ml');
288         obj.controller.view.search_depth.appendChild(ml);
289
290         var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
291         util.widgets.load_attributes(file);
292         ml.value = ml.getAttribute('value');
293         if (! ml.value) {
294             ml.value = 0;
295             ml.setAttribute('value',ml.value);
296         }
297
298         var cb = obj.controller.view.inactive;
299         obj.event_listeners.add(cb, 'command',function() {
300                 obj.include_inactive = null;
301                 cb.setAttribute('value',cb.checked ? "true" : "false");
302                 var file = new util.file('patron_search_prefs.'+obj.OpenILS.data.server_unadorned);
303                 util.widgets.save_attributes(file, { 'search_depth_ml' : [ 'value' ], 'inactive' : [ 'value' ] });
304             }, false
305         );
306         if (obj.include_inactive !== null)
307             cb.checked = obj.include_inactive == "true" ? true : false;
308         else
309             cb.checked = cb.getAttribute('value') == "true" ? true : false;
310
311         /* Populate the Patron Profile filter, if it exists */
312         if (obj.controller.view.profile) {
313             util.widgets.remove_children(obj.controller.view.profile);
314             var profile_ml = util.widgets.make_menulist(
315                 util.functional.map_list( obj.OpenILS.data.list.pgt,
316                     function(el,idx) {
317                         return [ el.name(), el.id() ]
318                     }
319                 ).sort(
320                     function(a,b) {
321                         if (a[0] < b[0]) return -1;
322                         if (a[0] > b[0]) return 1;
323                         return 0;
324                     }
325                 )
326             );
327             obj.event_listeners.add(profile_ml, 'command', function() {
328                     profile_ml.parentNode.setAttribute('value', profile_ml.value);
329                 }, false
330             );
331             profile_ml.setAttribute('id','profile_ml');
332
333             /* Add an empty menu item as the default profile */
334             var empty = document.createElement('menuitem'); 
335             profile_ml.firstChild.insertBefore(empty, profile_ml.firstChild.firstChild);
336             empty.setAttribute('label', '');
337             empty.setAttribute('value', ''); 
338             obj.controller.view.profile.appendChild(profile_ml);
339             profile_ml.value = profile_ml.getAttribute('value');
340         }
341     },
342
343     'cleanup' : function() {
344         var obj = this;
345         obj.controller.cleanup();
346         obj.event_listeners.removeAll();    
347     }, 
348
349     'on_submit' : function(q) {
350         var msg = 'Query = ' + q;
351         this.error.sdump('D_PATRON', msg);
352     },
353
354     'submit' : function() {
355         window.xulG.clear_left_deck();
356         var obj = this;
357         var query = {};
358         for (var i = 0; i < obj.controller.render_list.length; i++) {
359             var id = obj.controller.render_list[i][0];
360             var node = document.getElementById(id);
361             if (node && node.value != '') {
362                 if (id == 'inactive') {
363                     query[id] = node.getAttribute('value');
364                     // always include inactive when include_inactive is true
365                     if (obj.include_inactive !== null)
366                         query[id] = obj.include_inactive;
367                     obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + node.getAttribute('value') + '\n');
368                 } else if (id == 'profile') {
369                     query[id] = node.firstChild.getAttribute('value');
370                     obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + node.getAttribute('value') + '\n');
371                 } else {
372                     if (id == 'search_depth') {
373                         query[id] = node.firstChild.getAttribute('value'); 
374                     } else {
375                          var value = node.value.replace(/^\s+/,'').replace(/[\\\s]+$/,'');
376                         //value = value.replace(/\d/g,'');
377                         switch(id) {
378                             case 'family_name' :
379                             case 'first_given_name' :
380                             case 'second_given_name' :
381                                 value = value.replace(/^[\d\s]+/g,'').replace(/[\d\s]+$/g,'')
382                             break;
383                         }
384                         if (value != '') {
385                             query[id] = value;
386                             obj.error.sdump('D_DEBUG','id = ' + id + '  value = ' + value + '\n');
387                         }
388                     }
389                 } 
390             }
391         }
392         if (typeof obj.on_submit == 'function') {
393             obj.on_submit(query,obj.search_limit,obj.search_sort);
394         }
395         if (typeof window.xulG == 'object' 
396             && typeof window.xulG.on_submit == 'function') {
397             obj.error.sdump('D_PATRON','patron.search_form: Calling external .on_submit()\n');
398             window.xulG.on_submit(query,obj.search_limit,obj.search_sort);
399         } else {
400             obj.error.sdump('D_PATRON','patron.search_form: No external .on_query()\n');
401         }
402     },
403
404 }
405
406 dump('exiting patron/search_form.js\n');