<?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>";
	    include ("frames_footer.php"); 
		return;
	}
  	
	//From a number, creates a Stable ID "CONS + 8 digits"
	function create_stable_id($number){
		$digits = 1;
		$tmp = $number;
		while(floor($tmp / 10)>0){
			$digits++;
			$tmp = $tmp / 10;
		}
		$st_id = "CONS";
		for($int = 1; $int<=8-$digits; $int++){
			$st_id = $st_id."0";
		}
		$st_id = $st_id.$number;
		return $st_id;
	}
	 
	$success = true;
	
	//Get the maximum stable_id
	$khoueiry="SELECT MAX(\"STABLE_ID\") AS \"STABLE_ID\" FROM \"CONSTRUCTION\"";
	$stmt = pg_query($c,$khoueiry) or die("<strong>ERROR </strong>".pg_last_error($c));
	if($row=pg_fetch_array($stmt)){ 
		//extract the number
		$max_stable_id = substr($row["STABLE_ID"],4);
		$new_stable_id = create_stable_id($max_stable_id + 1);
	}
	
	pg_query($c,"BEGIN;")  or die("<strong>ERROR Start: </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 Bio: </strong>".pg_last_error($c));

	$khoueiry = "SELECT \"SPECIES\" FROM \"MOL_ENTITY\" WHERE \"MOL_ID\"=".$_POST["PROMOTER_ID"].";";
	$stmt = pg_query($c,$khoueiry);	
	if($row=pg_fetch_array($stmt) && $row["SPECIES"]!=''){ 
		$species = $row["SPECIES"];
		$khoueiry = "INSERT INTO \"MOL_ENTITY\" (\"MOL_ID\", \"SPECIES\",\"NAME\",\"TYPE\") VALUES (currval('\"S_BIO_ID\"'),'$species','".$_POST["NAME"]."','construction');";
	} else {
	  	$khoueiry = "INSERT INTO \"MOL_ENTITY\" (\"MOL_ID\", \"NAME\",\"TYPE\") VALUES (currval('\"S_BIO_ID\"'),'".$_POST["NAME"]."','construction');";
	}
  	
	pg_query($c,$khoueiry) or die("<strong>ERROR Molecule: </strong>".pg_last_error($c));
		
	//$khoueiry = "INSERT INTO \"CONSTRUCTION\" (\"STABLE_ID\", \"CONSTRUCTION_ID\", \"PROMOTER_ID\", \"NAME\", \"MIN_PROMOT_ID\", \"REPORTER_ID\", \"COMMENTS\") VALUES ('".$new_stable_id."', currval('\"S_BIO_ID\"'), ".$_POST["PROMOTER_ID"].", '".$_POST["NAME"]."', ".$_POST["MIN_PROMOT_ID"].", '".$_POST["REPORTER_GENE"]."', '".$_POST["COMMENTS"]."');";   	
		
	$khoueiry = "INSERT INTO \"CONSTRUCTION\" (\"STABLE_ID\", \"CONSTRUCTION_ID\", \"PROMOTER_ID\", \"NAME\", \"MIN_PROMOT_ID\", \"REPORTER_GENE\", \"COMMENTS\") VALUES ('".$new_stable_id."', currval('\"S_BIO_ID\"'), ".$_POST["PROMOTER_ID"].", '".$_POST["NAME"]."', ".$_POST["MIN_PROMOT_ID"].", '".$_POST["REPORTER_GENE"]."', '".$_POST["COMMENTS"]."');";   	
	
	pg_query($c,$khoueiry) or die("<strong>ERROR Construction: </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\"=".$_POST["PROMOTER_ID"];	  
	} else {
	    $khoueiry = "update \"ENTRY_ORIGIN\" set \"ANNOTATOR_ID\"=".$_SESSION['curationauthor'].", \"ANNOTATION_DATE\"=now() where \"BIO_ID\"=".$_POST["PROMOTER_ID"];		  
	}
	pg_query($c,$khoueiry) or die("<strong>ERROR Entry : </strong>".pg_last_error($c));
	  	  	
	pg_query($c, "COMMIT") or die("<strong>ERROR Exit: </strong>".pg_last_error($c));
		
	$khoueiry = "SELECT CURRVAL('\"S_BIO_ID\"');";
	$stmt=pg_query($c,$khoueiry); 
	$cur_id=pg_fetch_result($stmt,0);

	//edit the new region, if successfull			
	print '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=construct-edit.php?constructid='.$cur_id.'">'; 
	
	include ("frames_footer.php"); 

?>