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