Added copyright statement to all .pl and .pm files
[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 C4::Output;
42 use CGI;
43 use C4::Search;
44 use C4::Database;
45
46 sub StringSearch  {
47         my ($env,$searchstring,$type)=@_;
48         my $dbh = &C4Connect;
49         $searchstring=~ s/\'/\\\'/g;
50         my @data=split(' ',$searchstring);
51         my $count=@data;
52         my $query="Select aqbudget.bookfundid,startdate,enddate,budgetamount,bookfundname from aqbudget,aqbookfund where aqbudget.bookfundid=aqbookfund.bookfundid and (aqbudget.bookfundid like \"$data[0]%\") order by bookfundid";
53         my $sth=$dbh->prepare($query);
54         $sth->execute;
55         my @results;
56         my $cnt=0;
57         while (my $data=$sth->fetchrow_hashref){
58         push(@results,$data);
59         $cnt ++;
60         }
61         #  $sth->execute;
62         $sth->finish;
63         $dbh->disconnect;
64         return ($cnt,\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('searchfield');
69 my $offset=$input->param('offset');
70 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
71 my $bookfundid=$input->param('bookfundid');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 print $input->header;
76
77 #start the page and read in includes
78 print startpage();
79 print startmenu('admin');
80
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or  modify a record
83 if ($op eq 'add_form') {
84         #---- if primkey exists, it's a modify action, so read values to modify...
85         my $data;
86         if ($bookfundid) {
87                 my $dbh = &C4Connect;
88                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
89                 $sth->execute;
90                 $data=$sth->fetchrow_hashref;
91                 $sth->finish;
92         }
93         print <<printend
94         <script>
95         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
96         function isNotNull(f,noalert) {
97                 if (f.value.length ==0) {
98    return false;
99                 }
100                 return true;
101         }
102         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103         function toUC(f) {
104                 var x=f.value.toUpperCase();
105                 f.value=x;
106                 return true;
107         }
108         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
109         function isNum(v,maybenull) {
110         var n = new Number(v.value);
111         if (isNaN(n)) {
112                 return false;
113                 }
114         if (maybenull==0 && v.value=='') {
115                 return false;
116         }
117         return true;
118         }
119         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120         function isDate(f) {
121                 var t = Date.parse(f.value);
122                 if (isNaN(t)) {
123                         return false;
124                 }
125         }
126         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
127         function Check(f) {
128                 var ok=1;
129                 var _alertString="";
130                 var alertString2;
131                 if (f.bookfundid.value.length==0) {
132                         _alertString += "- bookfundid missing\\n";
133                 }
134                 if (f.bookfundname.value.length==0) {
135                         _alertString += "- bookfundname missing\\n";
136                 }
137                 if (_alertString.length==0) {
138                         document.Aform.submit();
139                 } else {
140                         alertString2 = "Form not submitted because of the following problem(s)\\n";
141                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
142                         alertString2 += _alertString;
143                         alert(alertString2);
144                 }
145         }
146         </SCRIPT>
147 printend
148 ;#/
149         if ($bookfundid) {
150                 print "<h1>Modify book fund</h1>";
151         } else {
152                 print "<h1>Add book fund</h1>";
153         }
154         print "<form action='$script_name' name=Aform method=post>";
155         print "<input type=hidden name=op value='add_validate'>";
156         print "<input type=hidden name=checked value=0>";
157         print "<table>";
158         if ($bookfundid) {
159                 print "<tr><td>Book fund</td><td><input type=hidden name=bookfundid value=$bookfundid>$bookfundid</td></tr>";
160         } else {
161                 print "<tr><td>Book fund</td><td><input type=text name=bookfundid size=5 maxlength=5 onBlur=toUC(this)></td></tr>";
162         }
163         print "<tr><td>Name</td><td><input type=text name=bookfundname size=40 maxlength=80 value='$data->{'bookfundname'}'>&nbsp;</td></tr>";
164         print "<tr><td>Group</td><td><input type=text name=bookfundgroup value='$data->{'bookfundgroup'}'></td></tr>";
165         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
166 print "</table>";
167         print "</form>";
168 ;
169                                                                                                         # END $OP eq ADD_FORM
170 ################## ADD_VALIDATE ##################################
171 # called by add_form, used to insert/modify data in DB
172 } elsif ($op eq 'add_validate') {
173         my $dbh=C4Connect;
174         my $query = "replace aqbookfund (bookfundid,bookfundname,bookfundgroup) values (";
175         $query.= $dbh->quote($input->param('bookfundid')).",";
176         $query.= $dbh->quote($input->param('bookfundname')).",";
177         $query.= $dbh->quote($input->param('bookfundgroup')).")";
178         my $sth=$dbh->prepare($query);
179         $sth->execute;
180         $sth->finish;
181         print "data recorded";
182         print "<form action='$script_name' method=post>";
183         print "<input type=submit value=OK>";
184         print "</form>";
185                                                                                                         # END $OP eq ADD_VALIDATE
186 ################## DELETE_CONFIRM ##################################
187 # called by default form, used to confirm deletion of data in DB
188 } elsif ($op eq 'delete_confirm') {
189         my $dbh = &C4Connect;
190 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
191 #       $sth->execute;
192 #       my $total = $sth->fetchrow_hashref;
193 #       $sth->finish;
194         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
195         $sth->execute;
196         my $data=$sth->fetchrow_hashref;
197         $sth->finish;
198         print mktablehdr;
199         print mktablerow(2,'#99cc33',bold('Book fund'),bold("$bookfundid"),'/images/background-mem.gif');
200         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=bookfundid value='$bookfundid'>";
201         print "<tr><td>Name</td><td>$data->{'bookfundname'}</td></tr>";
202         print "<tr><td>Group</td><td>$data->{'bookfundgroup'}</td></tr>";
203 #       if ($total->{'total'} >0) {
204 #               print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
205 #               print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
206 #       } else {
207                 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
208                 print "<tr><td><INPUT type=submit value='YES'></form></td><td><form action='$script_name' method=post><input type=submit value=NO></form></td></tr>";
209 #       }
210                                                                                                         # END $OP eq DELETE_CONFIRM
211 ################## DELETE_CONFIRMED ##################################
212 # called by delete_confirm, used to effectively confirm deletion of data in DB
213 } elsif ($op eq 'delete_confirmed') {
214         my $dbh=C4Connect;
215         my $bookfundid=uc($input->param('bookfundid'));
216         my $query = "delete from aqbookfund where bookfundid='$bookfundid'";
217         my $sth=$dbh->prepare($query);
218         $sth->execute;
219         $sth->finish;
220         print "data deleted";
221         print "<form action='$script_name' method=post>";
222         print "<input type=submit value=OK>";
223         print "</form>";
224                                                                                                         # END $OP eq DELETE_CONFIRMED
225 ################## DEFAULT ##################################
226 } else { # DEFAULT
227         my @inputs=(["text","searchfield",$searchfield],
228                 ["reset","reset","clr"]);
229         print mkheadr(2,'bookfund admin');
230         print mkformnotable("$script_name",@inputs);
231         print <<printend
232
233 printend
234         ;
235         if  ($searchfield ne '') {
236                 print "You Searched for <b>$searchfield<b><p>";
237         }
238         print mktablehdr;
239         print mktablerow(6,'#99cc33',bold('Book fund'),bold('Start date'),bold('End date'),bold('Budget amount'),
240         '&nbsp;','&nbsp;','/images/background-mem.gif');
241         my $env;
242         my ($count,$results)=StringSearch($env,$searchfield,'web');
243         my $toggle="white";
244         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
245                 #find out stats
246         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
247         #       $fines=$fines+0;
248                 if ($toggle eq 'white'){
249                         $toggle="#ffffcc";
250                 } else {
251                         $toggle="white";
252                 }
253                 print mktablerow(6,$toggle,$results->[$i]{'bookfundid'},
254                 $results->[$i]{'bookfundname'},$results->[$i]{'bookfundgroup'},
255                 mklink("$script_name?op=add_form&bookfundid=".$results->[$i]{'bookfundid'},'Edit'),
256                 mklink("$script_name?op=delete_confirm&bookfundid=".$results->[$i]{'bookfundid'},'Delete'));
257         }
258         print mktableft;
259         print "<form action='$script_name' method=post>";
260         print "<input type=hidden name=op value=add_form>";
261         if ($offset>0) {
262                 my $prevpage = $offset-$pagesize;
263                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
264         }
265         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
266         if ($offset+$pagesize<$count) {
267                 my $nextpage =$offset+$pagesize;
268                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
269         }
270         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
271         print "</form>";
272 } #---- END $OP eq DEFAULT
273 print endmenu('admin');
274 print endpage();