From 858d9cb88ff76432401370837adb16571e459eda Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 25 Mar 2020 15:58:14 +0100 Subject: [PATCH] Bug 24977: Enable translations for authorised values (POC) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Only a proof of concept to illustrate what's needed to enable translation for a new kind of object. Only authorised values retrieved with Koha::AuthorisedValues->get_descriptions_by_koha_field and Koha::AuthorisedValues->get_description_by_koha_field are translated. This is used for instance for shelving locations in catalogue/itemsearch.pl Test plan: 1. Run updatedatabase && update_dbix_class_files && reload starman 2. Go to Admin ยป Localization 3. Find a LOC authorised value and translate it 4. Go to Item search form and verify that it is translated 4. Verify that it is translated in results too Requires bug 24975 --- Koha/AuthorisedValues.pm | 11 ++-- Koha/DBIx/Component/L10nSource.pm | 24 +++++---- Koha/Schema/Result/AuthorisedValue.pm | 53 ++++++++++++++++++- .../data/mysql/atomicupdate/bug-24977.perl | 12 +++++ 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug-24977.perl diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index a4916d571e..b502eccb2b 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -24,6 +24,7 @@ use Carp; use Koha::Database; use Koha::AuthorisedValue; +use Koha::I18N; use Koha::MarcSubfieldStructures; use Koha::Cache::Memory::Lite; @@ -135,7 +136,11 @@ sub get_description_by_koha_field { my $av = $self->find_by_koha_field($params); return {} unless defined $av; - my $descriptions = { lib => $av->lib, opac_description => $av->opac_description }; + 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), + }; $memory_cache->set_in_cache( $cache_key, $descriptions ); return $descriptions; } @@ -154,8 +159,8 @@ sub get_descriptions_by_koha_field { my @descriptions = map { { authorised_value => $_->authorised_value, - lib => $_->lib, - opac_description => $_->opac_description + lib => db_t('authorised_value:' . $_->category, $_->lib), + opac_description => db_t('authorised_value:' . $_->category, $_->opac_description), } } @avs; $memory_cache->set_in_cache( $cache_key, \@descriptions ); diff --git a/Koha/DBIx/Component/L10nSource.pm b/Koha/DBIx/Component/L10nSource.pm index 933638e834..5ed26f5e84 100644 --- a/Koha/DBIx/Component/L10nSource.pm +++ b/Koha/DBIx/Component/L10nSource.pm @@ -52,16 +52,20 @@ sub update_l10n_source { my ($self, $group, $key, $text) = @_; my $l10n_source_rs = $self->result_source->schema->resultset('L10nSource'); - $l10n_source_rs->update_or_create( - { - group => $group, - key => $key, - text => $text, - }, - { - key => 'group_key', - } - ); + if (defined $text and $text ne '') { + $l10n_source_rs->update_or_create( + { + group => $group, + key => $key, + text => $text, + }, + { + key => 'group_key', + } + ); + } else { + $self->delete_l10n_source($group, $key); + } } =head2 delete_l10n_source diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 1a65d388c9..775ed3a91e 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -148,6 +148,57 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ +__PACKAGE__->load_components('+Koha::DBIx::Component::L10nSource'); + +sub insert { + my $self = shift; + + my $result = $self->next::method(@_); + + my $group = sprintf('authorised_value:%s', $self->get_column('category')); + $self->update_l10n_source( $group, + $self->authorised_value . ':intranet', + $self->lib ); + $self->update_l10n_source( $group, + $self->authorised_value . ':opac', + $self->lib_opac ); + + return $result; +} + +sub update { + my $self = shift; + + my $is_lib_changed = $self->is_column_changed('lib'); + my $is_lib_opac_changed = $self->is_column_changed('lib_opac'); + + my $result = $self->next::method(@_); + + my $group = sprintf('authorised_value:%s', $self->get_column('category')); + if ($is_lib_changed) { + $self->update_l10n_source( $group, + $self->authorised_value . ':intranet', + $self->lib ); + } + if ($is_lib_opac_changed) { + $self->update_l10n_source( $group, + $self->authorised_value . ':opac', + $self->lib_opac ); + } + + return $result; +} + +sub delete { + my $self = shift; + + 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:opac', $self->authorised_value)); + + return $result; +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/installer/data/mysql/atomicupdate/bug-24977.perl b/installer/data/mysql/atomicupdate/bug-24977.perl new file mode 100644 index 0000000000..0b0e5ff845 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-24977.perl @@ -0,0 +1,12 @@ +$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 != '' + 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 != '' + }); + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 24977 - populate l10n_source with authorised_values)\n"; +} -- 2.20.1