Merge branch 'bug2505_patches' of git://git.catalyst.net.nz/koha into to-push
[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 $pagesize    = 10;
73 my $op          = $input->param('op');
74 $searchfield =~ s/\,//g;
75 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
76     {
77         template_name   => "admin/itemtypes.tmpl",
78         query           => $input,
79         type            => "intranet",
80         authnotrequired => 0,
81         flagsrequired   => { parameters => 1 },
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 } else {
90     $template->param(else => 1);
91 }
92
93 my $dbh = C4::Context->dbh;
94
95 ################## ADD_FORM ##################################
96 # called by default. Used to create form to add or  modify a record
97 if ( $op eq 'add_form' ) {
98     #---- if primkey exists, it's a modify action, so read values to modify...
99     my $data;
100     if ($itemtype) {
101         my $sth = $dbh->prepare("select * from itemtypes where itemtype=?");
102         $sth->execute($itemtype);
103         $data = $sth->fetchrow_hashref;
104     }
105
106     my $imagesets = C4::Koha::getImageSets( checked => $data->{'imageurl'} );
107
108     my $remote_image = undef;
109     if ( defined $data->{imageurl} and $data->{imageurl} =~ /^http/i ) {
110         $remote_image = $data->{imageurl};
111     }
112
113     $template->param(
114         itemtype        => $itemtype,
115         description     => $data->{'description'},
116         rentalcharge    => sprintf( "%.2f", $data->{'rentalcharge'} ),
117         notforloan      => $data->{'notforloan'},
118         imageurl        => $data->{'imageurl'},
119         template        => C4::Context->preference('template'),
120         summary         => $data->{summary},
121         imagesets       => $imagesets,
122         remote_image    => $remote_image,
123     );
124
125     # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127     # called by add_form, used to insert/modify data in DB
128 }
129 elsif ( $op eq 'add_validate' ) {
130     my $query = "
131         SELECT itemtype
132         FROM   itemtypes
133         WHERE  itemtype = ?
134     ";
135     my $sth = $dbh->prepare($query);
136     $sth->execute($itemtype);
137     if ( $sth->fetchrow ) {             # it's a modification
138         my $query2 = '
139             UPDATE itemtypes
140             SET    description = ?
141                  , rentalcharge = ?
142                  , notforloan = ?
143                  , imageurl = ?
144                  , summary = ?
145             WHERE itemtype = ?
146         ';
147         $sth = $dbh->prepare($query2);
148         $sth->execute(
149             $input->param('description'),
150             $input->param('rentalcharge'),
151             ( $input->param('notforloan') ? 1 : 0 ),
152             (
153                 $input->param('image') eq 'removeImage' ? '' : (
154                       $input->param('image') eq 'remoteImage'
155                     ? $input->param('remoteImage')
156                     : $input->param('image') . ""
157                 )
158             ),
159             $input->param('summary'),
160             $input->param('itemtype')
161         );
162     }
163     else {    # add a new itemtype & not modif an old
164         my $query = "
165             INSERT INTO itemtypes
166                 (itemtype,description,rentalcharge, notforloan, imageurl,summary)
167             VALUES
168                 (?,?,?,?,?,?);
169             ";
170         my $sth = $dbh->prepare($query);
171                 my $image = $input->param('image');
172         $sth->execute(
173             $input->param('itemtype'),
174             $input->param('description'),
175             $input->param('rentalcharge'),
176             $input->param('notforloan') ? 1 : 0,
177             $image eq 'removeImage' ?           ''                 :
178             $image eq 'remoteImage' ? $input->param('remoteImage') :
179             $image,
180             $input->param('summary'),
181         );
182     }
183
184     print $input->redirect('itemtypes.pl');
185     exit;
186
187     # END $OP eq ADD_VALIDATE
188 ################## DELETE_CONFIRM ##################################
189     # called by default form, used to confirm deletion of data in DB
190 }
191 elsif ( $op eq 'delete_confirm' ) {
192     # Check both categoryitem and biblioitems, see Bug 199
193     my $total = 0;
194     for my $table ('biblioitems') {
195         my $sth =
196           $dbh->prepare(
197             "select count(*) as total from $table where itemtype=?");
198         $sth->execute($itemtype);
199         $total += $sth->fetchrow_hashref->{total};
200     }
201
202     my $sth =
203       $dbh->prepare(
204 "select itemtype,description,rentalcharge from itemtypes where itemtype=?"
205       );
206     $sth->execute($itemtype);
207     my $data = $sth->fetchrow_hashref;
208     $template->param(
209         itemtype        => $itemtype,
210         description     => $data->{description},
211         rentalcharge    => sprintf( "%.2f", $data->{rentalcharge} ),
212         imageurl        => $data->{imageurl},
213         total           => $total
214     );
215
216     # END $OP eq DELETE_CONFIRM
217 ################## DELETE_CONFIRMED ##################################
218   # called by delete_confirm, used to effectively confirm deletion of data in DB
219 }
220 elsif ( $op eq 'delete_confirmed' ) {
221     my $itemtype = uc( $input->param('itemtype') );
222     my $sth      = $dbh->prepare("delete from itemtypes where itemtype=?");
223     $sth->execute($itemtype);
224     $sth = $dbh->prepare("delete from issuingrules where itemtype=?");
225     $sth->execute($itemtype);
226     print $input->redirect('itemtypes.pl');
227     exit;
228     # END $OP eq DELETE_CONFIRMED
229 ################## DEFAULT ##################################
230 }
231 else {    # DEFAULT
232     my ($results) = StringSearch( $searchfield, 'web' );
233     my $page = $input->param('page') || 1;
234     my $first = ( $page - 1 ) * $pagesize;
235
236     # if we are on the last page, the number of the last word to display
237     # must not exceed the length of the results array
238     my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
239     my @loop;
240     foreach my $itemtype ( @{$results}[ $first .. $last ] ) {
241         $itemtype->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtype->{imageurl} );
242         $itemtype->{rentalcharge} = sprintf( '%.2f', $itemtype->{rentalcharge} );
243         push( @loop, $itemtype );
244     }
245
246     $template->param(
247         loop           => \@loop,
248         pagination_bar => pagination_bar(
249             $script_name, getnbpages( scalar @{$results}, $pagesize ),
250             $page,        'page'
251         )
252     );
253 }    #---- END $OP eq DEFAULT
254
255 output_html_with_http_headers $input, $cookie, $template->output;