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