use Carp;
-use Koha::Database;
+use C4::Languages;
+use Koha::Database;
use Koha::ItemType;
use base qw(Koha::Objects);
=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
# 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;
--- /dev/null
+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;
use Modern::Perl;
-use Test::More tests => 18;
+use Test::More tests => 20;
use Data::Dumper;
use Koha::Database;
my $database = Koha::Database->new();
my $schema = $database->schema();
$schema->txn_begin;
+Koha::ItemTypes->delete;
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' );
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;