<?php

	include ("frames_header.php"); 

	include ("connection.php");

	
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
		return;
	}
	
	
	$id = $_POST["id"];
	
	
	//include("update-interactions.php");
	$khoueiry="SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"TARGET_ID\" IN 
		(SELECT \"CONSTRUCTION_ID\" FROM \"CONSTRUCTION\" WHERE \"PROMOTER_ID\"=$id);";
	$stmt = pg_query($c,$khoueiry);
    while ($row=pg_fetch_array($stmt) ){
		//delete_evidence($c, $row["IN_SITU_ID"]);
		$stmp = pg_query($c,"BEGIN");
		$kmp = "DELETE FROM \"REG_EVIDENCE\" WHERE \"EVIDENCE\"=".$row["IN_SITU_ID"].";";
		$stmp = pg_query($c,$kmp);
		$kmp = "DELETE FROM \"REG_EVIDENCE\" WHERE \"CONTROL\"=".$row["IN_SITU_ID"].";";
		$stmp = pg_query($c,$kmp);
		$kmp = "DELETE FROM \"UPDATE_INTERACTIONS\" WHERE \"ISH_ID\"=".$row["IN_SITU_ID"].";";
		$stmp = pg_query($c,$kmp);
		$stmp = pg_query($c,"END");
	}
	
	
	$success = true; 
	pg_query($c, "BEGIN") or die("<strong>ERROR </strong>".pg_last_error($c));   
	
	//Need to remove all constructs and associated In_Situs
	$khoueiry="SELECT \"CONSTRUCTION_ID\" FROM \"CONSTRUCTION\" WHERE \"PROMOTER_ID\"=$id;";
    $stmt = pg_query($c,$khoueiry);
    while ($row=pg_fetch_array($stmt) ){
    	$cons_id = $row["CONSTRUCTION_ID"];
    	
    	//NEED TO DELETE ALL_IN_SITUS RELATED TO THIS CONSTRUCT
		//GET ALL IN_SITU_ID WHERE TARGET_ID=$id	
		$khoueiry="SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"TARGET_ID\"=$cons_id;";
    	$stmt = pg_query($c,$khoueiry);
    	while ($row=pg_fetch_array($stmt) ){
      		$in_situ_id = $row["IN_SITU_ID"];
	      	$requete = "delete from \"IN_SITU_CLUSTER\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"IN_SITU_CLUSTER\" where \"IN_SITU_ID_2\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"EMBRYO_MANIP\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"MUTANT\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"TARGET\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"PICTURE\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"IN_SITU\" where \"IN_SITU_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"REFERENCE\" where \"BIO_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"ENTRY_ORIGIN\" where \"BIO_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	
			$requete = "delete from \"BIO_ENTITY\" where \"BIO_ID\"=$in_situ_id";
			$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));
	  
		}	
	
		$khoueiry="DELETE FROM \"CONSTRUCTION\" WHERE \"CONSTRUCTION_ID\"=$cons_id;";
		pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
		$khoueiry="DELETE FROM \"SEQUENCE\" WHERE \"MOL_ID\"=$cons_id;";
		pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
		$khoueiry="DELETE FROM \"MOL_ENTITY\" WHERE \"MOL_ID\"=$cons_id;";
		pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
		
		$khoueiry="DELETE FROM \"BIO_ENTITY\" WHERE \"BIO_ID\"=$cons_id;";
		pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
		    	
	}
		
	$khoueiry="DELETE FROM \"GENE_REGULATION\" WHERE \"PROMOTER_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
	
	$khoueiry="SELECT * FROM \"MOTIF_COORD\" WHERE \"PROMOTER_ID\"=$id;";
	$stmt = pg_query($c,$khoueiry);
    while ($row=pg_fetch_array($stmt) ){
		$requete = "DELETE FROM \"MOTIF_COORD_GENE\" WHERE \"MOTIF_COORD_ID\"=".$row["MOTIF_COORD_ID"];
		pg_query($c, $requete) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	}
	
	$khoueiry="DELETE FROM \"MOTIF_COORD\" WHERE \"PROMOTER_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
	$khoueiry="DELETE FROM \"SEQUENCE\" WHERE \"MOL_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
	$khoueiry="DELETE FROM \"PROMOTER\" WHERE \"PROMOTER_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
		
	$khoueiry="DELETE FROM \"MOL_ENTITY\" WHERE \"MOL_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
	$khoueiry="DELETE FROM \"REFERENCE\" WHERE \"BIO_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
			
	$khoueiry="DELETE FROM \"ENTRY_ORIGIN\" WHERE \"BIO_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
	
	$khoueiry="DELETE FROM \"BIO_ENTITY\" WHERE \"BIO_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 
		
	pg_query($c, "COMMIT") or die("<strong>ERROR </strong>".pg_last_error($c)); 
	echo '<center><strong>Deletion performed</strong></center>'; 		    

	include ("frames_footer.php"); 


?>
