<?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>";
	    include ("frames_footer.php"); 
		return;
	}
		
	$id = $_POST["id"];
	
	//NEED TO DELETE ALL_IN_SITUS RELATED TO THIS CONSTRUCT
	//GET ALL IN_SITU_ID WHERE TARGET_ID=$id	
		
	//include("update-interactions.php");
	$khoueiry="SELECT DISTINCT \"IN_SITU_ID\" FROM \"TARGET\" WHERE \"TARGET_ID\"=$id;";
	$stmt = pg_query($c,$khoueiry);
    while ($row=pg_fetch_array($stmt) ){
		//delete_evidence($c, $row["IN_SITU_ID"]);
		/*
		$khoueiry="INSERT INTO \"UPDATE_INTERACTION\" (\"ID\", \"ISH_ID\" ) values 
				( nextval('\"S_UPDATE_INTERACTION\"'),".$row["IN_SITU_ID"].")";	  	
		pg_query($c,$khoueiry) or print ('<Strong>Error inserting UPDATE_INTERACTION: </Strong>'.pg_last_error($c));
		*/
		$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");		
		
	}
	
	
	pg_query($c, "BEGIN") or die("<strong>ERROR </strong>".pg_last_error($c));    
	
	//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\"=$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 \"SEQUENCE\" WHERE \"MOL_ID\"=$id;";
	pg_query($c, $khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c)); 

	$khoueiry="DELETE FROM \"CONSTRUCTION\" WHERE \"CONSTRUCTION_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 \"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 '<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>';
	include ("frames_footer.php"); 

?>
