[Koha-patches] [PATCH] restored tab-delimited report download

Galen Charlton galen.charlton at liblime.com
Sun Apr 5 18:32:12 CEST 2009


Previous patch broke this, always providing the
guided report download in CSV format regardless of
whether CSV or tab-delimited was requested.

Also, default name of downloaded file is reportresults.csv
for CSV output and reportresults.tab for tab-delimited.

Also removed a couple subroutine prototypes that
were leading to unnecessary compile-time warnings
under use warnings.
---
 reports/guided_reports.pl |   35 +++++++++++++++++++++--------------
 1 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/reports/guided_reports.pl b/reports/guided_reports.pl
index 037029c..c97d4aa 100755
--- a/reports/guided_reports.pl
+++ b/reports/guided_reports.pl
@@ -347,26 +347,33 @@ elsif ($phase eq 'Run this report'){
 elsif ($phase eq 'Export'){
     binmode STDOUT, ':utf8';
 
-	# export results to tab separated text
+	# 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!
     my $format = $input->param('format');
 	my ($sth, $q_errors) = execute_query($sql);
     unless ($q_errors and @$q_errors) {
         print $input->header(       -type => 'application/octet-stream',
-                                    -attachment=>'reportresults.csv'
+                                    -attachment=>"reportresults.$format"
                             );
-        my $csv = Text::CSV->new({binary => 1});
-        $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
-        if ($csv->combine(header_cell_values($sth))) {
-            print $csv->string(), "\n";
+        if ($format eq 'tab') {
+            print join("\t", header_cell_values($sth)), "\n";
+            while (my $row = $sth->fetchrow_arrayref()) {
+                print join("\t", @$row), "\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"; 
+            my $csv = Text::CSV->new({binary => 1});
+            $csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag();
+            if ($csv->combine(header_cell_values($sth))) {
+                print $csv->string(), "\n";
             } else {
-                push @$q_errors, { combine => $csv->error_diag() } ;
+                push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ;
+            }
+            while (my $row = $sth->fetchrow_arrayref()) {
+                if ($csv->combine(@$row)) {
+                    print $csv->string(), "\n"; 
+                } else {
+                    push @$q_errors, { combine => $csv->error_diag() } ;
+                }
             }
         }
         foreach my $err (@$q_errors, @errors) {
@@ -412,13 +419,13 @@ elsif ($phase eq 'Save Compound'){
 }
 
 # pass $sth, get back an array of names for the column headers
-sub header_cell_values ($) {
+sub header_cell_values {
     my $sth = shift or return ();
     return @{$sth->{NAME}};
 }
 
 # pass $sth, get back a TMPL_LOOP-able set of names for the column headers
-sub header_cell_loop ($) {
+sub header_cell_loop {
     my @headers = map { +{ cell => $_ } } header_cell_values (shift);
     return \@headers;
 }
-- 
1.5.6.5




More information about the Koha-patches mailing list