Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
[koha.git] / admin / aqbookfund.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget 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::Auth;
43 use C4::Context;
44 use C4::Output;
45 use C4::Interface::CGI::Output;
46 use C4::Search;
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 bookfundid,bookfundname,bookfundgroup from aqbookfund where (bookfundid like \"$data[0]%\") order by bookfundid";
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('searchfield');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
73 my $bookfundid=$input->param('bookfundid');
74 my $pagesize=20;
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77
78 my ($template, $borrowernumber, $cookie)
79     = get_template_and_user({template_name => "parameters/aqbookfund.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 $template->param(action => $script_name);
95
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         my $header;
103         if ($bookfundid) {
104                 my $dbh = C4::Context->dbh;
105                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
106                 $sth->execute;
107                 $data=$sth->fetchrow_hashref;
108                 $sth->finish;
109             }
110         if ($bookfundid) {
111             $header = "Modify book fund";
112         } else {
113             $header = "Add book fund";
114         }
115         $template->param(header => $header);
116         my $add_or_modify=0;
117         if ($bookfundid) {
118             $add_or_modify=1;
119         }
120         $template->param(add_or_modify => $add_or_modify);
121         $template->param(bookfundid =>$bookfundid);
122         $template->param(bookfundname =>$data->{'bookfundname'});
123         $template->param(bookfundgroup =>$data->{'bookfundgroup'});
124
125                                                                                                         # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127 # called by add_form, used to insert/modify data in DB
128 } elsif ($op eq 'add_validate') {
129         my $dbh = C4::Context->dbh;
130         my $bookfundid=uc($input->param('bookfundid'));
131         my $query = "delete from aqbookfund where bookfundid ='$bookfundid'";
132         my $sth=$dbh->prepare($query);
133         $sth->execute;
134         $sth->finish;
135         $query = "replace aqbookfund (bookfundid,bookfundname,bookfundgroup) values (";
136         $query.= $dbh->quote($input->param('bookfundid')).",";
137         $query.= $dbh->quote($input->param('bookfundname')).",";
138         $query.= $dbh->quote($input->param('bookfundgroup')).")";
139         my $sth=$dbh->prepare($query);
140         $sth->execute;
141         $sth->finish;
142                                                                                                         # END $OP eq ADD_VALIDATE
143 ################## DELETE_CONFIRM ##################################
144 # called by default form, used to confirm deletion of data in DB
145 } elsif ($op eq 'delete_confirm') {
146         my $dbh = C4::Context->dbh;
147 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
148 #       $sth->execute;
149 #       my $total = $sth->fetchrow_hashref;
150 #       $sth->finish;
151         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
152         $sth->execute;
153         my $data=$sth->fetchrow_hashref;
154         $sth->finish;
155         $template->param(bookfundid => $bookfundid);
156         $template->param(bookfundname => $data->{'bookfundname'});
157         $template->param(bookfundgroup => $data->{'bookfundgroup'});
158                                                                                                         # END $OP eq DELETE_CONFIRM
159 ################## DELETE_CONFIRMED ##################################
160 # called by delete_confirm, used to effectively confirm deletion of data in DB
161 } elsif ($op eq 'delete_confirmed') {
162         my $dbh = C4::Context->dbh;
163         my $bookfundid=uc($input->param('bookfundid'));
164         my $query = "delete from aqbookfund where bookfundid='$bookfundid'";
165         my $sth=$dbh->prepare($query);
166         $sth->execute;
167         $sth->finish;
168                                                                                                         # END $OP eq DELETE_CONFIRMED
169 ################## DEFAULT ##################################
170 } else { # DEFAULT
171        $template->param(scriptname => $script_name);
172        if  ($searchfield ne '') {
173                 $template->param(search => 1);
174                 $template->param(searchfield => $searchfield);
175         }
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                 #find out stats
182         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
183         #       $fines=$fines+0;
184            my @toggle = ();
185            my @bookfundid = ();
186            my @bookfundname = ();
187            my @bookfundgroup = ();
188            push(@toggle,$toggle);
189            push(@bookfundid,$results->[$i]{'bookfundid'});
190            push(@bookfundname,$results->[$i]{'bookfundname'});
191            push(@bookfundgroup,$results->[$i]{'bookfundgroup'});
192            if ($toggle eq 'white'){
193                         $toggle="#ffffcc";
194                 } else {
195                         $toggle="white";
196                 }
197         while (@toggle and @bookfundid and @bookfundname and @bookfundgroup) {
198            my %row_data;
199            $row_data{toggle} = shift @toggle;
200            $row_data{bookfundid} = shift @bookfundid;
201            $row_data{bookfundname} = shift @bookfundname;
202            $row_data{bookfundgroup} = shift @bookfundgroup;
203            push(@loop_data, \%row_data);
204        }
205        }
206        $template->param(bookfund => \@loop_data);
207 } #---- END $OP eq DEFAULT
208
209 output_html_with_http_headers $input, $cookie, $template->output;