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