adding authentification with Auth.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::Search;
46 use HTML::Template;
47
48 sub StringSearch  {
49         my ($env,$searchstring,$type)=@_;
50         my $dbh = C4::Context->dbh;
51         $searchstring=~ s/\'/\\\'/g;
52         my @data=split(' ',$searchstring);
53         my $count=@data;
54         my $query="select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid and (bookfundid like \"$data[0]%\") order by bookfundid";
55         my $sth=$dbh->prepare($query);
56         $sth->execute;
57         my @results;
58         my $cnt=0;
59         while (my $data=$sth->fetchrow_hashref){
60         push(@results,$data);
61         $cnt ++;
62         }
63         #  $sth->execute;
64         $sth->finish;
65         return ($cnt,\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('searchfield');
70 my $offset=$input->param('offset');
71 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
72 my $bookfundid=$input->param('bookfundid');
73 my $pagesize=20;
74 my $op = $input->param('op');
75 $searchfield=~ s/\,//g;
76
77 my ($template, $borrowernumber, $cookie)
78     = get_template_and_user({template_name => "parameters/aqbookfund.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 $template->param(action => $script_name);
94
95
96 ################## ADD_FORM ##################################
97 # called by default. Used to create form to add or  modify a record
98 if ($op eq 'add_form') {
99         #---- if primkey exists, it's a modify action, so read values to modify...
100         my $data;
101         my $header;
102         if ($bookfundid) {
103                 my $dbh = C4::Context->dbh;
104                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
105                 $sth->execute;
106                 $data=$sth->fetchrow_hashref;
107                 $sth->finish;
108             }
109         if ($bookfundid) {
110             $header = "Modify book fund";
111         } else {
112             $header = "Add book fund";
113         }
114         $template->param(header => $header);
115         my $add_or_modify=0;
116         if ($bookfundid) {
117             $add_or_modify=1;
118         }
119         $template->param(add_or_modify => $add_or_modify);
120         $template->param(bookfundid =>$bookfundid);
121         $template->param(bookfundname =>$data->{'bookfundname'});
122         $template->param(bookfundgroup =>$data->{'bookfundgroup'});
123
124                                                                                                         # END $OP eq ADD_FORM
125 ################## ADD_VALIDATE ##################################
126 # called by add_form, used to insert/modify data in DB
127 } elsif ($op eq 'add_validate') {
128         my $dbh = C4::Context->dbh;
129         my $bookfundid=uc($input->param('bookfundid'));
130         my $query = "delete from aqbookfund where bookfundid ='$bookfundid'";
131         my $sth=$dbh->prepare($query);
132         $sth->execute;
133         $sth->finish;
134         $query = "replace aqbookfund (bookfundid,bookfundname,bookfundgroup) values (";
135         $query.= $dbh->quote($input->param('bookfundid')).",";
136         $query.= $dbh->quote($input->param('bookfundname')).",";
137         $query.= $dbh->quote($input->param('bookfundgroup')).")";
138         my $sth=$dbh->prepare($query);
139         $sth->execute;
140         $sth->finish;
141                                                                                                         # END $OP eq ADD_VALIDATE
142 ################## DELETE_CONFIRM ##################################
143 # called by default form, used to confirm deletion of data in DB
144 } elsif ($op eq 'delete_confirm') {
145         my $dbh = C4::Context->dbh;
146 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
147 #       $sth->execute;
148 #       my $total = $sth->fetchrow_hashref;
149 #       $sth->finish;
150         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
151         $sth->execute;
152         my $data=$sth->fetchrow_hashref;
153         $sth->finish;
154         $template->param(bookfundid => $bookfundid);
155         $template->param(bookfundname => $data->{'bookfundname'});
156         $template->param(bookfundgroup => $data->{'bookfundgroup'});
157                                                                                                         # END $OP eq DELETE_CONFIRM
158 ################## DELETE_CONFIRMED ##################################
159 # called by delete_confirm, used to effectively confirm deletion of data in DB
160 } elsif ($op eq 'delete_confirmed') {
161         my $dbh = C4::Context->dbh;
162         my $bookfundid=uc($input->param('bookfundid'));
163         my $query = "delete from aqbookfund where bookfundid='$bookfundid'";
164         my $sth=$dbh->prepare($query);
165         $sth->execute;
166         $sth->finish;
167                                                                                                         # END $OP eq DELETE_CONFIRMED
168 ################## DEFAULT ##################################
169 } else { # DEFAULT
170        $template->param(scriptname => $script_name);
171        if  ($searchfield ne '') {
172                 $template->param(search => 1);
173                 $template->param(searchfield => $searchfield);
174         }
175        my $env;
176        my ($count,$results)=StringSearch($env,$searchfield,'web');
177        my $toggle="white";
178        my @loop_data =();
179        for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
180                 #find out stats
181         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
182         #       $fines=$fines+0;
183            my @toggle = ();
184            my @bookfundid = ();
185            my @bookfundname = ();
186            my @bookfundgroup = ();
187            push(@toggle,$toggle);
188            push(@bookfundid,$results->[$i]{'bookfundid'});
189            push(@bookfundname,$results->[$i]{'bookfundname'});
190            push(@bookfundgroup,$results->[$i]{'bookfundgroup'});
191            if ($toggle eq 'white'){
192                         $toggle="#ffffcc";
193                 } else {
194                         $toggle="white";
195                 }
196         while (@toggle and @bookfundid and @bookfundname and @bookfundgroup) { 
197            my %row_data;
198            $row_data{toggle} = shift @toggle;
199            $row_data{bookfundid} = shift @bookfundid;
200            $row_data{bookfundname} = shift @bookfundname;
201            $row_data{bookfundgroup} = shift @bookfundgroup;
202            push(@loop_data, \%row_data);
203        }
204        }
205        $template->param(bookfund => \@loop_data);
206 } #---- END $OP eq DEFAULT
207
208 print $input->header(-cookie => $cookie), $template->output;