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

<html>
<body bgcolor="#871F1F" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED</title>
<br>

<CENTER><H1>Delete an anatomy part.</H1></CENTER><br>
<hr size="1">
<br>

<?php

$name=$_GET["name"];
$type=$_GET["type"];
$anatid=$_GET["anatid"];
$species=$_GET["species"];
$nomstade=$_GET["nomstade"];
$stadedev = $_GET["stadedev"];
$childof = $_GET["childof"];

$requete="SELECT * from \"TARGET\" where \"REGION_ID\"='$anatid'";
$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
if($row=pg_fetch_array($stmt) )
{
 	print '<center><H2>ERROR: This anatomical part is in used to describe an expression pattern.</H2><H2>If you want to delete it remove the corresponding in situs first.</H2></center>';
	print "<br><br>";
	print '<center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';
	return;
}
else
{
/*Debut de la transaction*/
pg_query($c,"begin");

      $requete="delete from \"FATE\" where \"ANAT_ID\"='$anatid'";
      $stmt0 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
      
	/*Si ca se passe bien on continue sinon on s'en va (prévoir si necessaire) */

            $requete="delete from \"ANAT_ENTITY\" where \"SPECIES\"='$species' and \"DEV_STAGE_ID\" ='$stadedev' and \"ANAT_ID\"='$anatid'";
            
            $stmt1 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

	/*Si ca se passe bien on continue sinon on s'en va (prévoir si necessaire) */

                  $requete="delete from \"BIO_ENTITY\" where \"BIO_ID\"='$anatid'";
                  $stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
         
	/*Si ca se passe bien on continue sinon on s'en va (prévoir si necessaire) */
	//Faire Attention ici, pour le cas du Child_of mis a NULL, possible que c'est pas bon la valeur dans le code original
	// etait child_of='' mais probleme car child_of est de type int8. Voir si possibilité de changer le type de la colonne.

                        $requete="Update \"ANAT_ENTITY\" set \"CHILD_OF\"=null where \"CHILD_OF\"='$anatid'";
			//print $requete;
                        $stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
        /*Si ca se pass bien on continue sinon on s'en va (prévoir si necessaire) */

/* Commit de la transaction */
pg_query($c,"commit");

                                    print "<br><center><H1>\"$name\" deleted.</H1></center>";
                                    print '<tr><td align="center"><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center></td></tr>';

                        
           
        
}
?>