<?php
include("connection.php");
include("frames_header.php");
?>


<?php

$species = $_GET["species"];
$nomstade = $_GET["nomstade"];
$test='n';

$requete="select * from \"DEV_STAGE\" where \"SPECIES\"='$species' and \"NAME\"='$nomstade'";
$stmt00= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
if(!$row00=pg_fetch_array($stmt00))
{
/*Debut de la transaction*/
pg_query($c,"begin");

      print "<br><center><H1>Anatomical tree created ($nomstade,$species).</H1></center>";
      $requete="INSERT INTO \"DEV_STAGE\" (\"DEV_STAGE_ID\", \"SPECIES\", \"NAME\") values (nextval('\"S_DEV_STAGE_ID\"'), '$species', '$nomstade')";
      $stmt= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

           // ocifreestatement($stmt);

        $requete="select currval('\"S_DEV_STAGE_ID\"') as \"SEQVAL\"";
      	$stmt1= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
      	
      	if ($row1=pg_fetch_array($stmt1))
      	    $seqid=$row1["SEQVAL"];
      	    
            $requete="insert into \"BIO_ENTITY\" (\"BIO_ID\") values (nextval('\"S_BIO_ID\"'))";
            $stmt2= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
     
                  
                  $requete="insert into \"ANAT_ENTITY\" (\"ANAT_ID\", \"DEV_STAGE_ID\", \"SPECIES\", \"NAME\", \"TYPE\") values (currval('\"S_BIO_ID\"'), '$seqid', '$species', 'Whole embryo', 'organism')";
                  $stmt3= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
    
    
                  
                  $requete="insert into \"BIO_ENTITY\" (\"BIO_ID\") values (nextval('\"S_BIO_ID\"'))";
                  $stmt4= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
                  
                        
                        $requete="insert into \"ANAT_ENTITY\" (\"ANAT_ID\", \"DEV_STAGE_ID\", \"SPECIES\", \"NAME\", \"TYPE\") values (currval('\"S_BIO_ID\"'), '$seqid', '$species', 'No expression', 'cell')";
                        
			$stmt5= pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
                        
                  
                  
                  
                  
                  
                  
pg_query($c,"commit");

                  print'<form name="formulaire" action="tree-editor2.php" method="get" target="_self">';
                  print "<input name=\"species\" type=\"hidden\" value=\"$species\">";  
                  print"<input name=\"dev\" type=\"hidden\" value=\"$seqid\">";
                  print '<input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit">&nbsp;&nbsp;&nbsp;';
            
            
      
}
else
{

     // ocifreestatement($stmt00);    
      print "<br><center><H1>A stage with this name already exists for $species.</H1></center>";
      print '<br><br><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';
      return;
}
            
           

?>


<?php
      include("frames_footer.php");
?> 