Fixed Bug #542
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2 # NOTE: 4-character tabs
3
4 #script to administer the categories table
5 #written 20/02/2002 by paul.poulain@free.fr
6 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
7
8 # ALGO :
9 # this script use an $op to know what to do.
10 # if $op is empty or none of the above values,
11 #       - the default screen is build (with all records, or filtered datas).
12 #       - the   user can clic on add, modify or delete record.
13 # if $op=add_form
14 #       - if primkey exists, this is a modification,so we read the $primkey record
15 #       - builds the add/modify form
16 # if $op=add_validate
17 #       - the user has just send datas, so we create/modify the record
18 # if $op=delete_form
19 #       - we show the record having primkey=$primkey and ask for deletion validation form
20 # if $op=delete_confirm
21 #       - we delete the record having primkey=$primkey
22
23
24 # Copyright 2000-2002 Katipo Communications
25 #
26 # This file is part of Koha.
27 #
28 # Koha is free software; you can redistribute it and/or modify it under the
29 # terms of the GNU General Public License as published by the Free Software
30 # Foundation; either version 2 of the License, or (at your option) any later
31 # version.
32 #
33 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
34 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
35 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
36 #
37 # You should have received a copy of the GNU General Public License along with
38 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
39 # Suite 330, Boston, MA  02111-1307 USA
40
41 use strict;
42 use CGI;
43 use C4::Context;
44 use C4::Output;
45 use C4::Search;
46 use C4::Auth;
47 use C4::Interface::CGI::Output;
48 use HTML::Template;
49
50 sub StringSearch  {
51         my ($env,$searchstring,$type)=@_;
52         my $dbh = C4::Context->dbh;
53         $searchstring=~ s/\'/\\\'/g;
54         my @data=split(' ',$searchstring);
55         my $count=@data;
56         my $query="Select * from itemtypes where (description like \"$data[0]%\") order by itemtype";
57         my $sth=$dbh->prepare($query);
58         $sth->execute;
59         my @results;
60         my $cnt=0;
61         while (my $data=$sth->fetchrow_hashref){
62         push(@results,$data);
63         $cnt ++;
64         }
65         #  $sth->execute;
66         $sth->finish;
67         return ($cnt,\@results);
68 }
69
70 my $input = new CGI;
71 my $searchfield=$input->param('description');
72 my $offset=$input->param('offset');
73 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
74 my $itemtype=$input->param('itemtype');
75 my $pagesize=20;
76 my $op = $input->param('op');
77 $searchfield=~ s/\,//g;
78 my ($template, $borrowernumber, $cookie)
79     = get_template_and_user({template_name => "parameters/itemtypes.tmpl",
80                              query => $input,
81                              type => "intranet",
82                              authnotrequired => 0,
83                              flagsrequired => {parameters => 1},
84                              debug => 1,
85                              });
86
87 if ($op) {
88 $template->param(script_name => $script_name,
89                                                 $op              => 1); # we show only the TMPL_VAR names $op
90 } else {
91 $template->param(script_name => $script_name,
92                                                 else              => 1); # we show only the TMPL_VAR names $op
93 }
94 ################## ADD_FORM ##################################
95 # called by default. Used to create form to add or  modify a record
96 if ($op eq 'add_form') {
97         #start the page and read in includes
98         #---- if primkey exists, it's a modify action, so read values to modify...
99         my $data;
100         if ($itemtype) {
101                 my $dbh = C4::Context->dbh;
102                 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
103                 $sth->execute;
104                 $data=$sth->fetchrow_hashref;
105                 $sth->finish;
106         }
107         $template->param(itemtype => $itemtype,
108                                                         description => $data->{'description'},
109                                                         loanlength => $data->{'loanlength'},
110                                                         renewalsallowed => $data->{'renewalsallowed'},
111                                                         rentalcharge => $data->{'rentalcharge'});
112 ;
113                                                                                                         # END $OP eq ADD_FORM
114 ################## ADD_VALIDATE ##################################
115 # called by add_form, used to insert/modify data in DB
116 } elsif ($op eq 'add_validate') {
117         my $dbh = C4::Context->dbh;
118         my $query = "replace itemtypes (itemtype,description,loanlength,renewalsallowed,rentalcharge) values (";
119         $query.= $dbh->quote($input->param('itemtype')).",";
120         $query.= $dbh->quote($input->param('description')).",";
121         $query.= $dbh->quote($input->param('loanlength')).",";
122         if ($input->param('renewalsallowed') ne 1) {
123                 $query.= "0,";
124         } else {
125                 $query.= "1,";
126         }
127         $query.= $dbh->quote($input->param('rentalcharge')).")";
128         my $sth=$dbh->prepare($query);
129         $sth->execute;
130         $sth->finish;
131         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
132         exit;
133                                                                                                         # END $OP eq ADD_VALIDATE
134 ################## DELETE_CONFIRM ##################################
135 # called by default form, used to confirm deletion of data in DB
136 } elsif ($op eq 'delete_confirm') {
137         #start the page and read in includes
138         my $dbh = C4::Context->dbh;
139
140         # Check both categoryitem and biblioitems, see Bug 199
141         my $total = 0;
142         for my $table ('categoryitem', 'biblioitems') {
143            my $sth=$dbh->prepare("select count(*) as total from $table where itemtype=?");
144            $sth->execute($itemtype);
145            $total += $sth->fetchrow_hashref->{total};
146            $sth->finish;
147         }
148
149         my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype=?");
150         $sth->execute($itemtype);
151         my $data=$sth->fetchrow_hashref;
152         $sth->finish;
153
154         $template->param(itemtype => $itemtype,
155                                                         description => $data->{'description'},
156                                                         loanlength => $data->{'loanlength'},
157                                                         renewalsallowed => $data->{'renewalsallowed'},
158                                                         rentalcharge => $data->{'rentalcharge'},
159                                                         total => $total);
160                                                                                                         # END $OP eq DELETE_CONFIRM
161 ################## DELETE_CONFIRMED ##################################
162 # called by delete_confirm, used to effectively confirm deletion of data in DB
163 } elsif ($op eq 'delete_confirmed') {
164         #start the page and read in includes
165         my $dbh = C4::Context->dbh;
166         my $itemtype=uc($input->param('itemtype'));
167         my $query = "delete from itemtypes where itemtype='$itemtype'";
168         my $sth=$dbh->prepare($query);
169         $sth->execute;
170         $sth->finish;
171         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
172         exit;
173                                                                                                         # END $OP eq DELETE_CONFIRMED
174 ################## DEFAULT ##################################
175 } else { # DEFAULT
176         my $env;
177         my ($count,$results)=StringSearch($env,$searchfield,'web');
178         my $toggle="white";
179         my @loop_data;
180         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
181                 my %row_data;
182                 if ($toggle eq 'white'){
183                         $row_data{toggle}="#ffffcc";
184                 } else {
185                         $row_data{toggle}="white";
186                 }
187                 $row_data{itemtype} = $results->[$i]{'itemtype'};
188                 $row_data{description} = $results->[$i]{'description'};
189                 $row_data{loanlength} = $results->[$i]{'loanlength'};
190                 $row_data{renewalsallowed} = $results->[$i]{'renewalsallowed'};
191                 $row_data{rentalcharge} = $results->[$i]{'rentalcharge'};
192                 push(@loop_data, \%row_data);
193         }
194         $template->param(loop => \@loop_data);
195         if ($offset>0) {
196                 my $prevpage = $offset-$pagesize;
197                 $template->param(previous => "$script_name?offset=".$prevpage);
198         }
199         if ($offset+$pagesize<$count) {
200                 my $nextpage =$offset+$pagesize;
201                 $template->param(next => "$script_name?offset=".$nextpage);
202         }
203 } #---- END $OP eq DEFAULT
204 output_html_with_http_headers $input, $cookie, $template->output;
205
206 # Local Variables:
207 # tab-width: 4
208 # End: