From d74f94e99244519b127e511d71fbc4941f603a1b Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 22 Dec 2020 09:13:02 +0000 Subject: [PATCH] Bug 26791: Use Mojolicious record exporter when using ExportCircHistory This patch uses the Mojolicious record exporter to export records from the Circulation module. Test plan: 1. Change system preference "ExportCircHistory" to "Show" 2. Checkout items 39999000004571 and 39999000001310 to the "koha" borrower 3. Select checkouts from list and check the "Export" checkboxes 4. Click "Export" at the bottom of the page 5. Note that valid ISO 2709 MARC is generated in the export --- Koha/Exporter/Record.pm | 54 +++++++++++++++++++ Koha/Staff/Controller/Tools/Export.pm | 60 ++++++++++++++++++++-- Koha/Template.pm | 1 + .../prog/en/includes/checkouts-table.inc | 3 +- 4 files changed, 111 insertions(+), 7 deletions(-) diff --git a/Koha/Exporter/Record.pm b/Koha/Exporter/Record.pm index eaebe57639..5ade762462 100644 --- a/Koha/Exporter/Record.pm +++ b/Koha/Exporter/Record.pm @@ -11,6 +11,60 @@ use Koha::CsvProfiles; use Koha::Logger; use List::Util qw(all any); +sub new { + my ($class, $args) = @_; + my $self = {}; + $self->{record_ids} = delete $args->{record_ids}; + $self->{params} = $args // {}; + return bless ($self, $class); +} + +sub has_next { + my ($self) = @_; + my $has_next; + if ( $self->{record_ids} && scalar @{ $self->{record_ids} } > 0 ){ + $has_next = 1; + } + return $has_next; +} + +sub next { + my ($self) = @_; + my $output; + my $record_ids = $self->{record_ids}; + my $record_id = pop @$record_ids; + if ($record_id){ + my $record = _get_record_for_export({ + %{ $self->{params} }, + record_id => $record_id, + }); + if ($record){ + my $format =$self->{params}->{format}; + if ($format){ + if ($format eq 'iso2709'){ + my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) }; + if ( $errorcount_on_decode or $@ ) { + my $msg = "Record $record_id could not be exported. " . + ( $@ // '' ); + chomp $msg; + Koha::Logger->get->info( $msg ); + return; + } + $output = $record->as_usmarc(); + + } + elsif ($format eq 'xml'){ + $output = MARC::File::XML::record($record) . "\n"; + } + elsif ($format eq 'csv'){ + #FIXME + } + } + } + } + return $output; +} + sub _get_record_for_export { my ($params) = @_; my $record_type = $params->{record_type}; diff --git a/Koha/Staff/Controller/Tools/Export.pm b/Koha/Staff/Controller/Tools/Export.pm index f5ae047f05..08a0dcff45 100644 --- a/Koha/Staff/Controller/Tools/Export.pm +++ b/Koha/Staff/Controller/Tools/Export.pm @@ -6,6 +6,7 @@ use Koha::Exporter::Record; use C4::Biblio qw//; use Encode; use Carp; +use List::MoreUtils qw(uniq); use Koha::ItemTypes; use Koha::Authority::Types; @@ -77,8 +78,6 @@ sub _getbackupfilelist { } closedir($dir); } - use Data::Dumper; - warn Dumper(\@files); return \@files; } @@ -88,14 +87,65 @@ sub download { my ($flags,$loggedinuser) = $c->staff_authorize({ flagsrequired => { 'tools' => 'export_catalog' } }); my $record_type = $c->param('record_type') || 'bibs'; #FIXME: Remove bibs? - my $backupdir = C4::Context->config('backupdir'); - if ( $record_type eq 'bibs' or $record_type eq 'auths' ){ + my $output_format = $c->param('output_format') || 'iso2709'; + my $filename = $c->param('filename') || ( $output_format eq 'csv' ? 'koha.csv' : 'koha.mrc' ); + my $csv_profile_id = $c->param('csv_profile_id'); + my $export_remove_fields = $c->param('export_remove_fields') || q(); + my $dont_export_items = $c->param('dont_export_item') || 0; #FIXME: Shouldn't this be bibs specific? + my $itemnumbers = $c->every_param('itemnumbers'); #FIXME: Shouldn't this be bibs specific? + + my $branch = $c->every_param('branch'); + my $strip_items_not_from_libraries = $c->param('strip_items_not_from_libraries'); + my $only_export_items_for_branches = $strip_items_not_from_libraries ? $branch : undef; + + my $record_ids = (); + if ($record_type eq 'bibs'){ + my $biblionumbers = $c->every_param('biblionumbers'); + + $record_ids = $biblionumbers; + } + + #FIXME: Move this to module + #NOTE: Ensure record IDs are unique + #@record_ids = uniq @record_ids; + + #FIXME: Add "id_list_file" filter... + + my $content_type = 'application/octet-stream'; + $c->res->headers->content_type($content_type); + $c->res->headers->content_disposition("attachment; filename=$filename;"); + + my $exporter = Koha::Exporter::Record->new({ + record_type => $record_type, + record_ids => $record_ids, + format => $output_format, + filename => $filename, + itemnumbers => $itemnumbers, + dont_export_fields => $export_remove_fields, + csv_profile_id => $csv_profile_id, + export_items => (not $dont_export_items), + only_export_items_for_branches => $only_export_items_for_branches, + }); + my $drain; + $drain = sub { + my $c = shift; + if ( $exporter->has_next() ){ + my $record = $exporter->next; + $c->write($record,$drain); + } + else { + $c->write(''); + } + }; + $c->$drain; + #TODO: Refactor code from export.pl - $c->render( text => 'TODO: I need to rewrite the handler for this but it is coming.' ); + #$c->render( text => 'TODO: I need to rewrite the handler for this but it is coming.' ); } elsif ( $record_type eq 'db' or $record_type eq 'conf' ){ #TODO: Most of this code should go in a model module + my $backupdir = C4::Context->config('backupdir'); if ( $flags->{superlibrarian} and ( $record_type eq 'db' and C4::Context->config('backup_db_via_tools') diff --git a/Koha/Template.pm b/Koha/Template.pm index fa0b629369..5ae1dbdac6 100644 --- a/Koha/Template.pm +++ b/Koha/Template.pm @@ -14,6 +14,7 @@ sub prepare_template { my $template_filename = $args->{template_filename}; my $interface = $args->{interface}; if ($template_filename && $interface){ + #FIXME: See Bug 27293 about refactoring C4::Templates::gettemplate for providing the "language" param and "KohaOpacLanguage" cookie $template = C4::Templates::gettemplate($template_filename,$interface); if ($template){ if ($session){ diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/checkouts-table.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/checkouts-table.inc index 38a4700416..f562cf19a5 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/checkouts-table.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/checkouts-table.inc @@ -6,7 +6,7 @@ Show checkouts

-
+ @@ -77,7 +77,6 @@ [% END %] - -- 2.11.0