Bug 11629: (folllow-up) fix various issues
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
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');
73 $searchfield =~ s/\,//g;
74 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
75     {
76         template_name   => "admin/itemtypes.tmpl",
77         query           => $input,
78         type            => "intranet",
79         authnotrequired => 0,
80         flagsrequired   => { parameters => 'parameters_remaining_permissions' },
81         debug           => 1,
82     }
83 );
84
85 $template->param(script_name => $script_name);
86 if ($op) {
87         $template->param($op  => 1); # we show only the TMPL_VAR names $op
88 } else {
89     $template->param(else => 1);
90 }
91
92 my $dbh = C4::Context->dbh;
93
94 my $sip_media_type = $input->param('sip_media_type');
95 undef($sip_media_type) if defined($sip_media_type) and $sip_media_type =~ /^\s*$/;
96
97 ################## ADD_FORM ##################################
98 # called by default. Used to create form to add or  modify a record
99 if ( $op eq 'add_form' ) {
100     #---- if primkey exists, it's a modify action, so read values to modify...
101     my $data;
102     if ($itemtype) {
103         my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
104         $sth->execute($itemtype);
105         $data = $sth->fetchrow_hashref;
106     }
107
108     my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
109
110     my $remote_image = undef;
111     if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
112         $remote_image = $data->{imageurl};
113     }
114
115     $template->param(
116         itemtype        => $itemtype,
117         description     => $data->{'description'},
118         rentalcharge    => sprintf( "%.2f", $data->{'rentalcharge'} ),
119         notforloan      => $data->{'notforloan'},
120         imageurl        => $data->{'imageurl'},
121         template        => C4::Context->preference('template'),
122         summary         => $data->{summary},
123         checkinmsg      => $data->{'checkinmsg'},
124         checkinmsgtype  => $data->{'checkinmsgtype'},
125         imagesets       => $imagesets,
126         remote_image    => $remote_image,
127         sip_media_type  => $data->{sip_media_type},
128     );
129
130     # END $OP eq ADD_FORM
131 ################## ADD_VALIDATE ##################################
132     # called by add_form, used to insert/modify data in DB
133 }
134 elsif ( $op eq 'add_validate' ) {
135     my $query = "
136         SELECT itemtype
137         FROM   itemtypes
138         WHERE  itemtype = ?
139     ";
140     my $sth = $dbh->prepare($query);
141     $sth->execute($itemtype);
142     if ( $sth->fetchrow ) {             # it's a modification
143         my $query2 = '
144             UPDATE itemtypes
145             SET    description = ?
146                  , rentalcharge = ?
147                  , notforloan = ?
148                  , imageurl = ?
149                  , summary = ?
150                  , checkinmsg = ?
151                  , checkinmsgtype = ?
152                  , sip_media_type = ?
153             WHERE itemtype = ?
154         ';
155         $sth = $dbh->prepare($query2);
156         $sth->execute(
157             $input->param('description'),
158             $input->param('rentalcharge'),
159             ( $input->param('notforloan') ? 1 : 0 ),
160             (
161                 $input->param('image') eq 'removeImage' ? '' : (
162                       $input->param('image') eq 'remoteImage'
163                     ? $input->param('remoteImage')
164                     : $input->param('image') . ""
165                 )
166             ),
167             $input->param('summary'),
168             $input->param('checkinmsg'),
169             $input->param('checkinmsgtype'),
170             $sip_media_type,
171             $input->param('itemtype')
172         );
173     }
174     else {    # add a new itemtype & not modif an old
175         my $query = "
176             INSERT INTO itemtypes
177                 (itemtype,description,rentalcharge, notforloan, imageurl, summary, checkinmsg, checkinmsgtype, sip_media_type)
178             VALUES
179                 (?,?,?,?,?,?,?,?,?);
180             ";
181         my $sth = $dbh->prepare($query);
182                 my $image = $input->param('image');
183         $sth->execute(
184             $input->param('itemtype'),
185             $input->param('description'),
186             $input->param('rentalcharge'),
187             $input->param('notforloan') ? 1 : 0,
188             $image eq 'removeImage' ?           ''                 :
189             $image eq 'remoteImage' ? $input->param('remoteImage') :
190             $image,
191             $input->param('summary'),
192             $input->param('checkinmsg'),
193             $input->param('checkinmsgtype'),
194             $sip_media_type,
195         );
196     }
197
198     print $input->redirect('itemtypes.pl');
199     exit;
200
201     # END $OP eq ADD_VALIDATE
202 ################## DELETE_CONFIRM ##################################
203     # called by default form, used to confirm deletion of data in DB
204 }
205 elsif ( $op eq 'delete_confirm' ) {
206     # Check both items and biblioitems
207     my $sth = $dbh->prepare('
208         SELECT COUNT(*) AS total FROM (
209             SELECT itemtype AS t FROM biblioitems
210             UNION ALL
211             SELECT itype AS t FROM items
212         ) AS tmp
213         WHERE tmp.t=?
214     ');
215     $sth->execute($itemtype);
216     my $total = $sth->fetchrow_hashref->{'total'};
217
218     my $sth =
219       $dbh->prepare(
220 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
221       );
222     $sth->execute($itemtype);
223     my $data = $sth->fetchrow_hashref;
224     $template->param(
225         itemtype        => $itemtype,
226         description     => $data->{description},
227         rentalcharge    => sprintf( "%.2f", $data->{rentalcharge} ),
228         imageurl        => $data->{imageurl},
229         total           => $total
230     );
231
232     # END $OP eq DELETE_CONFIRM
233 ################## DELETE_CONFIRMED ##################################
234   # called by delete_confirm, used to effectively confirm deletion of data in DB
235 }
236 elsif ( $op eq 'delete_confirmed' ) {
237     my $itemtype = uc( $input->param('itemtype') );
238     my $sth      = $dbh->prepare("delete from itemtypes where itemtype=?");
239     $sth->execute($itemtype);
240     $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
241     $sth->execute($itemtype);
242     print $input->redirect('itemtypes.pl');
243     exit;
244     # END $OP eq DELETE_CONFIRMED
245 ################## DEFAULT ##################################
246 }
247 else {    # DEFAULT
248     my ($results) = StringSearch( $searchfield, 'web' );
249     my @loop;
250     foreach my $itemtype ( @{$results} ) {
251         $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
252         $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
253         push( @loop, $itemtype );
254     }
255
256     $template->param(
257         loop           => \@loop,
258     );
259 }    #---- END $OP eq DEFAULT
260
261 output_html_with_http_headers $input, $cookie, $template->output;