#!/usr/local/bin/perl -w
# This is a query and change of data in DB
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', 'text.coord']),
    "<P>";

    print $query->submit;
    print $query->endform;
   print qq{<P><A href="http://barley.itc.nl/examples.htm" TARGET= "vrml" >back</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);

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

$num=0;

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

$dbh->disconnect;

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

#---printing coordinates------------------------
print "<BR>";

$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 tname from texta where tid=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; 

  print qq{<P><BR><I><FONT COLOR=red> <FONT SIZE=0>image files available on the server: <BR></FONT></I></P>};
  print qq{<P><FONT SIZE=1> itc1a.jpg, itc2.jpg, itc3a.jpg, itc5a.jpg, Stone2.jpg, Stonewal.jpg<BR></FONT></P>}
}
#-------------text.coord -------------------------------------------
elsif (($query->param(keyword)) eq 'text.coord') {
#Query database for texture coordinates ----------------------------

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

$sth = $dbh->prepare("select * from textg where tidg=2");
$sth->execute;

$num_texture_coord=0;

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

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

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

#---printing texture coordinates------------------------
$k=0;
while ($k<$num_texture_coord) {
  $j=0;
  while ($j<4) { 
    print "$texture_coord[$k]->[$j++]  ";
   
  }
  $k++;
  print "<BR>";
}

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

print "<BR>New texture coordinates <EM>(tidg,enoseq,xt,yt)</EM>: <BR>";

#
#
print $query->startform(-TARGET=>"response");

print $query->textfield(-name=>'tidg', 
			-default=>$texture_coord[0]->[0],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'enoseq', 
			-default=>$texture_coord[0]->[1],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'xt', 
			-default=>$texture_coord[0]->[2],
			-size=>10,
			-maxlength=>60);
print $query->textfield(-name=>'yt', 
			-default=>$texture_coord[0]->[3],
			-size=>10,
			-maxlength=>60);
print "<BR>";

print $query->submit ('  Submit xy  ');
print $query->endform; 

}
#--------------text.coord END---------------------------------------
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 (tid, tname) values (2,'$file_name')");
    $sth->execute;
    $sth->finish;
    $dbh->disconnect;

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

  }
elsif (($query->param('tidg')) ne '') {
    
    $tidg=$query->param('tidg');
    $enoseq=$query->param('enoseq');
    $xt=$query->param('xt');
    $yt=$query->param('yt');
   
    $dbh = DBI->connect ("DBI:mysql:$database:$hostname", $user,$password);
    $sth = $dbh->prepare("update textg set xt=$xt,yt=$yt where tidg=$tidg and enoseqt=$enoseq");
    $sth->execute;
    $sth->finish;
    $dbh->disconnect;
    print "<BR>Coordinates changed";

    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------------------------------------------- 
  }
}


