From 2728fc701e81df1d3e3db189e16288cb56526f0d Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 2 Jan 2017 17:29:32 +0100 Subject: [PATCH] Bug 17835: Create a ItemtypeLocalization view Content-Type: text/plain; charset=utf-8 To properly move C4::Koha::GetItemTypes to Koha::ItemTypes we need DBIx::Class to make a join on the localization table to retrieve the possible translated description of the item types. To do so there are 2 possibilities. The first one would have been to rename the localization table to something like itemtype_localization. That way we could have had a relationship between itemtype_localization.code and itemtypes.itemtype That would have meant to create one table per "entity" (here an entity is itemtype) we allow the translability. There are pros and cons for this choice, so I opt for another solution. The other solution is to create a view on top of this localization table. With this new view we can define the missing relationship. That sounds like a quite clean solution and easy to implement. Once we have this relationship, the Koha::ItemTypes->search_with_localization will join on this view an return the same result as GetItemTypes( style => 'array' ). To replace GetItemtypes( style => 'hash' ) which is the default behavior of this subroutine, we can do something like: my $itemtypes = Koha::ItemTypes->search_with_localization; my %itemtypes = map { $_->{itemtype} => $_ } @{ $itemtypes->unblessed }; This patchset must not introduce big changes but it changes certain behaviors (which were wrong) in some scripts. Indeed sometimes the descriptions of the item types were not the translated ones. Moreover it happens that the item types displayed in a dropdown list were not ordered by translated description, but by description of code (itemtypes.itemtype value). These 2 behaviors are what we expect. Test plan: Bugs will be hard to catch since these patches change a lot of file, it will be easier to read the diff and catch possible typos or logic errors. However signoffers can focus on modified files and the item types values. Signed-off-by: Josef Moravec Signed-off-by: Lari Taskula Signed-off-by: Marcel de Rooy --- Koha/ItemTypes.pm | 21 ++++++++++++++++- Koha/Schema/Result/Itemtype.pm | 8 ++++++- Koha/Schema/Result/ItemtypeLocalization.pm | 32 ++++++++++++++++++++++++++ t/db_dependent/Koha/ItemTypes.t | 37 +++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 Koha/Schema/Result/ItemtypeLocalization.pm diff --git a/Koha/ItemTypes.pm b/Koha/ItemTypes.pm index 92fea9c..daa808b 100644 --- a/Koha/ItemTypes.pm +++ b/Koha/ItemTypes.pm @@ -19,8 +19,9 @@ use Modern::Perl; use Carp; -use Koha::Database; +use C4::Languages; +use Koha::Database; use Koha::ItemType; use base qw(Koha::Objects); @@ -35,6 +36,24 @@ Koha::ItemTypes - Koha ItemType Object set class =cut +=head3 search_with_localization + +my $itemtypes = Koha::ItemTypes->search_with_localization + +=cut + +sub search_with_localization { + my ( $self, $params, $attributes ) = @_; + + my $language = C4::Languages::getlanguage(); + $params->{'-or'} = { 'localization.lang' => [ $language, undef ] }; + $attributes->{order_by} = 'localization.translation' unless exists $attributes->{order_by}; + $attributes->{join} = 'localization'; + $attributes->{'+select'} = [ { coalesce => [qw( localization.translation me.description )] } ]; + $attributes->{'+as'} = ['translated_description']; + $self->SUPER::search( $params, $attributes ); +} + =head3 type =cut diff --git a/Koha/Schema/Result/Itemtype.pm b/Koha/Schema/Result/Itemtype.pm index 6884bf5..5af2466 100644 --- a/Koha/Schema/Result/Itemtype.pm +++ b/Koha/Schema/Result/Itemtype.pm @@ -198,6 +198,12 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-04-29 10:32:00 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1GiikODklVISOurHX37qjA +# Use the ItemtypeLocalization view to create the join on localization +__PACKAGE__->has_many( + "localization", + "Koha::Schema::Result::ItemtypeLocalization", + { "foreign.code" => "self.itemtype" }, + { cascade_copy => 0, cascade_delete => 0 }, +); -# You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/Koha/Schema/Result/ItemtypeLocalization.pm b/Koha/Schema/Result/ItemtypeLocalization.pm new file mode 100644 index 0000000..d596612 --- /dev/null +++ b/Koha/Schema/Result/ItemtypeLocalization.pm @@ -0,0 +1,32 @@ +package Koha::Schema::Result::ItemtypeLocalization; + +use base 'DBIx::Class::Core'; + +use Modern::Perl; + +__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); + +__PACKAGE__->table('itemtype_localizations'); +__PACKAGE__->result_source_instance->is_virtual(1); +__PACKAGE__->result_source_instance->view_definition( + "SELECT localization_id, code, lang, translation FROM localization WHERE entity='itemtypes'" +); + +__PACKAGE__->add_columns( + "localization_id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "code", + { data_type => "varchar", is_nullable => 0, size => 64 }, + "lang", + { data_type => "varchar", is_nullable => 0, size => 25 }, + "translation", + { data_type => "text", is_nullable => 1 }, +); + +__PACKAGE__->belongs_to( + "itemtype", + "Koha::Schema::Result::Itemtype", + { code => 'itemtype' } +); + +1; diff --git a/t/db_dependent/Koha/ItemTypes.t b/t/db_dependent/Koha/ItemTypes.t index 9ce1ad6..da05fd4 100755 --- a/t/db_dependent/Koha/ItemTypes.t +++ b/t/db_dependent/Koha/ItemTypes.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 18; +use Test::More tests => 20; use Data::Dumper; use Koha::Database; @@ -31,6 +31,7 @@ BEGIN { my $database = Koha::Database->new(); my $schema = $database->schema(); $schema->txn_begin; +Koha::ItemTypes->delete; Koha::ItemType->new( { @@ -56,6 +57,31 @@ Koha::ItemType->new( } )->store; +Koha::Localization->new( + { + entity => 'itemtypes', + code => 'type1', + lang => 'en', + translation => 'b translated itemtype desc' + } +)->store; +Koha::Localization->new( + { + entity => 'itemtypes', + code => 'type2', + lang => 'en', + translation => 'a translated itemtype desc' + } +)->store; +Koha::Localization->new( + { + entity => 'something_else', + code => 'type2', + lang => 'en', + translation => 'another thing' + } +)->store; + my $type = Koha::ItemTypes->find('type1'); ok( defined($type), 'first result' ); is( $type->itemtype, 'type1', 'itemtype/code' ); @@ -76,4 +102,13 @@ is( $type->summary, 'summary', 'summary' ); is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' ); is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' ); +my $itemtypes = Koha::ItemTypes->search_with_localization; +is( $itemtypes->count, 2, 'There are 2 item types' ); +my $first_itemtype = $itemtypes->next; +is( + $first_itemtype->translated_description, + 'a translated itemtype desc', + 'item types should be sorted by translated description' +); + $schema->txn_rollback; -- 2.1.4