Browse Source

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 <oleonard@myacpl.org>

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
16.11.x
Jonathan Druart 8 years ago
committed by Kyle M Hall
parent
commit
1626087562
  1. 1
      C4/Koha.pm
  2. 11
      C4/Record.pm
  3. 18
      Koha/AuthorisedValues.pm
  4. 34
      t/db_dependent/AuthorisedValues.t

1
C4/Koha.pm

@ -27,6 +27,7 @@ use C4::Context;
use Koha::Caches;
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);

11
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;

18
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(

34
t/db_dependent/AuthorisedValues.t

@ -1,12 +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;
@ -99,3 +100,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', );
};
};

Loading…
Cancel
Save