Bug 15800: Koha::AuthorisedValues - Remove C4::Koha::IsAuthorisedValueCategory
[koha.git] / reports / guided_reports.pl
index cbdecda..200260d 100755 (executable)
@@ -29,10 +29,12 @@ use C4::Auth qw/:DEFAULT get_session/;
 use C4::Output;
 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::Context;
 use C4::Log;
 use Koha::DateUtils qw/dt_from_string output_pref/;
+use Koha::AuthorisedValue;
+use Koha::AuthorisedValues;
 
 =head1 NAME
 
@@ -47,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';
@@ -705,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
@@ -771,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;
@@ -816,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";
             }
@@ -823,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() } ;
                 }
@@ -948,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(),
                 );
@@ -997,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;
             }
         }
     }