Bug 14926: Format the date according to the dateformat syspref
[koha.git] / reports / guided_reports.pl
index 366a12d..0cee490 100755 (executable)
@@ -31,6 +31,9 @@ use C4::Dates qw/format_date/;
 use C4::Debug;
 use C4::Branch; # XXX subfield_is_koha_internal_p
 use C4::Koha qw/IsAuthorisedValueCategory GetFrameworksLoop/;
+use C4::Context;
+use C4::Log;
+use Koha::DateUtils qw/dt_from_string output_pref/;
 
 =head1 NAME
 
@@ -75,7 +78,7 @@ if ( $input->param("filter_set") ) {
     $session->param('report_filter', $filter) if $session;
     $template->param( 'filter_set' => 1 );
 }
-elsif ($session) {
+elsif ($session and not $input->param('clear_filters')) {
     $filter = $session->param('report_filter');
 }
 
@@ -107,6 +110,7 @@ elsif ( $phase eq 'Build new' ) {
         'savedreports' => get_saved_reports($filter),
         'usecache' => $usecache,
         'groups_with_subgroups'=> groups_with_subgroups($group, $subgroup),
+        filters => $filter,
     );
 }
 
@@ -192,7 +196,7 @@ elsif ( $phase eq 'Update SQL'){
         push @errors, {sqlerr => $1};
     }
     elsif ($sql !~ /^(SELECT)/i) {
-        push @errors, {queryerr => 1};
+        push @errors, {queryerr => "No SELECT"};
     }
 
     if (@errors) {
@@ -236,6 +240,7 @@ elsif ( $phase eq 'Update SQL'){
                 'reportname'            => $reportname,
                 'id'                    => $id,
             );
+            logaction( "REPORTS", "MODIFY", $id, "$reportname | $sql" ) if C4::Context->preference("ReportsLog");
         }
         if ( $usecache ) {
             $template->param(
@@ -593,6 +598,7 @@ elsif ( $phase eq 'Save Report' ) {
                     cache_expiry   => $cache_expiry,
                     public         => $public,
                 } );
+                logaction( "REPORTS", "ADD", $id, "$name | $sql" ) if C4::Context->preference("ReportsLog");
             $template->param(
                 'save_successful' => 1,
                 'reportname'      => $name,
@@ -721,17 +727,12 @@ elsif ($phase eq 'Run this report'){
                     }
                     $labelid = $text;
                     $labelid =~ s/\W//g;
-                    $input =CGI::scrolling_list(      # FIXME: factor out scrolling_list
-                        -name     => "sql_params",
-                        -id       => "sql_params_".$labelid,
-                        -values   => \@authorised_values,
-#                     -default  => $value,
-                        -labels   => \%authorised_lib,
-                        -override => 1,
-                        -size     => 1,
-                        -multiple => 0,
-                        -tabindex => 1,
-                    );
+                    $input = {
+                        name    => "sql_params",
+                        id      => "sql_params_".$labelid,
+                        values  => \@authorised_values,
+                        labels  => \%authorised_lib,
+                    };
                 }
 
                 push @tmpl_parameters, {'entry' => $text, 'input' => $input, 'labelid' => $labelid };
@@ -750,9 +751,13 @@ elsif ($phase eq 'Run this report'){
             my @split = split /<<|>>/,$sql;
             my @tmpl_parameters;
             for(my $i=0;$i<$#split/2;$i++) {
-                my $quoted = C4::Context->dbh->quote($sql_params[$i]);
+                my $quoted = $sql_params[$i];
                 # if there are special regexp chars, we must \ them
                 $split[$i*2+1] =~ s/(\||\?|\.|\*|\(|\)|\%)/\\$1/g;
+                if ($split[$i*2+1] =~ /\|\s*date\s*$/) {
+                    $quoted = output_pref({ dt => dt_from_string($quoted), dateformat => 'iso', dateonly => 1 }) if $quoted;
+                }
+                $quoted = C4::Context->dbh->quote($quoted);
                 $sql =~ s/<<$split[$i*2+1]>>/$quoted/;
             }
             my ($sth, $errors) = execute_query($sql, $offset, $limit);
@@ -931,6 +936,7 @@ elsif ($phase eq 'Save Compound'){
 # pass $sth, get back an array of names for the column headers
 sub header_cell_values {
     my $sth = shift or return ();
+    return '' unless ($sth->{NAME});
     return @{$sth->{NAME}};
 }