use MARC::File::USMARC;
use C4::AuthoritiesMarc;
-use C4::Biblio;
+use C4::Biblio qw( GetMarcFromKohaField );
use C4::Record;
+use Koha::Biblios;
+use Koha::CsvProfiles;
+use Koha::Logger;
+use List::Util qw( all any );
sub _get_record_for_export {
my ($params) = @_;
my $record_type = $params->{record_type};
my $record_id = $params->{record_id};
+ my $conditions = $params->{record_conditions};
my $dont_export_fields = $params->{dont_export_fields};
my $clean = $params->{clean};
} elsif ( $record_type eq 'bibs' ) {
$record = _get_biblio_for_export( { %$params, biblionumber => $record_id } );
} else {
-
- # TODO log "record_type not supported"
+ Koha::Logger->get->warn( "Record_type $record_type not supported." );
+ }
+ if ( !$record ) {
+ Koha::Logger->get->warn( "Record $record_id could not be exported." );
return;
}
+ # If multiple conditions all are required to match (and)
+ # For matching against multiple marc targets all are also required to match
+ my %operators = (
+ '=' => sub {
+ return $_[0] eq $_[1];
+ },
+ '!=' => sub {
+ return $_[0] ne $_[1];
+ },
+ '>' => sub {
+ return $_[0] gt $_[1];
+ },
+ '<' => sub {
+ return $_[0] lt $_[1];
+ },
+ );
+ if ($conditions) {
+ foreach my $condition (@{$conditions}) {
+ my ($field_tag, $subfield, $operator, $match_value) = @{$condition};
+ my @fields = $record->field($field_tag);
+ my $no_target = 0;
+
+ if (!@fields) {
+ $no_target = 1;
+ }
+ else {
+ if ($operator eq '?') {
+ return unless any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
+ } elsif ($operator eq '!?') {
+ return if any { $subfield ? $_->subfield($subfield) : $_->data() } @fields;
+ } else {
+ my $op;
+ if (exists $operators{$operator}) {
+ $op = $operators{$operator};
+ } else {
+ die("Invalid operator: $op");
+ }
+ my @target_values = map { $subfield ? $_->subfield($subfield) : ($_->data()) } @fields;
+ if (!@target_values) {
+ $no_target = 1;
+ }
+ else {
+ return unless all { $op->($_, $match_value) } @target_values;
+ }
+ }
+ }
+ return if $no_target && $operator ne '!=';
+ }
+ }
+
if ($dont_export_fields) {
for my $f ( split / /, $dont_export_fields ) {
if ( $f =~ m/^(\d{3})(.)?$/ ) {
sub _get_authority_for_export {
my ($params) = @_;
my $authid = $params->{authid} || return;
- my $authority = Koha::Authority->get_from_authid($authid);
+ my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
return unless $authority;
return $authority->record;
}
my $biblionumber = $params->{biblionumber};
my $itemnumbers = $params->{itemnumbers};
my $export_items = $params->{export_items} // 1;
- my $only_export_items_for_branch = $params->{only_export_items_for_branch};
+ my $only_export_items_for_branches = $params->{only_export_items_for_branches};
- my $record = eval { C4::Biblio::GetMarcBiblio($biblionumber); };
+ my $biblio = Koha::Biblios->find($biblionumber);
+ my $record = eval { $biblio->metadata->record };
return if $@ or not defined $record;
if ($export_items) {
- C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, $itemnumbers );
- if ($only_export_items_for_branch) {
- my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch', '' ); # Should be GetFrameworkCode( $biblionumber )?
+ C4::Biblio::EmbedItemsInMarcBiblio({
+ marc_record => $record,
+ biblionumber => $biblionumber,
+ item_numbers => $itemnumbers });
+ if ($only_export_items_for_branches && @$only_export_items_for_branches) {
+ my %export_items_for_branches = map { $_ => 1 } @$only_export_items_for_branches;
+ my ( $homebranchfield, $homebranchsubfield ) = GetMarcFromKohaField( 'items.homebranch' );
for my $itemfield ( $record->field($homebranchfield) ) {
my $homebranch = $itemfield->subfield($homebranchsubfield);
- if ( $only_export_items_for_branch ne $homebranch ) {
+ unless ( $export_items_for_branches{$homebranch} ) {
$record->delete_field($itemfield);
}
}
my $csv_profile_id = $params->{csv_profile_id};
my $output_filepath = $params->{output_filepath};
- return unless $record_type;
+ if( !$record_type ) {
+ Koha::Logger->get->warn( "No record_type given." );
+ return;
+ }
return unless @$record_ids;
my $fh;
if ( $format eq 'iso2709' ) {
for my $record_id (@$record_ids) {
my $record = _get_record_for_export( { %$params, record_id => $record_id } );
+ next unless $record;
my $errorcount_on_decode = eval { scalar( MARC::File::USMARC->decode( $record->as_usmarc )->warnings() ) };
if ( $errorcount_on_decode or $@ ) {
- warn $@ if $@;
- warn "record (number $record_id) is invalid and therefore not exported because its reopening generates warnings above";
+ my $msg = "Record $record_id could not be exported. " .
+ ( $@ // '' );
+ chomp $msg;
+ Koha::Logger->get->info( $msg );
next;
}
print $record->as_usmarc();
print "\n";
for my $record_id (@$record_ids) {
my $record = _get_record_for_export( { %$params, record_id => $record_id } );
+ next unless $record;
print MARC::File::XML::record($record);
print "\n";
}
print MARC::File::XML::footer();
print "\n";
} elsif ( $format eq 'csv' ) {
- $csv_profile_id ||= C4::Csv::GetCsvProfileId( C4::Context->preference('ExportWithCsvProfile') );
+ die 'There is no valid csv profile defined for this export'
+ unless Koha::CsvProfiles->find( $csv_profile_id );
print marc2csv( $record_ids, $csv_profile_id, $itemnumbers );
}
=item csv_profile_id
- If the format is csv, a csv_profile_id can be provide to overwrite the default value (syspref ExportWithCsvProfile).
+ If the format is csv, you have to define a csv_profile_id.
=cut