From 8f6492b3de8abde1700123fa15bd14189995a040 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Wed, 26 Feb 2014 13:15:54 +0100
Subject: [PATCH] Bug 11679: FIX encoding in CSV file

Previously, the CSV file was not in encoded in utf8 and the csv2ods
command failed with the following error if diacritics existed:
  UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9
---
 reports/guided_reports.pl |    7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl
index 45796f2..1eb12b2 100755
--- a/reports/guided_reports.pl
+++ b/reports/guided_reports.pl
@@ -19,7 +19,7 @@
 
 
 use CGI qw/-utf8/;
-use Text::CSV;
+use Text::CSV::Encoded;
 use URI::Escape;
 use File::Temp;
 use C4::Reports::Guided;
@@ -772,7 +772,6 @@ elsif ($phase eq 'Run this report'){
 }
 
 elsif ($phase eq 'Export'){
-    binmode STDOUT, ':encoding(UTF-8)';
 
 	# export results to tab separated text or CSV
 	my $sql    = $input->param('sql');  # FIXME: use sql from saved report ID#, not new user-supplied SQL!
@@ -790,8 +789,8 @@ elsif ($phase eq 'Export'){
         } else {
             my $delimiter = C4::Context->preference('delimiter') || ',';
             my $csv_content = q||;
-            my $csv = Text::CSV->new({binary => 1, sep_char => $delimiter});
-            $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
+            my $csv = Text::CSV::Encoded->new({ encoding_out => 'utf8', 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))) {
                 $csv_content .= $csv->string(). "\n";
             } else {
-- 
1.7.10.4