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