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