
<?
include ("connection.php");
print "<title>ANISEED CURATOR - Change Dev Stage</title><br>";

include ("frames_header.php");
?>

<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
	   include ("frames_footer.php");
		return;
	}
?>

<?

$id=$_POST["id"];
$dev=$_POST["devstage"];

if (strlen($dev)==0) 
	{
	print "<center><strong>Please select a new developmental stage first</strong></center>";
	include ("frames_footer.php");
	return;
	}

//Recup du clone avant deletion
$requete="SELECT \"SPECIES\" from \"IN_SITU\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$species=$row["SPECIES"];
print $species."<br>";

$requete="SELECT distinct(\"TARGET_ID\") from \"TARGET\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$clone=$row["TARGET_ID"];

$requete="SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\" like 'Whole%' and \"DEV_STAGE_ID\"='$dev' and \"SPECIES\"='$species'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$whole=$row["ANAT_ID"];

//deletion des parties anatomiques
pg_query($c,"begin");
$requete="DELETE FROM \"TARGET\" where \"IN_SITU_ID\"='$id'";
pg_query($c,$requete);

$requete="UPDATE \"IN_SITU\" SET \"DEV_STAGE_ID\"='$dev' where \"IN_SITU_ID\"='$id'";
pg_query($c,$requete);


//Insertion du stade whole embryo comme base de la nouvelle annotation
$requete="INSERT INTO \"TARGET\" (\"TARGET_ID\",\"IN_SITU_ID\",\"REGION_ID\") values ('$clone','$id','$whole')";
print $requete;
pg_query($c,$requete) or die (pg_last_error($c));


pg_query($c,"commit");


print '<br><br><center><strong>Modifications done</strong><a href="insitu.php?id='.$id.'>Go back to the insitu</a>';
print '<br><br><strong>To see modifications, please load the insitu page again</strong></center>';


include ("frames_footer.php");
?>

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