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