ab56d449ea6ff2b362d21e2a008b3ebdc9e9ee8a
[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/>';
349
350                 WRAPPER html/table border=0 width='100%';
351                         WRAPPER html/row;
352                                 INCLUDE html/cell colspan=4 content="<b>Filterable Attributes</b><br/><br/>" style="border-top: solid black 2px;";
353                         END;
354
355                         core_table = params.stage1;
356
357                         tables = params.filter.keys;
358                         tables.unshift(core_table) IF tables.grep("^$core_table$");
359                         tables = tables.unique;
360
361                         FOR t = tables;
362                                 NEXT UNLESS params.filter.$t.keys;
363                                 f_table = INCLUDE find_table_id id=t;
364                                 table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
365
366                                 table_label = table.findvalue( 'label' );
367
368                                 WRAPPER html/row;
369                                         WRAPPER html/cell colspan=3 align='center' style="border: solid black 1px; background: lightgray;";
370                                                 IF t != f_table;
371                                                         link_xpath = '//*[@id="' _ t _ '"]';
372                                                         dim_link = config.findnodes(link_xpath);
373
374                                                         link_label_xpath =
375                                                                 '../../fields/field[@name="' _
376                                                                 dim_link.findvalue('@field') _ '"]/label';
377
378                                                         table_label = table_label _ ' -- ' _ dim_link.findvalue(link_label_xpath);
379                                                 END;
380                                                 table_label;
381                                         END;
382                                 END;
383
384                                 FOR f_field = params.filter.$t.keys;
385                                         logme([f_table,f_field]);
386                                         FOR f_fam = params.filter.$t.$f_field.keys;
387                                                 logme([f_table,f_field,f_fam]);
388                                                 f_widget = params.filter.$t.$f_field.$f_fam;
389                                                 logme([f_table,f_field,f_fam,f_widget]);
390
391                                                 WRAPPER html/row;
392                                                         field = table.findnodes( "fields/field[@name='$f_field']" );
393                                                         fam = config.findnodes( "/reporter/widgets/widget-family[@name='$f_fam']" );
394                                                         widget = fam.findnodes( "widget[@name='$f_widget']" );
395
396                                                         w_file = 'widgets/' _ fam.findvalue('@name') _ '.' _ widget.findvalue('@name');
397                                                         INCLUDE html/cell;
398                                                         INCLUDE html/cell align='right' content=field.findvalue( 'label' );
399
400                                                         WRAPPER html/cell align='right';
401                                                                 TRY;
402                                                                                 classname = table.findvalue('@id');
403                                                                                 fieldname = field.findvalue('@name');
404                                                                                 widgetname = widget.findvalue('@name');
405                                                                                 famname = fam.findvalue('@name');
406         
407                                                                                 input_prefix = "filter:$t:$fieldname:$famname:$widgetname";
408         
409                                                                                 INCLUDE $w_file widget_family=fam;
410                                                                 CATCH;
411                                                                         %]ARG!! Couldn't render widget [% $w_file %] ([% error.info %])!!![%
412                                                                 END;
413                                                         END;
414                                                 END;
415                                         END;
416                                 END;
417                         END;
418         
419                         WRAPPER html/row;
420                                 INCLUDE html/cell colspan=4 content="<br>";
421                         END;
422
423                         WRAPPER html/row;
424                                 INCLUDE html/cell colspan=4 content="<b>Attributes Available for Output</b><br/><br/>" style="border-top: solid black 2px;";
425                         END;
426
427                         table = config.findnodes( "/reporter/tables/table[@id='$core_table']" );
428                         classname = table.findvalue( "@id" );
429                         tname = table.findvalue( "label" );
430                         tdesc = table.findvalue( "desription" );
431
432                         WRAPPER html/row;
433                                 INCLUDE html/cell colspan=4 align='center' content=tname style="border: solid black 1px; background: lightgray;";
434                         END;
435
436                         pkey_field = table.findnodes('fields/field[@primary = "true"]');
437                         pid = pkey_field.findvalue('@name');
438                         plabel = pkey_field.findvalue('label');
439                 
440                         WRAPPER html/row;
441                                 WRAPPER html/cell align='right';
442                                         INCLUDE checkbox checked='checked' name="output:$classname:$pid" value="include" onclick="field_add_remove(this)";
443                                         INCLUDE hidden name="xform:type:$classname:$pid" value="count_dist";
444                                         %]
445                                                 <script language="javascript">
446                                                         outputs['[% "output:$classname:$pid" %]'] = '[% "Per group count of $plabel" %]';
447                                                         Widget.Select.addOption('output_order','[% "output:$classname:$pid" %]', '[% "Per group count of $plabel" %]');
448                                                 </script>
449                                         [%
450                                 END;
451                                 INCLUDE html/cell align='right' content="Per group count of $plabel";
452                                 INCLUDE html/cell align='center';
453                         END;
454
455
456                         tables = params.dims.keys;
457                         tables.unshift(core_table);
458                         tables = tables.unique;
459
460                         core_done = 0;
461
462                         FOR f_table = tables;
463                                 noheader = 0;
464                                 noheader = 1 IF f_table == core_table;
465                                 INCLUDE show_filter_atts;
466                                 WRAPPER html/row;
467                                         INCLUDE html/cell colspan=4 content="<br/><br/>";
468                                 END;
469                         END;
470                 END;
471         
472
473                 INCLUDE checkbox name="publicreport" value="t";
474                 'Public Report<br>';
475                 INCLUDE submit name="action" value="Run Now";
476                 INCLUDE submit name="action" value="Schedule";
477
478         END;
479 END;
480
481 BLOCK show_filter_atts;
482         t = f_table;
483         f_table = INCLUDE find_table_id id=t;
484         table = config.findnodes( "/reporter/tables/table[@id='$f_table']");
485         classname = table.findvalue( "@id" );
486         tname = table.findvalue( "label" );
487         tdesc = table.findvalue( "desription" );
488
489         IF ! noheader;
490                 WRAPPER html/row;
491                         WRAPPER html/cell colspan=4 align='center' style="border: solid black 1px; background: lightgray;";
492                                 IF t != f_table;
493                                         link_xpath = '//*[@id="' _ t _ '"]';
494                                         dim_link = config.findnodes(link_xpath);
495
496                                         link_label_xpath =
497                                                 '../../fields/field[@name="' _
498                                                 dim_link.findvalue('@field') _ '"]/label';
499                                         dim_link.findvalue(link_label_xpath);
500
501                                         ' -- ';
502                                 END;
503                                 tname;
504                         END;
505                 END;
506         END;
507
508         fields_xpath = 'fields/field[not(@primary) or @primary != "true"]';
509
510
511         count = 1;
512
513         FOR field = table.findnodes(fields_xpath);
514                 link_test = '../../links/link[@field="' _ field.findvalue('@name') _ '"]';
515                 logme(['link_test',link_test]);
516                 NEXT IF field.findnodes( link_test );
517
518                 fname = field.findvalue( "label" );
519                 fid = field.findvalue( "@name" );
520
521                 bg_color = 'white';
522                 IF count % 2;
523                         bg_color = 'lightcyan';
524                 END;
525
526                 WRAPPER html/row;
527
528                         WRAPPER html/cell align='right' col=bg_color;
529                                 INCLUDE checkbox name="output:$t:$fid" value="include" onclick="field_add_remove(this)";
530                                 %]
531                                         <script language="javascript">
532                                                 outputs['[% "output:$t:$fid" %]'] = '[% fname %]';
533                                         </script>
534                                 [%
535
536                         END;
537                         INCLUDE html/cell align='right' content=fname col=bg_color;
538                         WRAPPER html/cell align='right' col=bg_color;
539                                 'Output Transformation: ';
540                                 dtype = field.findvalue('@datatype');
541                                 WRAPPER select name="xform:type:$t:$fid";
542                                         INCLUDE option value='' content='None' selected=1;
543                                         FOR xform = dtype_xform_map.$dtype;
544                                                 xform_label = dtype_xforms.$xform.label;
545                                                 IF dtype_xforms.$xform.param;
546                                                         xform_label = "$xform_label (*)";
547                                                 END;
548                                                 INCLUDE option value=xform content=xform_label;
549                                         END;
550                                 END;
551                                 '<br/>';
552                                 '<small><super>*</super>Output Tranformation Parameter: </small>';
553                                 INCLUDE text name="xform:param:$t:$fid";
554                         END;
555                 END;
556                 count = count + 1;
557         END;
558 END;
559 %]