From 7ab983c466ea090b8c82c090b2ac07d5e4292909 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 21 Apr 2023 14:46:18 +0200 Subject: [PATCH] Bug 24977: WIP Adjust and continue --- C4/Biblio.pm | 24 ++------ Koha/AuthorisedValue.pm | 36 ++++++++++++ Koha/AuthorisedValues.pm | 56 +++++++++++++++++-- Koha/Schema/Result/AuthorisedValue.pm | 6 +- Koha/UI/Form/Builder/Item.pm | 9 +-- .../data/mysql/atomicupdate/bug-24977.perl | 2 +- 6 files changed, 102 insertions(+), 31 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 898abeb9f8a..5b57a5a1b7d 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1435,25 +1435,11 @@ sub GetAuthorisedValueDesc { my $dbh = C4::Context->dbh; if ( $category ne "" ) { my $cache = Koha::Caches->get_instance(); - my $cache_key = "AV_descriptions:" . $category; - my $av_descriptions = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); - if ( !$av_descriptions ) { - $av_descriptions = { - map { - $_->authorised_value => - { lib => $_->lib, lib_opac => $_->lib_opac } - } Koha::AuthorisedValues->search( - { category => $category }, - { - columns => [ 'authorised_value', 'lib_opac', 'lib' ] - } - )->as_list - }; - $cache->set_in_cache($cache_key, $av_descriptions); - } - return ( $opac && $av_descriptions->{$value}->{'lib_opac'} ) - ? $av_descriptions->{$value}->{'lib_opac'} - : $av_descriptions->{$value}->{'lib'}; + my $av = Koha::AuthorisedValue->get_from_cache($category, $value); + return db_t( + 'authorised_value:' . $category, + $opac ? $av->{translation_key_opac} : $av->{translation_key_staff} + ); } else { return $value; # if nothing is found return the original value } diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index da25d5e0584..1cbcdb6e2a8 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -22,6 +22,8 @@ use Modern::Perl; use Koha::Caches; use Koha::Database; +use Koha::I18N; + use base qw(Koha::Object Koha::Object::Limit::Library); my $cache = Koha::Caches->get_instance(); @@ -83,6 +85,40 @@ sub delete { $self->SUPER::delete(@_); } +=head3 translation_description + + my $translated_description = $av->translated_description; + +Returns a localized string for the Koha::AuthorisedValue lib or lib_opac field. + +=cut + +sub translated_description { + my ( $self, $interface ) = @_; + return db_t(sprintf('authorised_value:%s', $self->category), $self->translation_key); +} + +=head3 translation_key + + db_t('authorised_value', $av->translation_key); + +Returns the translation key for this authorised value object. + +=cut + +sub translation_key { + my ( $self, $interface ) = @_; + return sprintf "%s:%s", $self->authorised_value, ( $interface || 'opac' ); +} + +sub get_from_cache { + my ( $class, $category, $av ) = @_; + my @avs = @{Koha::AuthorisedValues->get_from_cache($category)}; + # FIXME Maybe we should store a hashref instead of an array + my ( $cached ) = grep {($_->{authorised_value} eq $av) ? $_ : ()} @avs; + return $cached; +} + =head3 opac_description my $description = $av->opac_description(); diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index dda7df24a90..de005ea5e5a 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -39,6 +39,53 @@ Koha::AuthorisedValues - Koha Authorised value Object set class =cut +=head3 get_from_cache + + my $avs = Koha::AuthorisedValues->get_from_cache($category, [{ library_limit => $library_id } ]); + +Returns an array of hashrefs representing authorised values. + +A library_limit parameter can be passed to limit the result to a given library. + +The translation_key is added to the list of authorised value's attributes + +=cut + +sub get_from_cache { + my ($self, $category, $params) = @_; + + my $library_limit = + ( $params && $params->{library_limit} ) + ? $params->{library_limit} || q{} + : q{}; + + my $cache = Koha::Caches->get_instance('authorised_values'); + my $cache_key = "AuthorisedValues:$category:$library_limit"; + # FIXME Maybe we should pass unsafe here, for perf purpose, but too many callers + # that could modify it + my $cached = $cache->get_from_cache($cache_key); + return $cached if $cached; + + my $avs = + $library_limit + ? $self->search_with_library_limits( + { category => $category }, + undef, + $params->{library_limit} + ) + : $self->search; + + return [ + map { + { + %{ $_->unblessed }, + translation_key_opac => $_->translation_key('opac'), + translation_key_staff => $_->translation_key('staff'), + } + } $avs->as_list + ]; +} + sub search_by_marc_field { my ( $self, $params ) = @_; my $frameworkcode = $params->{frameworkcode} || ''; @@ -114,8 +161,8 @@ sub get_description_by_koha_field { return {} unless defined $av; my $l10n_group = 'authorised_value:' . $av->category; my $descriptions = { - lib => db_t($l10n_group, $av->lib), - opac_description => db_t($l10n_group, $av->opac_description), + lib => db_t($l10n_group, $av->translation_key('staff')), + opac_description => db_t($l10n_group, $av->translation_key('opac')), }; $memory_cache->set_in_cache( $cache_key, $descriptions ); return $descriptions; @@ -133,10 +180,11 @@ sub get_descriptions_by_koha_field { my @avs = $self->search_by_koha_field($params)->as_list; my @descriptions = map { + my $l10n_group = 'authorised_value:' . $_->category; { authorised_value => $_->authorised_value, - lib => db_t('authorised_value:' . $_->category, $_->lib), - opac_description => db_t('authorised_value:' . $_->category, $_->opac_description), + lib => db_t($l10n_group, $_->translation_key('staff')), + opac_description => db_t($l10n_group, $_->translation_key('opac')), } } @avs; $memory_cache->set_in_cache( $cache_key, \@descriptions ); diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 71d982f0d92..7dd65d861a7 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -185,7 +185,7 @@ sub insert { my $group = sprintf('authorised_value:%s', $self->get_column('category')); $self->update_l10n_source( $group, - $self->authorised_value . ':intranet', + $self->authorised_value . ':staff', $self->lib ); $self->update_l10n_source( $group, $self->authorised_value . ':opac', @@ -205,7 +205,7 @@ sub update { my $group = sprintf('authorised_value:%s', $self->get_column('category')); if ($is_lib_changed) { $self->update_l10n_source( $group, - $self->authorised_value . ':intranet', + $self->authorised_value . ':staff', $self->lib ); } if ($is_lib_opac_changed) { @@ -223,7 +223,7 @@ sub delete { my $result = $self->next::method(@_); my $group = sprintf('authorised_value:%s', $self->get_column('category')); - $self->delete_l10n_source($group, sprintf('%s:intranet', $self->authorised_value)); + $self->delete_l10n_source($group, sprintf('%s:staff', $self->authorised_value)); $self->delete_l10n_source($group, sprintf('%s:opac', $self->authorised_value)); return $result; diff --git a/Koha/UI/Form/Builder/Item.pm b/Koha/UI/Form/Builder/Item.pm index d685e24877d..50e3cb1e1b2 100644 --- a/Koha/UI/Form/Builder/Item.pm +++ b/Koha/UI/Form/Builder/Item.pm @@ -189,10 +189,11 @@ sub generate_subfield_form { $subfield_data{IS_LOST_AV} = 1; push @authorised_values, qq{}; - my $av = GetAuthorisedValues( $subfield->{authorised_value} ); - for my $r (@$av) { - push @authorised_values, $r->{authorised_value}; - $authorised_lib{ $r->{authorised_value} } = $r->{lib}; + my $avs = Koha::AuthorisedValues->get_from_cache({ library_limit => $branch_limit }); + for my $av (@$avs) { + push @authorised_values, $av->{authorised_value}; + $authorised_lib{ $av->{authorised_value} } = + db_t('authorised_value', $av->{translation_key}); } } elsif ( $subfield->{authorised_value} eq "branches" ) { diff --git a/installer/data/mysql/atomicupdate/bug-24977.perl b/installer/data/mysql/atomicupdate/bug-24977.perl index 0b0e5ff8459..72bd0e340c3 100644 --- a/installer/data/mysql/atomicupdate/bug-24977.perl +++ b/installer/data/mysql/atomicupdate/bug-24977.perl @@ -2,7 +2,7 @@ $DBversion = 'XXX'; if( CheckVersion( $DBversion ) ) { $dbh->do(q{ INSERT IGNORE INTO l10n_source (`group`, `key`, `text`) - SELECT DISTINCT CONCAT('authorised_value:', category), CONCAT(authorised_value, ':intranet'), lib FROM authorised_values WHERE lib IS NOT NULL AND lib != '' + SELECT DISTINCT CONCAT('authorised_value:', category), CONCAT(authorised_value, ':staff'), lib FROM authorised_values WHERE lib IS NOT NULL AND lib != '' UNION SELECT DISTINCT CONCAT('authorised_value:', category), CONCAT(authorised_value, ':opac'), lib_opac FROM authorised_values WHERE lib_opac IS NOT NULL AND lib_opac != '' }); -- 2.25.1