From 51d9d038bffeb50c5e02ed155955c49a2461de28 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 4 Feb 2014 13:26:54 +0100 Subject: [PATCH] Bug 11679: Add ods as a format for reports This patch adds the ability to export the data generated by a report into a ods file. Test plan: Install csv2ods (cf commit message from bug 11603 for the install instructions) and verify you are able to generate an ods file from a report result. --- .../en/modules/reports/guided_reports_start.tt | 3 ++ reports/guided_reports.pl | 36 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt index 2ac91d6..0fac4f4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/guided_reports_start.tt @@ -820,6 +820,9 @@ canned reports and writing custom SQL reports.

diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl index b9c49ad..45796f2 100755 --- a/reports/guided_reports.pl +++ b/reports/guided_reports.pl @@ -21,6 +21,7 @@ use CGI qw/-utf8/; use Text::CSV; use URI::Escape; +use File::Temp; use C4::Reports::Guided; use C4::Auth qw/:DEFAULT get_session/; use C4::Output; @@ -761,6 +762,13 @@ elsif ($phase eq 'Run this report'){ else { push @errors, { no_sql_for_id => $report_id }; } + my $csv2ods_cmd = qx|which csv2ods|; + chomp $csv2ods_cmd; + my $csv2ods_is_installed = + ( not $csv2ods_cmd or not -f $csv2ods_cmd ) + ? 0 + : 1; + $template->param( csv2ods_is_installed => $csv2ods_is_installed ); } elsif ($phase eq 'Export'){ @@ -780,20 +788,42 @@ elsif ($phase eq 'Export'){ print join("\t", @$row), "\n"; } } else { - my $csv = Text::CSV->new({binary => 1}); + 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(); if ($csv->combine(header_cell_values($sth))) { - print $csv->string(), "\n"; + $csv_content .= $csv->string(). "\n"; } else { push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ; } while (my $row = $sth->fetchrow_arrayref()) { if ($csv->combine(@$row)) { - print $csv->string(), "\n"; + $csv_content .= $csv->string(). "\n"; } else { push @$q_errors, { combine => $csv->error_diag() } ; } } + if ( $format eq 'csv' ) { + print $csv_content; + } + elsif ( $format eq 'ods' ) { + my $csv_fh = File::Temp->new(); + my $ods_fh = File::Temp->new(); + print $csv_fh $csv_content; + my $csv_filepath = $csv_fh->filename; + my $ods_filepath = $ods_fh->filename; + + my $csv2ods_cmd = qx|which csv2ods|; + chomp $csv2ods_cmd; + qx| + $csv2ods_cmd -i $csv_filepath -o $ods_filepath -d '$delimiter' + |; + qx|cp $ods_filepath /tmp/pouet.ods|; + qx|cp $csv_filepath /tmp/pouet.csv|; + print $_ while <$ods_fh>; + close $csv_fh; + } } foreach my $err (@$q_errors, @errors) { print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n"; -- 1.7.10.4