Bug 22206: (follow-up) Voted RFC changes
[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 use Koha::ItemTypes;
35 use Koha::ItemType;
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 => 'manage_itemtypes' },
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");
63     my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
64     $template->param(
65         itemtype  => $itemtype,
66         imagesets => $imagesets,
67         searchcategory => $searchcategory,
68         can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
69     );
70 } elsif ( $op eq 'add_validate' ) {
71     my $is_a_modif   = $input->param('is_a_modif');
72     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
73     my $description  = $input->param('description');
74     my $rentalcharge = $input->param('rentalcharge');
75     my $rentalcharge_daily = $input->param('rentalcharge_daily');
76     my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
77     my $defaultreplacecost = $input->param('defaultreplacecost');
78     my $processfee = $input->param('processfee');
79     my $image = $input->param('image') || q||;
80
81     my $notforloan = $input->param('notforloan') ? 1 : 0;
82     my $imageurl =
83       $image eq 'removeImage' ? ''
84       : (
85           $image eq 'remoteImage' ? $input->param('remoteImage')
86         : $image
87       );
88     my $summary        = $input->param('summary');
89     my $checkinmsg     = $input->param('checkinmsg');
90     my $checkinmsgtype = $input->param('checkinmsgtype');
91     my $hideinopac     = $input->param('hideinopac') // 0;
92     my $searchcategory = $input->param('searchcategory');
93
94     if ( $itemtype and $is_a_modif ) {    # it's a modification
95         $itemtype->description($description);
96         $itemtype->rentalcharge($rentalcharge);
97         $itemtype->rentalcharge_daily($rentalcharge_daily);
98         $itemtype->rentalcharge_hourly($rentalcharge_hourly);
99         $itemtype->defaultreplacecost($defaultreplacecost);
100         $itemtype->processfee($processfee);
101         $itemtype->notforloan($notforloan);
102         $itemtype->imageurl($imageurl);
103         $itemtype->summary($summary);
104         $itemtype->checkinmsg($checkinmsg);
105         $itemtype->checkinmsgtype($checkinmsgtype);
106         $itemtype->sip_media_type($sip_media_type);
107         $itemtype->hideinopac($hideinopac);
108         $itemtype->searchcategory($searchcategory);
109
110         eval { $itemtype->store; };
111
112         if ($@) {
113             push @messages, { type => 'error', code => 'error_on_update' };
114         } else {
115             push @messages, { type => 'message', code => 'success_on_update' };
116         }
117     } elsif ( not $itemtype and not $is_a_modif ) {
118         my $itemtype = Koha::ItemType->new(
119             {
120                 itemtype            => $itemtype_code,
121                 description         => $description,
122                 rentalcharge        => $rentalcharge,
123                 rentalcharge_daily  => $rentalcharge_daily,
124                 rentalcharge_hourly => $rentalcharge_hourly,
125                 defaultreplacecost  => $defaultreplacecost,
126                 processfee          => $processfee,
127                 notforloan          => $notforloan,
128                 imageurl            => $imageurl,
129                 summary             => $summary,
130                 checkinmsg          => $checkinmsg,
131                 checkinmsgtype      => $checkinmsgtype,
132                 sip_media_type      => $sip_media_type,
133                 hideinopac          => $hideinopac,
134                 searchcategory      => $searchcategory,
135             }
136         );
137         eval { $itemtype->store; };
138
139         if ($@) {
140             push @messages, { type => 'error', code => 'error_on_insert' };
141         } else {
142             push @messages, { type => 'message', code => 'success_on_insert' };
143         }
144     } else {
145         push @messages,
146           { type => 'error',
147             code => 'already_exists',
148           };
149     }
150
151     $searchfield = '';
152     $op          = 'list';
153
154  } elsif ( $op eq 'delete_confirm' ) {
155     my $itemtype = Koha::ItemTypes->find($itemtype_code);
156     my $can_be_deleted = $itemtype->can_be_deleted();
157     if ($can_be_deleted == 0) {
158         push @messages, { type => 'error', code => 'cannot_be_deleted'};
159         $op = 'list';
160     } else {
161         $template->param( itemtype => $itemtype, );
162     }
163
164 } elsif ( $op eq 'delete_confirmed' ) {
165
166     my $itemtype = Koha::ItemTypes->find($itemtype_code);
167     my $deleted = eval { $itemtype->delete };
168     if ( $@ or not $deleted ) {
169         push @messages, { type => 'error', code => 'error_on_delete' };
170     } else {
171         push @messages, { type => 'message', code => 'success_on_delete' };
172     }
173
174     $op = 'list';
175 }
176
177 if ( $op eq 'list' ) {
178     my $itemtypes = Koha::ItemTypes->search;
179     $template->param(
180         itemtypes => $itemtypes,
181         messages  => \@messages,
182     );
183 }
184
185 $template->param( op => $op );
186
187 output_html_with_http_headers $input, $cookie, $template->output;