Clean up before final commits
[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 CGI;
42 use C4::Context;
43 use C4::Output;
44 use C4::Search;
45 use C4::Auth;
46 use C4::Interface::CGI::Output;
47
48 sub StringSearch  {
49         my ($env,$searchstring,$type)=@_;
50         my $dbh = C4::Context->dbh;
51         $searchstring=~ s/\'/\\\'/g;
52         my @data=split(' ',$searchstring);
53         my $count=@data;
54         my $query="";
55         my $sth=$dbh->prepare("Select printername,printqueue,printtype from printers where (printername like ?) order by printername");
56         $sth->execute("$data[0]%");
57         my @results;
58         my $cnt=0;
59         while (my $data=$sth->fetchrow_hashref){
60         push(@results,$data);
61         $cnt ++;
62         }
63         #  $sth->execute;
64         $sth->finish;
65         return ($cnt,\@results);
66 }
67
68 my $input = new CGI;
69 my $searchfield=$input->param('searchfield');
70 my $pkfield="";
71 my $reqsel="";
72 my $reqdel="";
73 #my $branchcode=$input->param('branchcode');
74 my $offset=$input->param('offset');
75 my $script_name="/cgi-bin/koha/admin/printers.pl";
76
77 my $pagesize=20;
78 my $op = $input->param('op');
79 $searchfield=~ s/\,//g;
80
81 my ($template, $loggedinuser, $cookie)
82     = get_template_and_user({template_name => "admin/printers.tmpl",
83                              query => $input,
84                              type => "intranet",
85                              authnotrequired => 0,
86                              flagsrequired => {parameters => 1},
87                               debug => 1,
88                              });
89
90
91 $template->param(searchfield => $searchfield,
92                  script_name => $script_name);
93
94 #start the page and read in includes
95
96 ################## ADD_FORM ##################################
97 # called by default. Used to create form to add or  modify a record
98 if ($op eq 'add_form') {
99         $template->param(add_form => 1);
100         #---- if primkey exists, it's a modify action, so read values to modify...
101         my $data;
102         if ($searchfield) {
103                 my $dbh = C4::Context->dbh;
104                 my $sth=$dbh->prepare("select printername,printqueue,printtype from printers where printername=?");
105                 $sth->execute($searchfield);
106                 $data=$sth->fetchrow_hashref;
107                 $sth->finish;
108         }
109
110         $template->param(printqueue => $data->{'printqueue'},
111                          printtype => $data->{'printtype'});
112                                                                                                         # END $OP eq ADD_FORM
113 ################## ADD_VALIDATE ##################################
114 # called by add_form, used to insert/modify data in DB
115 } elsif ($op eq 'add_validate') {
116         $template->param(add_validate => 1);
117         my $dbh = C4::Context->dbh;
118         my $sth=$dbh->prepare("replace printers (printername,printqueue,printtype) values (?,?,?)");
119         $sth->execute($input->param('printername'),$input->param('printqueue'),$input->param('printtype'));
120         $sth->finish;
121                                                                                                         # END $OP eq ADD_VALIDATE
122 ################## DELETE_CONFIRM ##################################
123 # called by default form, used to confirm deletion of data in DB
124 } elsif ($op eq 'delete_confirm') {
125         $template->param(delete_confirm => 1);
126         my $dbh = C4::Context->dbh;
127         my $sth=$dbh->prepare("select printername,printqueue,printtype from printers where printername=?");
128         $sth->execute($searchfield);
129         my $data=$sth->fetchrow_hashref;
130         $sth->finish;
131         $template->param(printqueue => $data->{'printqueue'},
132                          printtype  => $data->{'printtype'});
133         
134                                                                                                         # END $OP eq DELETE_CONFIRM
135 ################## DELETE_CONFIRMED ##################################
136 # called by delete_confirm, used to effectively confirm deletion of data in DB
137 } elsif ($op eq 'delete_confirmed') {
138         $template->param(delete_confirmed => 1);
139
140         my $dbh = C4::Context->dbh;
141         my $sth=$dbh->prepare("delete from printers where printername=?");
142         $sth->execute($searchfield);
143         $sth->finish;
144                                                                                                         # END $OP eq DELETE_CONFIRMED
145 ################## DEFAULT ##################################
146 } else { # DEFAULT
147         $template->param(else => 1);
148
149         my $env;
150         my ($count,$results)=StringSearch($env,$searchfield,'web');
151         my $toggle="white";
152         my @loop;
153         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
154                 my %row = ( printername => $results->[$i]{'printername'},
155                             printqueue  => $results->[$i]{'printqueue'},
156                             printtype   => $results->[$i]{'printtype'},
157                             toggle      => $toggle);
158                 push @loop, \%row;
159
160                 if ( $toggle eq 'white' )
161                 {
162                         $toggle = '#ffffcc';
163                 }
164                 else
165                 {
166                         $toggle = 'white';
167                 }
168         }
169         
170         $template->param(loop => \@loop);
171         
172         if ($offset>0) {
173                 $template->param(offsetgtzero => 1,
174                                  prevpage => $offset-$pagesize);
175         }
176         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
177         if ($offset+$pagesize<$count) {
178                 $template->param(ltcount => 1,
179                                  nextpage => $offset+$pagesize);
180         }
181
182 } #---- END $OP eq DEFAULT
183
184 output_html_with_http_headers $input, $cookie, $template->output;
185