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


$id=$_POST["embryoid"];



if (strlen($_FILES['neighfile']['tmp_name'])==0)
	{
	print "<br><br><center>Please submit a file containing the distances data.</center>";
	include ("frames_footer.php");
	return;
	}

$requete="SELECT * from \"EMBRYO_NEIGHBOR\" where \"EMBRYO_ID\"='$id'";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0) $INSERT=1; else $INSERT=0;


pg_query($c,"begin");

if ($INSERT==0)
{

$requete="DELETE FROM \"EMBRYO_NEIGHBOR\" where \"EMBRYO_ID\"='$id'";
$stmt=pg_query($c,$requete) or die ("Error... please contact your administrator !");

}


$handle=fopen($_FILES['neighfile']['tmp_name'],"r");
while (!feof($handle))
{
$content=trim(fgets($handle,8192));
if (strlen($content)==0) continue;


if ($content{0}=="#") continue;

// print $content."<br>";


$tmp=explode("\t",$content);


$cell_a=$tmp[0];
$cell_b=$tmp[1];
$dist=$tmp[2];
$contact=$tmp[5];

if ($cell_a=="default") continue;
if ($cell_b=="default") continue;
if (strlen($cell_a)==0) continue;
if (strlen($cell_b)==0) continue;


$requete="INSERT INTO \"EMBRYO_NEIGHBOR\" (\"EMBRYO_ID\",\"CELL_A\",\"CELL_B\",\"DISTANCE\",\"CONTACT\") values ('$id',(select \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$cell_a' and \"DEV_STAGE_ID\"=(select \"DEV_STAGE_ID\" from \"EMBRYO\" where \"EMBRYO_ID\"='$id')),(select \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$cell_b' and \"DEV_STAGE_ID\"=(select \"DEV_STAGE_ID\" from \"EMBRYO\" where \"EMBRYO_ID\"='$id')),'$dist','$contact')";

// print $requete."<br>";

$stmt=pg_query($c,$requete) or die ("Error... please contact your administrator !");


}
fclose($handle);


pg_query($c,"end");






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



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