multi-set reporting based on pivot of 1 grouping column
[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         INCLUDE logout.ttk;
9    END;
10
11    WRAPPER html/body;
12
13         PROCESS logic_header.ttk;
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                         var filters = {};
242                         function send_it (f) {
243                                 var bad_filters = [];
244                                 for (var i in filters) {
245                                         if (!f.elements[i].value) {
246                                                 bad_filters.push(filters[i]); 
247                                         }
248                                 }
249                                 if (bad_filters.length) {
250                                         var fstring = '';
251                                         for (var i in bad_filters)
252                                                 fstring = fstring + "\n\t" + bad_filters[i];
253
254                                         alert('You must choose a filter value for these filter:\n' + fstring);
255                                         return false;
256                                 }
257
258                                 Widget.Select.selectAll('output_order');
259                                 return true;
260                         }
261                 </script>
262                 <br/>
263         [%
264
265         WRAPPER html/table width="100%";
266                 WRAPPER html/row;
267                         WRAPPER html/cell align='center';
268                                 s1_name_xpath = '/reporter/tables/table[@id="' _ params.stage1 _ '"]/label';
269                                 INCLUDE anchor
270                                         content=config.findvalue( s1_name_xpath )
271                                         href="stage1?id=" _ params.stage1;
272                                 ' :: ' _ params.templatename;
273                         END;
274                 END;
275         END;
276
277         '<br/><br/>';
278
279         WRAPPER form id="stage3_new" name="stage3_new" action="stage3" onsubmit="return send_it(this);" method="POST";
280                 INCLUDE hidden name='stage2' value=CGI.param('id');
281
282                 '<br/><center><b>Report Name:</b>';
283                 INCLUDE text name='reportname' size="50";
284                 '</center><br/><br/>';
285
286                 WRAPPER html/table width="100%";
287                         WRAPPER html/row  style="border-top: solid black 2px;";
288                                 INCLUDE html/cell colspan=2 content='<b>Output and Sort Order</b>' style="border-top: solid black 2px;";
289                                 INCLUDE html/cell colspan=2 content='<b>Output Formats</b>' align="center" style="border-top: solid black 2px;";
290                         END;
291                         WRAPPER html/row;
292                                 WRAPPER html/cell rowspan=3 width="25%";
293                                         INCLUDE select multi=1 size=5 id='output_order' name='output_order';
294                                 END;
295                                 WRAPPER html/cell;
296                                         INCLUDE anchor href='javascript:void(0);' content='Up' onclick="Widget.Select.moveSelectedOptionsUp('output_order')";
297                                 END;
298
299                                 WRAPPER html/cell align="right";
300                                         INCLUDE checkbox name='output_format' value='csv' checked='checked';
301                                 END;
302                                 INCLUDE html/cell content="CSV" align="left";
303                         END;
304                         WRAPPER html/row;
305                                 WRAPPER html/cell;
306                                         INCLUDE anchor href='javascript:void(0);' content='Down' onclick="Widget.Select.moveSelectedOptionsDown('output_order')";
307                                 END;
308
309                                 WRAPPER html/cell align="right";
310                                         INCLUDE checkbox name='output_format' value='excel' checked='checked';
311                                 END;
312                                 INCLUDE html/cell content="Excel" align="left";
313                         END;
314                         WRAPPER html/row;
315                                 INCLUDE html/cell;
316                                 WRAPPER html/cell align="right" valign='top';
317                                         INCLUDE checkbox name='output_format' value='html' checked='checked';
318                                 END;
319                                 WRAPPER html/cell align="left";
320                                         'HTML<br/>';
321                                         WRAPPER html/table;
322                                                 WRAPPER html/row + html/cell colspan=2;
323                                                         "<i>Graph types</i>";
324                                                 END;
325                                                 WRAPPER html/row;
326                                                         WRAPPER html/cell;
327                                                                 INCLUDE checkbox name='html_graph_type' value='bar';
328                                                         END;
329                                                         INCLUDE html/cell content='Bar';
330                                                 END;
331                                                 WRAPPER html/row;
332                                                         WRAPPER html/cell;
333                                                                 INCLUDE checkbox name='html_graph_type' value='pie';
334                                                         END;
335                                                         INCLUDE html/cell content='Pie';
336                                                 END;
337                                                 WRAPPER html/row;
338                                                         WRAPPER html/cell;
339                                                                 INCLUDE checkbox name='html_graph_type' value='line';
340                                                         END;
341                                                         INCLUDE html/cell content='Line';
342                                                 END;
343                                         END;
344                                 END;
345                         END;
346                 END;
347
348                 '<br/><br/><b>Pivot Column:</b> ';
349                 WRAPPER select id='pivot_col' name='pivot_col';
350                         INCLUDE option value='' content='No Pivot';
351                 END;
352                 '<br/><br/>';
353
354                 WRAPPER html/table border=0 width='100%';
355                         WRAPPER html/row;
356                                 INCLUDE html/cell colspan=4 content="<b>Filterable Attributes</b><br/><br/>" style="border-top: solid black 2px;";
357                         END;
358
359                         core_table = params.stage1;
360
361                         tables = params.filter.keys;
362                         tables.unshift(core_table) IF tables.grep("^$core_table$");
363                         tables = tables.unique;
364
365                         FOR t = tables;
366                                 NEXT UNLESS params.filter.$t.keys;
367                                 f_table = INCLUDE find_table_id id=t;
368                                 table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
369
370                                 table_label = table.findvalue( 'label' );
371
372                                 WRAPPER html/row;
373                                         WRAPPER html/cell colspan=3 align='center' style="border: solid black 1px; background: lightgray;";
374                                                 IF t != f_table;
375                                                         link_xpath = '//*[@id="' _ t _ '"]';
376                                                         dim_link = config.findnodes(link_xpath);
377
378                                                         link_label_xpath =
379                                                                 '../../fields/field[@name="' _
380                                                                 dim_link.findvalue('@field') _ '"]/label';
381
382                                                         table_label = table_label _ ' -- ' _ dim_link.findvalue(link_label_xpath);
383                                                 END;
384                                                 table_label;
385                                         END;
386                                 END;
387
388                                 FOR f_field = params.filter.$t.keys;
389                                         logme([f_table,f_field]);
390                                         FOR f_fam = params.filter.$t.$f_field.keys;
391                                                 logme([f_table,f_field,f_fam]);
392                                                 f_widget = params.filter.$t.$f_field.$f_fam;
393                                                 logme([f_table,f_field,f_fam,f_widget]);
394
395                                                 WRAPPER html/row;
396                                                         field = table.findnodes( "fields/field[@name='$f_field']" );
397                                                         fam = config.findnodes( "/reporter/widgets/widget-family[@name='$f_fam']" );
398                                                         widget = fam.findnodes( "widget[@name='$f_widget']" );
399
400                                                         w_file = 'widgets/' _ fam.findvalue('@name') _ '.' _ widget.findvalue('@name');
401                                                         INCLUDE html/cell;
402                                                         INCLUDE html/cell align='right' content=field.findvalue( 'label' );
403
404                                                         WRAPPER html/cell align='right';
405                                                                 TRY;
406                                                                                 classname = table.findvalue('@id');
407                                                                                 fieldname = field.findvalue('@name');
408                                                                                 widgetname = widget.findvalue('@name');
409                                                                                 famname = fam.findvalue('@name');
410         
411                                                                                 input_prefix = "filter:$t:$fieldname:$famname:$widgetname";
412         
413                                                                                 INCLUDE $w_file widget_family=fam;
414                                                                 CATCH;
415                                                                         %]ARG!! Couldn't render widget [% $w_file %] ([% error.info %])!!![%
416                                                                 END;
417                                                         END;
418                                                 END;
419                                         END;
420                                 END;
421                         END;
422         
423                         WRAPPER html/row;
424                                 INCLUDE html/cell colspan=4 content="<br>";
425                         END;
426
427                         WRAPPER html/row;
428                                 INCLUDE html/cell colspan=4 content="<b>Attributes Available for Output</b><br/><br/>" style="border-top: solid black 2px;";
429                         END;
430
431                         table = config.findnodes( "/reporter/tables/table[@id='$core_table']" );
432                         classname = table.findvalue( "@id" );
433                         tname = table.findvalue( "label" );
434                         tdesc = table.findvalue( "desription" );
435
436                         WRAPPER html/row;
437                                 INCLUDE html/cell colspan=4 align='center' content=tname style="border: solid black 1px; background: lightgray;";
438                         END;
439
440                         pkey_field = table.findnodes('fields/field[@primary = "true"]');
441                         pid = pkey_field.findvalue('@name');
442                         plabel = pkey_field.findvalue('label');
443                 
444                         WRAPPER html/row;
445                                 WRAPPER html/cell align='right';
446                                         INCLUDE checkbox checked='checked' name="output:$classname:$pid" value="include" onclick="field_add_remove(this)";
447                                         INCLUDE hidden name="xform:type:$classname:$pid" value="count_dist";
448                                         %]
449                                                 <script language="javascript">
450                                                         outputs['[% "output:$classname:$pid" %]'] = '[% "Per group count of $plabel" %]';
451                                                         Widget.Select.addOption('output_order','[% "output:$classname:$pid" %]', '[% "Per group count of $plabel" %]');
452                                                 </script>
453                                         [%
454                                 END;
455                                 INCLUDE html/cell align='right' content="Per group count of $plabel";
456                                 INCLUDE html/cell align='center';
457                         END;
458
459
460                         tables = params.dims.keys;
461                         tables.unshift(core_table);
462                         tables = tables.unique;
463
464                         core_done = 0;
465
466                         FOR f_table = tables;
467                                 noheader = 0;
468                                 noheader = 1 IF f_table == core_table;
469                                 INCLUDE show_filter_atts;
470                                 WRAPPER html/row;
471                                         INCLUDE html/cell colspan=4 content="<br/><br/>";
472                                 END;
473                         END;
474                 END;
475         
476
477                 INCLUDE checkbox name="publicreport" value="t";
478                 'Public Report<br>';
479                 INCLUDE submit name="action" value="Run Now";
480                 INCLUDE submit name="action" value="Schedule";
481
482         END;
483 END;
484
485 BLOCK show_filter_atts;
486         t = f_table;
487         f_table = INCLUDE find_table_id id=t;
488         table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
489         classname = table.findvalue( "@id" );
490         tname = table.findvalue( "label" );
491         tdesc = table.findvalue( "desription" );
492
493         IF ! noheader;
494                 WRAPPER html/row;
495                         WRAPPER html/cell colspan=4 align='center' style="border: solid black 1px; background: lightgray;";
496                                 IF t != f_table;
497                                         link_xpath = '//*[@id="' _ t _ '"]';
498                                         dim_link = config.findnodes(link_xpath);
499
500                                         link_label_xpath =
501                                                 '../../fields/field[@name="' _
502                                                 dim_link.findvalue('@field') _ '"]/label';
503                                         dim_link.findvalue(link_label_xpath);
504
505                                         ' -- ';
506                                 END;
507                                 tname;
508                         END;
509                 END;
510         END;
511
512         fields_xpath = 'fields/field[not(@primary) or @primary != "true"]';
513
514
515         count = 1;
516
517         FOR field = table.findnodes(fields_xpath);
518                 link_test = '../../links/link[@field="' _ field.findvalue('@name') _ '"]';
519                 logme(['link_test',link_test]);
520                 NEXT IF field.findnodes( link_test );
521
522                 fname = field.findvalue( "label" );
523                 fid = field.findvalue( "@name" );
524
525                 bg_color = 'white';
526                 IF count % 2;
527                         bg_color = 'lightcyan';
528                 END;
529
530                 WRAPPER html/row;
531
532                         WRAPPER html/cell align='right' col=bg_color;
533                                 INCLUDE checkbox name="output:$t:$fid" value="include" onclick="field_add_remove(this)";
534                                 %]
535                                         <script language="javascript">
536                                                 outputs['[% "output:$t:$fid" %]'] = '[% fname %]';
537                                         </script>
538                                 [%
539
540                         END;
541                         INCLUDE html/cell align='right' content=fname col=bg_color;
542                         WRAPPER html/cell align='right' col=bg_color;
543                                 'Output Transformation: ';
544                                 dtype = field.findvalue('@datatype');
545                                 WRAPPER select name="xform:type:$t:$fid";
546                                         INCLUDE option value='' content='None' selected=1;
547                                         FOR xform = dtype_xform_map.$dtype;
548                                                 xform_label = dtype_xforms.$xform.label;
549                                                 IF dtype_xforms.$xform.param;
550                                                         xform_label = "$xform_label (*)";
551                                                 END;
552                                                 INCLUDE option value=xform content=xform_label;
553                                         END;
554                                 END;
555                                 '<br/>';
556                                 '<small><super>*</super>Output Tranformation Parameter: </small>';
557                                 INCLUDE text name="xform:param:$t:$fid";
558                         END;
559                 END;
560                 count = count + 1;
561         END;
562 END;
563 %]