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

(-)a/Koha/ItemType.pm (-32 / +53 lines)
Lines 17-23 package Koha::ItemType; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
21
use C4::Koha qw( getitemtypeimagelocation );
20
use C4::Koha qw( getitemtypeimagelocation );
22
use C4::Languages;
21
use C4::Languages;
23
use Koha::Caches;
22
use Koha::Caches;
Lines 52-59 sub store { Link Here
52
51
53
    if ( !$self->in_storage ) {
52
    if ( !$self->in_storage ) {
54
        $flush = 1;
53
        $flush = 1;
55
    }
54
    } else {
56
    else {
57
        my $self_from_storage = $self->get_from_storage;
55
        my $self_from_storage = $self->get_from_storage;
58
        $flush = 1 if ( $self_from_storage->description ne $self->description );
56
        $flush = 1 if ( $self_from_storage->description ne $self->description );
59
    }
57
    }
Lines 96-117 sub image_location { Link Here
96
sub translated_description {
94
sub translated_description {
97
    my ( $self, $lang ) = @_;
95
    my ( $self, $lang ) = @_;
98
    if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
96
    if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
97
99
        # If the value has already been fetched (eg. from sarch_with_localization),
98
        # If the value has already been fetched (eg. from sarch_with_localization),
100
        # do not search for it again
99
        # do not search for it again
101
        # Note: This is a bit hacky but should be fast
100
        # Note: This is a bit hacky but should be fast
102
        return $translated_description
101
        return $translated_description
103
             ? $translated_description
102
            ? $translated_description
104
             : $self->description;
103
            : $self->description;
105
    }
104
    }
106
    $lang ||= C4::Languages::getlanguage;
105
    $lang ||= C4::Languages::getlanguage;
107
    my $translated_description = Koha::Localizations->search({
106
    my $translated_description = Koha::Localizations->search(
108
        code => $self->itemtype,
107
        {
109
        entity => 'itemtypes',
108
            code   => $self->itemtype,
110
        lang => $lang
109
            entity => 'itemtypes',
111
    })->next;
110
            lang   => $lang
111
        }
112
    )->next;
112
    return $translated_description
113
    return $translated_description
113
         ? $translated_description->translation
114
        ? $translated_description->translation
114
         : $self->description;
115
        : $self->description;
115
}
116
}
116
117
117
=head3 translated_descriptions
118
=head3 translated_descriptions
Lines 119-136 sub translated_description { Link Here
119
=cut
120
=cut
120
121
121
sub translated_descriptions {
122
sub translated_descriptions {
122
    my ( $self ) = @_;
123
    my ($self) = @_;
123
    my @translated_descriptions = Koha::Localizations->search(
124
    my @translated_descriptions = Koha::Localizations->search(
124
        {   entity => 'itemtypes',
125
        {
126
            entity => 'itemtypes',
125
            code   => $self->itemtype,
127
            code   => $self->itemtype,
126
        }
128
        }
127
    )->as_list;
129
    )->as_list;
128
    return [ map {
130
    return [
129
        {
131
        map {
130
            lang => $_->lang,
132
            {
131
            translation => $_->translation,
133
                lang        => $_->lang,
132
        }
134
                translation => $_->translation,
133
    } @translated_descriptions ];
135
            }
136
        } @translated_descriptions
137
    ];
134
}
138
}
135
139
136
=head3 can_be_deleted
140
=head3 can_be_deleted
Lines 142-149 Counts up the number of biblioitems and items with itemtype (code) and hands bac Link Here
142
=cut
146
=cut
143
147
144
sub can_be_deleted {
148
sub can_be_deleted {
145
    my ($self) = @_;
149
    my ($self)         = @_;
146
    my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
150
    my $nb_items       = Koha::Items->search( { itype => $self->itemtype } )->count;
147
    my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
151
    my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
148
    return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
152
    return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
149
}
153
}
Lines 162-171 sub may_article_request { Link Here
162
    my $itemtype = $self->itemtype;
166
    my $itemtype = $self->itemtype;
163
    my $category = $params->{categorycode};
167
    my $category = $params->{categorycode};
164
168
165
    my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
169
    my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes(
166
        $category ? ( categorycode => $category ) : (),
170
        {
167
    });
171
            $category ? ( categorycode => $category ) : (),
168
    return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
172
        }
173
    );
174
    return ( $guess->{ $itemtype // q{} } || $guess->{'*'} ) ? 1 : q{};
169
}
175
}
170
176
171
=head3 _library_limits
177
=head3 _library_limits
Lines 176-183 sub may_article_request { Link Here
176
182
177
sub _library_limits {
183
sub _library_limits {
178
    return {
184
    return {
179
        class => "ItemtypesBranch",
185
        class   => "ItemtypesBranch",
180
        id => "itemtype",
186
        id      => "itemtype",
181
        library => "branchcode",
187
        library => "branchcode",
182
    };
188
    };
183
}
189
}
Lines 189-198 sub _library_limits { Link Here
189
=cut
195
=cut
190
196
191
sub parent {
197
sub parent {
192
    my ( $self ) = @_;
198
    my ($self) = @_;
193
    my $parent_rs = $self->_result->parent_type;
199
    my $parent_rs = $self->_result->parent_type;
194
    return unless $parent_rs;
200
    return unless $parent_rs;
195
    return Koha::ItemType->_new_from_dbic( $parent_rs );
201
    return Koha::ItemType->_new_from_dbic($parent_rs);
196
202
197
}
203
}
198
204
Lines 203-210 sub parent { Link Here
203
=cut
209
=cut
204
210
205
sub children_with_localization {
211
sub children_with_localization {
206
    my ( $self ) = @_;
212
    my ($self) = @_;
207
    return Koha::ItemTypes->search_with_localization({ parent_type => $self->itemtype });
213
    return Koha::ItemTypes->search_with_localization( { parent_type => $self->itemtype } );
214
}
215
216
=head3 to_api_mapping
217
218
This method returns the mapping for representing a Koha::ItemType object
219
on the API.
220
221
=cut
222
223
sub to_api_mapping {
224
    return { 'itemtype' => 'item_type' };
225
}
226
227
sub from_api_mapping {
228
    return { 'item_type' => 'itemtype' };
208
}
229
}
209
230
210
=head3 type
231
=head3 type
(-)a/api/v1/swagger/definitions/itemtype.yaml (-4 / +4 lines)
Lines 1-7 Link Here
1
---
1
---
2
type: object
2
type: object
3
properties:
3
properties:
4
  itemtype:
4
  item_type:
5
    type: string
5
    type: string
6
    description: Unique key, a code associated with the item type
6
    description: Unique key, a code associated with the item type
7
    readOnly: true
7
    readOnly: true
Lines 70-76 properties: Link Here
70
      - string
70
      - string
71
      - "null"
71
      - "null"
72
  sip_media_type:
72
  sip_media_type:
73
    description: SIP2 protocol media type for this itemtype
73
    description: SIP2 protocol media type for this item type
74
    type:
74
    type:
75
      - string
75
      - string
76
      - "null"
76
      - "null"
Lines 89-96 properties: Link Here
89
    description: Translations of description plain text
89
    description: Translations of description plain text
90
    type: array
90
    type: array
91
    items:
91
    items:
92
      $ref: "itemtype_translated_description.yaml"
92
      $ref: "item_type_translated_description.yaml"
93
93
94
additionalProperties: false
94
additionalProperties: false
95
required:
95
required:
96
  - itemtype
96
  - item_type
(-)a/api/v1/swagger/definitions/itemtype_translated_description.yaml (-13 lines)
Lines 1-13 Link Here
1
---
2
type: object
3
properties:
4
  lang:
5
    description: Language identifier
6
    type: string
7
  translation:
8
    description: Translated plain text
9
    type: string
10
additionalProperties: false
11
required:
12
  - lang
13
  - translation
(-)a/api/v1/swagger/paths/itemtypes.yaml (-4 / +4 lines)
Lines 1-10 Link Here
1
---
1
---
2
/itemtypes:
2
/item_types:
3
  get:
3
  get:
4
    x-mojo-to: ItemTypes#list
4
    x-mojo-to: ItemTypes#list
5
    operationId: listItemTypes
5
    operationId: listItemTypes
6
    tags:
6
    tags:
7
      - itemtypes
7
      - item_types
8
    summary: List item types
8
    summary: List item types
9
    produces:
9
    produces:
10
      - application/json
10
      - application/json
Lines 27-36 Link Here
27
      - $ref: "../swagger.yaml#/parameters/q_body"
27
      - $ref: "../swagger.yaml#/parameters/q_body"
28
    responses:
28
    responses:
29
      200:
29
      200:
30
        description: A list of itemtypes
30
        description: A list of item types
31
        schema:
31
        schema:
32
          items:
32
          items:
33
            $ref: "../swagger.yaml#/definitions/itemtype"
33
            $ref: "../swagger.yaml#/definitions/item_type"
34
          type: array
34
          type: array
35
      400:
35
      400:
36
        description: Bad request
36
        description: Bad request
(-)a/api/v1/swagger/swagger.yaml (-5 / +5 lines)
Lines 72-79 definitions: Link Here
72
    $ref: ./definitions/item.yaml
72
    $ref: ./definitions/item.yaml
73
  item_group:
73
  item_group:
74
    $ref: ./definitions/item_group.yaml
74
    $ref: ./definitions/item_group.yaml
75
  itemtype:
75
  item_type:
76
    $ref: ./definitions/itemtype.yaml
76
    $ref: ./definitions/item_type.yaml
77
  job:
77
  job:
78
    $ref: ./definitions/job.yaml
78
    $ref: ./definitions/job.yaml
79
  library:
79
  library:
Lines 265-272 paths: Link Here
265
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
265
    $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles
266
  "/import_batch_profiles/{import_batch_profile_id}":
266
  "/import_batch_profiles/{import_batch_profile_id}":
267
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
267
    $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}"
268
  /itemtypes:
268
  /item_types:
269
    $ref: ./paths/itemtypes.yaml#/~1itemtypes
269
    $ref: ./paths/item_types.yaml#/~1item_types
270
  /items:
270
  /items:
271
    $ref: ./paths/items.yaml#/~1items
271
    $ref: ./paths/items.yaml#/~1items
272
  "/items/{item_id}":
272
  "/items/{item_id}":
Lines 851-857 tags: Link Here
851
    name: items
851
    name: items
852
    x-displayName: Items
852
    x-displayName: Items
853
  - description: "Manage item types\n"
853
  - description: "Manage item types\n"
854
    name: itemtypes
854
    name: item_types
855
    x-displayName: Item Types
855
    x-displayName: Item Types
856
  - description: "Manage jobs\n"
856
  - description: "Manage jobs\n"
857
    name: jobs
857
    name: jobs
(-)a/t/db_dependent/api/v1/itemtypes.t (-12 / +11 lines)
Lines 32-50 my $builder = t::lib::TestBuilder->new; Link Here
32
my $t = Test::Mojo->new('Koha::REST::V1');
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
34
35
subtest 'list_itemtypes() tests' => sub {
35
subtest 'list_item_types() tests' => sub {
36
36
37
    plan tests => 12;
37
    plan tests => 12;
38
38
39
    $schema->storage->txn_begin;
39
    $schema->storage->txn_begin;
40
40
41
    my $itemtype = $builder->build_object(
41
    my $item_type = $builder->build_object(
42
        {
42
        {
43
            class => 'Koha::ItemTypes',
43
            class => 'Koha::ItemTypes',
44
            value => {
44
            value => {
45
                itemtype                     => 'TEST_IT',
45
                itemtype                     => 'TEST_IT',
46
                parent_type                  => undef,
46
                parent_type                  => undef,
47
                description                  => 'Test itemtype',
47
                description                  => 'Test item type',
48
                rentalcharge                 => 100.0,
48
                rentalcharge                 => 100.0,
49
                rentalcharge_daily           => 50.,
49
                rentalcharge_daily           => 50.,
50
                rentalcharge_daily_calendar  => 0,
50
                rentalcharge_daily_calendar  => 0,
Lines 54-60 subtest 'list_itemtypes() tests' => sub { Link Here
54
                processfee                   => 20.0,
54
                processfee                   => 20.0,
55
                notforloan                   => 0,
55
                notforloan                   => 0,
56
                imageurl          => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg',
56
                imageurl          => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg',
57
                summary           => 'An itemtype for testing',
57
                summary           => 'An item type for testing',
58
                checkinmsg        => 'Checking in test',
58
                checkinmsg        => 'Checking in test',
59
                checkinmsgtype    => 'message',
59
                checkinmsgtype    => 'message',
60
                sip_media_type    => 'spt',
60
                sip_media_type    => 'spt',
Lines 110-130 subtest 'list_itemtypes() tests' => sub { Link Here
110
110
111
    ## Authorized user tests
111
    ## Authorized user tests
112
    # No category, 404 expected
112
    # No category, 404 expected
113
    $t->get_ok("//$userid:$password@/api/v1/itemtypes")->status_is(200)->json_has('/0');
113
    $t->get_ok("//$userid:$password@/api/v1/item_types")->status_is(200)->json_has('/0');
114
114
115
    for my $json ( @{ $t->tx->res->json } ) {
115
    for my $json ( @{ $t->tx->res->json } ) {
116
        if ( $json->{itemtype} eq 'TEST_IT' ) {
116
        if ( $json->{item_type} eq 'TEST_IT' ) {
117
            is( $json->{description}, 'Test itemtype' );
117
            is( $json->{description}, 'Test item type' );
118
            ok( !exists $json->{translated_descriptions} );
118
            ok( !exists $json->{translated_descriptions} );
119
        }
119
        }
120
    }
120
    }
121
121
122
    $t->get_ok( "//$userid:$password@/api/v1/itemtypes" => { 'x-koha-embed' => 'translated_descriptions' } )
122
    $t->get_ok( "//$userid:$password@/api/v1/item_types" => { 'x-koha-embed' => 'translated_descriptions' } )
123
        ->status_is(200)->json_has('/0');
123
        ->status_is(200)->json_has('/0');
124
124
125
    for my $json ( @{ $t->tx->res->json } ) {
125
    for my $json ( @{ $t->tx->res->json } ) {
126
        if ( $json->{itemtype} eq 'TEST_IT' ) {
126
        if ( $json->{item_type} eq 'TEST_IT' ) {
127
            is( $json->{description}, 'Test itemtype' );
127
            is( $json->{description}, 'Test item type' );
128
            is_deeply(
128
            is_deeply(
129
                $json->{translated_descriptions},
129
                $json->{translated_descriptions},
130
                [
130
                [
Lines 136-142 subtest 'list_itemtypes() tests' => sub { Link Here
136
    }
136
    }
137
137
138
    # Unauthorized access
138
    # Unauthorized access
139
    $t->get_ok("//$unauth_userid:$password@/api/v1/itemtypes")->status_is(403);
139
    $t->get_ok("//$unauth_userid:$password@/api/v1/item_types")->status_is(403);
140
140
141
    $schema->storage->txn_rollback;
141
    $schema->storage->txn_rollback;
142
};
142
};
143
- 

Return to bug 34008