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

ini_set("memory_limit","16M");


?>


<?

if (strlen($_POST["devname"])==0) 
	{
	print "<br><br><center>Please choose a developmental stage first</center>";
	include ("frames_footer.php");
	return;
	}


if (strlen($_POST["author"])==0)
	{
	print "<br><br><center>Please choose an author first</center>";
	include ("frames_footer.php");
	return;
	}





/*
if (strlen($_FILES['objfile']['tmp_name'])==0)
	{
	print "<br><br><center>Please associate an Wavefront Object to this embryo first</center>";
	include ("frames_footer.php");
	return;
	}
*/










pg_query($c,"begin");

////// Step 1: BIO ENTITY
$requete="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\",\"CURATED\") values (nextval('\"S_BIO_ID\"'),'y')";
pg_query($c,$requete) or die ("Error ... Contact the administrator !");


////// Step 2:ENTRY ORIGIN
$requete="INSERT INTO \"ENTRY_ORIGIN\"(\"BIO_ID\",\"AUTHOR_ID\",\"ENTRY_DATE\") values (currval('\"S_BIO_ID\"'),'".$_POST["author"]."',current_date)";
$stmt=pg_query($c,$requete)  or die ("Error ... Contact the administrator !");

////// Step 3: EMBRYO

$NAME=$_POST["evol"]."@".$_POST["devname"]."@".$_POST["software"]."@".$_POST["resolution"];
if (strlen($_POST["complements"])>0) $NAME.="@".$_POST["complements"];
$NAME=str_replace(" ","_",$NAME);



$requete="INSERT INTO \"EMBRYO\" (\"EMBRYO_ID\",\"SPECIES\",\"DEV_STAGE_ID\",\"WILD_TYPE\",\"SOFTWARE\",\"NAME\",\"COMMENTS\") values (currval('\"S_BIO_ID\"'),'".$_POST["species"]."',(select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='".$_POST["devname"]."' and \"SPECIES\"='".$_POST["species"]."'),'".$_POST["wt"]."','".$_POST["software"]."','$NAME','".$_POST["comments"]."')";
$stmt=pg_query($c,$requete)  or die ("Error ... Contact the administrator !");



if (strlen($_FILES['objfile']['tmp_name'])>0)
{
$f1=fopen($_FILES['objfile']['tmp_name'],"r");
$text1=fread($f1,filesize($_FILES['objfile']['tmp_name']));
fclose($f1);

$oid1=pg_lo_create($c);
$requete="UPDATE \"EMBRYO\" set \"OBJ_DATA\"='$oid1' where \"EMBRYO_ID\"=currval('\"S_BIO_ID\"')";
$stmt = pg_query($c,$requete) or die ("<strong> ERROR </strong>".pg_last_error($c));
$handler=pg_lo_open($c,$oid1,"w")  or die ("Error ... Contact the administrator !");
pg_lo_write($handler,$text1)  or die ("Error ... Contact the administrator !");
pg_lo_close($handler)  or die ("Error ... Contact the administrator !");

}

pg_query($c,"commit");


print "<br><br><center><h3>The Embryo has been successfully entered into the database. You can now close this window</h3></center>";


print "FILESIZE=".filesize($_FILES['objfile']['tmp_name']);




?>

<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>



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