protect "my" reports from other people
[Evergreen.git] / Open-ILS / src / reporter / templates / stage3.ttk
1 [%
2
3 PROCESS inputs;
4 PROCESS class_manip;
5 PROCESS widget_manip;
6 PROCESS logic_header.ttk;
7 INCLUDE logout.ttk;
8
9 templates = DBI.tie('reporter.stage2', 'id')
10 reports = DBI.tie('reporter.stage3', 'id')
11 output = DBI.tie('reporter.output', 'id')
12 rpt = {};
13 tmpl = {};
14
15
16 IF CGI.param('output');
17         oid = CGI.param('output');
18         out = output.$oid;
19
20         rid = out.stage3;
21         rpt = reports.$rid;
22
23         tid = rpt.stage2;
24         tmpl = templates.$tid;
25 END;
26
27 IF CGI.param('id');
28         rid = CGI.param('id');
29         rpt = reports.$rid;
30         tid = rpt.stage2;
31         tmpl = templates.$tid;
32 END;
33
34
35 WRAPPER html/html;
36    INCLUDE header.ttk + navbar.ttk title="Reports";
37    WRAPPER html/body;
38       IF CGI.param('action') == 'Schedule';
39          PROCESS schedule_stage3;
40       ELSIF CGI.param('action') == 'Save';
41          PROCESS save_stage3;
42       ELSIF CGI.param('action') == 'delete_stage3';
43          PROCESS delete_stage3;
44       ELSIF CGI.param('action') == 'delete_output';
45          PROCESS delete_output;
46       ELSIF CGI.param('action') == 'rerun';
47          PROCESS rerun;
48       ELSIF CGI.param('action') == 'Run Now';
49          PROCESS save_stage3;
50                 ELSIF CGI.param('id');
51                         PROCESS view_stage3;
52                 ELSE;
53                         PROCESS list_stage3s;
54       END;
55    END;
56    INCLUDE footer.ttk;
57 END;
58
59
60 BLOCK schedule_stage3;
61         WRAPPER form method='POST';
62                 'Run Date:';
63                 INCLUDE text name='runtime' value='yyyy-mm-dd';
64                 '<br>';
65
66                 'Optional Recurrence:';
67                 INCLUDE text name='recurrence-count' value='0';
68                 WRAPPER select name='recurrence-interval';
69                         INCLUDE option value='days' content='Day(s)';
70                         INCLUDE option value='weeks' content='Week(s)';
71                         INCLUDE option value='months' content='Month(s)';
72                 END;
73
74                 FOR p = CGI.param();
75                         NEXT IF p == 'action';
76                         NEXT IF p == 'runtime';
77                         INCLUDE hidden name=p value=CGI.param(p);
78                 END;
79                 '<br>';
80                 INCLUDE submit name='action' value='Save';
81         END;
82 END;
83
84 BLOCK list_stage3s;
85 END;
86
87
88 BLOCK save_stage3;
89         stage2 = CGI.param('stage2');
90         runtime = CGI.param('runtime') OR 'now';
91         pub = CGI.param('publicreport') OR 'f';
92         owner = user.id();
93
94         p_obj = {};
95         FOR p = CGI.param();
96
97                 logme([p,CGI.param(p)]);
98
99                 parts = p.split(':');
100                 p1 = parts.0;
101                 p2 = parts.1;
102                 p3 = parts.2;
103                 p4 = parts.3;
104                 p5 = parts.4;
105                 p6 = parts.5;
106                 
107                 IF p6;
108
109                         IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.defined;
110                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.$p6.size > 1;
111                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6.push(CGI.param(p));
112                                 ELSE;
113                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5.$p6, CGI.param(p) ];
114                                         p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = tmp;
115                                 END;
116                         ELSE;
117                                 p_obj.$p1.$p2.$p3.$p4.$p5.$p6 = CGI.param(p);
118                         END;
119
120                 ELSIF p5;
121
122                         IF p_obj.$p1.$p2.$p3.$p4.$p5.defined;
123                                 IF p_obj.$p1.$p2.$p3.$p4.$p5.size > 1;
124                                         p_obj.$p1.$p2.$p3.$p4.$p5.push(CGI.param(p));
125                                 ELSE;
126                                         tmp = [ p_obj.$p1.$p2.$p3.$p4.$p5, CGI.param(p) ];
127                                         p_obj.$p1.$p2.$p3.$p4.$p5 = tmp;
128                                 END;
129                         ELSE;
130                                 p_obj.$p1.$p2.$p3.$p4.$p5 = CGI.param(p);
131                         END;
132
133                 ELSIF p4;
134
135                         IF p_obj.$p1.$p2.$p3.$p4.defined;
136                                 IF p_obj.$p1.$p2.$p3.$p4.size > 1;
137                                         p_obj.$p1.$p2.$p3.$p4.push(CGI.param(p));
138                                 ELSE;
139                                         tmp = [ p_obj.$p1.$p2.$p3.$p4, CGI.param(p) ];
140                                         p_obj.$p1.$p2.$p3.$p4 = tmp;
141                                 END;
142                         ELSE;
143                                 p_obj.$p1.$p2.$p3.$p4 = CGI.param(p);
144                         END;
145
146                 ELSIF p3;
147
148                         IF p_obj.$p1.$p2.$p3.defined;
149                                 IF p_obj.$p1.$p2.$p3.size > 1;
150                                         p_obj.$p1.$p2.$p3.push(CGI.param(p));
151                                 ELSE;
152                                         tmp = [ p_obj.$p1.$p2.$p3, CGI.param(p) ];
153                                         p_obj.$p1.$p2.$p3 = tmp;
154                                 END;
155                         ELSE;
156                                 p_obj.$p1.$p2.$p3 = CGI.param(p);
157                         END;
158
159                 ELSIF p2;
160
161                         IF p_obj.$p1.$p2.defined;
162                                 IF p_obj.$p1.$p2.size > 1;
163                                         p_obj.$p1.$p2.push(CGI.param(p));
164                                 ELSE;
165                                         tmp = [ p_obj.$p1.$p2, CGI.param(p) ];
166                                         p_obj.$p1.$p2 = tmp;
167                                 END;
168                         ELSE;
169                                 p_obj.$p1.$p2 = CGI.param(p);
170                         END;
171
172                 ELSE;
173
174                         p_obj.$p = CGI.param(p);
175
176                 END;
177         END;
178
179         params = utils.perl2JSON( p_obj );
180
181         recurrence = '0 sec';
182         IF CGI.param('recurrence-count') > 0;
183                 recurrence = CGI.param('recurrence-count') _ ' ' _ CGI.param('recurrence-interval');
184         END;
185
186         logme([stage2, params, owner, pub]);
187
188         q = DBI.prepare( stage3_insert );
189         CALL q.execute(stage2, params, owner, pub, runtime, recurrence);
190
191         FOR new_s3 = DBI.query("SELECT * FROM reporter.stage3_id_seq;");
192                 rid = new_s3.last_value;
193                 rpt = reports.$rid;
194                 tid = rpt.stage2;
195                 tmpl = templates.$tid;
196                 INCLUDE view_stage3;
197         END;
198 END;
199
200 BLOCK delete_stage3;
201         q = DBI.prepare( 'DELETE FROM reporter.stage3 WHERE id = ?' );
202         CALL q.execute(rpt.id);
203 END;
204
205 BLOCK delete_output;
206         q = DBI.prepare( 'DELETE FROM reporter.output WHERE id = ?' );
207         CALL q.execute(out.id);
208         INCLUDE view_stage3;
209 END;
210
211 BLOCK rerun;
212         q = DBI.prepare( 'UPDATE reporter.stage3 SET rerun = TRUE WHERE id = ?' );
213         CALL q.execute(rpt.id);
214         INCLUDE view_stage3;
215 END;
216
217 BLOCK view_stage3;
218         logme(rpt.params);
219         params = utils.JSON2perl( rpt.params );
220         s2_params = utils.JSON2perl( tmpl.params );
221
222
223         WRAPPER html/center;
224                 WRAPPER html/table style="border-collapse:collapse";
225                         WRAPPER html/row;
226                                 INCLUDE html/cell content="<b>Report Name</b>" style="border:solid gray 1px;";
227                                 INCLUDE html/cell content=params.reportname style="border:solid gray 1px;";
228                         END;
229                         WRAPPER html/row;
230                                 INCLUDE html/cell content="<b>Based on Template</b>" style="border:solid gray 1px;";
231                                 INCLUDE html/cell content=s2_params.templatename style="border:solid gray 1px;";
232                         END;
233                         WRAPPER html/row;
234                                 INCLUDE html/cell content="<b>Creation Time</b>" style="border:solid gray 1px;";
235                                 INCLUDE html/cell content=rpt.create_date style="border:solid gray 1px;";
236                         END;
237                         WRAPPER html/row;
238                                 INCLUDE html/cell content="<b>Scheduled Run Time</b>" style="border:solid gray 1px;";
239                                 INCLUDE html/cell content=rpt.runtime style="border:solid gray 1px;";
240                         END;
241                         WRAPPER html/row;
242                                 INCLUDE html/cell content="<b>Recurrence Interval</b>" style="border:solid gray 1px;";
243                                 INCLUDE html/cell content=rpt.recurrence style="border:solid gray 1px;";
244                         END;
245                         WRAPPER html/row;
246                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
247                                 WRAPPER html/cell style="border:solid gray 1px;";
248                                         INCLUDE anchor content='Refresh' href=CGI.url('-path', 1) _ '?id=' _ rpt.id;
249                                         '&nbsp;&nbsp;&nbsp;';
250                                         IF user.id == rpt.owner;
251                                                 INCLUDE anchor content='Run again' href=CGI.url('-path', 1) _ '?action=rerun&id=' _ rpt.id;
252                                                 '&nbsp;&nbsp;&nbsp;';
253                                                 INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_stage3&id=' _ rpt.id;
254                                                 '&nbsp;&nbsp;&nbsp;';
255                                         END;
256                                 END;
257                         END;
258                 END;
259         END;
260
261         '<br><br>';
262
263         type_map = { 'csv' => 'csv', 'excel' => 'xls', 'html' => 'html' };
264
265         WRAPPER html/center;
266                 '<b>Report Runs:</b>';
267                 run_q = DBI.prepare("SELECT * FROM reporter.output WHERE stage3 = ? ORDER BY queue_time DESC");
268                 FOR run = run_q.execute(rpt.id);
269                         WRAPPER html/table style="border-collapse:collapse";
270                                 WRAPPER html/row;
271                                         INCLUDE html/cell content="<b>Current State</b>" style="border:solid gray 1px;" width="20%";
272                                         WRAPPER html/cell style="border:solid gray 1px;";
273                                                 run.state;
274                                                 IF run.state == 'complete';
275                                                         '<br>';
276                                                         FOR f = params.output_format.list;
277                                                                 h = tmpl.stage1 _ '/' _ tmpl.id _
278                                                                         '/' _ rpt.id _ '/' _ run.id _
279                                                                         '/report-data.' _ type_map.$f;
280                                                                 link(content=f, href=h);
281                                                                 IF !loop.last;
282                                                                         '  |  ';
283                                                                 END;
284                                                         END;
285                                                 END;
286                                         END;
287                                 END;
288                                 WRAPPER html/row;
289                                         INCLUDE html/cell content="<b>Queue Time</b>" style="border:solid gray 1px;";
290                                         INCLUDE html/cell content=run.queue_time style="border:solid gray 1px;";
291                                 END;
292                                 IF run.run_time;
293                                         WRAPPER html/row;
294                                                 INCLUDE html/cell content="<b>Run Time</b>" style="border:solid gray 1px;";
295                                                 INCLUDE html/cell content=run.run_time style="border:solid gray 1px;";
296                                         END;
297                                 END;
298                                 IF run.complete_time;
299                                         WRAPPER html/row;
300                                                 INCLUDE html/cell content="<b>Complete Time</b>" style="border:solid gray 1px;";
301                                                 INCLUDE html/cell content=run.complete_time style="border:solid gray 1px;";
302                                         END;
303                                 END;
304                                 IF run.query;
305                                         WRAPPER html/row;
306                                                 INCLUDE html/cell content="<b>Query</b>" style="border:solid gray 1px;";
307                                                 INCLUDE html/cell content=run.query style="border:solid gray 1px;";
308                                         END;
309                                 END;
310                                 IF run.error;
311                                         WRAPPER html/row;
312                                                 INCLUDE html/cell content="<b>Error</b>" style="border:solid gray 1px;";
313                                                 INCLUDE html/cell content=run.error style="border:solid gray 1px;";
314                                         END;
315                                 END;
316                                 IF run.error_time;
317                                         WRAPPER html/row;
318                                                 INCLUDE html/cell content="<b>Error Time</b>" style="border:solid gray 1px;";
319                                                 INCLUDE html/cell content=run.error_time style="border:solid gray 1px;";
320                                         END;
321                                 END;
322                                 IF run.complete_time || run.error_time;
323                                         WRAPPER html/row;
324                                                 INCLUDE html/cell content="<b>Actions</b>" style="border:solid gray 1px;";
325                                                 WRAPPER html/cell style="border:solid gray 1px;";
326                                                         IF user.id == rpt.owner;
327                                                                 INCLUDE anchor content='Delete' href=CGI.url('-path', 1) _ '?action=delete_output&output=' _ run.id;
328                                                                 '&nbsp;&nbsp;&nbsp;';
329                                                         END;
330                                                 END;
331                                         END;
332                                 END;
333                         END;
334                         '<br><br>';
335                 END;
336         END;
337 END;
338
339 %]