Bug 7298: (follow-up) fix uninitialized variable warning
[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,recordtype";
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     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 => 'parameters_remaining_permissions'},
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
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(
96 "select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name"
97         );
98         $sth->execute($searchfield);
99         $data = $sth->fetchrow_hashref;
100         $sth->finish;
101     }
102     $template->param( $_ => $data->{$_} )
103       for (qw( host port db userid password checked rank timeout encoding ));
104     $template->param( $_ . $data->{$_} => 1 ) for (qw( syntax recordtype ));
105
106     # END $OP eq ADD_FORM
107 ################## ADD_VALIDATE ##################################
108     # called by add_form, used to insert/modify data in DB
109 } elsif ($op eq 'add_validate') {
110         my $dbh=C4::Context->dbh;
111         my $sth=$dbh->prepare("select * from z3950servers where name=?");
112         $sth->execute($input->param('searchfield'));
113         my $checked = $input->param('checked') ? 1 : 0;
114         if ($sth->rows) {
115         $template->param(confirm_update => 1);
116              $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=?,encoding=?,timeout=?,recordtype=? where name=?");
117                 $sth->execute($input->param('host'),
118                       $input->param('port'),
119                       $input->param('db'),
120                       $input->param('userid'),
121                       $input->param('password'),
122                       $input->param('searchfield'),
123                       $checked,
124                       $input->param('rank'),
125                           $input->param('syntax'),
126               $input->param('encoding'),
127               $input->param('timeout'),
128               $input->param('recordtype'),
129                       $input->param('searchfield'),
130                       );
131         } 
132         else {
133         $template->param(confirm_add => 1);
134                 $sth=$dbh->prepare(
135                   "INSERT INTO z3950servers " .
136               "(host,port,db,userid,password,name,checked,rank,syntax,encoding,timeout,recordtype) " .
137                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
138         $sth->execute(
139             $input->param('host'),     $input->param('port'),
140             $input->param('db'),       $input->param('userid'),
141             $input->param('password'), $input->param('searchfield'),
142             $checked,                  $input->param('rank'),
143             $input->param('syntax'),   $input->param('encoding'),
144             $input->param('timeout'),  $input->param('recordtype')
145         );
146     }
147     $sth->finish;
148
149     # END $OP eq ADD_VALIDATE
150 ################## DELETE_CONFIRM ##################################
151 # called by default form, used to confirm deletion of data in DB
152 } elsif ($op eq 'delete_confirm') {
153     $template->param( delete_confirm => 1 );
154     my $dbh = C4::Context->dbh;
155
156     my $sth2 = $dbh->prepare(
157 "select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name"
158     );
159     $sth2->execute($searchfield);
160     my $data = $sth2->fetchrow_hashref;
161     $sth2->finish;
162
163     $template->param(
164         host       => $data->{'host'},
165         port       => $data->{'port'},
166         db         => $data->{'db'},
167         userid     => $data->{'userid'},
168         password   => $data->{'password'},
169         checked    => $data->{'checked'},
170         rank       => $data->{'rank'},
171         syntax     => $data->{'syntax'},
172         timeout    => $data->{'timeout'},
173         recordtype => $data->{'recordtype'},
174         encoding   => $data->{'encoding'}
175     );
176
177                                                                                                         # END $OP eq DELETE_CONFIRM
178 ################## DELETE_CONFIRMED ##################################
179 # called by delete_confirm, used to effectively confirm deletion of data in DB
180 } elsif ($op eq 'delete_confirmed') {
181         $template->param(delete_confirmed => 1);
182         my $dbh=C4::Context->dbh;
183         my $sth=$dbh->prepare("delete from z3950servers where name=?");
184         $sth->execute($searchfield);
185         $sth->finish;
186                                                                                                         # END $OP eq DELETE_CONFIRMED
187 ################## DEFAULT ##################################
188 } else { # DEFAULT
189         $template->param(else => 1);
190         my ($count,$results)=StringSearch($searchfield,'web');
191         my @loop;
192
193         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
194                 my $urlsearchfield=$results->[$i]{name};
195                 $urlsearchfield=~s/ /%20/g;
196                 my %row = ( name => $results->[$i]{'name'},
197                         host => $results->[$i]{'host'},
198                         port => $results->[$i]{'port'},
199                         db => $results->[$i]{'db'},
200                         userid =>$results->[$i]{'userid'},
201                         password => ($results->[$i]{'password'}) ? ('#######') : ('&nbsp;'),
202                         checked => $results->[$i]{'checked'},
203                         rank => $results->[$i]{'rank'},
204                         syntax => $results->[$i]{'syntax'},
205                         encoding => $results->[$i]{'encoding'},
206       timeout => $results->[$i]{'timeout'},
207             recordtype => $results->[$i]{'recordtype'});
208                 push @loop, \%row;
209
210         }
211         $template->param(loop => \@loop);
212         if ($offset>0) {
213                 $template->param(offsetgtzero => 1,
214                                 prevpage => $offset-$pagesize);
215         }
216         if ($offset+$pagesize<$count) {
217                 $template->param(ltcount => 1,
218                                  nextpage => $offset+$pagesize);
219         }
220 } #---- END $OP eq DEFAULT
221 output_html_with_http_headers $input, $cookie, $template->output;