Bug 14828: use Koha::ItemType[s] in admin/itemtypes
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2002 Paul Poulain
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 =head1 admin/itemtypes.pl
22
23 =cut
24
25 use Modern::Perl;
26 use CGI qw ( -utf8 );
27
28 use File::Spec;
29
30 use C4::Koha;
31 use C4::Context;
32 use C4::Auth;
33 use C4::Output;
34
35 use Koha::ItemTypes;
36 use Koha::Localizations;
37
38 my $input         = new CGI;
39 my $searchfield   = $input->param('description');
40 my $itemtype_code = $input->param('itemtype');
41 my $op            = $input->param('op') // 'list';
42 my @messages;
43 $searchfield =~ s/\,//g if $searchfield;
44 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
45     {   template_name   => "admin/itemtypes.tt",
46         query           => $input,
47         type            => "intranet",
48         authnotrequired => 0,
49         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
50         debug           => 1,
51     }
52 );
53
54 my $dbh = C4::Context->dbh;
55
56 my $sip_media_type = $input->param('sip_media_type');
57 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
58
59 if ( $op eq 'add_form' ) {
60     my $itemtype = Koha::ItemTypes->find($itemtype_code);
61     my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
62     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT", ( $itemtype ? $itemtype->searchcategory : '' ) );
63     $template->param(
64         itemtype  => $itemtype,
65         imagesets => $imagesets,
66         searchcategory => $searchcategory,
67     );
68 } elsif ( $op eq 'add_validate' ) {
69     my $is_a_modif   = $input->param('is_a_modif');
70     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
71     my $description  = $input->param('description');
72     my $rentalcharge = $input->param('rentalcharge');
73     my $image = $input->param('image') || q||;
74
75     my $notforloan = $input->param('notforloan') ? 1 : 0;
76     my $imageurl =
77       $image eq 'removeImage' ? ''
78       : (
79           $image eq 'remoteImage' ? $input->param('remoteImage')
80         : $image
81       );
82     my $summary        = $input->param('summary');
83     my $checkinmsg     = $input->param('checkinmsg');
84     my $checkinmsgtype = $input->param('checkinmsgtype');
85     my $hideinopac     = $input->param('hideinopac') // 0;
86     my $searchcategory = $input->param('searchcategory');
87
88     if ( $itemtype and $is_a_modif ) {    # it's a modification
89         $itemtype->description($description);
90         $itemtype->rentalcharge($rentalcharge);
91         $itemtype->notforloan($notforloan);
92         $itemtype->imageurl($imageurl);
93         $itemtype->summary($summary);
94         $itemtype->checkinmsg($checkinmsg);
95         $itemtype->checkinmsgtype($checkinmsgtype);
96         $itemtype->sip_media_type($sip_media_type);
97         $itemtype->hideinopac($hideinopac);
98         $itemtype->searchcategory($searchcategory);
99
100         eval { $itemtype->store; };
101
102         if ($@) {
103             push @messages, { type => 'error', code => 'error_on_update' };
104         } else {
105             push @messages, { type => 'message', code => 'success_on_update' };
106         }
107     } elsif ( not $itemtype and not $is_a_modif ) {
108         my $itemtype = Koha::ItemType->new(
109             {   itemtype       => $itemtype_code,
110                 description    => $description,
111                 rentalcharge   => $rentalcharge,
112                 notforloan     => $notforloan,
113                 imageurl       => $imageurl,
114                 summary        => $summary,
115                 checkinmsg     => $checkinmsg,
116                 checkinmsgtype => $checkinmsgtype,
117                 sip_media_type => $sip_media_type,
118                 hideinopac     => $hideinopac,
119                 searchcategory => $searchcategory,
120             }
121         );
122         eval { $itemtype->store; };
123
124         if ($@) {
125             push @messages, { type => 'error', code => 'error_on_insert' };
126         } else {
127             push @messages, { type => 'message', code => 'success_on_insert' };
128         }
129     } else {
130         push @messages,
131           { type => 'error',
132             code => 'already_exists',
133           };
134     }
135
136     $searchfield = '';
137     $op          = 'list';
138 } elsif ( $op eq 'delete_confirm' ) {
139
140     # Check both items and biblioitems
141     my ($total) = $dbh->selectrow_array( '
142         SELECT COUNT(*) AS total FROM (
143             SELECT itemtype AS t FROM biblioitems
144             UNION ALL
145             SELECT itype AS t FROM items
146         ) AS tmp
147         WHERE tmp.t=?
148     ', {}, $itemtype_code );
149
150     if ($total) {
151         push @messages, { type => 'error', code => 'cannot_be_deleted', total => $total };
152         $op = 'list';
153     } else {
154         my $itemtype = Koha::ItemTypes->find($itemtype_code);
155         $template->param( itemtype => $itemtype, );
156     }
157
158 } elsif ( $op eq 'delete_confirmed' ) {
159     my $itemtype = Koha::ItemTypes->find($itemtype_code);
160     my $deleted = eval { $itemtype->delete };
161     if ( $@ or not $deleted ) {
162         push @messages, { type => 'error', code => 'error_on_delete' };
163     } else {
164         push @messages, { type => 'message', code => 'success_on_delete' };
165     }
166
167     $op = 'list';
168 }
169
170 if ( $op eq 'list' ) {
171     my $itemtypes = Koha::ItemTypes->search;
172     $template->param(
173         itemtypes => $itemtypes,
174         messages  => \@messages,
175     );
176 }
177
178 $template->param( op => $op );
179
180 output_html_with_http_headers $input, $cookie, $template->output;