Bug 30920: (follow-up) Cache key fixes
[koha.git] / 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 =head1 NAME
31
32 Koha::ItemType - Koha Item type Object class
33
34 =head1 API
35
36 =head2 Class methods
37
38 =cut
39
40 =head3 store
41
42 ItemType specific store to ensure relevant caches are flushed on change
43
44 =cut
45
46 sub store {
47     my ($self) = @_;
48
49     my $flush = 0;
50
51     if ( !$self->in_storage ) {
52         $flush = 1;
53     }
54     else {
55         my $self_from_storage = $self->get_from_storage;
56         $flush = 1 if ( $self_from_storage->description ne $self->description );
57     }
58
59     $self = $self->SUPER::store;
60
61     if ($flush) {
62         my $cache = Koha::Caches->get_instance();
63         my $key = "itemtype:description:en";
64         $cache->clear_from_cache($key);
65     }
66
67     return $self;
68 }
69
70 =head3 image_location
71
72 =cut
73
74 sub image_location {
75     my ( $self, $interface ) = @_;
76     return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
77 }
78
79 =head3 translated_description
80
81 =cut
82
83 sub translated_description {
84     my ( $self, $lang ) = @_;
85     if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
86         # If the value has already been fetched (eg. from sarch_with_localization),
87         # do not search for it again
88         # Note: This is a bit hacky but should be fast
89         return $translated_description
90              ? $translated_description
91              : $self->description;
92     }
93     $lang ||= C4::Languages::getlanguage;
94     my $translated_description = Koha::Localizations->search({
95         code => $self->itemtype,
96         entity => 'itemtypes',
97         lang => $lang
98     })->next;
99     return $translated_description
100          ? $translated_description->translation
101          : $self->description;
102 }
103
104 =head3 translated_descriptions
105
106 =cut
107
108 sub translated_descriptions {
109     my ( $self ) = @_;
110     my @translated_descriptions = Koha::Localizations->search(
111         {   entity => 'itemtypes',
112             code   => $self->itemtype,
113         }
114     )->as_list;
115     return [ map {
116         {
117             lang => $_->lang,
118             translation => $_->translation,
119         }
120     } @translated_descriptions ];
121 }
122
123 =head3 can_be_deleted
124
125 my $can_be_deleted = Koha::ItemType->can_be_deleted();
126
127 Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
128
129 =cut
130
131 sub can_be_deleted {
132     my ($self) = @_;
133     my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
134     my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
135     return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
136 }
137
138 =head3 may_article_request
139
140     Returns true if it is likely possible to make an article request for
141     this item type.
142     Optional parameter: categorycode (for patron).
143
144 =cut
145
146 sub may_article_request {
147     my ( $self, $params ) = @_;
148     return q{} if !C4::Context->preference('ArticleRequests');
149     my $itemtype = $self->itemtype;
150     my $category = $params->{categorycode};
151
152     my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
153         $category ? ( categorycode => $category ) : (),
154     });
155     return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
156 }
157
158 =head3 _library_limits
159
160  configure library limits
161
162 =cut
163
164 sub _library_limits {
165     return {
166         class => "ItemtypesBranch",
167         id => "itemtype",
168         library => "branchcode",
169     };
170 }
171
172 =head3 parent
173
174     Returns the ItemType object of the parent_type or undef.
175
176 =cut
177
178 sub parent {
179     my ( $self ) = @_;
180     my $parent_rs = $self->_result->parent_type;
181     return unless $parent_rs;
182     return Koha::ItemType->_new_from_dbic( $parent_rs );
183
184 }
185
186 =head3 children_with_localization
187
188     Returns the ItemType objects of the children of this type or undef.
189
190 =cut
191
192 sub children_with_localization {
193     my ( $self ) = @_;
194     return Koha::ItemTypes->search_with_localization({ parent_type => $self->itemtype });
195 }
196
197 =head3 type
198
199 =cut
200
201 sub _type {
202     return 'Itemtype';
203 }
204
205 1;