Bug 21242: If not searching for patron, move to log viewer
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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::IssuingRules;
26 use Koha::Localizations;
27
28 use base qw(Koha::Object);
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 sub store {
41     my ($self) = @_;
42
43     $self->rentalcharge(undef)       if $self->rentalcharge eq '';
44     $self->defaultreplacecost(undef) if $self->defaultreplacecost eq '';
45     $self->processfee(undef)         if $self->processfee eq '';
46     $self->notforloan(0) unless $self->notforloan;
47     $self->hideinopac(0) unless $self->hideinopac;
48
49     return $self->SUPER::store;
50 }
51
52 =head3 image_location
53
54 =cut
55
56 sub image_location {
57     my ( $self, $interface ) = @_;
58     return C4::Koha::getitemtypeimagelocation( $interface, $self->SUPER::imageurl );
59 }
60
61 =head3 translated_description
62
63 =cut
64
65 sub translated_description {
66     my ( $self, $lang ) = @_;
67     if ( my $translated_description = eval { $self->get_column('translated_description') } ) {
68         # If the value has already been fetched (eg. from sarch_with_localization),
69         # do not search for it again
70         # Note: This is a bit hacky but should be fast
71         return $translated_description
72              ? $translated_description
73              : $self->description;
74     }
75     $lang ||= C4::Languages::getlanguage;
76     my $translated_description = Koha::Localizations->search({
77         code => $self->itemtype,
78         entity => 'itemtypes',
79         lang => $lang
80     })->next;
81     return $translated_description
82          ? $translated_description->translation
83          : $self->description;
84 }
85
86 =head3 translated_descriptions
87
88 =cut
89
90 sub translated_descriptions {
91     my ( $self ) = @_;
92     my @translated_descriptions = Koha::Localizations->search(
93         {   entity => 'itemtypes',
94             code   => $self->itemtype,
95         }
96     );
97     return [ map {
98         {
99             lang => $_->lang,
100             translation => $_->translation,
101         }
102     } @translated_descriptions ];
103 }
104
105
106
107 =head3 can_be_deleted
108
109 my $can_be_deleted = Koha::ItemType->can_be_deleted();
110
111 Counts up the number of biblioitems and items with itemtype (code) and hands back the combined number of biblioitems and items with the itemtype
112
113 =cut
114
115 sub can_be_deleted {
116     my ($self) = @_;
117     my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count;
118     my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count;
119     return $nb_items + $nb_biblioitems == 0 ? 1 : 0;
120 }
121
122 =head3 may_article_request
123
124     Returns true if it is likely possible to make an article request for
125     this item type.
126     Optional parameter: categorycode (for patron).
127
128 =cut
129
130 sub may_article_request {
131     my ( $self, $params ) = @_;
132     return q{} if !C4::Context->preference('ArticleRequests');
133     my $itemtype = $self->itemtype;
134     my $category = $params->{categorycode};
135
136     my $guess = Koha::IssuingRules->guess_article_requestable_itemtypes({
137         $category ? ( categorycode => $category ) : (),
138     });
139     return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{};
140 }
141
142 =head3 type
143
144 =cut
145
146 sub _type {
147     return 'Itemtype';
148 }
149
150 1;