<?
include ("connection.php");


$name=$_POST["name"];
$type=$_POST["type"];
$supplier=$_POST["supplier"];
$comments=$_POST["comments"];
$assogene=$_POST["gene"];
$regulation=$_POST["regulation"];
$seq=$_POST["seq"];
$gene=$_POST["gene"];
$pmid=$_POST["pmid"];


$requete="SELECT \"NAME\" from \"GENE\" where \"NAME\"='$gene'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
if (pg_num_rows($stmt)==0)
	{
	print "<br><center>Transcript ID is not valid !!</center><br>";
	include ("frames_footer.php");
	return;
	}

if (strlen($name)==0) 
	{
	print "<br><center>Tool name is mandatory !</center><br>";
	include ("frames_footer.php");
	return;
	}


pg_query($c,"begin");

$requete="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\",\"CURATED\") values (nextval('\"S_BIO_ID\"'),'y')";
pg_query($c,$requete);

$requete="INSERT INTO \"MOL_ENTITY\" (\"MOL_ID\",\"NAME\",\"TYPE\") values (currval('\"S_BIO_ID\"'),'$name','tool')";
pg_query($c,$requete);

$requete="INSERT INTO \"MOLECULAR_TOOL\" (\"MOLECULAR_TOOL_ID\",\"NAME\",\"TYPE\",\"SUPPLIER\",\"COMMENTS\",\"ASSOCIATED_GENE_ID\",\"REGULATION\") values (currval('\"S_BIO_ID\"'),'$name','$type','$supplier','$comments','$gene','$regulation')";

pg_query($c,$requete) or die(pg_last_error($c));


if (strlen($seq)>0)
{

$oid=pg_lo_create($c);
$requete="INSERT INTO \"SEQUENCE\" (\"MOL_ID\",\"SEQUENCE\") values (currval('\"S_BIO_ID\"'),'$oid')";
$stmt=pg_query($c, $requete) or die(pg_last_error($c));
$handler=pg_lo_open($c,$oid,"w");
pg_lo_write($handler,$seq);
pg_lo_close($handler);

}

if (strlen($pmid)>0)
{
$requete="INSERT INTO \"REFERENCE\" (\"BIO_ID\",\"PUBMED_ID\") values (currval('\"S_BIO_ID\"'),'$pmid')";
$stmt=pg_query($c,$requete);
}

pg_query($c,"commit");

?>

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