Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 use Carp;
21
22 use C4::Koha;
23 use C4::Languages;
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 image_location
41
42 =cut
43
44 sub image_location {
45     my ( $self, $interface ) = @_;
46     return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
47 }
48
49 =head3 translated_description
50
51 =cut
52
53 sub translated_description {
54     my ( $self, $lang ) = @_;
55     if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
56         # If the value has already been fetched (eg. from sarch_with_localization),
57         # do not search for it again
58         # Note: This is a bit hacky but should be fast
59         return $translated_description
60              ? $translated_description
61              : $self->description;
62     }
63     $lang ||= C4::Languages::getlanguage;
64     my $translated_description = Koha::Localizations->search({
65         code => $self->itemtype,
66         entity => 'itemtypes',
67         lang => $lang
68     })->next;
69     return $translated_description
70          ? $translated_description->translation
71          : $self->description;
72 }
73
74 =head3 translated_descriptions
75
76 =cut
77
78 sub translated_descriptions {
79     my ( $self ) = @_;
80     my @translated_descriptions = Koha::Localizations->search(
81         {   entity => 'itemtypes',
82             code   => $self->itemtype,
83         }
84     );
85     return [ map {
86         {
87             lang => $_->lang,
88             translation => $_->translation,
89         }
90     } @translated_descriptions ];
91 }
92
93 =head3 can_be_deleted
94
95 my $can_be_deleted = Koha::ItemType->can_be_deleted();
96
97 Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
98
99 =cut
100
101 sub can_be_deleted {
102     my ($self) = @_;
103     my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
104     my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
105     return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
106 }
107
108 =head3 may_article_request
109
110     Returns true if it is likely possible to make an article request for
111     this item type.
112     Optional parameter: categorycode (for patron).
113
114 =cut
115
116 sub may_article_request {
117     my ( $self, $params ) = @_;
118     return q{} if !C4::Context->preference('ArticleRequests');
119     my $itemtype = $self->itemtype;
120     my $category = $params->{categorycode};
121
122     my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({
123         $category ? ( categorycode => $category ) : (),
124     });
125     return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
126 }
127
128 =head3 _library_limits
129
130  configure library limits
131
132 =cut
133
134 sub _library_limits {
135     return {
136         class => "ItemtypesBranch",
137         id => "itemtype",
138         library => "branchcode",
139     };
140 }
141
142 =head3 parent
143
144     Returns the ItemType object of the parent_type or undef.
145
146 =cut
147
148 sub parent {
149     my ( $self ) = @_;
150     my $parent_rs = $self->_result->parent_type;
151     return unless $parent_rs;
152     return Koha::ItemType->_new_from_dbic( $parent_rs );
153
154 }
155
156 =head3 children_with_localization
157
158     Returns the ItemType objects of the children of this type or undef.
159
160 =cut
161
162 sub children_with_localization {
163     my ( $self ) = @_;
164     return Koha::ItemTypes->search_with_localization({ parent_type => $self->itemtype });
165 }
166
167 =head3 type
168
169 =cut
170
171 sub _type {
172     return 'Itemtype';
173 }
174
175 1;