0bd913e78e05e80fffa611d4953f6b119a1d00b6
[Evergreen.git] / Open-ILS / src / reporter / templates / stage2.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 tmpl = {};
11
12
13 IF CGI.param('id');
14         tid = CGI.param('id');
15         tmpl = templates.$tid;
16 END;
17
18
19 WRAPPER html/html;
20    INCLUDE header.ttk + navbar.ttk title="Report Templates";
21    WRAPPER html/body;
22       IF CGI.param('action') == 'save';
23          PROCESS save_stage2;
24       ELSIF CGI.param('action') == 'edit';
25          PROCESS edit_stage2;
26       ELSIF CGI.param('action') == 'run';
27          PROCESS run_stage2;
28                 ELSE;
29          PROCESS view_stage2;
30       END;
31    END;
32    INCLUDE footer.ttk;
33 END;
34
35
36
37
38 BLOCK save_stage2;
39         stage1 = CGI.param('stage1');
40         pub = CGI.param('publictemplate') OR 'f';
41         owner = user.id();
42
43         p_obj = {};
44         FOR p = CGI.param();
45
46                 logme([p,CGI.param(p)]);
47
48                 parts = p.split(':');
49                 p1 = parts.0;
50                 p2 = parts.1;
51                 p3 = parts.2;
52                 p4 = parts.3;
53                 
54                 IF p4
55                         AND (CGI.param("$p1").list.grep("^$p2$").size > 0)
56                         AND (CGI.param("$p1:$p2").list.grep("^$p3$").size > 0)
57                         AND (CGI.param("$p1:$p2:$p3").list.grep("^$p4$").size > 0);
58
59                         p_obj.$p1.$p2.$p3.$p4 = CGI.param(p);
60
61                 ELSIF p2
62                         AND (CGI.param("$p1").list.grep(p2).size > 0);
63                         p_obj.dims.$p2 = 1;
64                 ELSIF p1 != 'filter';
65                         p_obj.$p1 = CGI.param(p);
66                 END;
67
68         END;
69
70         params = utils.perl2JSON( p_obj );
71
72         logme([stage1, params, owner, pub]);
73
74         q = DBI.prepare( stage2_insert );
75         CALL q.execute(stage1, params, owner, pub);
76
77         FOR new_s2 = DBI.query("SELECT * FROM reporter.stage2_id_seq;");
78                 tid = new_s2.last_value;
79                 INCLUDE view_stage2 tmpl = templates.$tid;
80         END;
81 END;
82
83
84
85
86 BLOCK edit_stage2;
87
88 END;
89
90
91
92
93
94 BLOCK view_stage2;
95         logme(tmpl.params);
96         params = utils.JSON2perl( tmpl.params );
97
98         WRAPPER html/table border=0 width='100%';
99                 WRAPPER html/row;
100                         WRAPPER html/cell colspan=5 align='center';
101                                 s1_name_xpath = '/reporter/tables/table[@id="' _ params.stage1 _ '"]/label';
102                                 INCLUDE anchor
103                                         content=config.findvalue( s1_name_xpath )
104                                         href="stage1?id=" _ params.stage1;
105                                 ' :: ' _ params.templatename _ '<br>';
106                                 INCLUDE anchor
107                                         content="Create a report from this template"
108                                         href=CGI.url('-path', 1) _ '?action=run&id=' _ tmpl.id;
109                         END;
110                 END;
111
112                 WRAPPER html/row;
113                         INCLUDE html/cell colspan=4 content="<b>Filterable Attributes:</b>";
114                 END;
115
116                 core_table = params.stage1;
117
118                 tables = params.filter.keys;
119                 tables.unshift(core_table);
120                 tables = tables.unique;
121
122                 FOR t = tables;
123                         logme(['table id or link id', t]);
124
125                         f_table = INCLUDE find_table_id id=t;
126
127                         table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
128                         WRAPPER html/row;
129                                 INCLUDE html/cell;
130                                 WRAPPER html/cell colspan=4 align='left';
131
132                                         IF t != f_table;
133                                                 link_xpath = '//*[@id="' _ t _ '"]';
134                                                 dim_link = config.findnodes(link_xpath);
135
136                                                 link_label_xpath =
137                                                         '../../fields/field[@name="' _
138                                                         dim_link.findvalue('@field') _ '"]/label';
139                                                 dim_link.findvalue(link_label_xpath);
140
141                                                 ' -- ';
142                                         END;
143
144                                         table.findvalue( 'label' );
145                                 END;
146                         END;
147
148                         FOR f_field = params.filter.$t.keys;
149                                 logme([f_table,f_field]);
150                                 FOR f_fam = params.filter.$t.$f_field.keys;
151                                         logme([f_table,f_field,f_fam]);
152                                         f_widget = params.filter.$t.$f_field.$f_fam;
153                                         logme([f_table,f_field,f_fam,f_widget]);
154
155                                         WRAPPER html/row;
156                                                 field = table.findnodes( "fields/field[@name='$f_field']" );
157                                                 fam = config.findnodes( "/reporter/widgets/widget-family[@name='$f_fam']" );
158                                                 widget = fam.findnodes( "widget[@name='$f_widget']" );
159
160                                                 INCLUDE html/cell colspan=2;
161                                                 INCLUDE html/cell align='right' content=field.findvalue( 'label' );
162                                                 INCLUDE html/cell align='center' content=fam.findvalue( 'label' );
163                                                 INCLUDE html/cell align='left' content=widget.findvalue( 'label' );
164                                         END;
165                                 END;
166                         END;
167                 END;
168
169                 WRAPPER html/row;
170                         INCLUDE html/cell colspan=5 content="<br>";
171                 END;
172
173                 WRAPPER html/row;
174                         INCLUDE html/cell colspan=5 content="<b>Attributes Available for Output</b>";
175                 END;
176
177                 tables = params.dims.keys;
178                 tables.unshift(core_table);
179                 tables = tables.unique;
180
181                 FOR t = tables;
182
183                         f_table = INCLUDE find_table_id id=t;
184                         
185                         table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
186                         tname = table.findvalue( "label" );
187                         tdesc = table.findvalue( "desription" );
188
189                         #NEXT IF table.findvalue('@fact-table') == 'true';
190                         
191                         WRAPPER html/row;
192                                 INCLUDE html/cell;
193                                 WRAPPER html/cell colspan=4 align='left';
194                                         IF t != f_table;
195                                                 link_xpath = '//*[@id="' _ t _ '"]';
196                                                 dim_link = config.findnodes(link_xpath);
197
198                                                 link_label_xpath =
199                                                         '../../fields/field[@name="' _
200                                                         dim_link.findvalue('@field') _ '"]/label';
201                                                 dim_link.findvalue(link_label_xpath);
202
203                                                 ' -- ';
204                                         END;
205                                         tname;
206                                 END;
207                         END;
208
209
210                         FOR field = table.findnodes('fields/field[not(@primary) or @primary != "true"]');
211                                 link_test = '../../links/link[@field="' _ field.findvalue('@name') _ '"]';
212                                 logme(['link_test',link_test]);
213                                 NEXT IF field.findnodes( link_test );
214
215                                 fname = field.findvalue( "label" );
216                                 fdesc = field.findvalue( "desription" );
217
218                                 WRAPPER html/row;
219
220                                         INCLUDE html/cell colspan=2;
221                                         INCLUDE html/cell align='right' content=fname;
222                                         INCLUDE html/cell colspan=2 align='center' content=fdesc;
223                                 END;
224                         END;
225                 END;
226
227         END;
228 END;
229
230
231 BLOCK run_stage2;
232         logme(tmpl.params);
233         params = utils.JSON2perl( tmpl.params );
234
235    '<br/>';
236         WRAPPER form id="stage3_new" name="stage3_new" action="stage3" method="POST";
237                 INCLUDE hidden name='stage2' value=CGI.param('id');
238
239                 '<br>Report Name:';
240                 INCLUDE text name='reportname';
241
242                 WRAPPER html/table border=0 width='100%';
243                         WRAPPER html/row;
244                                 WRAPPER html/cell colspan=4 align='center';
245                                         s1_name_xpath = '/reporter/tables/table[@id="' _ params.stage1 _ '"]/label';
246                                         INCLUDE anchor
247                                                 content=config.findvalue( s1_name_xpath )
248                                                 href="stage1?id=" _ params.stage1;
249                                         ' :: ' _ params.templatename;
250                                 END;
251                         END;
252
253                         WRAPPER html/row;
254                                 INCLUDE html/cell colspan=4 content="<b>Filterable Attributes:</b>";
255                         END;
256
257                         core_table = params.stage1;
258
259                         tables = params.filter.keys;
260                         tables.unshift(core_table);
261                         tables = tables.unique;
262
263                         FOR t = tables;
264                                 f_table = INCLUDE find_table_id id=t;
265                                 logme([f_table]);
266                                 table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
267                                 WRAPPER html/row;
268                                         WRAPPER html/cell colspan=3 align='left';
269                                                 IF t != f_table;
270                                                         link_xpath = '//*[@id="' _ t _ '"]';
271                                                         dim_link = config.findnodes(link_xpath);
272
273                                                         link_label_xpath =
274                                                                 '../../fields/field[@name="' _
275                                                                 dim_link.findvalue('@field') _ '"]/label';
276                                                         dim_link.findvalue(link_label_xpath);
277
278                                                         ' -- ';
279                                                 END;
280                                                 table.findvalue( 'label' );
281                                         END;
282                                 END;
283
284                                 FOR f_field = params.filter.$t.keys;
285                                         logme([f_table,f_field]);
286                                         FOR f_fam = params.filter.$t.$f_field.keys;
287                                                 logme([f_table,f_field,f_fam]);
288                                                 f_widget = params.filter.$t.$f_field.$f_fam;
289                                                 logme([f_table,f_field,f_fam,f_widget]);
290
291                                                 WRAPPER html/row;
292                                                         field = table.findnodes( "fields/field[@name='$f_field']" );
293                                                         fam = config.findnodes( "/reporter/widgets/widget-family[@name='$f_fam']" );
294                                                         widget = fam.findnodes( "widget[@name='$f_widget']" );
295
296                                                         w_file = 'widgets/' _ fam.findvalue('@name') _ '.' _ widget.findvalue('@name');
297                                                         INCLUDE html/cell;
298                                                         INCLUDE html/cell align='right' content=field.findvalue( 'label' );
299
300                                                         WRAPPER html/cell align='center';
301                                                                 TRY;
302                                                                                 classname = table.findvalue('@id');
303                                                                                 fieldname = field.findvalue('@name');
304                                                                                 widgetname = widget.findvalue('@name');
305                                                                                 famname = fam.findvalue('@name');
306         
307                                                                                 input_prefix = "filter:$t:$fieldname:$famname:$widgetname";
308         
309                                                                                 INCLUDE $w_file widget_family=fam;
310                                                                 CATCH;
311                                                                         %]ARG!! Couldn't render widget [% $w_file %] ([% error.info %])!!![%
312                                                                 END;
313                                                         END;
314                                                 END;
315                                         END;
316                                 END;
317                         END;
318         
319                         WRAPPER html/row;
320                                 INCLUDE html/cell colspan=4 content="<br>";
321                         END;
322
323                         WRAPPER html/row;
324                                 INCLUDE html/cell colspan=4 content="<b>Attributes Available for Output</b>";
325                         END;
326
327                         table = config.findnodes( "/reporter/tables/table[@id='$core_table']" );
328                         classname = table.findvalue( "@id" );
329                         tname = table.findvalue( "label" );
330                         tdesc = table.findvalue( "desription" );
331
332                         WRAPPER html/row;
333                                 INCLUDE html/cell colspan=4 align='left' content=tname;
334                         END;
335
336                         pkey_field = table.findnodes('fields/field[@primary = "true"]');
337                         pid = pkey_field.findvalue('@name');
338                         plabel = pkey_field.findvalue('label');
339                 
340                         WRAPPER html/row;
341                                 WRAPPER html/cell align='right';
342                                         INCLUDE checkbox checked='checked' name="output:$classname:$pid" value="include";
343                                         INCLUDE hidden name="xform:type:$classname:$pid" value="count_dist";
344                                 END;
345                                 INCLUDE html/cell align='right' content="Per group count of $plabel";
346                                 INCLUDE html/cell align='center';
347                         END;
348
349
350                         tables = params.dims.keys;
351                         tables.unshift(core_table);
352                         tables = tables.unique;
353
354                         core_done = 0;
355
356                         FOR f_table = tables;
357                                 noheader = 0;
358                                 noheader = 1 IF f_table == core_table;
359                                 INCLUDE show_filter_atts;
360                         END;
361                 END;
362         
363                 INCLUDE checkbox name="publicreport" value="t";
364                 'Public Report<br>';
365                 INCLUDE submit name="action" value="Run Now";
366                 INCLUDE submit name="action" value="Schedule";
367
368         END;
369 END;
370
371 BLOCK show_filter_atts;
372         t = f_table;
373         f_table = INCLUDE find_table_id id=t;
374         table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
375         classname = table.findvalue( "@id" );
376         tname = table.findvalue( "label" );
377         tdesc = table.findvalue( "desription" );
378
379         IF ! noheader;
380                 WRAPPER html/row;
381                         WRAPPER html/cell colspan=4 align='left';
382                                 IF t != f_table;
383                                         link_xpath = '//*[@id="' _ t _ '"]';
384                                         dim_link = config.findnodes(link_xpath);
385
386                                         link_label_xpath =
387                                                 '../../fields/field[@name="' _
388                                                 dim_link.findvalue('@field') _ '"]/label';
389                                         dim_link.findvalue(link_label_xpath);
390
391                                         ' -- ';
392                                 END;
393                                 tname;
394                         END;
395                 END;
396         END;
397
398         fields_xpath = 'fields/field[not(@primary) or @primary != "true"]';
399
400         FOR field = table.findnodes(fields_xpath);
401                 link_test = '../../links/link[@field="' _ field.findvalue('@name') _ '"]';
402                 logme(['link_test',link_test]);
403                 NEXT IF field.findnodes( link_test );
404
405                 fname = field.findvalue( "label" );
406                 fid = field.findvalue( "@name" );
407
408                 WRAPPER html/row;
409
410                         WRAPPER html/cell align='right';
411                                 INCLUDE checkbox name="output:$t:$fid" value="include";
412                         END;
413                         INCLUDE html/cell align='right' content=fname;
414                         WRAPPER html/cell align='center';
415                                 'Output Transformation: ';
416                                 dtype = field.findvalue('@datatype');
417                                 WRAPPER select name="xform:type:$t:$fid";
418                                         INCLUDE option value='' content='None' selected=1;
419                                         FOR xform = dtype_xform_map.$dtype;
420                                                 xform_label = dtype_xforms.$xform.label;
421                                                 IF dtype_xforms.$xform.param;
422                                                         xform_label = "$xform_label (*)";
423                                                 END;
424                                                 INCLUDE option value=xform content=xform_label;
425                                         END;
426                                 END;
427                                 '<br/>';
428                                 '<small><super>*</super>Output Tranformation Parameter: </small>';
429                                 INCLUDE text name="xform:param:$t:$fid";
430                         END;
431                 END;
432         END;
433 END;
434 %]