<?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;
}
	
if (!isset($_POST["begin"]) || !is_numeric($_POST["begin"]) || $_POST["begin"]==0){
 	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){
	print "<br><br><center><strong>Stop position is empty or incorrect!</strong></center><br><br>";
	include ("frames_footer.php"); 
	return;  
}

if($_POST["begin"]>$_POST["stop"]){
	print "<br><br><center><strong>Begin position larger than Stop position!</strong></center><br><br>";
	include ("frames_footer.php"); 
	return;  
}
	
	$id=$_POST["id"];
	//$motif=$_POST["motif"];
	$begin=$_POST["begin"];
	$oldbegin=$_POST["oldbegin"];
	$stop=$_POST["stop"];
	$oldstop=$_POST["oldstop"];
	$promoter=$_POST["promoter"];
	//$orientation=$_POST["orientation"];
	//$update=$_POST["update"];
	//$newbegin=$_POST["newbegin"];

	if($oldbegin!=$begin || $oldstop!=$stop){	
	
		//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;	  
		}
		
		//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\"=$promoter";
		$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");
			
			$nbegin = $begin-$row["BEGIN"];			
			if($row["BEGIN"]<0 && $begin>0) { $nbegin--; }
			$sequence = substr($region,$nbegin,$size);			
		}				
		
		pg_query($c,"begin") or die("<strong>ERROR </strong>".pg_last_error($c));
	
		$requete="update \"MOTIF_COORD\" set \"BEGIN\"='$begin',\"STOP\"='$stop',\"SEQUENCE\"='$sequence' where \"MOTIF_COORD_ID\"='$id'";

		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\"=".$promoter;	  
		} else {
		    $khoueiry = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=now() where \"BIO_ID\"=".$promoter;		  
		}
		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");				

?>

