Bug 32336: (QA follow-up) Use $metadata->schema
[srvgit] / Koha / ItemType.pm
1 package Koha::ItemType;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20
21 use C4::Koha qw( getitemtypeimagelocation );
22 use C4::Languages;
23 use Koha::Caches;
24 use Koha::Database;
25 use Koha::CirculationRules;
26 use Koha::Localizations;
27
28 use base qw(Koha::Object Koha::Object::Limit::Library);
29
30 my $cache = Koha::Caches->get_instance();
31
32 =head1 NAME
33
34 Koha::ItemType - Koha Item type Object class
35
36 =head1 API
37
38 =head2 Class methods
39
40 =cut
41
42 =head3 store
43
44 ItemType specific store to ensure relevant caches are flushed on change
45
46 =cut
47
48 sub store {
49     my ($self) = @_;
50
51     my $flush = 0;
52
53     if ( !$self->in_storage ) {
54         $flush = 1;
55     }
56     else {
57         my $self_from_storage = $self->get_from_storage;
58         $flush = 1 if ( $self_from_storage->description ne $self->description );
59     }
60
61     $self = $self->SUPER::store;
62
63     if ($flush) {
64         my $key = "itemtype:description:en";
65         $cache->clear_from_cache($key);
66     }
67
68     return $self;
69 }
70
71 =head2 delete
72
73 ItemType specific C<delete> to clear relevant caches on delete.
74
75 =cut
76
77 sub delete {
78     my $self = shift @_;
79     $cache->clear_from_cache('itemtype:description:en');
80     $self->SUPER::delete(@_);
81 }
82
83 =head3 image_location
84
85 =cut
86
87 sub image_location {
88     my ( $self, $interface ) = @_;
89     return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
90 }
91
92 =head3 translated_description
93
94 =cut
95
96 sub translated_description {
97     my ( $self, $lang ) = @_;
98     if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
99         # If the value has already been fetched (eg. from sarch_with_localization),
100         # do not search for it again
101         # Note: This is a bit hacky but should be fast
102         return $translated_description
103              ? $translated_description
104              : $self->description;
105     }
106     $lang ||= C4::Languages::getlanguage;
107     my $translated_description = Koha::Localizations->search({
108         code => $self->itemtype,
109         entity => 'itemtypes',
110         lang => $lang
111     })->next;
112     return $translated_description
113          ? $translated_description->translation
114          : $self->description;
115 }
116
117 =head3 translated_descriptions
118
119 =cut
120
121 sub translated_descriptions {
122     my ( $self ) = @_;
123     my @translated_descriptions = Koha::Localizations->search(
124         {   entity => 'itemtypes',
125             code   => $self->itemtype,
126         }
127     )->as_list;
128     return [ map {
129         {
130             lang => $_->lang,
131             translation => $_->translation,
132         }
133     } @translated_descriptions ];
134 }
135
136 =head3 can_be_deleted
137
138 my $can_be_deleted = Koha::ItemType->can_be_deleted();
139
140 Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
141
142 =cut
143
144 sub can_be_deleted {
145     my ($self) = @_;
146     my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
147     my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
148     return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
149 }
150
151 =head3 may_article_request
152
153     Returns true if it is likely possible to make an article request for
154     this item type.
155     Optional parameter: categorycode (for patron).
156
157 =cut
158
159 sub may_article_request {
160     my ( $self, $params ) = @_;
161     return q{} if !C4::Context->preference('ArticleRequests');
162     my $itemtype = $self->itemtype;
163     my $category = $params->{categorycode};
164
165     my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
166         $category ? ( categorycode => $category ) : (),
167     });
168     return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
169 }
170
171 =head3 _library_limits
172
173  configure library limits
174
175 =cut
176
177 sub _library_limits {
178     return {
179         class => "ItemtypesBranch",
180         id => "itemtype",
181         library => "branchcode",
182     };
183 }
184
185 =head3 parent
186
187     Returns the ItemType object of the parent_type or undef.
188
189 =cut
190
191 sub parent {
192     my ( $self ) = @_;
193     my $parent_rs = $self->_result->parent_type;
194     return unless $parent_rs;
195     return Koha::ItemType->_new_from_dbic( $parent_rs );
196
197 }
198
199 =head3 children_with_localization
200
201     Returns the ItemType objects of the children of this type or undef.
202
203 =cut
204
205 sub children_with_localization {
206     my ( $self ) = @_;
207     return Koha::ItemTypes->search_with_localization({ parent_type => $self->itemtype });
208 }
209
210 =head3 type
211
212 =cut
213
214 sub _type {
215     return 'Itemtype';
216 }
217
218 1;