From 129cedf943c09d084ad6e09e8de60a87962094e6 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Fri, 25 Jul 2014 17:01:42 +0200 Subject: [PATCH] Bug 11331 - CSV export for viewlog.pl is missing newlines When you try to export the result of tools/viewlog.pl in csv, file cannot be correctly loaded : - newline is missing after each record, - strings should be enclosed in "" - columns are not the same as for screen output This patch corrects this by using like other export Text::CSV. Adds a header line made with the keys of first data. For that, all data values are initialiszed with empty string. Test plan : - Use a database with some logs, see sysprefs /cgi-bin/koha/admin/preferences.pl?tab=logs - Go to export page /cgi-bin/koha/tools/viewlog.pl - Select a module - Click on "To a file" and choose a file name - Click on "Submit" - Open file => Without this patch : newline is missing, multi-lines cells are not enclosed in "", there are no column headings => Without this patch : each line is a data line, complexe cells are enclosed in "", there are column headings - Test the export of all modules to see that all headings are necessary - Check the output to screen in the browser Signed-off-by: Owen Leonard The CSV export is significantly improved. I question the usefulness of including biblioitemnumber in the output. A better inclusion would be itemnumber. Signed-off-by: Katrin Fischer While this feature is still not perfect, this is a big improvement. Passes tests and QA script, restores basic functionality. Signed-off-by: Tomas Cohen Arazi --- C4/Log.pm | 1 - tools/viewlog.pl | 62 ++++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/C4/Log.pm b/C4/Log.pm index 671fcdb8a4..80c3493dfd 100644 --- a/C4/Log.pm +++ b/C4/Log.pm @@ -241,7 +241,6 @@ sub GetLogs { my @logs; while( my $row = $sth->fetchrow_hashref ) { - $row->{$row->{module}} = 1; push @logs , $row; } return \@logs; diff --git a/tools/viewlog.pl b/tools/viewlog.pl index 3fe7b0a05d..217530ed80 100755 --- a/tools/viewlog.pl +++ b/tools/viewlog.pl @@ -18,10 +18,11 @@ # with Koha; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -use strict; -#use warnings; FIXME - Bug 2505 +use Modern::Perl; + use C4::Auth; use CGI; +use Text::CSV::Encoded; use C4::Context; use C4::Koha; use C4::Dates; @@ -131,22 +132,31 @@ if ($do_it) { @data=@$results; my $total = scalar @data; foreach my $result (@data){ + # Init additional columns for CSV export + $result->{'biblionumber'} = q{}; + $result->{'biblioitemnumber'} = q{}; + $result->{'barcode'} = q{}; + $result->{'userfirstname'} = q{}; + $result->{'usersurname'} = q{}; + $result->{'borrowerfirstname'} = q{}; + $result->{'borrowersurname'} = q{}; + if (substr($result->{'info'}, 0, 4) eq 'item' || $result->{module} eq "CIRCULATION"){ # get item information so we can create a working link my $itemnumber=$result->{'object'}; $itemnumber=$result->{'info'} if ($result->{module} eq "CIRCULATION"); my $item=GetItem($itemnumber); - $result->{'biblionumber'}=$item->{'biblionumber'}; - $result->{'biblioitemnumber'}=$item->{'biblionumber'}; - $result->{'barcode'}=$item->{'barcode'}; + if ($item) { + $result->{'biblionumber'}=$item->{'biblionumber'}; + $result->{'biblioitemnumber'}=$item->{'biblionumber'}; + $result->{'barcode'}=$item->{'barcode'}; + } } #always add firstname and surname for librarian/user if ($result->{'user'}){ my $userdetails = C4::Members::GetMemberDetails($result->{'user'}); - if ($userdetails->{'firstname'}){ + if ($userdetails){ $result->{'userfirstname'} = $userdetails->{'firstname'}; - } - if ($userdetails->{'surname'}){ $result->{'usersurname'} = $userdetails->{'surname'}; } } @@ -154,10 +164,8 @@ if ($do_it) { if ($result->{module} eq "CIRCULATION" || $result->{module} eq "MEMBERS" || $result->{module} eq "FINES"){ if($result->{'object'}){ my $borrowerdetails = C4::Members::GetMemberDetails($result->{'object'}); - if ($borrowerdetails->{'firstname'}){ - $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'}; - } - if ($borrowerdetails->{'surname'}){ + if ($borrowerdetails){ + $result->{'borrowerfirstname'} = $borrowerdetails->{'firstname'}; $result->{'borrowersurname'} = $borrowerdetails->{'surname'}; } } @@ -187,18 +195,30 @@ if ($do_it) { output_html_with_http_headers $input, $cookie, $template->output; } else { # Printing to a csv file + my $content = q{}; + my $delimiter = C4::Context->preference('delimiter') || ','; + if (@data) { + my $csv = Text::CSV::Encoded->new( { encoding_out => 'utf8', sep_char => $delimiter } ); + $csv or die "Text::CSV::Encoded->new FAILED: " . Text::CSV::Encoded->error_diag(); + # First line with heading + # Exporting bd id seems useless + my @headings = grep { $_ ne 'action_id' } sort keys $data[0]; + if ( $csv->combine( @headings ) ) { + $content .= $csv->string() . "\n"; + } + # Lines of logs + foreach my $line (@data) { + my @cells = map { $line->{$_} } @headings; + if ( $csv->combine( @cells ) ) { + $content .= $csv->string() . "\n"; + } + } + } print $input->header( -type => 'text/csv', - -attachment => "$basename.csv", - -filename => "$basename.csv" + -attachment => $basename . '.csv', ); - my $sep = C4::Context->preference("delimiter"); - foreach my $line (@data) { - #next unless $modules[0] eq "catalogue"; - foreach (qw(timestamp firstname surname action info title author)) { - print $line->{$_} . $sep; - } - } + print $content; } exit; } else { -- 2.39.5