From 603d427607917e7f1c0de24ca0814d9950aaf1fc 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 Signed-off-by: Orex Digital Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi --- 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 56edb03d8b..80aefa715f 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1001,39 +1001,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(); @@ -1097,6 +1086,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 f1633dedee..94deac1c28 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -883,28 +883,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.39.5