From cc72655f4d215dd348ae8e53e77496c3cf056eaa Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 15 Nov 2024 16:12:33 +0100 Subject: [PATCH] Bug 38460: [WIP] Enable translations for authorised values (alternative) As it's a WIP, not every place shows the translated description. One place where it works is the items table in the "edit item" page Test plan: 1. Edit one of LOC authorised values, you should see links to translate descriptions (for both staff and opac) Add translations for a language 2. Switch to this language, if not already done 3. Find a biblio record, edit one of its items and change the loc field to the value you translated. Save. 4. Now in the table at the top of the page you should see the translated description --- C4/Biblio.pm | 29 +++++++------------ Koha/AuthorisedValue.pm | 12 ++++++++ Koha/Schema/Result/AuthorisedValue.pm | 26 +++++++++++++++-- Koha/Template/Plugin/AuthorisedValues.pm | 6 ++-- .../data/mysql/atomicupdate/bug-38460.pl | 28 ++++++++++++++++++ .../en/modules/admin/authorised_values.tt | 3 ++ 6 files changed, 82 insertions(+), 22 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug-38460.pl diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 6f162e2119..991429a849 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1502,25 +1502,18 @@ sub GetAuthorisedValueDesc { my $dbh = C4::Context->dbh; if ( $category ne "" ) { - $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); + my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); + my $cache_key = 'GetAuthorisedValueDesc:authorised_values'; + my $authorised_values = $memory_cache->get_from_cache($cache_key); + unless ($authorised_values) { + $authorised_values = { map { $_->authorised_value => $_ } Koha::AuthorisedValues->as_list }; + $memory_cache->set_in_cache($cache_key, $authorised_values); } - return ( $opac && $av_descriptions->{$value}->{'lib_opac'} ) - ? $av_descriptions->{$value}->{'lib_opac'} - : $av_descriptions->{$value}->{'lib'}; + my $av = $authorised_values->{$value}; + return unless $av; + + my $lang = C4::Languages::getlanguage(); + return $opac ? $av->opac_translated_description : $av->localization('lib', $lang); } else { return $value; # if nothing is found return the original value } diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index 5669ab6086..999f0c45f1 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -103,6 +103,18 @@ sub opac_description { return $self->lib_opac() || $self->lib(); } +=head3 opac_translated_description + +my $description = $av->opac_translated_description($lang); + +=cut + +sub opac_translated_description { + my ($self, $lang) = @_; + + return $self->localization('lib_opac', $lang) || $self->localization('lib', $lang); +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::AuthorisedValue object diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 874c166b02..25ce3ae907 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -142,6 +142,21 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 authorised_values_localizations + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "authorised_values_localizations", + "Koha::Schema::Result::AuthorisedValuesLocalization", + { "foreign.authorised_value_id" => "self.id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 category Type: belongs_to @@ -173,9 +188,16 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-01-21 13:39:29 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:LH9dpEEzlVVsjNVv/jnONg +# Created by DBIx::Class::Schema::Loader v0.07052 @ 2024-11-15 16:11:15 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:mlwkraQyS4N5mlE7J5g7qQ +__PACKAGE__->load_components('+Koha::Schema::Component::Localization'); +__PACKAGE__->localization_add_relationships( + 'authorised_values_localizations', + 'authorised_value_id' => 'id', + 'lib', + 'lib_opac', +); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Koha/Template/Plugin/AuthorisedValues.pm b/Koha/Template/Plugin/AuthorisedValues.pm index 4e74c1bbad..7f9fe53189 100644 --- a/Koha/Template/Plugin/AuthorisedValues.pm +++ b/Koha/Template/Plugin/AuthorisedValues.pm @@ -22,15 +22,17 @@ use Template::Plugin; use base qw( Template::Plugin ); use C4::Koha qw( GetAuthorisedValues ); +use C4::Languages; use Koha::AuthorisedValues; sub GetByCode { my ( $self, $category, $code, $opac ) = @_; my $av = Koha::AuthorisedValues->search({ category => $category, authorised_value => $code }); + my $lang = C4::Languages::getlanguage(); return $av->count ? $opac - ? $av->next->opac_description - : $av->next->lib + ? $av->next->opac_translated_description($lang) + : $av->next->localization('lib', $lang) : $code; } diff --git a/installer/data/mysql/atomicupdate/bug-38460.pl b/installer/data/mysql/atomicupdate/bug-38460.pl new file mode 100755 index 0000000000..6e9ab64860 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-38460.pl @@ -0,0 +1,28 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => '38460', + description => 'Add authorised_values_localizations table', + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + unless ( TableExists('authorised_values_localizations') ) { + $dbh->do( " + CREATE TABLE `authorised_values_localizations` ( + `authorised_values_localizations_id` int(11) NOT NULL AUTO_INCREMENT, + `authorised_value_id` int(11) NOT NULL, + `property` varchar(100) NOT NULL, + `lang` varchar(25) NOT NULL, + `translation` mediumtext DEFAULT NULL, + PRIMARY KEY (`authorised_values_localizations_id`), + UNIQUE KEY `authorised_value_id_property_lang` (`authorised_value_id`, `property`, `lang`), + CONSTRAINT `authorised_values_localizations_authorised_value_id_fk` + FOREIGN KEY (`authorised_value_id`) REFERENCES `authorised_values` (`id`) + ON DELETE CASCADE ON UPDATE CASCADE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + " ); + } + }, +}; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt index 2bfb4f3f7f..06c8b71590 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt @@ -180,10 +180,12 @@
  • + [% INCLUDE 'localization-link.inc' source='AuthorisedValue' object_id=av.id property='lib' %]
  • + [% INCLUDE 'localization-link.inc' source='AuthorisedValue' object_id=av.id property='lib_opac' %]