#!/usr/local/bin/perl -w
# This is a query and change of data 
use DBI;

use CGI;


$database = '***';
$hostname = '***';
$user = '***';
$password = '***';
$ss='';

$query = new CGI;
print $query->header;
$TITLE="Change Example";

$path_info = $query->path_info;

# If no path information is provided, then we create 
# a side-by-side frame set
if (!$path_info) {
    &print_frameset;
    exit 0;
}


#Start HTML page

&print_html_header;
&print_query if $path_info=~/query/;
&print_response if $path_info=~/response/;
&print_end;
exit;

#-------------------------------------------
# Subroutines
#-------------------------------------------

sub print_html_header {
    print $query->start_html($TITLE);
}
#--------------------------------------------
sub print_end {
       print $query->end_html;
}
#--------------------------------------------
sub print_frameset {
    $script_name = $query->script_name;
    print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset rows="35,65">
<frame src="$script_name/query" name="query">
<frame src="$script_name/response" name="response">
</frameset>
EOF
    ;
    exit 0;
}

#-----------------------------------------------
sub print_query {
    $script_name = $query->script_name;
    print "<H1>Change</H1>\n";
    print $query->startform(-action=>"$script_name/response",-TARGET=>"response");

    print "<P>What would like to change? ",
    $query->popup_menu(-name=>'keyword',
		       -values=>['coordinates','texture']),
    "<P>";

    print $query->submit;
    print $query->endform;
   print qq{<P><A href="http://barley.itc.nl/index.html" TARGET= "vrml" >home</A>};
}
#--------------------------------------------------
sub print_response {
    print "<H1>Values</H1>\n";
    unless ($query->param) {
	print "<b>No query submitted yet.</b>";
	return;
    }
       print "<P>Old ", $query->param(keyword),":";

if (($query->param(keyword)) eq 'coordinates') {

#Query database--1------------------------------------------------

$dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);

$sth = $dbh->prepare("select fid,nid,xc,yc,zc from surfd,face,node where sidd=15 and enoseqs=1 and fids=fid and nidf=nid");
$sth->execute;

$num=0;

#print "<BR>";
while ( @field = $sth->fetchrow) { 
  
  @all[$num]=[@field]; 
  $num++;
}

print "<BR>";
$sth->finish;
$dbh->disconnect;

#Query database END--1-------------------------------------------

#---printing coordinates------------------------
$k=0;
while ($k<$num) {
  $j=1;
  while ($j<5) { 
    print "$all[$k]->[$j++]  ";
   
  }
  $k++;
  print "<BR>";
}

#--------END printing-------------------------------------

print "<BR>New coordinates <EM>(nid,x,y,z)</EM>: <BR>";

#
#
print $query->startform(-TARGET=>"response");
print $query->textfield(-name=>'nid', 
			-default=>$all[0]->[1],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'x', 
			-default=>$all[0]->[2],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'y', 
			-default=>$all[0]->[3],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'z', 
			-default=>$all[0]->[4],
			-size=>10,
			-maxlength=>60);
print $query->submit ('  Submit xyz  ');
print $query->endform; 

}
elsif (($query->param(keyword)) eq 'texture') {
#Query database--4-----------------------------------------------

  $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);

  $sth = $dbh->prepare("select fname from texta where tida=2");
  $sth->execute;
  $name_texture = $sth->fetchrow;
  $sth->finish;
  $dbh->disconnect;

#Query database END--4-------------------------------------------


  print qq{<P><A href="http://barley.itc.nl/VRML/$name_texture">See the old</A>};
  print "<BR><BR>New texture:<BR>";

  print $query->startform(-TARGET=>"response");
  print $query->textfield(-name=>'filename', 
			-default=>$name_texture,
			-size=>18,
			-maxlength=>90);
  print $query->submit ('Submit filename');
  print $query->endform; 


}

elsif (($query->param('filename')) ne '') {
  
    $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user, $password);
    $file_name=$query->param('filename');
    $sth = $dbh->prepare("replace into texta (tida, fname) values (2,'$file_name')");
    $sth->execute;
    $sth->finish;
    $dbh->disconnect;

    print "<BR> Texture done";
    print qq {<P><hr><A HREF="http://barley.itc.nl/cgi-bin/tvrml_text.cgi" TARGET="blank">See the changes</A>};  

  }
  else {
    
    $nid=$query->param('nid');
    $x=$query->param('x');
    $y=$query->param('y');
    $z=$query->param('z');

    $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);
    $sth = $dbh->prepare("replace into node (nid, xc,yc,zc) values ($nid,$x,$y,$z)");
    $sth->execute;
    $sth->finish;
    $dbh->disconnect;
    print "<BR>Coordinates changed";

    print qq {<P><hr><A HREF="http://barley.itc.nl/cgi-bin/tvrml.cgi" TARGET="blank">See the changes</A>};  


#Query database END--3------------------------------------------- 
  }
}


