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