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;
76 #start the page and read in includes
78 print startmenu('admin');
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...
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)) {
137 _alertString += "- loan length is not a number\\n";
139 if (!isNum(f.rentalcharge,0)) {
140 _alertString += "- loan length 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 "data recorded";
200 print "<form action='$script_name' method=post>";
201 print "<input type=submit value=OK>";
203 # END $OP eq ADD_VALIDATE
204 ################## DELETE_CONFIRM ##################################
205 # called by default form, used to confirm deletion of data in DB
206 } elsif ($op eq 'delete_confirm') {
207 my $dbh = C4::Context->dbh;
208 my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
210 my $total = $sth->fetchrow_hashref;
212 # FIXME - There's already a $sth in this scope.
213 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
215 my $data=$sth->fetchrow_hashref;
218 print mktablerow(2,'#99cc33',bold('Item type'),bold("$itemtype"),'/images/background-mem.gif');
219 print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=itemtype value='$itemtype'>";
220 print "<tr><td>Description</td><td>$data->{'description'}</td></tr>";
221 print "<tr><td>Loan length</td><td>$data->{'loanlength'}</td></tr>";
222 print "<tr><td>Renewals allowed</td><td>$data->{'renewalsallowed'}</td></tr>";
223 print "<tr><td>Rental charge</td><td>$data->{'rentalcharge'}</td></tr>";
224 if ($total->{'total'} >0) {
225 print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
226 print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
228 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
229 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>";
231 # END $OP eq DELETE_CONFIRM
232 ################## DELETE_CONFIRMED ##################################
233 # called by delete_confirm, used to effectively confirm deletion of data in DB
234 } elsif ($op eq 'delete_confirmed') {
235 my $dbh = C4::Context->dbh;
236 my $itemtype=uc($input->param('itemtype'));
237 my $query = "delete from itemtypes where itemtype='$itemtype'";
238 my $sth=$dbh->prepare($query);
241 print "data deleted";
242 print "<form action='$script_name' method=post>";
243 print "<input type=submit value=OK>";
245 # END $OP eq DELETE_CONFIRMED
246 ################## DEFAULT ##################################
248 my @inputs=(["text","description",$searchfield],
249 ["reset","reset","clr"]);
250 print mkheadr(2,'Item types admin');
251 print mkformnotable("$script_name",@inputs);
256 if ($searchfield ne '') {
257 print "You Searched for <b>$searchfield<b><p>";
260 print mktablerow(7,'#99cc33',bold('Code'),bold('Description'),bold('loan<br>length'),bold('Renewals<br>allowed')
261 ,bold('Rental<br>charge'),' ',' ','/images/background-mem.gif');
263 my ($count,$results)=StringSearch($env,$searchfield,'web');
265 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
267 # my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
269 if ($toggle eq 'white'){
274 print mktablerow(7,$toggle,$results->[$i]{'itemtype'},
275 $results->[$i]{'description'},$results->[$i]{'loanlength'},
276 $results->[$i]{'renewalsallowed'}==1?'Yes':'No',$results->[$i]{'rentalcharge'},
277 mklink("$script_name?op=add_form&itemtype=".$results->[$i]{'itemtype'},'Edit'),
278 mklink("$script_name?op=delete_confirm&itemtype=".$results->[$i]{'itemtype'},'Delete'));
281 print "<form action='$script_name' method=post>";
282 print "<input type=hidden name=op value=add_form>";
284 my $prevpage = $offset-$pagesize;
285 print mklink("$script_name?offset=".$prevpage,'<< Prev');
287 print " ";
288 if ($offset+$pagesize<$count) {
289 my $nextpage =$offset+$pagesize;
290 print mklink("$script_name?offset=".$nextpage,'Next >>');
292 print "<br><input type=image src=\"/images/button-add-new.gif\" WIDTH=188 HEIGHT=44 ALT=\"Add itemtype\" BORDER=0 ></a><br>";
294 } #---- END $OP eq DEFAULT
295 print endmenu('admin');