when deleting an itemtype, don't check issuingrules, but delete them too
[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 $sth=$dbh->prepare("Select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name like ?) order by rank,name");
39         $sth->execute("$data[0]\%");
40         my @results;
41         while (my $data=$sth->fetchrow_hashref) {
42             push(@results,$data);
43         }
44         #  $sth->execute;
45         $sth->finish;
46         $dbh->disconnect;
47         return (scalar(@results),\@results);
48 }
49
50 my $input = new CGI;
51 my $searchfield=$input->param('searchfield');
52 my $offset=$input->param('offset');
53 my $script_name="/cgi-bin/koha/admin/z3950servers.pl";
54
55 my $pagesize=20;
56 my $op = $input->param('op');
57 $searchfield=~ s/\,//g;
58
59 my ($template, $loggedinuser, $cookie) 
60     = get_template_and_user({template_name => "parameters/z3950servers.tmpl",
61                                 query => $input,
62                                 type => "intranet",
63                                 authnotrequired => 0,
64                                 flagsrequired => {parameters => 1},
65                                 debug => 1,
66                                 });
67
68
69 $template->param(script_name => $script_name,
70                  searchfield => $searchfield);
71
72
73 ################## ADD_FORM ##################################
74 # called by default. Used to create form to add or  modify a record
75 if ($op eq 'add_form') {
76         $template->param(add_form => 1);
77         #---- if primkey exists, it's a modify action, so read values to modify...
78         my $data;
79         if ($searchfield) {
80                 my $dbh = C4::Context->dbh;
81                 my $sth=$dbh->prepare("select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name = ?) order by rank,name");
82                 $sth->execute($searchfield);
83                 $data=$sth->fetchrow_hashref;
84                 $sth->finish;
85         }
86         
87         $template->param(host => $data->{'host'},
88                          port => $data->{'port'},
89                          db   => $data->{'db'},
90                          userid => $data->{'userid'},
91                          password => $data->{'password'},
92                          checked => $data->{'checked'},
93                          rank => $data->{'rank'});
94                                                                                                         # END $OP eq ADD_FORM
95 ################## ADD_VALIDATE ##################################
96 # called by add_form, used to insert/modify data in DB
97 } elsif ($op eq 'add_validate') {
98         $template->param(add_validate => 1);
99         my $dbh=C4::Context->dbh;
100         my $sth=$dbh->prepare("select * from z3950servers where name=?");
101         $sth->execute($input->param('searchfield'));
102         if ($sth->rows) {
103                 $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=? where name=?");
104                 $sth->execute($input->param('host'),
105                       $input->param('port'),
106                       $input->param('db'),
107                       $input->param('userid'),
108                       $input->param('password'),
109                       $input->param('searchfield'),
110                       $input->param('checked'),
111                       $input->param('rank'),
112                          $input->param('syntax'),
113                       $input->param('searchfield'),
114                       );
115         } else {
116                 $sth=$dbh->prepare("insert into z3950servers (host,port,db,userid,password,name,checked,rank,syntax) values (?, ?, ?, ?, ?, ?, ?, ?,?)");
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                       $input->param('checked'),
124                       $input->param('rank'),
125                          $input->param('syntax'),
126                       );
127         }
128         $sth->finish;
129                                                                                                         # END $OP eq ADD_VALIDATE
130 ################## DELETE_CONFIRM ##################################
131 # called by default form, used to confirm deletion of data in DB
132 } elsif ($op eq 'delete_confirm') {
133         $template->param(delete_confirm => 1);
134         my $dbh = C4::Context->dbh;
135
136         my $sth2=$dbh->prepare("select host,port,db,userid,password,name,id,checked,rank,syntax from z3950servers where (name = ?) order by rank,name");
137         $sth2->execute($searchfield);
138         my $data=$sth2->fetchrow_hashref;
139         $sth2->finish;
140
141         $template->param(host => $data->{'host'},
142                          port => $data->{'port'},
143                          db   => $data->{'db'},
144                          userid => $data->{'userid'},
145                          password => $data->{'password'},
146                          checked => $data->{'checked'},
147                          rank => $data->{'rank'});
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
163         my $env;
164         my ($count,$results)=StringSearch($env,$searchfield,'web');
165         my @loop;
166         my $toggle = 'white';
167         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
168                         
169                 my $urlsearchfield=$results->[$i]{name};
170                 $urlsearchfield=~s/ /%20/g;
171                 my %row = ( name => $results->[$i]{'name'},
172                         host => $results->[$i]{'host'},
173                         port => $results->[$i]{'port'},
174                         db => $results->[$i]{'db'},
175                         userid =>$results->[$i]{'userid'},
176                         password => ($results->[$i]{'password'}) ? ('#######') : ('&nbsp;'),
177                         checked => $results->[$i]{'checked'},
178                         rank => $results->[$i]{'rank'},
179                         syntax => $results->[$i]{'syntax'},
180                         toggle => $toggle);
181                 push @loop, \%row;
182
183                 if ( $toggle eq 'white' )
184                 {
185                         $toggle = '#ffffcc';
186                 }
187                 else
188                 {
189                         $toggle = 'white';
190                 }
191
192         }
193         $template->param(loop => \@loop);
194         if ($offset>0) {
195                 $template->param(offsetgtzero => 1,
196                                 prevpage => $offset-$pagesize);
197         }
198         if ($offset+$pagesize<$count) {
199                 $template->param(ltcount => 1,
200                                  nextpage => $offset+$pagesize);
201         }
202 } #---- END $OP eq DEFAULT
203
204 output_html_with_http_headers $input, $cookie, $template->output;