Fixed &C4Connect problem and formating
[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
29 sub StringSearch  {
30         my ($env,$searchstring,$type)=@_;
31         my $dbh = C4::Context->dbh;
32         $searchstring=~ s/\'/\\\'/g;
33         my @data=split(' ',$searchstring);
34         my $count=@data;
35         my $query="Select host,port,db,userid,password,name,id,checked,rank from z3950servers where (name like \"$data[0]\%\") order by rank,name";
36         my $sth=$dbh->prepare($query);
37         $sth->execute;
38         my @results;
39         my $cnt=0;
40         while (my $data=$sth->fetchrow_hashref) {
41             push(@results,$data);
42             $cnt ++;
43         }
44         #  $sth->execute;
45         $sth->finish;
46         $dbh->disconnect;
47         return ($cnt,\@results);
48 }
49
50 my $input = new CGI;
51 my $searchfield=$input->param('searchfield');
52 my $reqsel="select host,port,db,userid,password,name,id,checked,rank from z3950servers where (name = '$searchfield') order by rank,name";
53 my $reqdel="delete from z3950servers where name='$searchfield'";
54 my $offset=$input->param('offset');
55 my $script_name="/cgi-bin/koha/admin/z3950servers.pl";
56
57 my $pagesize=20;
58 my $op = $input->param('op');
59 $searchfield=~ s/\,//g;
60 print $input->header;
61
62 #start the page and read in includes
63 print startpage();
64 print startmenu('admin');
65
66 ################## ADD_FORM ##################################
67 # called by default. Used to create form to add or  modify a record
68 if ($op eq 'add_form') {
69         #---- if primkey exists, it's a modify action, so read values to modify...
70         my $data;
71         if ($searchfield) {
72                 my $dbh = C4::Context->dbh;
73                 my $sth=$dbh->prepare("select host,port,db,userid,password,name,id,checked,rank from z3950servers where (name = '$searchfield') order by rank,name");
74                 $sth->execute;
75                 $data=$sth->fetchrow_hashref;
76                 $sth->finish;
77         }
78         print <<printend
79         <script>
80         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
81         function isNotNull(f,noalert) {
82                 if (f.value.length ==0) {
83                     return false;
84                 }
85                 return true;
86         }
87         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88         function toUC(f) {
89                 var x=f.value.toUpperCase();
90                 f.value=x;
91                 return true;
92         }
93         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
94         function isNum(v,maybenull) {
95         var n = new Number(v.value);
96         if (isNaN(n)) {
97                 return false;
98                 }
99         if (maybenull==0 && v.value=='') {
100                 return false;
101         }
102         return true;
103         }
104         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105         function isDate(f) {
106                 var t = Date.parse(f.value);
107                 if (isNaN(t)) {
108                         return false;
109                 }
110         }
111         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
112         function Check(f) {
113                 var ok=1;
114                 var _alertString="";
115                 var alertString2;
116                 if (f.searchfield.value.length==0) {
117                         _alertString += "- site name missing\\n";
118                 }
119                 if (f.host.value.length==0) {
120                         _alertString += "- host missing\\n";
121                 }
122                 if (f.port.value.length==0) {
123                         _alertString += "- port missing\\n";
124                 }
125                 if (f.db.value.length==0) {
126                         _alertString += "- database missing\\n";
127                 }
128                 if (isNaN(f.port.value)) {
129                         _alertString += "- port must be a number\\n";
130                 }
131                 if (isNaN(f.rank.value)) {
132                         _alertString += "- rank must be a number\\n";
133                 }
134                 if (isNaN(f.checked.value) || f.checked.value<0 || f.checked.value>1) {
135                         _alertString += "- checked must be 0 or 1\\n";
136                 }
137                 if (_alertString.length==0) {
138                         document.Aform.submit();
139                 } else {
140                         alertString2 = "Form not submitted because of the following problem(s)\\n";
141                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
142                         alertString2 += _alertString;
143                         alert(alertString2);
144                 }
145         }
146         </SCRIPT>
147 printend
148 ;#/
149         if ($searchfield) {
150                 print "<h1>Modify Z39.50 Server</h1>";
151         } else {
152                 print "<h1>Add Z39.50 Server</h1>";
153         }
154         print "<form action='$script_name' name=Aform method=post>";
155         print "<input type=hidden name=op value='add_validate'>";
156         print "<table>";
157         if ($searchfield) {
158                 print "<tr><td>Z39.50 Server</td><td><input type=hidden name=searchfield value=\"$searchfield\">$searchfield</td></tr>\n";
159         } else {
160                 print "<tr><td>Z39.50 Server</td><td><input type=text name=searchfield size=40></td></tr>\n";
161         }
162         print "<tr><td>Hostname</td><td><input type=text name=host size=30 value='$data->{'host'}'></td></tr>\n";
163         print "<tr><td>Port</td><td><input type=text name=port size=5 value='$data->{'port'}' onBlur=isNum(this)></td></tr>\n";
164         print "<tr><td>Database</td><td><input type=text name=db value='$data->{'db'}'></td></tr>\n";
165         print "<tr><td>Userid</td><td><input type=text name=userid value='$data->{'userid'}'></td></tr>\n";
166         print "<tr><td>Password</td><td><input type=text name=password value='$data->{'password'}'></td></tr>\n";
167         print "<tr><td>Checked (searched by default)</td><td><input type=text size=1 name=checked value='$data->{'checked'}' onBlur=isNum(this)></td></tr>";
168         print "<tr><td>Rank (display order)</td><td><input type=text name=rank size=4 value='$data->{'rank'}' onBlur=isNum(this)></td></tr>";
169         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
170         print "</table>";
171         print "</form>";
172 ;
173                                                                                                         # END $OP eq ADD_FORM
174 ################## ADD_VALIDATE ##################################
175 # called by add_form, used to insert/modify data in DB
176 } elsif ($op eq 'add_validate') {
177         my $dbh=C4::Context->dbh;
178         my $sth=$dbh->prepare("select * from z3950servers where name=?");
179         $sth->execute($input->param('searchfield'));
180         if ($sth->rows) {
181                 $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=? where name=?");
182                 $sth->execute($input->param('host'),
183                       $input->param('port'),
184                       $input->param('db'),
185                       $input->param('userid'),
186                       $input->param('password'),
187                       $input->param('searchfield'),
188                       $input->param('checked'),
189                       $input->param('rank'),
190                       $input->param('searchfield')
191                       );
192         } else {
193                 $sth=$dbh->prepare("insert into z3950servers (host,port,db,userid,password,name,checked,rank) values (?, ?, ?, ?, ?, ?, ?, ?)");
194                 $sth->execute($input->param('host'),
195                       $input->param('port'),
196                       $input->param('db'),
197                       $input->param('userid'),
198                       $input->param('password'),
199                       $input->param('searchfield'),
200                       $input->param('checked'),
201                       $input->param('rank'),
202                       );
203         }
204         $sth->finish;
205         print "data recorded";
206         print "<form action='$script_name' method=post>";
207         print "<input type=submit value=OK>";
208         print "</form>";
209                                                                                                         # END $OP eq ADD_VALIDATE
210 ################## DELETE_CONFIRM ##################################
211 # called by default form, used to confirm deletion of data in DB
212 } elsif ($op eq 'delete_confirm') {
213         my $dbh = C4::Context->dbh;
214         my $sth=$dbh->prepare("select count(*) as total from borrowers where branchcode='$searchfield'");
215         $sth->execute;
216         my $total = $sth->fetchrow_hashref;
217         $sth->finish;
218         print "$reqsel";
219         my $sth=$dbh->prepare($reqsel);
220         $sth->execute;
221         my $data=$sth->fetchrow_hashref;
222         $sth->finish;
223         print mktablehdr;
224         print mktablerow(2,'#99cc33',bold('Branch code'),bold("$searchfield"),'/images/background-mem.gif');
225         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=searchfield value='$searchfield'>";
226         print "<tr><td>Branch code</td><td>$data->{'branchcode'}</td></tr>";
227         print "<tr><td>&nbsp; name</td><td>$data->{'branchname'}</td></tr>";
228         print "<tr><td>&nbsp; adress</td><td>$data->{'branchaddress1'}</td></tr>";
229         print "<tr><td>&nbsp;</td><td>$data->{'branchaddress2'}</td></tr>";
230         print "<tr><td>&nbsp;</td><td>$data->{'branchaddress3'}</td></tr>";
231         print "<tr><td>&nbsp;phone</td><td>$data->{'branchphone'}</td></tr>";
232         print "<tr><td>&nbsp; fax</td><td>$data->{'branchfax'}</td></tr>";
233         print "<tr><td>&nbsp; e-mail</td><td>$data->{'branchemail'}</td></tr>";
234         print "<tr><td>&nbsp; issuing</td><td>$data->{'issuing'}</td></tr>";
235         if ($total->{'total'} >0) {
236                 print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
237                 print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
238         } else {
239                 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
240                 print "<tr><td><INPUT type=submit value='YES'></form></td><td><form action='$script_name' method=post><input type=submit value=NO></form></td></tr>";
241         }
242                                                                                                         # END $OP eq DELETE_CONFIRM
243 ################## DELETE_CONFIRMED ##################################
244 # called by delete_confirm, used to effectively confirm deletion of data in DB
245 } elsif ($op eq 'delete_confirmed') {
246         my $dbh=C4::Context->dbh;
247 #       my $searchfield=$input->param('branchcode');
248         my $sth=$dbh->prepare($reqdel);
249         $sth->execute;
250         $sth->finish;
251         print "data deleted";
252         print "<form action='$script_name' method=post>";
253         print "<input type=submit value=OK>";
254         print "</form>";
255                                                                                                         # END $OP eq DELETE_CONFIRMED
256 ################## DEFAULT ##################################
257 } else { # DEFAULT
258         my @inputs=(["text","searchfield",$searchfield],
259                 ["reset","reset","clr"]);
260         print mkheadr(2,'branches admin');
261         print mkformnotable("$script_name",@inputs);
262         print <<printend
263
264 printend
265         ;
266         if  ($searchfield ne '') {
267                 print "You Searched for <b>$searchfield<b><p>";
268         }
269         print mktablehdr;
270         print mktablerow(10,'#99cc33',bold('Site'),bold('hostname'),bold('port'),
271         bold('database'),bold('Userid'),bold('Password'),bold('Checked'),bold('Rank'),
272         '&nbsp;','&nbsp;','/images/background-mem.gif');
273         my $env;
274         my ($count,$results)=StringSearch($env,$searchfield,'web');
275         my $toggle="white";
276         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
277                 if ($toggle eq 'white'){
278                         $toggle="#ffffcc";
279                 } else {
280                         $toggle="white";
281                 }
282                 
283                 my $urlsearchfield=$results->[$i]{name};
284                 $urlsearchfield=~s/ /%20/g;
285                 print mktablerow(10,$toggle,
286                         $results->[$i]{'name'},
287                         $results->[$i]{'host'},
288                         $results->[$i]{'port'},
289                         $results->[$i]{'db'},
290                         $results->[$i]{'userid'},
291                         ($results->[$i]{'password'}) ? ('#######') : ('&nbsp;'),
292                         $results->[$i]{'checked'},
293                         $results->[$i]{'rank'},
294                 mklink("$script_name?op=add_form&searchfield=$urlsearchfield".'','Edit'),
295                 mklink("$script_name?op=delete_confirm&searchfield=$urlsearchfield",'Delete'));
296         }
297         print mktableft;
298         print "<form action='$script_name' method=post>";
299         print "<input type=hidden name=op value=add_form>";
300         if ($offset>0) {
301                 my $prevpage = $offset-$pagesize;
302                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
303         }
304         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
305         if ($offset+$pagesize<$count) {
306                 my $nextpage =$offset+$pagesize;
307                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
308         }
309         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
310         print "</form>";
311 } #---- END $OP eq DEFAULT
312 print endmenu('admin');
313 print endpage();