From c8fbb9c58bd72031b77d52327004ba441e71cdb9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 10 Aug 2016 11:36:40 +0100 Subject: [PATCH] Bug 17249: Remove GetKohaAuthorisedValuesFromField - Add search_by_marc_field This patch adds a new Koha::AuthorisedValues->search_by_marc_field method. It will permit to replace several subroutine from C4::Koha dealing with authorised values. It also uses this new methods to replace an occurrence of GetKohaAuthorisedValuesFromField in C4::Record::marcrecord2csv Test plan: prove t/db_dependent/AuthorisedValues.t should return green Signed-off-by: Owen Leonard Signed-off-by: Martin Renvoize Signed-off-by: Mason James --- C4/Koha.pm | 1 + C4/Record.pm | 11 +++++++--- Koha/AuthorisedValues.pm | 18 ++++++++++++++++ t/db_dependent/AuthorisedValues.t | 35 ++++++++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/C4/Koha.pm b/C4/Koha.pm index bb3581123d..66e17a5950 100644 --- a/C4/Koha.pm +++ b/C4/Koha.pm @@ -28,6 +28,7 @@ use C4::Branch; # Can be removed? use Koha::Cache; use Koha::DateUtils qw(dt_from_string); use Koha::Libraries; +use Koha::MarcSubfieldStructures; use DateTime::Format::MySQL; use Business::ISBN; use autouse 'Data::cselectall_arrayref' => qw(Dumper); diff --git a/C4/Record.pm b/C4/Record.pm index 440b48e924..d081ac782a 100644 --- a/C4/Record.pm +++ b/C4/Record.pm @@ -37,6 +37,7 @@ use Text::CSV::Encoded; #marc2csv use Koha::SimpleMARC qw(read_field); use Koha::XSLT_Handler; use Koha::CsvProfiles; +use Koha::AuthorisedValues; use Carp; use vars qw(@ISA @EXPORT); @@ -585,18 +586,22 @@ sub marcrecord2csv { # If it is a subfield my @loop_values; if ( $tag->{subfieldtag} ) { + my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, tagsubfield => $tag->{subfieldtag}, }); + $av = $av->count ? $av->unblessed : []; + my $av_description_mapping = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av }; # For each field foreach my $field (@fields) { my @subfields = $field->subfield( $tag->{subfieldtag} ); foreach my $subfield (@subfields) { - my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, $tag->{subfieldtag}, $frameworkcode, undef); - push @loop_values, (defined $authvalues->{$subfield}) ? $authvalues->{$subfield} : $subfield; + push @loop_values, (defined $av_description_mapping->{$subfield}) ? $av_description_mapping->{$subfield} : $subfield; } } # Or a field } else { - my $authvalues = GetKohaAuthorisedValuesFromField( $tag->{fieldtag}, undef, $frameworkcode, undef); + my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $frameworkcode, tagfield => $tag->{fieldtag}, }); + $av = $av->count ? $av->unblessed : []; + my $authvalues = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av }; foreach my $field ( @fields ) { my $value; diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index d0eea2a332..cbab8deb39 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -24,6 +24,7 @@ use Carp; use Koha::Database; use Koha::AuthorisedValue; +use Koha::MarcSubfieldStructures; use base qw(Koha::Objects); @@ -62,6 +63,23 @@ sub search { return $self->SUPER::search( { %$params, %$or, }, $join ); } +sub search_by_marc_field { + my ( $self, $params ) = @_; + my $frameworkcode = $params->{frameworkcode} || ''; + my $tagfield = $params->{tagfield}; + my $tagsubfield = $params->{tagsubfield}; + + return unless $tagfield or $tagsubfield; + + return $self->SUPER::search( + { 'marc_subfield_structures.frameworkcode' => $frameworkcode, + ( defined $tagfield ? ( 'marc_subfield_structures.tagfield' => $tagfield ) : () ), + ( defined $tagsubfield ? ( 'marc_subfield_structures.tagsubfield' => $tagsubfield ) : () ), + }, + { join => { category => 'marc_subfield_structures' } } + ); +} + sub categories { my ( $self ) = @_; my $rs = $self->_resultset->search( diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index a1cbfef459..a4c2a70d69 100644 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -1,11 +1,13 @@ #!/usr/bin/perl use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use C4::Context; use Koha::AuthorisedValue; use Koha::AuthorisedValues; +use Koha::AuthorisedValueCategories; +use Koha::MarcSubfieldStructures; my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; @@ -95,3 +97,34 @@ my @categories = Koha::AuthorisedValues->new->categories; is( @categories, 2, 'There should have 2 categories inserted' ); is( $categories[0], $av4->category, 'The first category should be correct (ordered by category name)' ); is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' ); + +subtest 'search_by_*_field' => sub { + plan tests => 1; + my $loc_cat = Koha::AuthorisedValueCategories->find('LOC'); + $loc_cat->delete if $loc_cat; + my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } ); + $mss->delete if $mss; + $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'd', frameworkcode => '' } ); + $mss->delete if $mss; + Koha::AuthorisedValueCategory->new( { category_name => 'LOC' } )->store; + Koha::AuthorisedValueCategory->new( { category_name => 'ANOTHER_4_TESTS' } )->store; + Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => '', authorised_value => 'LOC', kohafield => 'items.location' } )->store; + Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'c', frameworkcode => 'ACQ', authorised_value => 'LOC', kohafield => 'items.location' } )->store; + Koha::MarcSubfieldStructure->new( { tagfield => 952, tagsubfield => 'd', frameworkcode => '', authorised_value => 'ANOTHER_4_TESTS', kohafield => 'items.another_field' } )->store; + Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_1' } )->store; + Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_2' } )->store; + Koha::AuthorisedValue->new( { category => 'LOC', authorised_value => 'location_3' } )->store; + Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'an_av' } )->store; + Koha::AuthorisedValue->new( { category => 'ANOTHER_4_TESTS', authorised_value => 'another_av' } )->store; + subtest 'search_by_marc_field' => sub { + plan tests => 4; + my $avs; + $avs = Koha::AuthorisedValues->search_by_marc_field(); + is ( $avs, undef ); + $avs = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => '' }); + is ( $avs, undef ); + $avs = Koha::AuthorisedValues->search_by_marc_field({ tagfield => 952, tagsubfield => 'c'}); + is( $avs->count, 3, 'default fk'); + is( $avs->next->authorised_value, 'location_1', ); + }; +}; -- 2.39.5