<?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	
	
	pg_query($c, "BEGIN") or die("<strong>ERROR Start </strong>".pg_last_error($c));    
	
  	//FROM ALL MOTIF_COORD TO DELETE
    $motif_list= array();
	$khoueiry="SELECT * FROM \"MOTIF_COORD\" WHERE \"MOTIF_ID\"=$id;";
    $stmt = pg_query($c,$khoueiry);
    while ($row=pg_fetch_array($stmt) ){ 
    	$motif_list[]=$row;
  	}
  	
  	foreach($motif_list as $motif){
	
		$khoueiry="DELETE FROM \"MOTIF_COORD_GENE\" WHERE \"MOTIF_COORD_ID\"=".$motif["MOTIF_COORD_ID"];
		pg_query($c,$khoueiry) or die("<strong>ERROR deleting motif coordinate </strong>".pg_last_error($c));
		
		$khoueiry="DELETE FROM \"MOTIF_COORD\" WHERE \"MOTIF_COORD_ID\"=".$motif["MOTIF_COORD_ID"];
		pg_query($c,$khoueiry) or die("<strong>ERROR deleting motif coordinate </strong>".pg_last_error($c));      	    	  
  
		$khoueiry="DELETE FROM \"BIO_ENTITY\" WHERE \"BIO_ID\"=".$motif["MOTIF_COORD_ID"];
		pg_query($c,$khoueiry) or die("<strong>ERROR motif coordinate entity </strong>".pg_last_error($c));    
	}
	
  	$khoueiry = "DELETE FROM \"MOTIF_REGUL\" WHERE \"MOTIF_ID\"=$id;";
  	pg_query($c,$khoueiry) or die("<strong>ERROR deleting motif </strong>".pg_last_error($c));
  	
 	$khoueiry="DELETE FROM \"BIO_ENTITY\" WHERE \"BIO_ID\"=$id;";
	pg_query($c,$khoueiry) or die("<strong>ERROR deleting motif entity </strong>".pg_last_error($c));
  	  	
	pg_query($c, "COMMIT") or die("<strong>ERROR finishing </strong>".pg_last_error($c));

	echo '<strong>Deletion Performed!</strong>';
	include ("frames_footer.php"); 

?>