# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
+use Modern::Perl;
use CGI qw/-utf8/;
-use Text::CSV;
+use Text::CSV::Encoded;
use URI::Escape;
+use File::Temp;
+use File::Basename qw( dirname );
use C4::Reports::Guided;
use C4::Auth qw/:DEFAULT get_session/;
use C4::Output;
'savedreports' => get_saved_reports($filter),
'usecache' => $usecache,
'groups_with_subgroups'=> groups_with_subgroups($group, $subgroup),
- dateformat => C4::Context->preference('dateformat'),
);
}
+elsif ( $phase eq 'Delete Multiple') {
+ my @ids = $input->param('ids');
+ delete_report( @ids );
+ print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
+ exit;
+}
+
elsif ( $phase eq 'Delete Saved') {
# delete a report from the saved reports list
- my $id = $input->param('reports');
- delete_report($id);
+ my $ids = $input->param('reports');
+ delete_report($ids);
print $input->redirect("/cgi-bin/koha/reports/guided_reports.pl?phase=Use%20saved");
exit;
}
my $totpages = int($total/$limit) + (($total % $limit) > 0 ? 1 : 0);
my $url = "/cgi-bin/koha/reports/guided_reports.pl?reports=$report_id&phase=Run%20this%20report&limit=$limit";
if (@sql_params) {
- $url = join('&sql_params=', $url, map { URI::Escape::uri_escape_utf8($_) } @sql_params);
+ $url = join('&sql_params=', $url, map { URI::Escape::uri_escape($_) } @sql_params);
}
$template->param(
'results' => \@rows,
'execute' => 1,
'name' => $name,
'notes' => $notes,
- 'errors' => $errors,
+ 'errors' => defined($errors) ? [ $errors ] : undef,
'pagination_bar' => pagination_bar($url, $totpages, $input->param('page')),
'unlimited_total' => $total,
+ 'sql_params' => \@sql_params,
);
}
}
}
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!
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.$format"
- );
+ my ( $type, $content );
if ($format eq 'tab') {
- print join("\t", header_cell_values($sth)), "\n";
+ $type = 'application/octet-stream';
+ $content .= join("\t", header_cell_values($sth)) . "\n";
while (my $row = $sth->fetchrow_arrayref()) {
- print join("\t", @$row), "\n";
+ $content .= join("\t", @$row) . "\n";
}
} else {
- 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 => 'HEADER ROW: ' . $csv->error_diag() } ;
- }
- while (my $row = $sth->fetchrow_arrayref()) {
- if ($csv->combine(@$row)) {
- print $csv->string(), "\n";
+ 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});
+ $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";
} 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)) {
+ $content .= $csv->string() . "\n";
+ } else {
+ push @$q_errors, { combine => $csv->error_diag() } ;
+ }
}
}
+ elsif ( $format eq 'ods' ) {
+ $type = 'application/vnd.oasis.opendocument.spreadsheet';
+ my $ods_fh = File::Temp->new( UNLINK => 0 );
+ my $ods_filepath = $ods_fh->filename;
+
+ use OpenOffice::OODoc;
+ my $tmpdir = dirname $ods_filepath;
+ odfWorkingDirectory( $tmpdir );
+ my $container = odfContainer( $ods_filepath, create => 'spreadsheet' );
+ my $doc = odfDocument (
+ container => $container,
+ part => 'content'
+ );
+ my $table = $doc->getTable(0);
+ my @headers = header_cell_values( $sth );
+ my $rows = $sth->fetchall_arrayref();
+ my ( $nb_rows, $nb_cols ) = ( scalar(@$rows), scalar(@{$rows->[0]}) );
+ $doc->expandTable( $table, $nb_rows, $nb_cols );
+
+ my $row = $doc->getRow( $table, 0 );
+ my $j = 0;
+ for my $header ( @headers ) {
+ $doc->cellValue( $row, $j, $header );
+ $j++;
+ }
+ for ( my $i = 1; $i < $nb_rows +1 ; $i++ ) {
+ $row = $doc->getRow( $table, $i );
+ for ( my $j = 0 ; $j < $nb_cols ; $j++ ) {
+ # FIXME Bug 11944
+ my $value = Encode::encode( 'UTF8', $rows->[$i - 1][$j] );
+ $doc->cellValue( $row, $j, $value );
+ }
+ }
+ $doc->save();
+ binmode(STDOUT);
+ open $ods_fh, '<', $ods_filepath;
+ $content .= $_ while <$ods_fh>;
+ unlink $ods_filepath;
+ }
}
+ print $input->header(
+ -type => $type,
+ -attachment=>"reportresults.$format"
+ );
+ print $content;
+
foreach my $err (@$q_errors, @errors) {
print "# ERROR: " . (map {$_ . ": " . $err->{$_}} keys %$err) . "\n";
} # here we print all the non-fatal errors at the end. Not super smooth, but better than nothing.
my $sth = shift or return ();
my @cols;
foreach my $c (@{$sth->{NAME}}) {
+ # TODO in Bug 11944
#FIXME apparently DBI still needs a utf8 fix for this?
utf8::decode($c);
push @cols, $c;