From 14211e4a30f503d6c4fad79f26d9d44b0f9add69 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 16 Nov 2016 17:46:34 +0100 Subject: [PATCH] Bug 17642: Add and use get_descriptions_by_koha_field Content-Type: text/plain; charset=utf-8 Ok I am silly, we needed to replace to use the cache mechanism for search_by_koha_field, not find_by_koha_field... Let's create another subroutine Signed-off-by: Marcel de Rooy --- C4/Search.pm | 2 +- C4/XSLT.pm | 4 ++-- Koha/AuthorisedValues.pm | 22 ++++++++++++++++++++++ basket/basket.pl | 2 +- catalogue/detail.pl | 6 +++--- catalogue/moredetail.pl | 4 ++-- circ/returns.pl | 2 +- opac/opac-basket.pl | 4 ++-- opac/opac-detail.pl | 6 +++--- reports/catalogue_stats.pl | 8 ++++---- reports/issues_stats.pl | 4 ++-- reports/reserves_stats.pl | 8 ++++---- t/db_dependent/AuthorisedValues.t | 21 ++++++++++++++++++++- 13 files changed, 67 insertions(+), 26 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 9379eeb..4d5c0ed 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1850,7 +1850,7 @@ sub searchResults { # though it is possible to have different authvals for different fws. my $shelflocations = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) }; # get notforloan authorised value list (see $shelflocations FIXME) my $av = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => 'items.notforloan', authorised_value => { not => undef } }); diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 50ab8e6..a829402 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -274,9 +274,9 @@ sub buildKohaItemsNamespace { } my $shelflocations = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => GetFrameworkCode($biblionumber), kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => GetFrameworkCode($biblionumber), kohafield => 'items.location' } ) }; my $ccodes = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => GetFrameworkCode($biblionumber), kohafield => 'items.ccode' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => GetFrameworkCode($biblionumber), kohafield => 'items.ccode' } ) }; my %branches = map { $_->branchcode => $_->branchname } Koha::Libraries->search({}, { order_by => 'branchname' }); diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index 7ceb77a..d9e6d68 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -139,6 +139,28 @@ sub get_description_by_koha_field { return $descriptions; } +sub get_descriptions_by_koha_field { + my ( $self, $params ) = @_; + my $frameworkcode = $params->{frameworkcode} || ''; + my $kohafield = $params->{kohafield}; + + my $memory_cache = Koha::Cache::Memory::Lite->get_instance; + my $cache_key = "AV_descriptions:$frameworkcode:$kohafield"; + my $cached = $memory_cache->get_from_cache($cache_key); + return @$cached if $cached; + + my @avs = $self->search_by_koha_field($params); + my @descriptions = map { + { + authorised_value => $_->authorised_value, + lib => $_->lib, + opac_description => $_->opac_description + } + } @avs; + $memory_cache->set_in_cache( $cache_key, \@descriptions ); + return @descriptions; +} + sub categories { my ( $self ) = @_; my $rs = $self->_resultset->search( diff --git a/basket/basket.pl b/basket/basket.pl index 83cb180..62a37ab 100755 --- a/basket/basket.pl +++ b/basket/basket.pl @@ -77,7 +77,7 @@ foreach my $biblionumber ( @bibs ) { } my $shelflocations = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; for my $itm (@items) { if ($itm->{'location'}){ diff --git a/catalogue/detail.pl b/catalogue/detail.pl index 09aa55c..2bf81c2 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -190,11 +190,11 @@ $dat->{'showncount'} = scalar @items + @hostitems; $dat->{'hiddencount'} = scalar @all_items + @hostitems - scalar @items; my $shelflocations = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.location' } ) }; my $collections = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) }; my $copynumbers = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) }; my (@itemloop, @otheritemloop, %itemfields); my $norequests = 1; diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl index 80409d7..1d50d50 100755 --- a/catalogue/moredetail.pl +++ b/catalogue/moredetail.pl @@ -120,9 +120,9 @@ $data->{'showncount'}=$showncount; $data->{'hiddencount'}=$hiddencount; # can be zero my $ccodes = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.ccode' } ) }; my $copynumbers = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $fw, kohafield => 'items.copynumber' } ) }; my $itemtypes = GetItemTypes; $data->{'itemtypename'} = $itemtypes->{$data->{'itemtype'}}->{'translated_description'}; diff --git a/circ/returns.pl b/circ/returns.pl index b91778a..5927759 100755 --- a/circ/returns.pl +++ b/circ/returns.pl @@ -552,7 +552,7 @@ my $returned_counter = ( C4::Context->preference('numReturnedItemsToShow') ) ? C my $count = 0; my @riloop; my $shelflocations = - { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) }; foreach ( sort { $a <=> $b } keys %returneditems ) { my %ri; if ( $count++ < $returned_counter ) { diff --git a/opac/opac-basket.pl b/opac/opac-basket.pl index 82734b1..bfa9688 100755 --- a/opac/opac-basket.pl +++ b/opac/opac-basket.pl @@ -86,9 +86,9 @@ foreach my $biblionumber ( @bibs ) { $hasauthors = 1; } my $collections = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) }; my $shelflocations = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; # COinS format FIXME: for books Only my $coins_format; diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index 41c1d22..a7e643c 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -546,11 +546,11 @@ if ( $itemtype ) { } my $shelflocations = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.location' } ) }; my $collections = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.ccode' } ) }; my $copynumbers = - { map { $_->authorised_value => $_->opac_description } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) }; + { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => $dat->{frameworkcode}, kohafield => 'items.copynumber' } ) }; #coping with subscriptions my $subscriptionsnumber = CountSubscriptionFromBiblionumber($biblionumber); diff --git a/reports/catalogue_stats.pl b/reports/catalogue_stats.pl index 97a4509..be516c1 100755 --- a/reports/catalogue_stats.pl +++ b/reports/catalogue_stats.pl @@ -122,8 +122,8 @@ if ($do_it) { my $itemtypes = GetItemTypes( style => 'array' ); - my @authvals = map { { code => $_->authorised_value, description => $_->lib } } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ); - my @locations = map { { code => $_->authorised_value, description => $_->lib } } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ); + my @authvals = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ); + my @locations = map { { code => $_->{authorised_value}, description => $_->{lib} } } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ); foreach my $kohafield (qw(items.notforloan items.materials)) { my $subfield_structure = GetMarcSubfieldStructureFromKohaField($kohafield); @@ -314,7 +314,7 @@ sub calculate { } else { $sth->execute(); } - my $rowauthvals = { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => $origline } ) }; + my $rowauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origline } ) }; while ( my ($celvalue) = $sth->fetchrow ) { my %cell; if (defined $celvalue and $celvalue ne '') { @@ -377,7 +377,7 @@ sub calculate { } else { $sth2->execute(); } - my $colauthvals = { map { $_->authorised_value => $_->lib } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) }; + my $colauthvals = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => $origcolumn } ) }; while ( my ($celvalue) = $sth2->fetchrow ) { my %cell; if (defined $celvalue and $celvalue ne '') { diff --git a/reports/issues_stats.pl b/reports/issues_stats.pl index 5c7370e..63501be 100755 --- a/reports/issues_stats.pl +++ b/reports/issues_stats.pl @@ -90,8 +90,8 @@ $template->param(do_it => $do_it, our $itemtypes = GetItemTypes(); our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']}); -my $locations = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; -my $ccodes = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; +my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; +my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; our $Bsort1 = GetAuthorisedValues("Bsort1"); our $Bsort2 = GetAuthorisedValues("Bsort2"); diff --git a/reports/reserves_stats.pl b/reports/reserves_stats.pl index f591be1..79dd391 100755 --- a/reports/reserves_stats.pl +++ b/reports/reserves_stats.pl @@ -84,8 +84,8 @@ $template->param(do_it => $do_it, my $itemtypes = GetItemTypes(); my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']}); -my $locations = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; -my $ccodes = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; +my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; +my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; my $Bsort1 = GetAuthorisedValues("Bsort1"); my $Bsort2 = GetAuthorisedValues("Bsort2"); @@ -330,8 +330,8 @@ sub null_to_zzempty ($) { } sub display_value { my ( $crit, $value ) = @_; - my $locations = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; - my $ccodes = { map { ( $_->authorised_value => $_->lib ) } Koha::AuthorisedValues->search_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; + my $locations = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' }, { order_by => ['description'] } ) }; + my $ccodes = { map { ( $_->{authorised_value} => $_->{lib} ) } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' }, { order_by => ['description'] } ) }; my $itemtypes = GetItemTypes(); my $Bsort1 = GetAuthorisedValues("Bsort1"); my $Bsort2 = GetAuthorisedValues("Bsort2"); diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index 00ced7e..5f344d3 100644 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -119,7 +119,7 @@ is( $categories[0], $av4->category, 'The first category should be correct (order is( $categories[1], $av1->category, 'The second category should be correct (ordered by category name)' ); subtest 'search_by_*_field + find_by_koha_field + get_description' => sub { - plan tests => 4; + plan tests => 5; my $loc_cat = Koha::AuthorisedValueCategories->find('LOC'); $loc_cat->delete if $loc_cat; my $mss = Koha::MarcSubfieldStructures->search( { tagfield => 952, tagsubfield => 'c', frameworkcode => '' } ); @@ -200,4 +200,23 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub { { kohafield => 'items.restricted', authorised_value => undef } ); is_deeply( $descriptions, {}, ) ; # This could be arguable, we could return undef instead }; + subtest 'get_descriptions_by_koha_field' => sub { + plan tests => 1; + my @descriptions = Koha::AuthorisedValues->get_descriptions_by_koha_field( { kohafield => 'items.restricted' } ); + is_deeply( + \@descriptions, + [ + { + authorised_value => '', + lib => $av_empty_string->lib, + opac_description => $av_empty_string->lib_opac + }, + { + authorised_value => $av_0->authorised_value, + lib => $av_0->lib, + opac_description => $av_0->lib_opac + } + ], + ); + }; }; -- 2.1.4