2370de93e19d2e5b1ee92e49201972040148b13a
[srvgit] / 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         = CGI->new;
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         flagsrequired   => { parameters => 'manage_itemtypes' },
49     }
50 );
51
52 my $dbh = C4::Context->dbh;
53
54 my $sip_media_type = $input->param('sip_media_type');
55 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
56
57 if ( $op eq 'add_form' ) {
58     my $itemtype = Koha::ItemTypes->find($itemtype_code);
59
60     my $parent_type = $itemtype ? $itemtype->parent_type : undef;
61     my $parent_types = Koha::ItemTypes->search({parent_type=>undef,itemtype => {'!='=>$itemtype_code}});
62     my $imagesets = C4::Koha::getImageSets( checked => ( $itemtype ? $itemtype->imageurl : undef ) );
63     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT");
64     my $translated_languages = C4::Languages::getTranslatedLanguages( undef , C4::Context->preference('template') );
65     $template->param(
66         itemtype  => $itemtype,
67         parent_type => $parent_type,
68         parent_types => $parent_types,
69         is_a_parent => $itemtype ? Koha::ItemTypes->search({parent_type=>$itemtype_code})->count : 0,
70         imagesets => $imagesets,
71         searchcategory => $searchcategory,
72         can_be_translated => ( scalar(@$translated_languages) > 1 ? 1 : 0 ),
73     );
74 } elsif ( $op eq 'add_validate' ) {
75     my $is_a_modif   = $input->param('is_a_modif');
76     my $itemtype     = Koha::ItemTypes->find($itemtype_code);
77     my $parent_type  = $input->param('parent_type') || undef;
78     my $description  = $input->param('description');
79     my $rentalcharge = $input->param('rentalcharge');
80     my $rentalcharge_daily = $input->param('rentalcharge_daily');
81     my $rentalcharge_hourly = $input->param('rentalcharge_hourly');
82     my $defaultreplacecost = $input->param('defaultreplacecost');
83     my $processfee = $input->param('processfee');
84     my $image = $input->param('image') || q||;
85     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
86
87     my $notforloan = $input->param('notforloan') ? 1 : 0;
88     my $imageurl =
89       $image eq 'removeImage' ? ''
90       : (
91           $image eq 'remoteImage' ? $input->param('remoteImage')
92         : $image
93       );
94     my $summary        = $input->param('summary');
95     my $checkinmsg     = $input->param('checkinmsg');
96     my $checkinmsgtype = $input->param('checkinmsgtype');
97     my $hideinopac     = $input->param('hideinopac') // 0;
98     my $searchcategory = $input->param('searchcategory');
99     my $rentalcharge_daily_calendar  = $input->param('rentalcharge_daily_calendar') // 0;
100     my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
101     my $automatic_checkin = $input->param('automatic_checkin') // 0;
102
103     if ( $itemtype and $is_a_modif ) {    # it's a modification
104         $itemtype->description($description);
105         $itemtype->parent_type($parent_type);
106         $itemtype->rentalcharge($rentalcharge);
107         $itemtype->rentalcharge_daily($rentalcharge_daily);
108         $itemtype->rentalcharge_hourly($rentalcharge_hourly);
109         $itemtype->defaultreplacecost($defaultreplacecost);
110         $itemtype->processfee($processfee);
111         $itemtype->notforloan($notforloan);
112         $itemtype->imageurl($imageurl);
113         $itemtype->summary($summary);
114         $itemtype->checkinmsg($checkinmsg);
115         $itemtype->checkinmsgtype($checkinmsgtype);
116         $itemtype->sip_media_type($sip_media_type);
117         $itemtype->hideinopac($hideinopac);
118         $itemtype->searchcategory($searchcategory);
119         $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
120         $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
121         $itemtype->automatic_checkin($automatic_checkin);
122
123         eval {
124           $itemtype->store;
125           $itemtype->replace_library_limits( \@branches );
126         };
127
128         if ($@) {
129             push @messages, { type => 'alert', code => 'error_on_update' };
130         } else {
131             push @messages, { type => 'message', code => 'success_on_update' };
132         }
133     } elsif ( not $itemtype and not $is_a_modif ) {
134         my $itemtype = Koha::ItemType->new(
135             {
136                 itemtype            => $itemtype_code,
137                 description         => $description,
138                 parent_type         => $parent_type,
139                 rentalcharge        => $rentalcharge,
140                 rentalcharge_daily  => $rentalcharge_daily,
141                 rentalcharge_hourly => $rentalcharge_hourly,
142                 defaultreplacecost  => $defaultreplacecost,
143                 processfee          => $processfee,
144                 notforloan          => $notforloan,
145                 imageurl            => $imageurl,
146                 summary             => $summary,
147                 checkinmsg          => $checkinmsg,
148                 checkinmsgtype      => $checkinmsgtype,
149                 sip_media_type      => $sip_media_type,
150                 hideinopac          => $hideinopac,
151                 searchcategory      => $searchcategory,
152                 rentalcharge_daily_calendar  => $rentalcharge_daily_calendar,
153                 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
154                 automatic_checkin   => $automatic_checkin,
155             }
156         );
157         eval {
158           $itemtype->store;
159           $itemtype->replace_library_limits( \@branches );
160         };
161
162         if ($@) {
163             push @messages, { type => 'alert', code => 'error_on_insert' };
164         } else {
165             push @messages, { type => 'message', code => 'success_on_insert' };
166         }
167     } else {
168         push @messages,
169           { type => 'alert',
170             code => 'already_exists',
171           };
172     }
173
174     $searchfield = '';
175     $op          = 'list';
176
177  } elsif ( $op eq 'delete_confirm' ) {
178     my $itemtype = Koha::ItemTypes->find($itemtype_code);
179     my $can_be_deleted = $itemtype->can_be_deleted();
180     if ($can_be_deleted == 0) {
181         push @messages, { type => 'alert', code => 'cannot_be_deleted'};
182         $op = 'list';
183     } else {
184         $template->param( itemtype => $itemtype, );
185     }
186
187 } elsif ( $op eq 'delete_confirmed' ) {
188
189     my $itemtype = Koha::ItemTypes->find($itemtype_code);
190     my $deleted = eval { $itemtype->delete };
191     if ( $@ or not $deleted ) {
192         push @messages, { type => 'alert', code => 'error_on_delete' };
193     } else {
194         push @messages, { type => 'message', code => 'success_on_delete' };
195     }
196
197     $op = 'list';
198 }
199
200 if ( $op eq 'list' ) {
201     $template->param(
202         itemtypes => scalar Koha::ItemTypes->search,
203         messages  => \@messages,
204     );
205 }
206
207 $template->param( op => $op );
208
209 output_html_with_http_headers $input, $cookie, $template->output;