Bug 10937: cleanup and rename DOCTYPECAT to ITEMTYPECAT
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 admin/itemtypes.pl
21
22 script to administer the categories table
23 written 20/02/2002 by paul.poulain@free.fr
24  This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
25
26  ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29         - the default screen is build (with all records, or filtered datas).
30         - the   user can clic on add, modify or delete record.
31  if $op=add_form
32         - if primkey exists, this is a modification,so we read the $primkey record
33         - builds the add/modify form
34  if $op=add_validate
35         - the user has just send datas, so we create/modify the record
36  if $op=delete_form
37         - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirm
39         - we delete the record having primkey=$primkey
40
41 =cut
42
43 use strict;
44 #use warnings; FIXME - Bug 2505
45 use CGI qw ( -utf8 );
46
47 use List::Util qw/min/;
48 use File::Spec;
49
50 use C4::Koha;
51 use C4::Context;
52 use C4::Auth;
53 use C4::Output;
54
55 sub StringSearch {
56     my ( $searchstring, $type ) = @_;
57     my $dbh = C4::Context->dbh;
58     $searchstring =~ s/\'/\\\'/g;
59     my @data = split( ' ', $searchstring );
60     my $sth = $dbh->prepare(
61         "SELECT * FROM itemtypes WHERE (description LIKE ?) ORDER BY itemtype"
62         );
63     $sth->execute("$data[0]%");
64     return $sth->fetchall_arrayref({});         # return ref-to-array of ref-to-hashes
65                                                                 # like [ fetchrow_hashref(), fetchrow_hashref() ... ]
66 }
67
68 my $input       = new CGI;
69 my $searchfield = $input->param('description');
70 my $script_name = "/cgi-bin/koha/admin/itemtypes.pl";
71 my $itemtype    = $input->param('itemtype');
72 my $op          = $input->param('op') // 'list';
73 my @messages;
74 $searchfield =~ s/\,//g;
75 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76     {
77         template_name   => "admin/itemtypes.tt",
78         query           => $input,
79         type            => "intranet",
80         authnotrequired => 0,
81         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
82         debug           => 1,
83     }
84 );
85
86 $template->param(script_name => $script_name);
87 if ($op) {
88         $template->param($op  => 1); # we show only the TMPL_VAR names $op
89 }
90
91 my $dbh = C4::Context->dbh;
92
93 my $sip_media_type = $input->param('sip_media_type');
94 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
95
96 ################## ADD_FORM ##################################
97 # called by default. Used to create form to add or  modify a record
98 if ( $op eq 'add_form' ) {
99     #---- if primkey exists, it's a modify action, so read values to modify...
100     my $data;
101     if ($itemtype) {
102         my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
103         $sth->execute($itemtype);
104         $data = $sth->fetchrow_hashref;
105     }
106
107     my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
108
109     my $remote_image = undef;
110     if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
111         $remote_image = $data->{imageurl};
112     }
113
114     my $searchcategory = GetAuthorisedValues("ITEMTYPECAT", $data->{'searchcategory'});
115
116     $template->param(
117         itemtype        => $itemtype,
118         description     => $data->{'description'},
119         rentalcharge    => sprintf( "%.2f", $data->{'rentalcharge'} ),
120         notforloan      => $data->{'notforloan'},
121         imageurl        => $data->{'imageurl'},
122         template        => C4::Context->preference('template'),
123         summary         => $data->{summary},
124         checkinmsg      => $data->{'checkinmsg'},
125         checkinmsgtype  => $data->{'checkinmsgtype'},
126         imagesets       => $imagesets,
127         remote_image    => $remote_image,
128         sip_media_type  => $data->{sip_media_type},
129         hideinopac      => $data->{'hideinopac'},
130         searchcategory  => $searchcategory,
131     );
132
133     # END $OP eq ADD_FORM
134 ################## ADD_VALIDATE ##################################
135     # called by add_form, used to insert/modify data in DB
136 }
137 elsif ( $op eq 'add_validate' ) {
138     my $is_a_modif = $input->param('is_a_modif');
139     my ( $already_exists ) = $dbh->selectrow_array(q|
140         SELECT itemtype
141         FROM   itemtypes
142         WHERE  itemtype = ?
143     |, undef, $itemtype );
144     if ( $already_exists and $is_a_modif ) { # it's a modification
145         my $query2 = '
146             UPDATE itemtypes
147             SET    description = ?
148                  , rentalcharge = ?
149                  , notforloan = ?
150                  , imageurl = ?
151                  , summary = ?
152                  , checkinmsg = ?
153                  , checkinmsgtype = ?
154                  , sip_media_type = ?
155                  , hideinopac = ?
156                  , searchcategory = ?
157             WHERE itemtype = ?
158         ';
159         my $sth = $dbh->prepare($query2);
160         $sth->execute(
161             $input->param('description'),
162             $input->param('rentalcharge'),
163             ( $input->param('notforloan') ? 1 : 0 ),
164             (
165                 $input->param('image') eq 'removeImage' ? '' : (
166                       $input->param('image') eq 'remoteImage'
167                     ? $input->param('remoteImage')
168                     : $input->param('image') . ""
169                 )
170             ),
171             $input->param('summary'),
172             $input->param('checkinmsg'),
173             $input->param('checkinmsgtype'),
174             $sip_media_type,
175             $input->param('hideinopac') ? 1 : 0,
176             $input->param('searchcategory'),
177             $input->param('itemtype')
178         );
179     }
180     elsif ( not $already_exists and not $is_a_modif ) {
181         my $query = "
182             INSERT INTO itemtypes
183                 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type, hideinopac, searchcategory)
184             VALUES
185                 (?,?,?,?,?,?,?,?,?,?,?);
186             ";
187         my $sth = $dbh->prepare($query);
188                 my $image = $input->param('image');
189         $sth->execute(
190             $input->param('itemtype'),
191             $input->param('description'),
192             $input->param('rentalcharge'),
193             $input->param('notforloan') ? 1 : 0,
194             $image eq 'removeImage' ?           ''                 :
195             $image eq 'remoteImage' ? $input->param('remoteImage') :
196             $image,
197             $input->param('summary'),
198             $input->param('checkinmsg'),
199             $input->param('checkinmsgtype'),
200             $sip_media_type,
201             $input->param('hideinopac') ? 1 : 0,
202             $input->param('searchcategory'),
203         );
204     }
205     else {
206         push @messages, {
207             type => 'error',
208             code => 'already_exists',
209         };
210     }
211
212     $searchfield = '';
213     $op = 'list';
214     # END $OP eq ADD_VALIDATE
215 ################## DELETE_CONFIRM ##################################
216     # called by default form, used to confirm deletion of data in DB
217 }
218 elsif ( $op eq 'delete_confirm' ) {
219     # Check both items and biblioitems
220     my $sth = $dbh->prepare('
221         SELECT COUNT(*) AS total FROM (
222             SELECT itemtype AS t FROM biblioitems
223             UNION ALL
224             SELECT itype AS t FROM items
225         ) AS tmp
226         WHERE tmp.t=?
227     ');
228     $sth->execute($itemtype);
229     my $total = $sth->fetchrow_hashref->{'total'};
230
231     my $sth =
232       $dbh->prepare(
233 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
234       );
235     $sth->execute($itemtype);
236     my $data = $sth->fetchrow_hashref;
237     $template->param(
238         itemtype        => $itemtype,
239         description     => $data->{description},
240         rentalcharge    => sprintf( "%.2f", $data->{rentalcharge} ),
241         imageurl        => $data->{imageurl},
242         total           => $total
243     );
244
245     # END $OP eq DELETE_CONFIRM
246 ################## DELETE_CONFIRMED ##################################
247   # called by delete_confirm, used to effectively confirm deletion of data in DB
248 }
249 elsif ( $op eq 'delete_confirmed' ) {
250     my $itemtype = uc( $input->param('itemtype') );
251     my $sth      = $dbh->prepare("delete from itemtypes where itemtype=?");
252     $sth->execute($itemtype);
253     $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
254     $sth->execute($itemtype);
255     print $input->redirect('itemtypes.pl');
256     exit;
257     # END $OP eq DELETE_CONFIRMED
258 ################## DEFAULT ##################################
259 }
260
261 if ( $op eq 'list' ) {
262     my ($results) = StringSearch( $searchfield, 'web' );
263     my @loop;
264     foreach my $itemtype ( @{$results} ) {
265         $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
266         $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
267         push( @loop, $itemtype );
268     }
269
270     $template->param(
271         loop     => \@loop,
272         else     => 1,
273         messages => \@messages,
274     );
275 }
276
277 output_html_with_http_headers $input, $cookie, $template->output;