From f9eddeaa0848efbd18e99cb969a9a07f904706f5 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 17 Jan 2022 17:50:26 +0100 Subject: [PATCH] Bug 29897: Move code to private method for reusability Sponsored-by: Orex Digital --- Koha/Biblio.pm | 111 +++++++++++++++++++++++++++-------- t/db_dependent/Koha/Biblio.t | 13 ++-- 2 files changed, 90 insertions(+), 34 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index c7cc2e24a91..ed835abe218 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -966,39 +966,28 @@ sub get_marc_notes { return \@marcnotes; } -=head3 get_marc_contributors - - my $contributors = $biblio->get_marc_contributors; - -Get all contributors (but first author) from the MARC record and returns them in an array. -They are stored in different fields depending on MARC flavour - -=cut - -sub get_marc_contributors { +sub _get_marc_authors { my ( $self, $params ) = @_; - my ( $mintag, $maxtag, $fields_filter ); - my $marcflavour = C4::Context->preference('marcflavour'); + my $fields_filter = $params->{fields_filter}; + my $mintag = $params->{mintag}; + my $maxtag = $params->{maxtag}; + + my $AuthoritySeparator = C4::Context->preference('AuthoritySeparator'); + my $marcflavour = C4::Context->preference('marcflavour'); # tagslib useful only for UNIMARC author responsibilities - my $tagslib; - if ( $marcflavour eq "UNIMARC" ) { - $tagslib = C4::Biblio::GetMarcStructure( 1, $self->frameworkcode, { unsafe => 1 }); - $mintag = "700"; - $maxtag = "712"; - $fields_filter = '7..'; - } else { # marc21/normarc - $mintag = "700"; - $maxtag = "720"; - $fields_filter = '7..'; - } + my $tagslib = $marcflavour eq "UNIMARC" + ? C4::Biblio::GetMarcStructure( 1, $self->frameworkcode, { unsafe => 1 } ) + : undef; my @marcauthors; - my $AuthoritySeparator = C4::Context->preference('AuthoritySeparator'); - foreach my $field ( $self->metadata->record->field($fields_filter) ) { - next unless $field->tag() >= $mintag && $field->tag() <= $maxtag; + + next + if $mintag && $field->tag() < $mintag + || $maxtag && $field->tag() > $maxtag; + my @subfields_loop; my @link_loop; my @subfields = $field->subfields(); @@ -1062,6 +1051,76 @@ sub get_marc_contributors { return \@marcauthors; } +=head3 get_marc_contributors + + my $contributors = $biblio->get_marc_contributors; + +Get all contributors (but first author) from the MARC record and returns them in an array. +They are stored in different fields depending on MARC flavour (700..712 for MARC21) + +=cut + +sub get_marc_contributors { + my ( $self, $params ) = @_; + + my ( $mintag, $maxtag, $fields_filter ); + my $marcflavour = C4::Context->preference('marcflavour'); + + if ( $marcflavour eq "UNIMARC" ) { + $mintag = "700"; + $maxtag = "712"; + $fields_filter = '7..'; + } else { # marc21/normarc + $mintag = "700"; + $maxtag = "720"; + $fields_filter = '7..'; + } + + return $self->_get_marc_authors( + { + fields_filter => $fields_filter, + mintag => $mintag, + maxtag => $maxtag + } + ); +} + +=head3 get_marc_authors + + my $authors = $biblio->get_marc_authors; + +Get all authors from the MARC record and returns them in an array. +They are stored in different fields depending on MARC flavour +(main author from 100 then secondary authors from 700..712). + +=cut + +sub get_marc_authors { + my ( $self, $params ) = @_; + + my ( $mintag, $maxtag, $fields_filter ); + my $marcflavour = C4::Context->preference('marcflavour'); + + if ( $marcflavour eq "UNIMARC" ) { + $fields_filter = '200'; + } else { # marc21/normarc + $fields_filter = '100'; + } + + my @first_authors = @{$self->_get_marc_authors( + { + fields_filter => $fields_filter, + mintag => $mintag, + maxtag => $maxtag + } + )}; + + my @other_authors = @{$self->get_marc_contributors}; + + return [@first_authors, @other_authors]; +} + + =head3 to_api my $json = $biblio->to_api; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 71512fddd10..3d58d7018bd 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -861,28 +861,25 @@ subtest 'current_checkouts() and old_checkouts() tests' => sub { subtest 'get_marc_contributors() tests' => sub { - plan tests => 1; + plan tests => 2; $schema->storage->txn_begin; - my $biblio = $builder->build_sample_biblio; + my $biblio = $builder->build_sample_biblio({ author => 'Main author' }); my $record = $biblio->metadata->record; # add author information my $field = MARC::Field->new('700','1','','a' => 'Jefferson, Thomas'); $record->append_fields($field); - $field = MARC::Field->new('700','1','','d' => '1743-1826'); - $record->append_fields($field); - $field = MARC::Field->new('700','1','','e' => 'former owner.'); - $record->append_fields($field); - $field = MARC::Field->new('700','1','','5' => 'MH'); + $field = MARC::Field->new('701','1','','d' => 'Secondary author 2'); $record->append_fields($field); # get record C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); $biblio = Koha::Biblios->find( $biblio->biblionumber ); - is( 4, @{$biblio->get_marc_contributors}, 'get_marc_contributors retrieves correct number of author subfields' ); + is( @{$biblio->get_marc_authors}, 3, 'get_marc_authors retrieves correct number of author subfields' ); + is( @{$biblio->get_marc_contributors}, 2, 'get_marc_contributors retrieves correct number of author subfields' ); $schema->storage->txn_rollback; }; -- 2.25.1