Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / admin / itemtypes.pl
1 #!/usr/bin/perl
2
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)
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 * from itemtypes where (description like \"$data[0]%\") order by itemtype";
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('description');
68 my $offset=$input->param('offset');
69 my $script_name="/cgi-bin/koha/admin/itemtypes.pl";
70 my $itemtype=$input->param('itemtype');
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 ($itemtype) {
86                 my $dbh = C4::Context->dbh;
87                 my $sth=$dbh->prepare("select itemtype,description,loanlength,renewalsallowed,rentalcharge from itemtypes where itemtype='$itemtype'");
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.itemtype.value.length==0) {
131                         _alertString += "- itemtype missing\\n";
132                 }
133                 if (!(isNotNull(window.document.Aform.description,1))) {
134                         _alertString += "- description missing\\n";
135                 }
136                 if (!isNum(f.loanlength,0)) {
137                         _alertString += "- loan length is not a number\\n";
138                 }
139                 if (!isNum(f.rentalcharge,0)) {
140                         _alertString += "- loan length is not a number\\n";
141                 }
142                 if (_alertString.length==0) {
143                         document.Aform.submit();
144                 } else {
145                         alertString2 = "Form not submitted because of the following problem(s)\\n";
146                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
147                         alertString2 += _alertString;
148                         alert(alertString2);
149                 }
150         }
151         </SCRIPT>
152 printend
153 ;#/
154         if ($itemtype) {
155                 print "<h1>Modify item type</h1>";
156         } else {
157                 print "<h1>Add item type</h1>";
158         }
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>";
162         print "<table>";
163         if ($itemtype) {
164                 print "<tr><td>Item type</td><td><input type=hidden name=itemtype value=$itemtype>$itemtype</td></tr>";
165         } else {
166                 print "<tr><td>Item type</td><td><input type=text name=itemtype size=5 maxlength=3 onBlur=toUC(this)></td></tr>";
167         }
168         print "<tr><td>Description</td><td><input type=text name=description size=40 maxlength=80 value='$data->{'description'}'>&nbsp;</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>";
172         } else {
173                 print "<tr><td>Renewals allowed</td><td><input type=checkbox name=renewalsallowed value=1></td></tr>";
174         }
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>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
178 print "</table>";
179         print "</form>";
180 ;
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) {
191                 $query.= "0,";
192         } else {
193                 $query.= "1,";
194         }
195         $query.= $dbh->quote($input->param('rentalcharge')).")";
196         my $sth=$dbh->prepare($query);
197         $sth->execute;
198         $sth->finish;
199         print "data recorded";
200         print "<form action='$script_name' method=post>";
201         print "<input type=submit value=OK>";
202         print "</form>";
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'");
209         $sth->execute;
210         my $total = $sth->fetchrow_hashref;
211         $sth->finish;
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'");
214         $sth->execute;
215         my $data=$sth->fetchrow_hashref;
216         $sth->finish;
217         print mktablehdr;
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>";
227         } else {
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>";
230         }
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);
239         $sth->execute;
240         $sth->finish;
241         print "data deleted";
242         print "<form action='$script_name' method=post>";
243         print "<input type=submit value=OK>";
244         print "</form>";
245                                                                                                         # END $OP eq DELETE_CONFIRMED
246 ################## DEFAULT ##################################
247 } else { # DEFAULT
248         my @inputs=(["text","description",$searchfield],
249                 ["reset","reset","clr"]);
250         print mkheadr(2,'Item types admin');
251         print mkformnotable("$script_name",@inputs);
252         print <<printend
253
254 printend
255         ;
256         if  ($searchfield ne '') {
257                 print "You Searched for <b>$searchfield<b><p>";
258         }
259         print mktablehdr;
260         print mktablerow(7,'#99cc33',bold('Code'),bold('Description'),bold('loan<br>length'),bold('Renewals<br>allowed')
261         ,bold('Rental<br>charge'),'&nbsp;','&nbsp;','/images/background-mem.gif');
262         my $env;
263         my ($count,$results)=StringSearch($env,$searchfield,'web');
264         my $toggle="white";
265         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
266                 #find out stats
267         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
268         #       $fines=$fines+0;
269                 if ($toggle eq 'white'){
270                         $toggle="#ffffcc";
271                 } else {
272                         $toggle="white";
273                 }
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'));
279         }
280         print mktableft;
281         print "<form action='$script_name' method=post>";
282         print "<input type=hidden name=op value=add_form>";
283         if ($offset>0) {
284                 my $prevpage = $offset-$pagesize;
285                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
286         }
287         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
288         if ($offset+$pagesize<$count) {
289                 my $nextpage =$offset+$pagesize;
290                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
291         }
292         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add itemtype\" BORDER=0 ></a><br>";
293         print "</form>";
294 } #---- END $OP eq DEFAULT
295 print endmenu('admin');
296 print endpage();