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)
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.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
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
30 my ($env,$searchstring,$type)=@_;
31 my $dbh = C4::Context->dbh;
32 $searchstring=~ s/\'/\\\'/g;
33 my @data=split(' ',$searchstring);
35 my $query="Select host,port,db,userid,password,name,id,checked,rank from
36 z3950servers where (name like \"$data[0]\%\") order by rank,name"; my
37 $sth=$dbh->prepare($query); $sth->execute;
40 while (my $data=$sth->fetchrow_hashref) {
47 return ($cnt,\@results);
51 my $searchfield=$input->param('searchfield');
52 my $reqsel="select host,port,db,userid,password,name,id,checked,rank from
53 z3950servers where (name = '$searchfield') order by rank,name";my
54 $reqdel="delete from z3950servers where name='$searchfield'";my
55 $offset=$input->param('offset');my
56 $script_name="/cgi-bin/koha/admin/z3950servers.pl";
58 my $op = $input->param('op');
59 $searchfield=~ s/\,//g;
62 #start the page and read in includes
64 print startmenu('admin');
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...
72 my $dbh = C4::Context->dbh;
73 my $sth=$dbh->prepare("select
74 host,port,db,userid,password,name,id,checked,rank from z3950servers where (name
75 = '$searchfield') order by rank,name"); $sth->execute;
76 $data=$sth->fetchrow_hashref; $sth->finish;
80 ///////////////////////////////////////////////////////////////////////////////
81 ////////////////////////////////// function isNotNull(f,noalert) {
82 if (f.value.length ==0) {
87 ///////////////////////////////////////////////////////////////////////////////
88 ////////////////////////////////// function toUC(f) {
89 var x=f.value.toUpperCase();
93 ///////////////////////////////////////////////////////////////////////////////
94 ////////////////////////////////// function isNum(v,maybenull) {
95 var n = new Number(v.value);
99 if (maybenull==0 && v.value=='') {
104 ///////////////////////////////////////////////////////////////////////////////
105 ////////////////////////////////// function isDate(f) {
106 var t = Date.parse(f.value);
111 ///////////////////////////////////////////////////////////////////////////////
112 ////////////////////////////////// function Check(f) {
116 if (f.searchfield.value.length==0) {
117 _alertString += "- site name missing\\n";
119 if (f.host.value.length==0) {
120 _alertString += "- host missing\\n";
122 if (f.port.value.length==0) {
123 _alertString += "- port missing\\n";
125 if (f.db.value.length==0) {
126 _alertString += "- database missing\\n";
128 if (isNaN(f.port.value)) {
129 _alertString += "- port must be a number\\n";
131 if (isNaN(f.rank.value)) {
132 _alertString += "- rank must be a number\\n";
134 if (isNaN(f.checked.value) || f.checked.value<0 || f.checked.value>1) {
135 _alertString += "- checked must be 0 or 1\\n";
137 if (_alertString.length==0) {
138 document.Aform.submit();
140 alertString2 = "Form not submitted because of the following problem(s)\\n";
142 "-------------------------------------------------------------------------------
143 -----\\n\\n"; alertString2 += _alertString; alert(alertString2);
150 print "<h1>Modify Z39.50 Server</h1>";
152 print "<h1>Add Z39.50 Server</h1>";
154 print "<form action='$script_name' name=Aform method=post>";
155 print "<input type=hidden name=op value='add_validate'>";
158 print "<tr><td>Z39.50 Server</td><td><input type=hidden name=searchfield
159 value=\"$searchfield\">$searchfield</td></tr>\n"; } else {
160 print "<tr><td>Z39.50 Server</td><td><input type=text name=searchfield
161 size=40></td></tr>\n"; }
162 print "<tr><td>Hostname</td><td><input type=text name=host size=30
163 value='$data->{'host'}'></td></tr>\n"; print "<tr><td>Port</td><td><input
164 type=text name=port size=5 value='$data->{'port'}'
165 onBlur=isNum(this)></td></tr>\n"; print "<tr><td>Database</td><td><input
166 type=text name=db value='$data->{'db'}'></td></tr>\n"; print
167 "<tr><td>Userid</td><td><input type=text name=userid
168 value='$data->{'userid'}'></td></tr>\n"; print "<tr><td>Password</td><td><input
169 type=text name=password value='$data->{'password'}'></td></tr>\n"; print
170 "<tr><td>Checked (searched by default)</td><td><input type=text size=1
171 name=checked value='$data->{'checked'}' onBlur=isNum(this)></td></tr>"; print
172 "<tr><td>Rank (display order)</td><td><input type=text name=rank size=4
173 value='$data->{'rank'}' onBlur=isNum(this)></td></tr>"; print
174 "<tr><td> </td><td><INPUT type=button value='OK'
175 onClick='Check(this.form)'></td></tr>"; print "</table>"; print "</form>";;
176 # END $OP eq ADD_FORM################## ADD_VALIDATE
177 ################################### called by add_form, used to insert/modify
178 data in DB} elsif ($op eq 'add_validate') { my $dbh=C4::Context->dbh; my
179 $sth=$dbh->prepare("select * from z3950servers where name=?");
180 $sth->execute($input->param('searchfield')); if ($sth->rows) {
181 $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?,
182 password=?, name=?, checked=?, rank=? where name=?");
183 $sth->execute($input->param('host'), $input->param('port'),
185 $input->param('userid'),
186 $input->param('password'),
187 $input->param('searchfield'),
188 $input->param('checked'),
189 $input->param('rank'),
190 $input->param('searchfield')
193 $sth=$dbh->prepare("insert into z3950servers
194 (host,port,db,userid,password,name,checked,rank) values (?, ?, ?, ?, ?, ?, ?,
195 ?)"); $sth->execute($input->param('host'), $input->param('port'),
197 $input->param('userid'),
198 $input->param('password'),
199 $input->param('searchfield'),
200 $input->param('checked'),
201 $input->param('rank'),
205 print "data recorded";
206 print "<form action='$script_name' method=post>";
207 print "<input type=submit value=OK>";
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
215 branchcode='$searchfield'"); $sth->execute;
216 my $total = $sth->fetchrow_hashref;
219 my $sth=$dbh->prepare($reqsel);
221 my $data=$sth->fetchrow_hashref;
224 print mktablerow(2,'#99cc33',bold('Branch
225 code'),bold("$searchfield"),'/images/background-mem.gif'); print "<form
226 action='$script_name' method=post><input type=hidden name=op
227 value=delete_confirmed><input type=hidden name=searchfield
228 value='$searchfield'>"; print "<tr><td>Branch
229 code</td><td>$data->{'branchcode'}</td></tr>"; print "<tr><td>
230 name</td><td>$data->{'branchname'}</td></tr>"; print "<tr><td>
231 adress</td><td>$data->{'branchaddress1'}</td></tr>"; print
232 "<tr><td> </td><td>$data->{'branchaddress2'}</td></tr>"; print
233 "<tr><td> </td><td>$data->{'branchaddress3'}</td></tr>"; print
234 "<tr><td> phone</td><td>$data->{'branchphone'}</td></tr>"; print
235 "<tr><td> fax</td><td>$data->{'branchfax'}</td></tr>"; print
236 "<tr><td> e-mail</td><td>$data->{'branchemail'}</td></tr>"; print
237 "<tr><td> issuing</td><td>$data->{'issuing'}</td></tr>"; if
238 ($total->{'total'} >0) { print "<tr><td colspan=2 align=center><b>This record
239 is used $total->{'total'} times. Deletion not possible</b></td></tr>"; print
240 "<tr><td colspan=2></form><form action='$script_name' method=post><input
241 type=submit value=OK></form></td></tr>"; } else { print "<tr><td colspan=2
242 align=center>CONFIRM DELETION</td></tr>"; print "<tr><td><INPUT type=submit
243 value='YES'></form></td><td><form action='$script_name' method=post><input
244 type=submit value=NO></form></td></tr>"; } # END $OP eq
245 DELETE_CONFIRM################## DELETE_CONFIRMED
246 ################################### called by delete_confirm, used to
247 effectively confirm deletion of data in DB} elsif ($op eq 'delete_confirmed') {
248 my $dbh=C4::Context->dbh;# my $searchfield=$input->param('branchcode'); my
249 $sth=$dbh->prepare($reqdel); $sth->execute;
251 print "data deleted";
252 print "<form action='$script_name' method=post>";
253 print "<input type=submit value=OK>";
255 # END $OP eq DELETE_CONFIRMED
256 ################## DEFAULT ##################################
258 my @inputs=(["text","searchfield",$searchfield],
259 ["reset","reset","clr"]);
260 print mkheadr(2,'branches admin');
261 print mkformnotable("$script_name",@inputs);
266 if ($searchfield ne '') {
267 print "You Searched for <b>$searchfield<b><p>";
270 print mktablerow(10,'#99cc33',bold('Site'),bold('hostname'),bold('port'),
271 bold('database'),bold('Userid'),bold('Password'),bold('Checked'),bold('Rank'),
272 ' ',' ','/images/background-mem.gif');
274 my ($count,$results)=StringSearch($env,$searchfield,'web');
276 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count);
277 $i++){ if ($toggle eq 'white'){
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'}) ? ('#######') : (' '),
292 $results->[$i]{'checked'},
293 $results->[$i]{'rank'},
294 mklink("$script_name?op=add_form&searchfield=$urlsearchfield".'','Edit'),
296 mklink("$script_name?op=delete_confirm&searchfield=$urlsearchfield",'Delete'));
298 print "<form action='$script_name' method=post>";
299 print "<input type=hidden name=op value=add_form>";
301 my $prevpage = $offset-$pagesize;
302 print mklink("$script_name?offset=".$prevpage,'<< Prev');
304 print " ";
305 if ($offset+$pagesize<$count) {
306 my $nextpage =$offset+$pagesize;
307 print mklink("$script_name?offset=".$nextpage,'Next >>');
309 print "<br><input type=image src=\"/images/button-add-new.gif\" WIDTH=188
310 HEIGHT=44 ALT=\"Add budget\" BORDER=0 ></a><br>"; print "</form>";
311 } #---- END $OP eq DEFAULT
312 print endmenu('admin');