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