<?php
	//connection
	include ("frames_header.php");
	include("connection.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></center>";
	    include ("frames_footer.php"); 
		return;
	}
?>

<?php

//not being used for the moment...
//include("utils.php");

$id=$_POST["regid"];
$motif_id=$_POST["motif"];

if (isset($_POST["begin"]) && is_numeric($_POST["begin"]) && $_POST["begin"]!=0){
	$begin=$_POST["begin"];   
} else {
 	print "<br><br><center><strong>Begin position is empty or incorrect!</strong></center><br><br>";
 	include ("frames_footer.php"); 
	return; 
}

if (isset($_POST["stop"]) && is_numeric($_POST["stop"]) && $_POST["stop"]!=0){
	$stop=$_POST["stop"];   
} else { 
	print "<br><br><center><strong>Stop position is empty or incorrect!</strong></center><br><br>";
	include ("frames_footer.php"); 
	return;  
}

if($begin>$stop){
	print "<br><br><center><strong>Begin position larger than Stop position!</strong></center><br><br>";
	include ("frames_footer.php"); 
	return;  
}

//0 does not exist!
$add=1;
if(($begin*$stop)<=0){ $add = 0; }
$size = abs($stop-$begin)+$add;
if(!is_int($size)){
	print "<br><br><center><strong>Non integer values entered!</strong></center><br><br>";
	include ("frames_footer.php"); 
	return;	  
}

//if (isset($_POST["seq"])) $sequence=$_POST["seq"]; 
//	else print "<br><br><center><strong>Sequence is empty !</strong></center><br><br>";

$comments = "";
if (isset($_POST["comments"])){
	$comments=$_POST["comments"];   
}
//	else print "<br><br><center><strong>Comments is empty !</strong></center><br><br>";

//Recup du motif_id et du promoter_id
/*
$requete="SELECT \"SIZE\", \"MATRIX\" from \"MOTIF_REGUL\" where \"MOTIF_ID\"=$motif";
$stmt=pg_query($c,$requete);
if ($row=pg_fetch_array($stmt)) {
	$matrix = $row["MATRIX"];
	$size = $row["SIZE"];
}
*/
/*
//need to update the matrix?
if($_POST["update"]==1){
  $update='y';
} else {
  $update='n';
}
*/

/*
//need to say if it is front or backward...
if($_POST["reverse"]==1){
  $orientation='-';
} else {
  $orientation='+';
}
*/

//Get the sequence from the Regulatory region...
$requete="SELECT A.\"BEGIN\", A.\"END\", B.\"SEQUENCE\" from \"PROMOTER\" A, \"SEQUENCE\" B where \"MOL_ID\"=\"PROMOTER_ID\" AND \"PROMOTER_ID\"=$id";
$stmt=pg_query($c,$requete);
if ($row=pg_fetch_array($stmt)) {
  
  	//check for confirmity...
  	if($begin<$row["BEGIN"] || $stop>$row["END"]){
		print "<br/><center><strong>Given motif coordinates exceed sequence coordinates!</strong></center><br/>";
		include ("frames_footer.php"); 
		return;	    
	}
  
	$lob = $row["SEQUENCE"];
	pg_query ($c, "begin");		
	$handle_lo = pg_lo_open($c,$lob,"r");
	$region=pg_lo_read($handle_lo);
	pg_query($c,"end");
	$add=0;
	if($row["BEGIN"]<0 && $begin>0) { $add = -1; }
	$sequence = substr($region,$begin-$row["BEGIN"]+$add,$size);
}

//Insertion
pg_query($c,"begin") or die("<strong>ERROR </strong>".pg_last_error($c));

$khoueiry="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\", \"CURATED\" ) values ( nextval('\"S_BIO_ID\"'),'n')";
pg_query($c,$khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c));

/*
//update the matrix if needed...
if($update=='y'){  
	$reverseP = false;
  	if($orientation=='-'){
	    $reverseP = true;
	}	
  	//utils.php
	$matrix = update_matrix($matrix,$sequence,$reverseP,true);
	
	$requete="UPDATE \"MOTIF_REGUL\" SET \"MATRIX\" = '$matrix' where \"MOTIF_ID\"=$motif";
	$stmt=pg_query($c,$requete);	
	
	if(!pg_query($c,$khoueiry)){
	  echo '<p>Error updating the matrix</p>';
	  return;
	} 
}
*/				
//$stop = $begin + $size;
//$requete="insert into \"MOTIF_COORD\" (\"MOTIF_COORD_ID\", \"MOTIF_ID\",\"SEQUENCE\",\"PROMOTER_ID\",\"BEGIN\",\"STOP\", \"ORIENTATION\", \"UPDATE_MATRIX\") values (currval('\"S_BIO_ID\"'),$motif_id,'$sequence',$id,'$begin','$stop','$orientation','$update')";

$requete="insert into \"MOTIF_COORD\" (\"MOTIF_COORD_ID\", \"MOTIF_ID\",\"SEQUENCE\",\"PROMOTER_ID\",\"BEGIN\",\"STOP\",\"COMMENTS\") values (currval('\"S_BIO_ID\"'),$motif_id,'$sequence',$id,'$begin','$stop','$comments')";
pg_query($c,$requete) or die("<strong>Error</strong> ".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\"='$id'";	  
	} else {
	      $khoueiry = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=now() where \"BIO_ID\"='$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>';

include ("frames_footer.php"); 
		
?>
