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

<html>
<body bgcolor="#999966" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED CURATOR - Add Anatomical Part</title>
<br>
 
<?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>';
	   	return;
	}
?>




<?php
//for test : $test='y' for real insert $test='n'
$test='y';

$region = $_GET["region"];

if (count($region)==0) 
	{
	print "Error... please select at least one anatomical part !";
	return;
	}

$dev = $_GET["dev"];
$cloneid=$_GET["cloneid"];
$substrate=$_GET["substrate"];
$species=$_GET["species"];
$insituid=$_GET["insituid"];
$notsure=$_GET["notsure"];
$partof=$_GET["partof"];
$subcell=$_GET["subcell"];
$intensity=$_GET["intensity"];
$method=$_GET["method"];

//Daniel Sobral 6 October 2006
//Added an exception so no In_Situs can be made for regulatory regions...
//kept regulatory regions in the molecule list so they can have a sequence....
//$requete= "select * from \"MOL_ENTITY\" where \"NAME\"='$cloneid' AND \"TYPE\"!='regulatory_region'";

$requete= "SELECT * FROM \"MOL_ENTITY\" WHERE \"MOL_ID\" IN (SELECT \"MOL_ID\" FROM \"MOL_ENTITY\" where \"NAME\"='$cloneid' AND \"TYPE\"!='regulatory_region') OR \"MOL_ID\" IN (SELECT \"CONSTRUCTION_ID\"  FROM \"CONSTRUCTION\" WHERE \"STABLE_ID\"='$cloneid')";
$stmt = pg_query($c,$requete);

$found = FALSE;

//print $requete."<br>";
while($row=pg_fetch_array($stmt))
{
	//if it is a construction we do not control for species...
	if($row["TYPE"]=='construction' || $row["SPECIES"]==$species){


		////// AJOUT /////
		if ($row["TYPE"]=="clone") {$id=$row["MOL_ID"];$found=true;break;}
		/////////////////

		$id=$row["MOL_ID"];
		$found = TRUE;  
	}	
}
	
if($found){
	
/*Debut de la transaction*/
pg_query($c,"begin");
for ($i=0;$i<count($region);$i++)
{
	$requete= "insert into \"TARGET\" (\"TARGET_ID\", \"IN_SITU_ID\", \"REGION_ID\", \"METHOD\", \"INTENSITY\", \"SUBSTRATE\", \"SUBCELL_POS\", \"NAME\", \"SURE\", \"PART_OF\")
	values ('$id','$insituid','".$region[$i]."', '$method', '$intensity', '$substrate', '$subcell','$cloneid','$notsure', '$partof')";
	$stmt1 = pg_query($c,$requete) or die ("<strong>Error </strong>".pg_last_error($c));
}

// /*$requete = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=current_date where \"BIO_ID\"=$id";*/
		$resultset = pg_query($c,$requete) or die("<strong>ERROR </strong>".pg_last_error($c));	
	
		print '<center><H1>Insert done for '.$cloneid.'</H1></center>';
		print ' <br><center> To see changes refresh the "IN SITU FULL REPORT" page</center><br>';
		//OCICommit($connection);
		
pg_query($c,"commit");
	
}
else 
{
 echo "<center><strong><br>\"WARNING\" &nbsp;----> &nbsp;Clone name not found in ANISEED database<br> </strong><center>";
 echo"<br><center><strong> ERROR : Insert NOT done for $cloneid </strong></center><br>";
}

//echo"region = $region; dev = $dev; cloneid= $cloneid; substrate=$substrate; species = $species; <br>
//	insituid = $insituid; notsure = $notsure; partof = $partof; subcell = $subcell <br>";
print '<br><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';
//ocifreestatement($stmt1);
//ocifreestatement($stmt);
?>

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

