Lines 21-26
Link Here
|
21 |
use CGI qw/-utf8/; |
21 |
use CGI qw/-utf8/; |
22 |
use Text::CSV; |
22 |
use Text::CSV; |
23 |
use URI::Escape; |
23 |
use URI::Escape; |
|
|
24 |
use File::Temp; |
24 |
use C4::Reports::Guided; |
25 |
use C4::Reports::Guided; |
25 |
use C4::Auth qw/:DEFAULT get_session/; |
26 |
use C4::Auth qw/:DEFAULT get_session/; |
26 |
use C4::Output; |
27 |
use C4::Output; |
Lines 761-766
elsif ($phase eq 'Run this report'){
Link Here
|
761 |
else { |
762 |
else { |
762 |
push @errors, { no_sql_for_id => $report_id }; |
763 |
push @errors, { no_sql_for_id => $report_id }; |
763 |
} |
764 |
} |
|
|
765 |
my $csv2ods_cmd = qx|which csv2ods|; |
766 |
chomp $csv2ods_cmd; |
767 |
my $csv2ods_is_installed = |
768 |
( not $csv2ods_cmd or not -f $csv2ods_cmd ) |
769 |
? 0 |
770 |
: 1; |
771 |
$template->param( csv2ods_is_installed => $csv2ods_is_installed ); |
764 |
} |
772 |
} |
765 |
|
773 |
|
766 |
elsif ($phase eq 'Export'){ |
774 |
elsif ($phase eq 'Export'){ |
Lines 771-800
elsif ($phase eq 'Export'){
Link Here
|
771 |
my $format = $input->param('format'); |
779 |
my $format = $input->param('format'); |
772 |
my ($sth, $q_errors) = execute_query($sql); |
780 |
my ($sth, $q_errors) = execute_query($sql); |
773 |
unless ($q_errors and @$q_errors) { |
781 |
unless ($q_errors and @$q_errors) { |
774 |
print $input->header( -type => 'application/octet-stream', |
782 |
my ( $type, $content ); |
775 |
-attachment=>"reportresults.$format" |
|
|
776 |
); |
777 |
if ($format eq 'tab') { |
783 |
if ($format eq 'tab') { |
778 |
print join("\t", header_cell_values($sth)), "\n"; |
784 |
$type = 'application/octet-stream'; |
|
|
785 |
$content .= join("\t", header_cell_values($sth)) . "\n"; |
779 |
while (my $row = $sth->fetchrow_arrayref()) { |
786 |
while (my $row = $sth->fetchrow_arrayref()) { |
780 |
print join("\t", @$row), "\n"; |
787 |
$content .= join("\t", @$row) . "\n"; |
781 |
} |
788 |
} |
782 |
} else { |
789 |
} else { |
783 |
my $csv = Text::CSV->new({binary => 1}); |
790 |
my $delimiter = C4::Context->preference('delimiter') || ','; |
|
|
791 |
my $csv_content = q||; |
792 |
my $csv = Text::CSV->new({binary => 1, sep_char => $delimiter}); |
784 |
$csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag(); |
793 |
$csv or die "Text::CSV->new({binary => 1}) FAILED: " . Text::CSV->error_diag(); |
785 |
if ($csv->combine(header_cell_values($sth))) { |
794 |
if ($csv->combine(header_cell_values($sth))) { |
786 |
print $csv->string(), "\n"; |
795 |
$csv_content .= $csv->string(). "\n"; |
787 |
} else { |
796 |
} else { |
788 |
push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ; |
797 |
push @$q_errors, { combine => 'HEADER ROW: ' . $csv->error_diag() } ; |
789 |
} |
798 |
} |
790 |
while (my $row = $sth->fetchrow_arrayref()) { |
799 |
while (my $row = $sth->fetchrow_arrayref()) { |
791 |
if ($csv->combine(@$row)) { |
800 |
if ($csv->combine(@$row)) { |
792 |
print $csv->string(), "\n"; |
801 |
$csv_content .= $csv->string() . "\n"; |
793 |
} else { |
802 |
} else { |
794 |
push @$q_errors, { combine => $csv->error_diag() } ; |
803 |
push @$q_errors, { combine => $csv->error_diag() } ; |
795 |
} |
804 |
} |
796 |
} |
805 |
} |
|
|
806 |
if ( $format eq 'csv' ) { |
807 |
$type = 'application/csv'; |
808 |
$content = $csv_content; |
809 |
} |
810 |
elsif ( $format eq 'ods' ) { |
811 |
$type = 'application/vnd.oasis.opendocument.spreadsheet'; |
812 |
my $csv_fh = File::Temp->new(); |
813 |
my $ods_fh = File::Temp->new(); |
814 |
print $csv_fh $csv_content; |
815 |
my $csv_filepath = $csv_fh->filename; |
816 |
my $ods_filepath = $ods_fh->filename; |
817 |
|
818 |
my $csv2ods_cmd = qx|which csv2ods|; |
819 |
chomp $csv2ods_cmd; |
820 |
qx| |
821 |
$csv2ods_cmd -i $csv_filepath -o $ods_filepath -d '$delimiter' |
822 |
|; |
823 |
|
824 |
binmode(STDOUT); |
825 |
$content .= $_ while <$ods_fh>; |
826 |
} |
797 |
} |
827 |
} |
|
|
828 |
print $input->header( |
829 |
-type => $type, |
830 |
-attachment=>"reportresults.$format" |
831 |
); |
832 |
print $content; |
833 |
|
798 |
foreach my $err (@$q_errors, @errors) { |
834 |
foreach my $err (@$q_errors, @errors) { |
799 |
print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n"; |
835 |
print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n"; |
800 |
} # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing. |
836 |
} # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing. |
801 |
- |
|
|