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)
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.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
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
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
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
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.
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
47 my ($env,$searchstring,$type)=@_;
48 my $dbh = C4::Context->dbh;
49 $searchstring=~ s/\'/\\\'/g;
50 my @data=split(' ',$searchstring);
52 my $query="Select * from itemtypes where (description like \"$data[0]%\") order by itemtype";
53 my $sth=$dbh->prepare($query);
57 while (my $data=$sth->fetchrow_hashref){
63 return ($cnt,\@results);
67 my $searchfield=$input->param('description');
68 my $offset=$input->param('offset');
69 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
70 my $itemtype=$input->param('itemtype');
72 my $op = $input->param('op');
73 $searchfield=~ s/\,//g;
77 ################## ADD_FORM ##################################
78 # called by default. Used to create form to add or modify a record
79 if ($op eq 'add_form') {
80 #start the page and read in includes
82 print startmenu('admin');
83 #---- if primkey exists, it's a modify action, so read values to modify...
86 my $dbh = C4::Context->dbh;
87 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
89 $data=$sth->fetchrow_hashref;
94 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
95 function isNotNull(f,noalert) {
96 if (f.value.length ==0) {
101 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103 var x=f.value.toUpperCase();
107 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
108 function isNum(v,maybenull) {
109 var n = new Number(v.value);
113 if (maybenull==0 && v.value=='') {
118 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120 var t = Date.parse(f.value);
125 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
130 if (f.itemtype.value.length==0) {
131 _alertString += "- itemtype missing\\n";
133 if (!(isNotNull(window.document.Aform.description,1))) {
134 _alertString += "- description missing\\n";
136 if ((!isNum(f.loanlength,0)) && f.loanlength.value.length > 0) {
137 _alertString += "- loan length is not a number\\n";
139 if ((!isNum(f.rentalcharge,0)) && f.rentalcharge.value.length > 0) {
140 _alertString += "- rental charge is not a number\\n";
142 if (_alertString.length==0) {
143 document.Aform.submit();
145 alertString2 = "Form not submitted because of the following problem(s)\\n";
146 alertString2 += "------------------------------------------------------------------------------------\\n\\n";
147 alertString2 += _alertString;
155 print "<h1>Modify item type</h1>";
157 print "<h1>Add item type</h1>";
159 print "<form action='$script_name' name=Aform method=post>";
160 print "<input type=hidden name=op value='add_validate'>";
161 print "<input type=hidden name=checked value=0>";
164 print "<tr><td>Item type</td><td><input type=hidden name=itemtype value=$itemtype>$itemtype</td></tr>";
166 print "<tr><td>Item type</td><td><input type=text name=itemtype size=5 maxlength=3 onBlur=toUC(this)></td></tr>";
168 print "<tr><td>Description</td><td><input type=text name=description size=40 maxlength=80 value='$data->{'description'}'> </td></tr>";
169 print "<tr><td>loan length</td><td><input type=text name=loanlength value='$data->{'loanlength'}'></td></tr>";
170 if ($data->{'renewalsallowed'} eq 1) {
171 print "<tr><td>Renewals allowed</td><td><input type=checkbox name=renewalsallowed checked value=1></td></tr>";
173 print "<tr><td>Renewals allowed</td><td><input type=checkbox name=renewalsallowed value=1></td></tr>";
175 # print "<tr><td>Renewals allowed</td><td><input type=text name=renewalsallowed value='$data->{'renewalsallowed'}'></td></tr>";
176 print "<tr><td>Rental charge</td><td><input type=text name=rentalcharge value='$data->{'rentalcharge'}'></td></tr>";
177 print "<tr><td> </td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
181 # END $OP eq ADD_FORM
182 ################## ADD_VALIDATE ##################################
183 # called by add_form, used to insert/modify data in DB
184 } elsif ($op eq 'add_validate') {
185 my $dbh = C4::Context->dbh;
186 my $query = "replace itemtypes (itemtype,description,loanlength,renewalsallowed,rentalcharge) values (";
187 $query.= $dbh->quote($input->param('itemtype')).",";
188 $query.= $dbh->quote($input->param('description')).",";
189 $query.= $dbh->quote($input->param('loanlength')).",";
190 if ($input->param('renewalsallowed') ne 1) {
195 $query.= $dbh->quote($input->param('rentalcharge')).")";
196 my $sth=$dbh->prepare($query);
199 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=itemtypes.pl\"></html>";
201 # END $OP eq ADD_VALIDATE
202 ################## DELETE_CONFIRM ##################################
203 # called by default form, used to confirm deletion of data in DB
204 } elsif ($op eq 'delete_confirm') {
205 #start the page and read in includes
207 print startmenu('admin');
208 my $dbh = C4::Context->dbh;
209 my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
211 my $total = $sth->fetchrow_hashref;
213 # FIXME - There's already a $sth in this scope.
214 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
216 my $data=$sth->fetchrow_hashref;
219 print mktablerow(2,'#99cc33',bold('Item type'),bold("$itemtype"),'/images/background-mem.gif');
220 print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=itemtype value='$itemtype'>";
221 print "<tr><td>Description</td><td>$data->{'description'}</td></tr>";
222 print "<tr><td>Loan length</td><td>$data->{'loanlength'}</td></tr>";
223 print "<tr><td>Renewals allowed</td><td>$data->{'renewalsallowed'}</td></tr>";
224 print "<tr><td>Rental charge</td><td>$data->{'rentalcharge'}</td></tr>";
225 if ($total->{'total'} >0) {
226 print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
227 print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
229 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
230 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>";
232 # END $OP eq DELETE_CONFIRM
233 ################## DELETE_CONFIRMED ##################################
234 # called by delete_confirm, used to effectively confirm deletion of data in DB
235 } elsif ($op eq 'delete_confirmed') {
236 #start the page and read in includes
238 print startmenu('admin');
239 my $dbh = C4::Context->dbh;
240 my $itemtype=uc($input->param('itemtype'));
241 my $query = "delete from itemtypes where itemtype='$itemtype'";
242 my $sth=$dbh->prepare($query);
245 print "data deleted";
246 print "<form action='$script_name' method=post>";
247 print "<input type=submit value=OK>";
249 # END $OP eq DELETE_CONFIRMED
250 ################## DEFAULT ##################################
252 #start the page and read in includes
254 print startmenu('admin');
255 my @inputs=(["text","description",$searchfield],
256 ["reset","reset","clr"]);
257 print mkheadr(2,'Item types admin');
258 print mkformnotable("$script_name",@inputs);
259 if ($searchfield ne '') {
260 print "You Searched for <b>$searchfield<b><p>";
263 print mktablerow(7,'#99cc33',bold('Code'),bold('Description'),bold('loan<br>length'),bold('Renewals<br>allowed')
264 ,bold('Rental<br>charge'),' ',' ','/images/background-mem.gif');
266 my ($count,$results)=StringSearch($env,$searchfield,'web');
268 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
270 # my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
272 if ($toggle eq 'white'){
277 print mktablerow(7,$toggle,$results->[$i]{'itemtype'},
278 $results->[$i]{'description'},$results->[$i]{'loanlength'},
279 $results->[$i]{'renewalsallowed'}==1?'Yes':'No',$results->[$i]{'rentalcharge'},
280 mklink("$script_name?op=add_form&itemtype=".$results->[$i]{'itemtype'},'Edit'),
281 mklink("$script_name?op=delete_confirm&itemtype=".$results->[$i]{'itemtype'},'Delete'));
284 print "<form action='$script_name' method=post>";
285 print "<input type=hidden name=op value=add_form>";
287 my $prevpage = $offset-$pagesize;
288 print mklink("$script_name?offset=".$prevpage,'<< Prev');
290 print " ";
291 if ($offset+$pagesize<$count) {
292 my $nextpage =$offset+$pagesize;
293 print mklink("$script_name?offset=".$nextpage,'Next >>');
295 print "<br><input type=image src=\"/images/button-add-new.gif\" WIDTH=188 HEIGHT=44 ALT=\"Add itemtype\" BORDER=0 ></a><br>";
297 } #---- END $OP eq DEFAULT
298 print endmenu('admin');