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