From d6f19c05218054175336cf12927aa8575a465a30 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 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 Requires bug 24975 --- Koha/AuthorisedValues.pm | 5 +- Koha/DBIx/Component/L10nSource.pm | 2 + Koha/Schema/Result/AuthorisedValue.pm | 61 ++++++++++++++++++- .../data/mysql/atomicupdate/bug-24977.perl | 12 ++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug-24977.perl diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index a4916d571e..cfe76a76e9 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; @@ -154,8 +155,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 9d4ea398d9..51efc19f4f 100644 --- a/Koha/DBIx/Component/L10nSource.pm +++ b/Koha/DBIx/Component/L10nSource.pm @@ -54,6 +54,8 @@ sub update_l10n_source { my $l10n_source_rs = $self->result_source->schema->resultset('L10nSource')->search({ context => $context }); + @sources = grep { defined $_ && $_ ne '' } @sources; + # Insert missing l10n_source rows foreach my $source (@sources) { my $count = $l10n_source_rs->search({ source => $source })->count; diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 1a65d388c9..4f2bbae0fd 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -148,6 +148,63 @@ __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 - -# You can replace this text with custom code or comments, and it will be preserved on regeneration +__PACKAGE__->load_components('+Koha::DBIx::Component::L10nSource'); + +sub insert { + my $self = shift; + + my $result = $self->next::method(@_); + + my @authorised_values = $self->result_source->resultset->search(undef, { + result_class => 'DBIx::Class::ResultClass::HashRefInflator', + columns => ['lib', 'lib_opac'], + }); + my @sources = map { $_->{lib}, $_->{lib_opac} } @authorised_values; + my $category = $self->get_column('category'); + my $context = "authorised_value:$category"; + $self->update_l10n_source($context, @sources); + + return $result; +} + +sub update { + my $self = shift; + + my $is_lib_changed = $self->is_column_changed('lib') || $self->is_column_changed('lib_opac'); + + my $result = $self->next::method(@_); + + if ($is_lib_changed) { + my $category = $self->get_column('category'); + my @authorised_values = $self->result_source->resultset->search( + { category => $category }, + { + result_class => 'DBIx::Class::ResultClass::HashRefInflator', + columns => ['lib', 'lib_opac'], + } + ); + my @sources = map { $_->{lib}, $_->{lib_opac} } @authorised_values; + my $context = "authorised_value:$category"; + $self->update_l10n_source($context, @sources); + } + + return $result; +} + +sub delete { + my $self = shift; + + my $result = $self->next::method(@_); + + my @authorised_values = $self->result_source->resultset->search(undef, { + result_class => 'DBIx::Class::ResultClass::HashRefInflator', + columns => ['lib', 'lib_opac'], + }); + my @sources = map { $_->{lib}, $_->{lib_opac} } @authorised_values; + my $category = $self->get_column('category'); + my $context = "authorised_value:$category"; + $self->update_l10n_source($context, @sources); + + return $result; +} 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..fceceafc9d --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug-24977.perl @@ -0,0 +1,12 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + $dbh->do(q{ + INSERT INTO l10n_source (context, source) + SELECT DISTINCT CONCAT('authorised_value:', category), lib FROM authorised_values WHERE lib IS NOT NULL AND lib != '' + UNION + SELECT DISTINCT CONCAT('authorised_value:', category), 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