Bug 16154: CGI->multi_param - Declare a list
[koha.git] / reports / guided_reports.pl
index 0cee490..7af02bc 100755 (executable)
@@ -27,13 +27,14 @@ use File::Basename qw( dirname );
 use C4::Reports::Guided;
 use C4::Auth qw/:DEFAULT get_session/;
 use C4::Output;
-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::Koha qw/GetFrameworksLoop/;
+use C4::Branch;
 use C4::Context;
 use C4::Log;
 use Koha::DateUtils qw/dt_from_string output_pref/;
+use Koha::AuthorisedValue;
+use Koha::AuthorisedValues;
 
 =head1 NAME
 
@@ -48,7 +49,7 @@ Script to control the guided report creation
 my $input = new CGI;
 my $usecache = C4::Context->ismemcached;
 
-my $phase = $input->param('phase');
+my $phase = $input->param('phase') // '';
 my $flagsrequired;
 if ( $phase eq 'Build new' or $phase eq 'Delete Saved' ) {
     $flagsrequired = 'create_reports';
@@ -115,7 +116,7 @@ elsif ( $phase eq 'Build new' ) {
 }
 
 elsif ( $phase eq 'Delete Multiple') {
-    my @ids = $input->param('ids');
+    my @ids = $input->multi_param('ids');
     delete_report( @ids );
     print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
     exit;
@@ -234,6 +235,7 @@ elsif ( $phase eq 'Update SQL'){
                     subgroup => $subgroup,
                     notes => $notes,
                     public => $public,
+                    cache_expiry => $cache_expiry,
                 } );
             $template->param(
                 'save_successful'       => 1,
@@ -318,7 +320,7 @@ elsif ( $phase eq 'Choose these columns' ) {
     # next step is the constraints
     my $area    = $input->param('area');
     my $type    = $input->param('type');
-    my @columns = $input->param('columns');
+    my @columns = $input->multi_param('columns');
     my $column  = join( ',', @columns );
 
     $template->param(
@@ -343,9 +345,9 @@ elsif ( $phase eq 'Choose these criteria' ) {
     my $area     = $input->param('area');
     my $type     = $input->param('type');
     my $column   = $input->param('column');
-    my @definitions = $input->param('definition');
+    my @definitions = $input->multi_param('definition');
     my $definition = join (',',@definitions);
-    my @criteria = $input->param('criteria_column');
+    my @criteria = $input->multi_param('criteria_column');
     my $query_criteria;
     foreach my $crit (@criteria) {
         my $value = $input->param( $crit . "_value" );
@@ -357,9 +359,13 @@ elsif ( $phase eq 'Choose these criteria' ) {
             my $tovalue   = $input->param( "to_"   . $crit . "_value" );
 
             # If the range values are dates
-            if ($fromvalue =~ C4::Dates->regexp('syspref') && $tovalue =~ C4::Dates->regexp('syspref')) {
-                $fromvalue = C4::Dates->new($fromvalue)->output("iso");
-                $tovalue = C4::Dates->new($tovalue)->output("iso");
+            my $fromvalue_dt;
+            $fromvalue_dt = eval { dt_from_string( $fromvalue ); } if ( $fromvalue );
+            my $tovalue_dt;
+            $tovalue_dt = eval { dt_from_string( $tovalue ); } if ($tovalue);
+            if ( $fromvalue_dt && $tovalue_dt ) {
+                $fromvalue = output_pref( { dt => dt_from_string( $fromvalue_dt ), dateonly => 1, dateformat => 'iso' } );
+                $tovalue   = output_pref( { dt => dt_from_string( $tovalue_dt ), dateonly => 1, dateformat => 'iso' } );
             }
 
             if ($fromvalue && $tovalue) {
@@ -369,8 +375,10 @@ elsif ( $phase eq 'Choose these criteria' ) {
         } else {
 
             # If value is a date
-            if ($value =~ C4::Dates->regexp('syspref')) {
-                $value = C4::Dates->new($value)->output("iso");
+            my $value_dt;
+            $value_dt  =  eval { dt_from_string( $value ); } if ( $value );
+            if ( $value_dt ) {
+                $value = output_pref( { dt => dt_from_string( $value_dt ), dateonly => 1, dateformat => 'iso' } );
             }
             # don't escape runtime parameters, they'll be at runtime
             if ($value =~ /<<.*>>/) {
@@ -419,7 +427,7 @@ elsif ( $phase eq 'Choose these operations' ) {
     my $column   = $input->param('column');
     my $criteria = $input->param('criteria');
        my $definition = $input->param('definition');
-    my @total_by = $input->param('total_by');
+    my @total_by = $input->multi_param('total_by');
     my $totals;
     foreach my $total (@total_by) {
         my $value = $input->param( $total . "_tvalue" );
@@ -466,7 +474,7 @@ elsif ( $phase eq 'Build report' ) {
     my $query_criteria=$crit;
     # split the columns up by ,
     my @columns = split( ',', $column );
-    my @order_by = $input->param('order_by');
+    my @order_by = $input->multi_param('order_by');
 
     my $query_orderby;
     foreach my $order (@order_by) {
@@ -613,7 +621,7 @@ elsif ($phase eq 'Run this report'){
     my $limit      = $input->param('limit') || 20;
     my $offset     = 0;
     my $report_id  = $input->param('reports');
-    my @sql_params = $input->param('sql_params');
+    my @sql_params = $input->multi_param('sql_params');
     # offset algorithm
     if ($input->param('page')) {
         $offset = ($input->param('page') - 1) * $limit;
@@ -699,7 +707,7 @@ elsif ($phase eq 'Run this report'){
                         #---- "true" authorised value
                     }
                     else {
-                        if ( IsAuthorisedValueCategory($authorised_value) ) {
+                        if ( Koha::AuthorisedValues->search({ category => $authorised_value })->count ) {
                             my $query = '
                             SELECT authorised_value,lib
                             FROM authorised_values
@@ -765,7 +773,7 @@ elsif ($phase eq 'Run this report'){
             unless ($sth) {
                 die "execute_query failed to return sth for report $report_id: $sql";
             } else {
-                my $headers= header_cell_loop($sth);
+                my $headers = header_cell_loop($sth);
                 $template->param(header_row => $headers);
                 while (my $row = $sth->fetchrow_arrayref()) {
                     my @cells = map { +{ cell => $_ } } @$row;
@@ -810,6 +818,7 @@ elsif ($phase eq 'Export'){
         if ($format eq 'tab') {
             $type = 'application/octet-stream';
             $content .= join("\t", header_cell_values($sth)) . "\n";
+            $content = Encode::decode('UTF-8', $content);
             while (my $row = $sth->fetchrow_arrayref()) {
                 $content .= join("\t", @$row) . "\n";
             }
@@ -817,10 +826,10 @@ elsif ($phase eq 'Export'){
             my $delimiter = C4::Context->preference('delimiter') || ',';
             if ( $format eq 'csv' ) {
                 $type = 'application/csv';
-                my $csv = Text::CSV::Encoded->new({ encoding_out => 'utf8', sep_char => $delimiter});
+                my $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter});
                 $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag();
                 if ($csv->combine(header_cell_values($sth))) {
-                    $content .= $csv->string(). "\n";
+                    $content .= Encode::decode('UTF-8', $csv->string()) . "\n";
                 } else {
                     push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
                 }
@@ -942,12 +951,12 @@ sub header_cell_values {
 
 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
 sub header_cell_loop {
-    my @headers = map { +{ cell => $_ } } header_cell_values (shift);
+    my @headers = map { +{ cell => decode('UTF-8',$_) } } header_cell_values (shift);
     return \@headers;
 }
 
 foreach (1..6) {
-     $template->{VARS}->{'build' . $_} and $template->{VARS}->{'buildx' . $_} and last;
+     $template->{VARS}->{'build' . $_} and last;
 }
 $template->param(   'referer' => $input->referer(),
                 );
@@ -991,12 +1000,21 @@ sub create_non_existing_group_and_subgroup {
         my $report_groups = C4::Reports::Guided::get_report_groups;
         if (not exists $report_groups->{$group}) {
             my $groupdesc = $input->param('groupdesc') // $group;
-            C4::Koha::AddAuthorisedValue('REPORT_GROUP', $group, $groupdesc);
+            Koha::AuthorisedValue->new({
+                category => 'REPORT_GROUP',
+                authorised_value => $group,
+                lib => $groupdesc,
+            })->store;
         }
         if (defined $subgroup and $subgroup ne '') {
             if (not exists $report_groups->{$group}->{subgroups}->{$subgroup}) {
                 my $subgroupdesc = $input->param('subgroupdesc') // $subgroup;
-                C4::Koha::AddAuthorisedValue('REPORT_SUBGROUP', $subgroup, $subgroupdesc, $group);
+                Koha::AuthorisedValue->new({
+                    category => 'REPORT_SUBGROUP',
+                    authorised_value => $subgroup,
+                    lib => $subgroupdesc,
+                    lib_opac => $group,
+                })->store;
             }
         }
     }