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


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

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

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

// print $text1."<br>";
// print_r($_FILES);
// print "<br>";
// print filesize($_FILES['objfile']['tmp_name']);


pg_query($c,"begin");

////// Step 1: BIO ENTITY
$ID=$_POST["ID"];
// $requete="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\",\"CURATED\") values (nextval('\"S_BIO_ID\"'),'y')";
// pg_query($c,$requete);


////// Step 2:ENTRY ORIGIN
$requete="UPDATE \"ENTRY_ORIGIN\" SET \"AUTHOR_ID\"='".$_POST["author"]."', \"ENTRY_DATE\"=current_date where \"BIO_ID\"='$ID'";
$stmt=pg_query($c,$requete);

////// 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="UPDATE \"EMBRYO\" SET \"SPECIES\"='".$_POST["species"]."',\"DEV_STAGE_ID\"=(
select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"SPECIES\"='".$_POST["species"]."' and \"NAME\"='".$_POST["devname"]."'),\"WILD_TYPE\"='".$_POST["wt"]."',
\"SOFTWARE\"='".$_POST["software"]."',\"COMMENTS\"='".$_POST["comments"]."',\"NAME\"='$NAME' where \"EMBRYO_ID\"='$ID'";

// $requete="INSERT INTO \"EMBRYO\" (\"EMBRYO_ID\",\"SPECIES\",\"DEV_STAGE_ID\",\"WILD_TYPE\",\"SOFTWARE\",\"NAME\",\"COMMENTS\") values ('$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(pg_last_error($c));



if (strlen($text1)>0)
{
$oid1=pg_lo_create($c);
$requete="UPDATE \"EMBRYO\" set \"OBJ_DATA\"='$oid1' where \"EMBRYO_ID\"='$ID'";
$stmt = pg_query($c,$requete) or die ("<strong> ERROR </strong>".pg_last_error($c));
$handler=pg_lo_open($c,$oid1,"w");
pg_lo_write($handler,$text1);
pg_lo_close($handler);
}





pg_query($c,"commit");










?>


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


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