]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
Add cointainer() and record_list() filters to QueryParser
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / standing_penalties.js
1 var list; var archived_list; var data; var error; var net; var rows; var archived_rows;
2
3 function default_focus() { document.getElementById('apply_btn').focus(); } // parent interfaces often call this
4
5 function penalty_init() {
6     try {
7         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
8
9         commonStrings = document.getElementById('commonStrings');
10         patronStrings = document.getElementById('patronStrings');
11
12         if (typeof JSAN == 'undefined') {
13             throw(
14                 commonStrings.getString('common.jsan.missing')
15             );
16         }
17
18         JSAN.errorLevel = "die"; // none, warn, or die
19         JSAN.addRepository('..');
20
21         JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
22         XML_HTTP_SERVER = data.server_unadorned;
23
24         JSAN.use('util.error'); error = new util.error();
25         JSAN.use('util.network'); net = new util.network();
26         JSAN.use('patron.util'); 
27         JSAN.use('util.list'); 
28         JSAN.use('util.functional'); 
29         JSAN.use('util.widgets');
30
31         init_list();
32         init_archived_list();
33         document.getElementById('date1').year = document.getElementById('date1').year - 1;
34         document.getElementById('cmd_apply_penalty').addEventListener('command', handle_apply_penalty, false);
35         document.getElementById('cmd_remove_penalty').addEventListener('command', handle_remove_penalty, false);
36         document.getElementById('cmd_edit_penalty').addEventListener('command', handle_edit_penalty, false);
37         document.getElementById('cmd_archive_penalty').addEventListener('command', handle_archive_penalty, false);
38         document.getElementById('cmd_retrieve_archived_penalties').addEventListener('command', handle_retrieve_archived_penalties, false);
39         populate_list();
40         default_focus();
41
42     } catch(E) {
43         var err_prefix = 'standing_penalties.js -> penalty_init() : ';
44         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
45     }
46 }
47
48 function init_list() {
49     try {
50
51         list = new util.list( 'ausp_list' );
52         list.init( 
53             {
54                 'columns' : patron.util.ausp_columns({}),
55                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
56                 'retrieve_row' : retrieve_row,
57                 'on_select' : generate_handle_selection(list)
58             } 
59         );
60
61     } catch(E) {
62         var err_prefix = 'standing_penalties.js -> init_list() : ';
63         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
64     }
65 }
66
67 function init_archived_list() {
68     try {
69
70         archived_list = new util.list( 'archived_ausp_list' );
71         archived_list.init( 
72             {
73                 'columns' : patron.util.ausp_columns({}),
74                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
75                 'retrieve_row' : retrieve_row, // We're getting fleshed objects for now, but if we move to just ausp.id's, then we'll need to put a per-id fetcher in here
76                 'on_select' : generate_handle_selection(archived_list)
77             } 
78         );
79
80     } catch(E) {
81         var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
82         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
83     }
84 }
85
86
87 function retrieve_row (params) { // callback function for fleshing rows in a list
88     params.row_node.setAttribute('retrieve_id',params.row.my.ausp.id()); 
89     params.on_retrieve(params.row); 
90     return params.row; 
91 }
92
93 function generate_handle_selection(which_list) {
94     return function (ev) { // handler for list row selection event
95         var sel = which_list.retrieve_selection();
96         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
97         if (which_list == list) { // top list
98             if (ids.length > 0) {
99                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
100                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
101                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
102             } else {
103                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
104                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
105                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
106             }
107         }
108     };
109 }
110
111 function populate_list() {
112     try {
113
114         rows = {};
115         list.clear();
116         for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
117             var row_params = {
118                 'row' : {
119                     'my' : {
120                         'ausp' : xulG.patron.standing_penalties()[i],
121                         'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
122                         'au' : xulG.patron,
123                     }
124                 }
125             };
126             rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
127         };
128
129     } catch(E) {
130         var err_prefix = 'standing_penalties.js -> populate_list() : ';
131         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
132     }
133 }
134
135 function handle_apply_penalty(ev) {
136     try {
137         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
138         JSAN.use('util.window');
139         var win = new util.window();
140         var my_xulG = win.open(
141             urls.XUL_NEW_STANDING_PENALTY,
142             'new_standing_penalty',
143             'chrome,resizable,modal',
144             {}
145         );
146
147         if (!my_xulG.id) { return 0; }
148
149         var penalty = new ausp();
150         penalty.usr( xulG.patron.id() );
151         penalty.isnew( 1 );
152         penalty.standing_penalty( my_xulG.id );
153         penalty.org_unit( ses('ws_ou') );
154         penalty.note( my_xulG.note );
155         net.simple_request(
156             'FM_AUSP_APPLY', 
157             [ ses(), penalty ],
158             generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
159         );
160
161         document.getElementById('progress').hidden = false;
162
163     } catch(E) {
164         alert('error: ' + E);
165         var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
166         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
167     }
168 }
169
170 function generate_request_handler_for_penalty_apply(penalty,id) {
171     return function(reqobj) {
172         try {
173
174             var req = reqobj.getResultObject();
175             if (typeof req.ilsevent != 'undefined') {
176                 error.standard_unexpected_error_alert(
177                     patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
178                     req
179                 );
180             } else {
181                 penalty.id(req);
182                 JSAN.use('patron.util'); JSAN.use('util.functional');
183                 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
184                 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
185                 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
186
187                 var row_params = {
188                     'row' : {
189                         'my' : {
190                             'ausp' : penalty,
191                             'csp' : data.hash.csp[ penalty.standing_penalty() ],
192                             'au' : xulG.patron,
193                         }
194                     }
195                 };
196                 rows[ penalty.id() ] = list.append( row_params );
197             }
198             /*
199             if (xulG && typeof xulG.refresh == 'function') {
200                 xulG.refresh();
201             }
202             */
203             document.getElementById('progress').hidden = true;
204
205         } catch(E) {
206             var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
207             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
208         }
209     };
210 }
211  
212 function handle_remove_penalty(ev) {
213     try {
214
215         var sel = list.retrieve_selection();
216         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
217         if (! ids.length > 0 ) return;
218
219         var funcs = [];
220         for (var i = 0; i < ids.length; i++) {
221             funcs.push( generate_penalty_remove_function(ids[i]) );
222         } 
223         funcs.push(
224             function() {
225                 /*
226                 if (xulG && typeof xulG.refresh == 'function') {
227                     xulG.refresh();
228                 }
229                 */
230                 document.getElementById('progress').hidden = true;
231             }
232         );
233         document.getElementById('progress').hidden = false;
234         JSAN.use('util.exec'); var exec = new util.exec();
235         exec.chain(funcs);
236
237     } catch(E) {
238         var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
239         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
240     }
241 }
242
243 function generate_penalty_remove_function(id) {
244     return function() {
245         try {
246
247             var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
248             penalty.isdeleted(1);
249
250             var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
251             if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
252                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
253             } else {
254                 var node = rows[ id ].my_node;
255                 var parentNode = node.parentNode;
256                 parentNode.removeChild( node );
257                 delete(rows[ id ]);
258             }
259
260         } catch(E) {
261             var err_prefix = 'standing_penalties.js -> penalty_remove_function() : ';
262             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
263         }
264     }; 
265 }
266
267 function handle_edit_penalty(ev) {
268     try {
269
270         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
271         JSAN.use('util.window');
272         var win = new util.window();
273
274         var sel = list.retrieve_selection();
275         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
276         if (ids.length > 0) {
277             for (var i = 0; i < ids.length; i++) {
278                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
279                 var my_xulG = win.open(
280                     urls.XUL_EDIT_STANDING_PENALTY,
281                     'new_standing_penalty',
282                     'chrome,resizable,modal',
283                     { 
284                         'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(), 
285                         'note' : penalty.note() 
286                     }
287                 );
288                 if (my_xulG.modify) {
289                     document.getElementById('progress').hidden = false;
290                     penalty.note( my_xulG.note ); /* this is for rendering, and propogates by reference to the object associated with the row in the GUI */
291                     penalty.standing_penalty( my_xulG.id );
292                     penalty.ischanged( 1 );
293                     dojo.require('openils.PermaCrud');
294                     var pcrud = new openils.PermaCrud( { authtoken :ses() });
295                     pcrud.update( penalty, {
296                         timeout : 10, // makes it synchronous
297                         onerror : function(r) {
298                             try {
299                                 document.getElementById('progress').hidden = true;
300                                 var res = openils.Util.readResponse(r,true);
301                                 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
302                             } catch(E) {
303                                 alert(E);
304                             }
305                         },
306                         oncomplete : function gen_func(p,row_id) {
307                             return function(r) {
308                                 try {
309                                     var res = openils.Util.readResponse(r,true);
310                                     /* FIXME - test for success */
311                                     var row_params = rows[row_id];
312                                     row_params.row.my.ausp = p;
313                                     row_params.row.my.csp = p.standing_penalty();
314                                     list.refresh_row( row_params );
315                                     document.getElementById('progress').hidden = true;
316                                 } catch(E) {
317                                     alert(E);
318                                 }
319                             }
320                         }(penalty,ids[i])
321                     });
322                 }
323             } 
324             /*
325             if (xulG && typeof xulG.refresh == 'function') {
326                 xulG.refresh();
327             }
328             */
329         }
330
331     } catch(E) {
332         var err_prefix = 'standing_penalties.js -> handle_edit_penalty() : ';
333         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
334     }
335 }
336
337 function handle_archive_penalty(ev) {
338     try {
339         var outstanding_requests = 0;
340         var sel = list.retrieve_selection();
341         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
342         if (ids.length > 0) {
343             document.getElementById('progress').hidden = false;
344             for (var i = 0; i < ids.length; i++) {
345                 outstanding_requests++;
346                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
347                 penalty.ischanged( 1 );
348                 penalty.stop_date( util.date.formatted_date(new Date(),'%F') );
349                 dojo.require('openils.PermaCrud');
350                 var pcrud = new openils.PermaCrud( { authtoken :ses() });
351                 pcrud.update( penalty, {
352                     onerror : function(r) {
353                         try {
354                             var res = openils.Util.readResponse(r,true);
355                             error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
356                         } catch(E) {
357                             alert(E);
358                         }
359                         if (--outstanding_requests==0) {
360                             document.getElementById('progress').hidden = true;
361                         }
362                     },
363                     oncomplete : function gen_func(row_id) {
364                         return function(r) {
365                             try {
366                                 var res = openils.Util.readResponse(r,true);
367                                 /* FIXME - test for success */
368                                 var node = rows[row_id].my_node;
369                                 var parentNode = node.parentNode;
370                                 parentNode.removeChild( node );
371                                 delete(rows[row_id]);
372                             } catch(E) {
373                                 alert(E);
374                             }
375                             if (--outstanding_requests==0) {
376                                 document.getElementById('progress').hidden = true;
377                             }
378                         }
379                     }(ids[i])
380                 });
381             } 
382             /*
383             if (xulG && typeof xulG.refresh == 'function') {
384                 xulG.refresh();
385             }
386             */
387         }
388
389     } catch(E) {
390         var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
391         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
392     }
393 }
394
395 function handle_retrieve_archived_penalties() {
396     try {
397         document.getElementById('archived_progress').hidden = false;
398         archived_list.clear(); archived_rows = {};
399         JSAN.use('util.date');
400         dojo.require('openils.PermaCrud');
401         var pcrud = new openils.PermaCrud( { authtoken :ses() });
402         var date2 = document.getElementById('date2').dateValue;
403         date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
404         pcrud.search(
405             'ausp',
406             {
407                 usr : xulG.patron.id(),
408                 stop_date : {
409                     'between' : [ 
410                         document.getElementById('date1').value, 
411                         document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ? 
412                             'now' : util.date.formatted_date( date2 ,'%F')
413                     ]
414                 }
415             },
416             {
417                 async : true,
418                 streaming : true,
419                 onerror : function(r) {
420                     try {
421                         var res = openils.Util.readResponse(r,true);
422                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
423                     } catch(E) {
424                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
425                     }
426                 },
427                 oncomplete : function() {
428                     document.getElementById('archived_progress').hidden = true;
429                 },
430                 onresponse : function(r) {
431                     try {
432                         var my_ausp = openils.Util.readResponse(r);
433                         var row_params = {
434                             'row' : {
435                                 'my' : {
436                                     'ausp' : my_ausp,
437                                     'csp' : my_ausp.standing_penalty(),
438                                     'au' : xulG.patron,
439                                 }
440                             }
441                         };
442                         archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
443                     } catch(E) {
444                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
445                     }
446                 }
447             }
448         );
449     } catch(E) {
450         var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
451         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
452     }
453 }