View | Details | Raw Unified | Return to bug 17835
Collapse All | Expand All

(-)a/Koha/ItemTypes.pm (-1 / +20 lines)
Lines 19-26 use Modern::Perl; Link Here
19
19
20
use Carp;
20
use Carp;
21
21
22
use Koha::Database;
22
use C4::Languages;
23
23
24
use Koha::Database;
24
use Koha::ItemType;
25
use Koha::ItemType;
25
26
26
use base qw(Koha::Objects);
27
use base qw(Koha::Objects);
Lines 35-40 Koha::ItemTypes - Koha ItemType Object set class Link Here
35
36
36
=cut
37
=cut
37
38
39
=head3 search_with_localization
40
41
my $itemtypes = Koha::ItemTypes->search_with_localization
42
43
=cut
44
45
sub search_with_localization {
46
    my ( $self, $params, $attributes ) = @_;
47
48
    my $language = C4::Languages::getlanguage();
49
    $params->{'-or'} = { 'localization.lang' => [ $language, undef ] };
50
    $attributes->{order_by} = 'localization.translation' unless exists $attributes->{order_by};
51
    $attributes->{join} = 'localization';
52
    $attributes->{'+select'} = [ { coalesce => [qw( localization.translation me.description )] } ];
53
    $attributes->{'+as'} = ['translated_description'];
54
    $self->SUPER::search( $params, $attributes );
55
}
56
38
=head3 type
57
=head3 type
39
58
40
=cut
59
=cut
(-)a/Koha/Schema/Result/Itemtype.pm (-1 / +7 lines)
Lines 198-203 __PACKAGE__->has_many( Link Here
198
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-04-29 10:32:00
198
# Created by DBIx::Class::Schema::Loader v0.07042 @ 2016-04-29 10:32:00
199
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1GiikODklVISOurHX37qjA
199
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1GiikODklVISOurHX37qjA
200
200
201
# Use the ItemtypeLocalization view to create the join on localization
202
__PACKAGE__->has_many(
203
  "localization",
204
  "Koha::Schema::Result::ItemtypeLocalization",
205
  { "foreign.code" => "self.itemtype" },
206
  { cascade_copy => 0, cascade_delete => 0 },
207
);
201
208
202
# You can replace this text with custom content, and it will be preserved on regeneration
203
1;
209
1;
(-)a/Koha/Schema/Result/ItemtypeLocalization.pm (+32 lines)
Line 0 Link Here
1
package Koha::Schema::Result::ItemtypeLocalization;
2
3
use base 'DBIx::Class::Core';
4
5
use Modern::Perl;
6
7
__PACKAGE__->table_class('DBIx::Class::ResultSource::View');
8
9
__PACKAGE__->table('itemtype_localizations');
10
__PACKAGE__->result_source_instance->is_virtual(1);
11
__PACKAGE__->result_source_instance->view_definition(
12
    "SELECT localization_id, code, lang, translation FROM localization WHERE entity='itemtypes'"
13
);
14
15
__PACKAGE__->add_columns(
16
  "localization_id",
17
  { data_type => "integer", is_auto_increment => 1, is_nullable => 0 },
18
  "code",
19
  { data_type => "varchar", is_nullable => 0, size => 64 },
20
  "lang",
21
  { data_type => "varchar", is_nullable => 0, size => 25 },
22
  "translation",
23
  { data_type => "text", is_nullable => 1 },
24
);
25
26
__PACKAGE__->belongs_to(
27
    "itemtype",
28
    "Koha::Schema::Result::Itemtype",
29
    { code => 'itemtype' }
30
);
31
32
1;
(-)a/t/db_dependent/Koha/ItemTypes.t (-2 / +36 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 18;
22
use Test::More tests => 20;
23
use Data::Dumper;
23
use Data::Dumper;
24
use Koha::Database;
24
use Koha::Database;
25
25
Lines 31-36 BEGIN { Link Here
31
my $database = Koha::Database->new();
31
my $database = Koha::Database->new();
32
my $schema   = $database->schema();
32
my $schema   = $database->schema();
33
$schema->txn_begin;
33
$schema->txn_begin;
34
Koha::ItemTypes->delete;
34
35
35
Koha::ItemType->new(
36
Koha::ItemType->new(
36
    {
37
    {
Lines 56-61 Koha::ItemType->new( Link Here
56
    }
57
    }
57
)->store;
58
)->store;
58
59
60
Koha::Localization->new(
61
    {
62
        entity      => 'itemtypes',
63
        code        => 'type1',
64
        lang        => 'en',
65
        translation => 'b translated itemtype desc'
66
    }
67
)->store;
68
Koha::Localization->new(
69
    {
70
        entity      => 'itemtypes',
71
        code        => 'type2',
72
        lang        => 'en',
73
        translation => 'a translated itemtype desc'
74
    }
75
)->store;
76
Koha::Localization->new(
77
    {
78
        entity      => 'something_else',
79
        code        => 'type2',
80
        lang        => 'en',
81
        translation => 'another thing'
82
    }
83
)->store;
84
59
my $type = Koha::ItemTypes->find('type1');
85
my $type = Koha::ItemTypes->find('type1');
60
ok( defined($type), 'first result' );
86
ok( defined($type), 'first result' );
61
is( $type->itemtype,       'type1',          'itemtype/code' );
87
is( $type->itemtype,       'type1',          'itemtype/code' );
Lines 76-79 is( $type->summary, 'summary', 'summary' ); Link Here
76
is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
102
is( $type->checkinmsg,     'checkinmsg',     'checkinmsg' );
77
is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
103
is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' );
78
104
105
my $itemtypes = Koha::ItemTypes->search_with_localization;
106
is( $itemtypes->count, 2, 'There are 2 item types' );
107
my $first_itemtype = $itemtypes->next;
108
is(
109
    $first_itemtype->translated_description,
110
    'a translated itemtype desc',
111
    'item types should be sorted by translated description'
112
);
113
79
$schema->txn_rollback;
114
$schema->txn_rollback;
80
- 

Return to bug 17835