Bug 7679: Fix two SQL queries when using patron attributes
[koha.git] / reports / dictionary.pl
index f27ce10..7d86f8d 100755 (executable)
@@ -4,26 +4,26 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
 use strict;
 use warnings;
 use C4::Auth;
-use CGI;
+use CGI qw ( -utf8 );
 use C4::Output;
 use C4::Reports::Guided;
-use C4::Dates;
+use Koha::DateUtils;
 
 =head1 NAME
 
@@ -42,7 +42,7 @@ my $definition_description = $input->param('definition_description');
 my $area  = $input->param('area') || '';
 my $no_html = 0;    # this will be set if we dont want to print out an html::template
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
-    {   template_name   => "reports/dictionary.tmpl",
+    {   template_name   => "reports/dictionary.tt",
         query           => $input,
         type            => "intranet",
         authnotrequired => 0,
@@ -55,7 +55,7 @@ if ($phase eq 'View Dictionary'){
     # view the dictionary we use to set up abstract variables such as all borrowers over fifty who live in a certain town
     my $definitions = get_from_dictionary($area);
     $template->param(
-        'areas'=> areas(),
+        'areas'            => areas( $area ),
         'start_dictionary' => 1,
         'definitions'      => $definitions,
     );
@@ -70,7 +70,7 @@ elsif ( $phase eq 'New Term step 2' ) {
     # Choosing the area
     $template->param(
         'step_2'                 => 1,
-        'areas'                  => areas(),
+        'areas'                  => areas( $area ),
         'definition_name'        => $definition_name,
         'definition_description' => $definition_description,
     );
@@ -92,7 +92,7 @@ elsif ( $phase eq 'New Term step 3' ) {
 elsif ( $phase eq 'New Term step 4' ) {
 
     # Choosing the values
-    my @columns                = $input->param('columns');
+    my @columns                = $input->multi_param('columns');
     my $columnstring           = join( ',', @columns );
     my @column_loop;
     foreach my $column (@columns) {
@@ -124,14 +124,13 @@ elsif ( $phase eq 'New Term step 4' ) {
                'definition_description' => $definition_description,
                'columns' => \@column_loop,
                'columnstring' => $columnstring,
-                'DHTMLcalendar_dateformat' => C4::Dates->DHTMLcalendar(),
        );
 }
 
 elsif ( $phase eq 'New Term step 5' ) {
     # Confirmation screen
     my $columnstring           = $input->param('columnstring');
-    my @criteria               = $input->param('criteria_column');
+    my @criteria               = $input->multi_param('criteria_column');
     my $query_criteria;
     my @criteria_loop;
 
@@ -142,42 +141,45 @@ elsif ( $phase eq 'New Term step 5' ) {
             $tmp_hash{'name'}  = $crit;
             $tmp_hash{'value'} = $value;
             push @criteria_loop, \%tmp_hash;
-            if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
-                my $date = C4::Dates->new($value);
-                $value = $date->output("iso");
-            }
+            my $value_dt = eval { dt_from_string( $value ) };
+            $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
+                if ( $value_dt );
+
             $query_criteria .= " AND $crit='$value'";
         }
-        $value = $input->param( $crit . "_start_value" );
-        if ($value) {
-            my %tmp_hash;
-            $tmp_hash{'name'}  = "$crit Start";
-            $tmp_hash{'value'} = $value;
-            push @criteria_loop, \%tmp_hash;
-            if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
-                my $date = C4::Dates->new($value);
-                $value = $date->output("iso");
-            }
-            $query_criteria .= " AND $crit >= '$value'";
-        }
-        $value = $input->param( $crit . "_end_value" );
-        if ($value) {
-            my %tmp_hash;
-            $tmp_hash{'name'}  = "$crit End";
-            $tmp_hash{'value'} = $value;
-            push @criteria_loop, \%tmp_hash;
-            if ( $value =~ C4::Dates->regexp( C4::Context->preference('dateformat') ) ) {
-                my $date = C4::Dates->new($value);
-                $value = $date->output("iso");
+
+        if ( my $date_type_value = $input->param( $crit . "_date_type_value" ) ) {
+            if ( $date_type_value eq 'range' ) {
+                if ( $value = $input->param( $crit . "_start_value" ) ) {
+                    my %tmp_hash;
+                    $tmp_hash{'name'}  = "$crit Start";
+                    $tmp_hash{'value'} = $value;
+                    push @criteria_loop, \%tmp_hash;
+                    my $value_dt = eval { dt_from_string( $value ) };
+                    $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
+                        if ( $value_dt );
+
+                    $query_criteria .= " AND $crit >= '$value'";
+                }
+
+                if ( $value = $input->param( $crit . "_end_value" ) ) {
+                    my %tmp_hash;
+                    $tmp_hash{'name'}  = "$crit End";
+                    $tmp_hash{'value'} = $value;
+                    push @criteria_loop, \%tmp_hash;
+                    my $value_dt = eval { dt_from_string( $value ) };
+                    $value = output_pref( { dt => $value_dt, dateonly => 1, dateformat => 'iso' } )
+                        if ( $value_dt );
+
+                    $query_criteria .= " AND $crit <= '$value'";
+                }
             }
-            $query_criteria .= " AND $crit <= '$value'";
+            # else we want all dates
         }
     }
-    my %report_areas = map @$_, get_report_areas();
     $template->param(
         'step_5'                 => 1,
         'area'                   => $area,
-        'areaname'               => $report_areas{$area},
         'definition_name'        => $definition_name,
         'definition_description' => $definition_description,
         'query'                  => $query_criteria,
@@ -209,14 +211,17 @@ if (!$no_html){
 }
 
 sub areas {
+
+    my $selected = shift;
+
     my $areas = get_report_areas();
     my @a;
-    foreach (@$areas) {
+    foreach my $area ( @$areas ) {
         push @a, {
-            id => $_->[0],
-            name => $_->[1],
-            selected => ($_->[0] eq $area),
+            id       => $area,
+            selected => ( $area eq $selected )
         };
     }
+
     return \@a;
 }