really proud of this commit :-)
[koha.git] / admin / z3950servers.pl
1 #!/usr/bin/perl
2
3 #script to administer the branches 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 use C4::Context;
28 use HTML::Template;
29 use C4::Auth;
30 use C4::Interface::CGI::Output;
31
32 sub StringSearch  {
33         my ($env,$searchstring,$type)=@_;
34         my $dbh = C4::Context->dbh;
35         $searchstring=~ s/\'/\\\'/g;
36         my @data=split(' ',$searchstring);
37         my $count=@data;
38         my $query="Select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name like \"$data[0]\%\") order by rank,name";
39         my $sth=$dbh->prepare($query);
40         $sth->execute;
41         my @results;
42         my $cnt=0;
43         while (my $data=$sth->fetchrow_hashref) {
44             push(@results,$data);
45             $cnt ++;
46         }
47         #  $sth->execute;
48         $sth->finish;
49         $dbh->disconnect;
50         return ($cnt,\@results);
51 }
52
53 my $input = new CGI;
54 my $searchfield=$input->param('searchfield');
55 my $reqsel="select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name = '$searchfield') order by rank,name";
56 my $reqdel="delete from z3950servers where name='$searchfield'";
57 my $offset=$input->param('offset');
58 my $script_name="/cgi-bin/koha/admin/z3950servers.pl";
59
60 my $pagesize=20;
61 my $op = $input->param('op');
62 $searchfield=~ s/\,//g;
63
64 my ($template, $loggedinuser, $cookie) 
65     = get_template_and_user({template_name => "parameters/z3950servers.tmpl",
66                              query => $input,
67                              type => "intranet",
68                              authnotrequired => 0,
69                              debug => 1,
70                              });
71
72
73 $template->param(script_name => $script_name,
74                  searchfield => $searchfield);
75
76
77 ################## ADD_FORM ##################################
78 # called by default. Used to create form to add or  modify a record
79 if ($op eq 'add_form') {
80         $template->param(add_form => 1);
81         #---- if primkey exists, it's a modify action, so read values to modify...
82         my $data;
83         if ($searchfield) {
84                 my $dbh = C4::Context->dbh;
85                 my $sth=$dbh->prepare("select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name = '$searchfield') order by rank,name");
86                 $sth->execute;
87                 $data=$sth->fetchrow_hashref;
88                 $sth->finish;
89         }
90         
91         $template->param(host => $data->{'host'},
92                          port => $data->{'port'},
93                          db   => $data->{'db'},
94                          userid => $data->{'userid'},
95                          password => $data->{'password'},
96                          checked => $data->{'checked'},
97                          rank => $data->{'rank'});
98                                                                                                         # END $OP eq ADD_FORM
99 ################## ADD_VALIDATE ##################################
100 # called by add_form, used to insert/modify data in DB
101 } elsif ($op eq 'add_validate') {
102         $template->param(add_validate => 1);
103         my $dbh=C4::Context->dbh;
104         my $sth=$dbh->prepare("select * from z3950servers where name=?");
105         $sth->execute($input->param('searchfield'));
106         if ($sth->rows) {
107                 $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=? where name=?");
108                 $sth->execute($input->param('host'),
109                       $input->param('port'),
110                       $input->param('db'),
111                       $input->param('userid'),
112                       $input->param('password'),
113                       $input->param('searchfield'),
114                       $input->param('checked'),
115                       $input->param('rank'),
116                          $input->param('syntax'),
117                       $input->param('searchfield'),
118                       );
119         } else {
120                 $sth=$dbh->prepare("insert into z3950servers (host,port,db,userid,password,name,checked,rank,syntax) values (?, ?, ?, ?, ?, ?, ?, ?,?)");
121                 $sth->execute($input->param('host'),
122                       $input->param('port'),
123                       $input->param('db'),
124                       $input->param('userid'),
125                       $input->param('password'),
126                       $input->param('searchfield'),
127                       $input->param('checked'),
128                       $input->param('rank'),
129                          $input->param('syntax'),
130                       );
131         }
132         $sth->finish;
133                                                                                                         # END $OP eq ADD_VALIDATE
134 ################## DELETE_CONFIRM ##################################
135 # called by default form, used to confirm deletion of data in DB
136 } elsif ($op eq 'delete_confirm') {
137         $template->param(delete_confirm => 1);
138         my $dbh = C4::Context->dbh;
139
140         my $sth2=$dbh->prepare($reqsel);
141         $sth2->execute;
142         my $data=$sth2->fetchrow_hashref;
143         $sth2->finish;
144
145         $template->param(host => $data->{'host'},
146                          port => $data->{'port'},
147                          db   => $data->{'db'},
148                          userid => $data->{'userid'},
149                          password => $data->{'password'},
150                          checked => $data->{'checked'},
151                          rank => $data->{'rank'});
152
153                                                                                                         # END $OP eq DELETE_CONFIRM
154 ################## DELETE_CONFIRMED ##################################
155 # called by delete_confirm, used to effectively confirm deletion of data in DB
156 } elsif ($op eq 'delete_confirmed') {
157         $template->param(delete_confirmed => 1);
158         my $dbh=C4::Context->dbh;
159         my $sth=$dbh->prepare($reqdel);
160         $sth->execute;
161         $sth->finish;
162                                                                                                         # END $OP eq DELETE_CONFIRMED
163 ################## DEFAULT ##################################
164 } else { # DEFAULT
165         $template->param(else => 1);
166
167         my $env;
168         my ($count,$results)=StringSearch($env,$searchfield,'web');
169         my @loop;
170         my $toggle = 'white';
171         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
172                         
173                 my $urlsearchfield=$results->[$i]{name};
174                 $urlsearchfield=~s/ /%20/g;
175                 my %row = ( name => $results->[$i]{'name'},
176                         host => $results->[$i]{'host'},
177                         port => $results->[$i]{'port'},
178                         db => $results->[$i]{'db'},
179                         userid =>$results->[$i]{'userid'},
180                         password => ($results->[$i]{'password'}) ? ('#######') : ('&nbsp;'),
181                         checked => $results->[$i]{'checked'},
182                         rank => $results->[$i]{'rank'},
183                         syntax => $results->[$i]{'syntax'},
184                         toggle => $toggle);
185                 push @loop, \%row;
186
187                 if ( $toggle eq 'white' )
188                 {
189                         $toggle = '#ffffcc';
190                 }
191                 else
192                 {
193                         $toggle = 'white';
194                 }
195
196         }
197         $template->param(loop => \@loop);
198         if ($offset>0) {
199                 $template->param(offsetgtzero => 1,
200                                 prevpage => $offset-$pagesize);
201         }
202         if ($offset+$pagesize<$count) {
203                 $template->param(ltcount => 1,
204                                  nextpage => $offset+$pagesize);
205         }
206 } #---- END $OP eq DEFAULT
207
208 output_html_with_http_headers $input, $cookie, $template->output;