@@ -, +, @@ GetItemtypes( style => 'hash' ) my $itemtypes = Koha::ItemTypes->search_with_localization; my %itemtypes = map { $_->{itemtype} => $_ } @{ $itemtypes->unblessed }; --- 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 --- a/Koha/ItemTypes.pm +++ a/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 --- a/Koha/Schema/Result/Itemtype.pm +++ a/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; --- a/Koha/Schema/Result/ItemtypeLocalization.pm +++ a/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; --- a/t/db_dependent/Koha/ItemTypes.t +++ a/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; --