<?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>";
	    print '</center></body></html>';
		return;
	}
			
	$gene_name = trim($_POST["gene"]);
	//VERIFY IF GENE_ID EXISTS...
	$khoueiry = "SELECT * FROM \"GENE\" WHERE \"NAME\"='".$gene_name."'";
	$stmt = pg_query($c,$khoueiry);
		
	if($row=pg_fetch_array($stmt)){    
	    
		pg_query($c, "BEGIN") or die(pg_last_error($c));

		//new entity
		//$khoueiry="INSERT INTO \"GENE_REGULATION\" (\"PROMOTER_ID\", \"GENE_ID\", \"TYPE\")values (".$_POST["ID"].",".$row["GENE_ID"].",'".$_POST["TYPE"]."')";	
		
		$khoueiry="INSERT INTO \"GENE_REGULATION\" (\"PROMOTER_ID\", \"GENE_ID\") values (".$_POST["id"].",".$row["GENE_ID"].")";	
  	
  		pg_query($c,$khoueiry) or die(pg_last_error($c));  		
  	
		$test="SELECT \"IS_ADMIN\" from \"AUTHOR\" where \"LOGIN\"='".$_SESSION["login"]."'";
		$stest=pg_query($c,$test);
		$rtest=pg_fetch_array($stest);
		if ($rtest["IS_ADMIN"]=='y'){
			$khoueiry = "update \"ENTRY_ORIGIN\" set \"CURATOR_ID\"=".$_SESSION['curationauthor'].", \"CURATION_DATE\"=now() where \"BIO_ID\"=".$_POST["id"];	  
		} else {
		    $khoueiry = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=now() where \"BIO_ID\"=".$_POST["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>';
		
	} else {
		echo '<p>Did not find gene '.$_POST["GENE"].' in the database</p>';	  
	}		     

	include ("frames_footer.php"); 

?>