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