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

<html>
<body bgcolor="#999966" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED</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;
	}
?>


<?

$name=$_GET["name"];
$type=$_GET["type"];
//$tools=$_GET["tools"];
$tools=$_GET["moltools"];
$from=$_GET["from"];
$to=$_GET["to"];
$id=$_GET["id"];

//print $tools;

$species=$_GET["species"];


//Controle pour savoir si le gene existe
$requete="SELECT \"GENE_ID\" from \"GENE\" where \"NAME\"='$name'";

$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0)
	{
	print "<br><center><strong>Error: The genemodel \"$name\" was not found in the database</strong></center>";
	return;
	}
else
	{
	$row=pg_fetch_array($stmt);
	$geneid=$row["GENE_ID"];
	
	
	}

//Controle pour savoir si le mol tool existe
if (strlen($tools)>0)
{
$tools=trim($tools);
//$requete="SELECT \"MOL_ID\" from \"MOL_ENTITY\" where \"NAME\"='$tools'";
$requete="SELECT \"MOLECULAR_TOOL_ID\" from \"MOLECULAR_TOOL\" where \"NAME\"='$tools'";
$stmt=pg_query($c,$requete);
if (pg_num_rows($stmt)==0)
	{
	print "<br><center><strong>Error: The molecular tool \"$tools\" was not found in the database</strong></center>";
	print "<br>$requete";
	return;
	}
else
	{
	$row=pg_fetch_array($stmt);
	//$inibid=$row["MOL_ID"];
	$inibid=$row["MOLECULAR_TOOL_ID"];
	
	}
}


if (strlen($type)==0)
{
print "<br><center><strong>Error: Please select a regulation type before !</strong></center>";
return;
}

if (strlen($from)>0)
{
//Conversion des stades de developpement
$requete="select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$from' and \"SPECIES\"='$species'";
//print $requete;
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$from=$row["DEV_STAGE_ID"];
}
else
{
print "<br><center><strong>Error: Please select \"FROM_ID\" before !</strong></center>";
return;
}

if (strlen($to)>0)
{
$requete="select \"DEV_STAGE_ID\" from \"DEV_STAGE\" where \"NAME\"='$to' and \"SPECIES\"='$species'";
//print "<br>$requete<br>";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$to=$row["DEV_STAGE_ID"];
}


//Insertion

$option="";
$option2="";

if (strlen($tools)>0) {$option.=",\"INHIBITOR_ID\"";$option2.=",'$inibid'";}
if (strlen($to)>0) {$option.=",\"TO_ID\"";$option2.=",'$to'";}


$requete="INSERT INTO \"MUTANT\"(\"BIO_ID\",\"IN_SITU_ID\",\"UP_DOWN\",\"NAME\",\"FROM_ID\"".$option.")
values('$geneid','$id','$type','$name','$from'".$option2.")";



//$requete="INSERT INTO \"MUTANT\"(\"BIO_ID\",\"IN_SITU_ID\",\"FROM_ID\",\"TO_ID\",\"UP_DOWN\",\"NAME\",\"INHIBITOR_ID\") values('$geneid','$id','$from','$to','$type','$name','$inibid')";

//print $requete;

pg_query($c,"begin");
pg_query($c,$requete);
pg_query($c,"commit");


print '<center><H1>Insert done for '.$cloneid.'</H1></center>';
print ' <br><center> To see changes refresh the "IN SITU FULL REPORT" page</center><br>';


?>

<br><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>
<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script>

</body>
</html>