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