3 # Copyright 2007 Liblime ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
19 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
28 Script to control the guided report creation
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
40 template_name => "reports/guided_reports_start.tmpl",
44 flagsrequired => { editcatalogue => 1 },
49 my $phase = $input->param('phase');
50 my $no_html = 0; # this will be set if we dont want to print out an html::template
53 $template->param( 'start' => 1 );
58 elsif ( $phase eq 'Build new' ) {
61 $template->param( 'build1' => 1 );
64 my $areas = C4::Reports::get_report_areas();
65 $template->param( 'areas' => $areas );
69 elsif ( $phase eq 'Used saved' ) {
72 # get list of reports and display them
73 $template->param( 'saved1' => 1 );
74 my $reports = get_saved_reports();
75 $template->param( 'savedreports' => $reports );
78 elsif ( $phase eq 'Report on this Area' ) {
80 # they have choosen a new report and the area to report on
82 my $area = $input->param('areas');
89 my $types = C4::Reports::get_report_types();
90 $template->param( 'types' => $types );
93 elsif ( $phase eq 'Choose this type' ) {
95 # they have chosen type and area
96 # get area and type and pass them to the template
97 my $area = $input->param('area');
98 my $type = $input->param('types');
106 my $columns = get_columns($area);
107 $template->param( 'columns' => $columns );
110 elsif ( $phase eq 'Choose these columns' ) {
112 # we now know type, area, and columns
113 # next step is the constraints
114 my $area = $input->param('area');
115 my $type = $input->param('type');
116 my @columns = $input->param('columns');
117 my $column = join( ',', @columns );
118 my $definitions = get_from_dictionary($area);
125 my $criteria = get_criteria($area);
126 $template->param( 'criteria' => $criteria,
127 'definitions' => $definitions);
130 elsif ( $phase eq 'Choose these criteria' ) {
131 my $area = $input->param('area');
132 my $type = $input->param('type');
133 my $column = $input->param('column');
134 my @definitions = $input->param('definition');
135 my $definition = join (',',@definitions);
136 my @criteria = $input->param('criteria_column');
138 foreach my $crit (@criteria) {
139 my $value = $input->param( $crit . "_value" );
141 $query_criteria .= " AND $crit='$value'";
150 'definition' => $definition,
151 'criteriastring' => $query_criteria,
155 my @columns = split( ',', $column );
158 # build structue for use by tmpl_loop to choose columns to order by
159 # need to do something about the order of the order :)
160 # we also want to use the %columns hash to get the plain english names
161 foreach my $col (@columns) {
163 $total{'name'} = $col;
166 $select1{'value'} = 'sum';
167 push @selects, \%select1;
169 $select2{'value'} = 'min';
170 push @selects, \%select2;
172 $select3{'value'} = 'max';
173 push @selects, \%select3;
175 $select4{'value'} = 'avg';
176 push @selects, \%select4;
178 $select5{'value'} = 'count';
179 push @selects, \%select5;
181 $total{'select'} = \@selects;
182 push @total_by, \%total;
185 $template->param( 'total_by' => \@total_by );
188 elsif ( $phase eq 'Choose Totals' ) {
189 my $area = $input->param('area');
190 my $type = $input->param('type');
191 my $column = $input->param('column');
192 my $criteria = $input->param('criteria');
193 my $definition = $input->param('definition');
194 my @total_by = $input->param('total_by');
196 foreach my $total (@total_by) {
197 my $value = $input->param( $total . "_tvalue" );
198 $totals .= "$value($total),";
206 'criteriastring' => $criteria,
208 'definition' => $definition,
212 my @columns = split( ',', $column );
215 # build structue for use by tmpl_loop to choose columns to order by
216 # need to do something about the order of the order :)
217 foreach my $col (@columns) {
219 $order{'name'} = $col;
222 $select1{'value'} = 'asc';
223 push @selects, \%select1;
225 $select2{'value'} = 'desc';
226 push @selects, \%select2;
227 $order{'select'} = \@selects;
228 push @order_by, \%order;
231 $template->param( 'order_by' => \@order_by );
234 elsif ( $phase eq 'Build Report' ) {
236 # now we have all the info we need and can build the sql
237 my $area = $input->param('area');
238 my $type = $input->param('type');
239 my $column = $input->param('column');
240 my $crit = $input->param('criteria');
241 my $totals = $input->param('totals');
242 my $definition = $input->param('definition');
243 # my @criteria = split( ',', $crit );
244 my $query_criteria=$crit;
245 # split the columns up by ,
246 my @columns = split( ',', $column );
247 my @order_by = $input->param('order_by');
250 foreach my $order (@order_by) {
251 my $value = $input->param( $order . "_ovalue" );
252 if ($query_orderby) {
253 $query_orderby .= ",$order $value";
256 $query_orderby = " ORDER BY $order $value";
262 build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
270 elsif ( $phase eq 'Save' ) {
271 # Save the report that has just been built
272 my $sql = $input->param('sql');
273 my $type = $input->param('type');
281 elsif ( $phase eq 'Save Report' ) {
282 # save the sql pasted in by a user
283 my $sql = $input->param('sql');
284 my $name = $input->param('reportname');
285 my $type = $input->param('type');
286 my $notes = $input->param('notes');
287 save_report( $sql, $name, $type, $notes );
290 elsif ( $phase eq 'Execute' ) {
291 # run the sql, and output results in a template
292 my $sql = $input->param('sql');
293 my $type = $input->param('type');
294 my $results = execute_query($sql,$type);
296 'results' => $results,
302 elsif ($phase eq 'Run this report'){
303 # execute a saved report
304 my $report = $input->param('reports');
305 my ($sql,$type) = get_saved_report($report);
306 my $results = execute_query($sql,$type);
308 'results' => $results,
314 elsif ($phase eq 'Export'){
315 # export results to tab separated text
316 my $sql = $input->param('sql');
318 print $input->header( -type => 'application/octet-stream',
319 -attachment=>'reportresults.csv');
320 my $format=$input->param('format');
321 my $results = execute_query($sql,1,$format);
326 elsif ($phase eq 'Create report from SQL'){
327 # alllow the user to paste in sql
328 $template->param('create' => 1);
329 my $types = C4::Reports::get_report_types();
330 $template->param( 'types' => $types );
333 elsif ($phase eq 'Create Compound Report'){
334 my $reports = get_saved_reports();
335 $template->param( 'savedreports' => $reports,
340 elsif ($phase eq 'Save Compound'){
341 my $master = $input->param('master');
342 my $subreport = $input->param('subreport');
343 # my $compound_report = create_compound($master,$subreport);
344 # my $results = run_compound($compound_report);
345 my ($mastertables,$subtables) = create_compound($master,$subreport);
346 $template->param( 'save_compound' => 1,
347 master=>$mastertables,
352 elsif ($phase eq 'View Dictionary'){
353 # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
354 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
356 template_name => "reports/dictionary.tmpl",
359 authnotrequired => 0,
360 flagsrequired => { editcatalogue => 1 },
364 my $areas = C4::Reports::get_report_areas();
365 my $definitions = get_from_dictionary();
366 $template->param( 'areas' => $areas ,
367 'start_dictionary' => 1,
368 'definitions' => $definitions,
371 elsif ($phase eq 'Add New Definition'){
372 # display form allowing them to add a new definition
373 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
375 template_name => "reports/dictionary.tmpl",
378 authnotrequired => 0,
379 flagsrequired => { editcatalogue => 1 },
384 $template->param( 'new_dictionary' => 1,
388 elsif ($phase eq 'New Term step 2'){
390 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
392 template_name => "reports/dictionary.tmpl",
395 authnotrequired => 0,
396 flagsrequired => { editcatalogue => 1 },
400 my $areas = C4::Reports::get_report_areas();
401 my $definition_name=$input->param('definition_name');
402 my $definition_description=$input->param('definition_description');
403 $template->param( 'step_2' => 1,
405 'definition_name' => $definition_name,
406 'definition_description' => $definition_description,
410 elsif ($phase eq 'New Term step 3'){
411 # Choosing the columns
412 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
414 template_name => "reports/dictionary.tmpl",
417 authnotrequired => 0,
418 flagsrequired => { editcatalogue => 1 },
422 my $area = $input->param('areas');
423 my $columns = get_columns($area);
424 my $definition_name=$input->param('definition_name');
425 my $definition_description=$input->param('definition_description');
426 $template->param( 'step_3' => 1,
428 'columns' => $columns,
429 'definition_name' => $definition_name,
430 'definition_description' => $definition_description,
434 elsif ($phase eq 'New Term step 4'){
435 # Choosing the values
436 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
438 template_name => "reports/dictionary.tmpl",
441 authnotrequired => 0,
442 flagsrequired => { editcatalogue => 1 },
446 my $area=$input->param('area');
447 my $definition_name=$input->param('definition_name');
448 my $definition_description=$input->param('definition_description');
449 my @columns = $input->param('columns');
450 my $columnstring = join (',',@columns);
452 foreach my $column (@columns){
454 $tmp_hash{'name'}=$column;
455 my $type =get_column_type($column);
456 if ($type eq 'distinct'){
457 my $values = get_distinct_values($column);
458 $tmp_hash{'values'} = $values;
459 $tmp_hash{'distinct'} = 1;
462 if ($type eq 'DATE'){
465 if ($type eq 'TEXT'){
471 push @column_loop,\%tmp_hash;
474 $template->param( 'step_4' => 1,
476 'definition_name' => $definition_name,
477 'definition_description' => $definition_description,
478 'columns' => \@column_loop,
479 'columnstring' => $columnstring,
484 elsif ($phase eq 'New Term step 5'){
485 # Confirmation screen
486 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
488 template_name => "reports/dictionary.tmpl",
491 authnotrequired => 0,
492 flagsrequired => { editcatalogue => 1 },
496 my $area = $input->param('area');
497 my $columnstring = $input->param('columnstring');
498 my $definition_name=$input->param('definition_name');
499 my $definition_description=$input->param('definition_description');
500 my @criteria = $input->param('criteria_column');
503 foreach my $crit (@criteria) {
504 my $value = $input->param( $crit . "_value" );
506 $query_criteria .= " AND $crit='$value'";
508 $tmp_hash{'name'}=$crit;
509 $tmp_hash{'value'} = $value;
510 push @criteria_loop,\%tmp_hash;
513 $value = $input->param( $crit . "_start_value" );
515 $query_criteria .= " AND $crit > '$value'";
517 $tmp_hash{'name'}="$crit Start";
518 $tmp_hash{'value'} = $value;
519 push @criteria_loop,\%tmp_hash;
521 $value = $input->param( $crit . "_end_value" );
523 $query_criteria .= " AND $crit <= '$value'";
525 $tmp_hash{'name'}="$crit End";
526 $tmp_hash{'value'} = $value;
527 push @criteria_loop,\%tmp_hash;
530 $template->param( 'step_5' => 1,
532 'definition_name' => $definition_name,
533 'definition_description' => $definition_description,
534 'query' => $query_criteria,
535 'columnstring' => $columnstring,
536 'criteria_loop' => \@criteria_loop,
540 elsif ($phase eq 'New Term step 6'){
542 my $area = $input->param('area');
543 my $definition_name=$input->param('definition_name');
544 my $definition_description=$input->param('definition_description');
545 my $sql=$input->param('sql');
546 save_dictionary($definition_name,$definition_description,$sql,$area);
548 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");
551 elsif ($phase eq 'Delete Definition'){
553 my $id = $input->param('id');
554 delete_definition($id);
555 print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=View%20Dictionary");
559 output_html_with_http_headers $input, $cookie, $template->output;