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