Bug 7902 - Adding Function to Check for Existence of Tables
[koha.git] / reports / guided_reports.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Liblime ltd
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI;
23 use Text::CSV;
24 use URI::Escape;
25 use C4::Reports::Guided;
26 use C4::Auth qw/:DEFAULT get_session/;
27 use C4::Output;
28 use C4::Dates;
29 use C4::Debug;
30 use C4::Branch; # XXX subfield_is_koha_internal_p
31
32 =head1 NAME
33
34 guided_reports.pl
35
36 =head1 DESCRIPTION
37
38 Script to control the guided report creation
39
40 =cut
41
42 my $input = new CGI;
43
44 my $phase = $input->param('phase');
45 my $flagsrequired;
46 if ( $phase eq 'Build new' or $phase eq 'Delete Saved' ) {
47     $flagsrequired = 'create_reports';
48 }
49 elsif ( $phase eq 'Use saved' ) {
50     $flagsrequired = 'execute_reports';
51 } else {
52     $flagsrequired = '*';
53 }
54
55 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
56     {
57         template_name   => "reports/guided_reports_start.tmpl",
58         query           => $input,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { reports => $flagsrequired },
62         debug           => 1,
63     }
64 );
65 my $session = $cookie ? get_session($cookie->value) : undef;
66
67 my $filter;
68 if ( $input->param("filter_set") ) {
69     $filter = {};
70     $filter->{$_} = $input->param("filter_$_") foreach qw/date author keyword/;
71     $session->param('report_filter', $filter) if $session;
72     $template->param( 'filter_set' => 1 );
73 }
74 elsif ($session) {
75     $filter = $session->param('report_filter');
76 }
77
78
79 my @errors = ();
80 if ( !$phase ) {
81     $template->param( 'start' => 1 );
82     # show welcome page
83 }
84 elsif ( $phase eq 'Build new' ) {
85     # build a new report
86     $template->param( 'build1' => 1 );
87     $template->param( 'areas' => get_report_areas() );
88 }
89 elsif ( $phase eq 'Use saved' ) {
90     # use a saved report
91     # get list of reports and display them
92     $template->param(
93         'saved1' => 1,
94         'savedreports' => get_saved_reports($filter),
95     );
96     if ($filter) {
97         while ( my ($k, $v) = each %$filter ) {
98             $template->param( "filter_$k" => $v ) if $v;
99         }
100     }
101 }
102
103 elsif ( $phase eq 'Delete Saved') {
104         
105         # delete a report from the saved reports list
106         my $id = $input->param('reports');
107         delete_report($id);
108     print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
109         exit;
110 }               
111
112 elsif ( $phase eq 'Show SQL'){
113         
114         my $id = $input->param('reports');
115     my ($sql,$type,$reportname,$notes) = get_saved_report($id);
116         $template->param(
117         'id'      => $id,
118         'reportname' => $reportname,
119         'notes'      => $notes,
120                 'sql'     => $sql,
121                 'showsql' => 1,
122     );
123 }
124
125 elsif ( $phase eq 'Edit SQL'){
126         
127     my $id = $input->param('reports');
128     my ($sql,$type,$reportname,$notes) = get_saved_report($id);
129     $template->param(
130             'sql'        => $sql,
131             'reportname' => $reportname,
132         'notes'      => $notes,
133         'id'         => $id,
134             'editsql'    => 1,
135     );
136 }
137
138 elsif ( $phase eq 'Update SQL'){
139     my $id         = $input->param('id');
140     my $sql        = $input->param('sql');
141     my $reportname = $input->param('reportname');
142     my $notes      = $input->param('notes');
143     my @errors;
144     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
145         push @errors, {sqlerr => $1};
146     }
147     elsif ($sql !~ /^(SELECT)/i) {
148         push @errors, {queryerr => 1};
149     }
150     if (@errors) {
151         $template->param(
152             'errors'    => \@errors,
153             'sql'       => $sql,
154         );
155     }
156     else {
157         update_sql( $id, $sql, $reportname, $notes );
158         $template->param(
159             'save_successful'       => 1,
160             'reportname'            => $reportname,
161             'id'                    => $id,
162         );
163     }
164     
165 }
166
167 elsif ($phase eq 'retrieve results') {
168         my $id = $input->param('id');
169         my ($results,$name,$notes) = format_results($id);
170         # do something
171         $template->param(
172                 'retresults' => 1,
173                 'results' => $results,
174                 'name' => $name,
175                 'notes' => $notes,
176     );
177 }
178
179 elsif ( $phase eq 'Report on this Area' ) {
180
181     # they have choosen a new report and the area to report on
182     $template->param(
183         'build2' => 1,
184         'area'   => $input->param('areas'),
185         'types'  => get_report_types(),
186     );
187 }
188
189 elsif ( $phase eq 'Choose this type' ) {
190
191     # they have chosen type and area
192     # get area and type and pass them to the template
193     my $area = $input->param('area');
194     my $type = $input->param('types');
195     $template->param(
196         'build3' => 1,
197         'area'   => $area,
198         'type'   => $type,
199         columns  => get_columns($area,$input),
200     );
201 }
202
203 elsif ( $phase eq 'Choose these columns' ) {
204
205     # we now know type, area, and columns
206     # next step is the constraints
207     my $area    = $input->param('area');
208     my $type    = $input->param('type');
209     my @columns = $input->param('columns');
210     my $column  = join( ',', @columns );
211     $template->param(
212         'build4' => 1,
213         'area'   => $area,
214         'type'   => $type,
215         'column' => $column,
216         definitions => get_from_dictionary($area),
217         criteria    => get_criteria($area,$input),
218     );
219 }
220
221 elsif ( $phase eq 'Choose these criteria' ) {
222     my $area     = $input->param('area');
223     my $type     = $input->param('type');
224     my $column   = $input->param('column');
225         my @definitions = $input->param('definition');
226         my $definition = join (',',@definitions);
227     my @criteria = $input->param('criteria_column');
228         my $query_criteria;
229     foreach my $crit (@criteria) {
230         my $value = $input->param( $crit . "_value" );
231         
232         # If value is not defined, then it may be range values
233         if (!defined $value) {
234
235             my $fromvalue = $input->param( "from_" . $crit . "_value" );
236             my $tovalue   = $input->param( "to_"   . $crit . "_value" );
237             
238             # If the range values are dates
239             if ($fromvalue =~ C4::Dates->regexp('syspref') && $tovalue =~ C4::Dates->regexp('syspref')) { 
240                 $fromvalue = C4::Dates->new($fromvalue)->output("iso");
241                 $tovalue = C4::Dates->new($tovalue)->output("iso");
242             }
243
244             if ($fromvalue && $tovalue) {
245                 $query_criteria .= " AND $crit >= '$fromvalue' AND $crit <= '$tovalue'";
246             }
247
248         } else {
249
250             # If value is a date
251             if ($value =~ C4::Dates->regexp('syspref')) { 
252                 $value = C4::Dates->new($value)->output("iso");
253             }
254         # don't escape runtime parameters, they'll be at runtime
255         if ($value =~ /<<.*>>/) {
256             $query_criteria .= " AND $crit=$value";
257         } else {
258             $query_criteria .= " AND $crit='$value'";
259         }
260         }
261     }
262
263     $template->param(
264         'build5'         => 1,
265         'area'           => $area,
266         'type'           => $type,
267         'column'         => $column,
268         'definition'     => $definition,
269         'criteriastring' => $query_criteria,
270     );
271
272     # get columns
273     my @columns = split( ',', $column );
274     my @total_by;
275
276     # build structue for use by tmpl_loop to choose columns to order by
277     # need to do something about the order of the order :)
278         # we also want to use the %columns hash to get the plain english names
279     foreach my $col (@columns) {
280         my %total = (name => $col);
281         my @selects = map {+{ value => $_ }} (qw(sum min max avg count));
282         $total{'select'} = \@selects;
283         push @total_by, \%total;
284     }
285
286     $template->param( 'total_by' => \@total_by );
287 }
288
289 elsif ( $phase eq 'Choose These Operations' ) {
290     my $area     = $input->param('area');
291     my $type     = $input->param('type');
292     my $column   = $input->param('column');
293     my $criteria = $input->param('criteria');
294         my $definition = $input->param('definition');
295     my @total_by = $input->param('total_by');
296     my $totals;
297     foreach my $total (@total_by) {
298         my $value = $input->param( $total . "_tvalue" );
299         $totals .= "$value($total),";
300     }
301
302     $template->param(
303         'build6'         => 1,
304         'area'           => $area,
305         'type'           => $type,
306         'column'         => $column,
307         'criteriastring' => $criteria,
308         'totals'         => $totals,
309         'definition'     => $definition,
310     );
311
312     # get columns
313     my @columns = split( ',', $column );
314     my @order_by;
315
316     # build structue for use by tmpl_loop to choose columns to order by
317     # need to do something about the order of the order :)
318     foreach my $col (@columns) {
319         my %order = (name => $col);
320         my @selects = map {+{ value => $_ }} (qw(asc desc));
321         $order{'select'} = \@selects;
322         push @order_by, \%order;
323     }
324
325     $template->param( 'order_by' => \@order_by );
326 }
327
328 elsif ( $phase eq 'Build Report' ) {
329
330     # now we have all the info we need and can build the sql
331     my $area     = $input->param('area');
332     my $type     = $input->param('type');
333     my $column   = $input->param('column');
334     my $crit     = $input->param('criteria');
335     my $totals   = $input->param('totals');
336     my $definition = $input->param('definition');
337     my $query_criteria=$crit;
338     # split the columns up by ,
339     my @columns = split( ',', $column );
340     my @order_by = $input->param('order_by');
341
342     my $query_orderby;
343     foreach my $order (@order_by) {
344         my $value = $input->param( $order . "_ovalue" );
345         if ($query_orderby) {
346             $query_orderby .= ",$order $value";
347         }
348         else {
349             $query_orderby = " ORDER BY $order $value";
350         }
351     }
352
353     # get the sql
354     my $sql =
355       build_query( \@columns, $query_criteria, $query_orderby, $area, $totals, $definition );
356     $template->param(
357         'showreport' => 1,
358         'sql'        => $sql,
359         'type'       => $type
360     );
361 }
362
363 elsif ( $phase eq 'Save' ) {
364         # Save the report that has just been built
365     my $sql  = $input->param('sql');
366     my $type = $input->param('type');
367     $template->param(
368         'save' => 1,
369         'sql'  => $sql,
370         'type' => $type
371     );
372 }
373
374 elsif ( $phase eq 'Save Report' ) {
375     # save the sql pasted in by a user 
376     my $sql  = $input->param('sql');
377     my $name = $input->param('reportname');
378     my $type = $input->param('types');
379     my $notes = $input->param('notes');
380     if ($sql =~ /;?\W?(UPDATE|DELETE|DROP|INSERT|SHOW|CREATE)\W/i) {
381         push @errors, {sqlerr => $1};
382     }
383     elsif ($sql !~ /^(SELECT)/i) {
384         push @errors, {queryerr => 1};
385     }
386     if (@errors) {
387         $template->param(
388             'errors'    => \@errors,
389             'sql'       => $sql,
390             'reportname'=> $name,
391             'type'      => $type,
392             'notes'     => $notes,
393         );
394     }
395     else {
396         my $id = save_report( $borrowernumber, $sql, $name, $type, $notes );
397         $template->param(
398             'save_successful'       => 1,
399             'reportname'            => $name,
400             'id'                    => $id,
401         );
402     }
403 }
404
405 elsif ($phase eq 'Run this report'){
406     # execute a saved report
407     my $limit  = 20;    # page size. # TODO: move to DB or syspref?
408     my $offset = 0;
409     my $report = $input->param('reports');
410     my @sql_params = $input->param('sql_params');
411     # offset algorithm
412     if ($input->param('page')) {
413         $offset = ($input->param('page') - 1) * $limit;
414     }
415     my ($sql,$type,$name,$notes) = get_saved_report($report);
416     unless ($sql) {
417         push @errors, {no_sql_for_id=>$report};   
418     } 
419     my @rows = ();
420     # if we have at least 1 parameter, and it's not filled, then don't execute but ask for parameters
421     if ($sql =~ /<</ && !@sql_params) {
422         # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
423         my @split = split /<<|>>/,$sql;
424         my @tmpl_parameters;
425         for(my $i=0;$i<($#split/2);$i++) {
426             my ($text,$authorised_value) = split /\|/,$split[$i*2+1];
427             my $input;
428             if ($authorised_value eq "date") {
429                $input = 'date';
430             }
431             elsif ($authorised_value) {
432                 my $dbh=C4::Context->dbh;
433                 my @authorised_values;
434                 my %authorised_lib;
435                 # builds list, depending on authorised value...
436                 if ( $authorised_value eq "branches" ) {
437                     my $branches = GetBranchesLoop();
438                     foreach my $thisbranch (@$branches) {
439                         push @authorised_values, $thisbranch->{value};
440                         $authorised_lib{$thisbranch->{value}} = $thisbranch->{branchname};
441                     }
442                 }
443                 elsif ( $authorised_value eq "itemtypes" ) {
444                     my $sth = $dbh->prepare("SELECT itemtype,description FROM itemtypes ORDER BY description");
445                     $sth->execute;
446                     while ( my ( $itemtype, $description ) = $sth->fetchrow_array ) {
447                         push @authorised_values, $itemtype;
448                         $authorised_lib{$itemtype} = $description;
449                     }
450                 }
451                 elsif ( $authorised_value eq "cn_source" ) {
452                     my $class_sources = GetClassSources();
453                     my $default_source = C4::Context->preference("DefaultClassificationSource");
454                     foreach my $class_source (sort keys %$class_sources) {
455                         next unless $class_sources->{$class_source}->{'used'} or
456                                     ($class_source eq $default_source);
457                         push @authorised_values, $class_source;
458                         $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
459                     }
460                 }
461                 elsif ( $authorised_value eq "categorycode" ) {
462                     my $sth = $dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
463                     $sth->execute;
464                     while ( my ( $categorycode, $description ) = $sth->fetchrow_array ) {
465                         push @authorised_values, $categorycode;
466                         $authorised_lib{$categorycode} = $description;
467                     }
468
469                     #---- "true" authorised value
470                 }
471                 else {
472                     my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
473
474                     $authorised_values_sth->execute( $authorised_value);
475
476                     while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
477                         push @authorised_values, $value;
478                         $authorised_lib{$value} = $lib;
479                         # For item location, we show the code and the libelle
480                         $authorised_lib{$value} = $lib;
481                     }
482                 }
483                 $input =CGI::scrolling_list(      # FIXME: factor out scrolling_list
484                     -name     => "sql_params",
485                     -values   => \@authorised_values,
486 #                     -default  => $value,
487                     -labels   => \%authorised_lib,
488                     -override => 1,
489                     -size     => 1,
490                     -multiple => 0,
491                     -tabindex => 1,
492                 );
493
494             } else {
495                 $input = "<input type='text' name='sql_params'/>";
496             }
497             push @tmpl_parameters, {'entry' => $text, 'input' => $input };
498         }
499         $template->param('sql'         => $sql,
500                         'name'         => $name,
501                         'sql_params'   => \@tmpl_parameters,
502                         'enter_params' => 1,
503                         'reports'      => $report,
504                         );
505     } else {
506         # OK, we have parameters, or there are none, we run the report
507         # if there were parameters, replace before running
508         # split on ??. Each odd (2,4,6,...) entry should be a parameter to fill
509         my @split = split /<<|>>/,$sql;
510         my @tmpl_parameters;
511         for(my $i=0;$i<$#split/2;$i++) {
512             my $quoted = C4::Context->dbh->quote($sql_params[$i]);
513             # if there are special regexp chars, we must \ them
514             $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
515             $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
516         }
517         my ($sth, $errors) = execute_query($sql, $offset, $limit);
518         my $total = nb_rows($sql) || 0;
519         unless ($sth) {
520             die "execute_query failed to return sth for report $report: $sql";
521         } else {
522             my $headref = $sth->{NAME} || [];
523             my @headers = map { +{ cell => $_ } } @$headref;
524             $template->param(header_row => \@headers);
525             while (my $row = $sth->fetchrow_arrayref()) {
526                 my @cells = map { +{ cell => $_ } } @$row;
527                 push @rows, { cells => \@cells };
528             }
529         }
530
531         my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
532         my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report&amp;phase=Run%20this%20report";
533         if (@sql_params) {
534             $url = join('&amp;sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params);
535         }
536         $template->param(
537             'results' => \@rows,
538             'sql'     => $sql,
539             'id'      => $report,
540             'execute' => 1,
541             'name'    => $name,
542             'notes'   => $notes,
543             'errors'  => $errors,
544             'pagination_bar'  => pagination_bar($url, $totpages, $input->param('page')),
545             'unlimited_total' => $total,
546         );
547     }
548 }
549
550 elsif ($phase eq 'Export'){
551     binmode STDOUT, ':encoding(UTF-8)';
552
553         # export results to tab separated text or CSV
554         my $sql    = $input->param('sql');  # FIXME: use sql from saved report ID#, not new user-supplied SQL!
555     my $format = $input->param('format');
556         my ($sth, $q_errors) = execute_query($sql);
557     unless ($q_errors and @$q_errors) {
558         print $input->header(       -type => 'application/octet-stream',
559                                     -attachment=>"reportresults.$format"
560                             );
561         if ($format eq 'tab') {
562             print join("\t", header_cell_values($sth)), "\n";
563             while (my $row = $sth->fetchrow_arrayref()) {
564                 print join("\t", @$row), "\n";
565             }
566         } else {
567             my $csv = Text::CSV->new({binary => 1});
568             $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
569             if ($csv->combine(header_cell_values($sth))) {
570                 print $csv->string(), "\n";
571             } else {
572                 push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
573             }
574             while (my $row = $sth->fetchrow_arrayref()) {
575                 if ($csv->combine(@$row)) {
576                     print $csv->string(), "\n"; 
577                 } else {
578                     push @$q_errors, { combine => $csv->error_diag() } ;
579                 }
580             }
581         }
582         foreach my $err (@$q_errors, @errors) {
583             print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
584         }   # here we print all the non-fatal errors at the end.  Not super smooth, but better than nothing.
585         exit;
586     }
587     $template->param(
588         'sql'           => $sql,
589         'execute'       => 1,
590         'name'          => 'Error exporting report!',
591         'notes'         => '',
592         'errors'        => $q_errors,
593     );
594 }
595
596 elsif ($phase eq 'Create report from SQL') {
597         # allow the user to paste in sql
598     if ($input->param('sql')) {
599         $template->param(
600             'sql'           => $input->param('sql'),
601             'reportname'    => $input->param('reportname'),
602             'notes'         => $input->param('notes'),
603         );
604     }
605         $template->param('create' => 1);
606 }
607
608 elsif ($phase eq 'Create Compound Report'){
609         $template->param( 'savedreports' => get_saved_reports(),
610                 'compound' => 1,
611         );
612 }
613
614 elsif ($phase eq 'Save Compound'){
615     my $master    = $input->param('master');
616         my $subreport = $input->param('subreport');
617         my ($mastertables,$subtables) = create_compound($master,$subreport);
618         $template->param( 'save_compound' => 1,
619                 master=>$mastertables,
620                 subsql=>$subtables
621         );
622 }
623
624 # pass $sth, get back an array of names for the column headers
625 sub header_cell_values {
626     my $sth = shift or return ();
627     return @{$sth->{NAME}};
628 }
629
630 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
631 sub header_cell_loop {
632     my @headers = map { +{ cell => $_ } } header_cell_values (shift);
633     return \@headers;
634 }
635
636 foreach (1..6) {
637      $template->{VARS}->{'build' . $_} and $template->{VARS}->{'buildx' . $_} and last;
638 }
639 $template->param(   'referer' => $input->referer(),
640                     'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
641                 );
642
643 output_html_with_http_headers $input, $cookie, $template->output;