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