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