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