<?php
include("connection.php");
?>
<html>
<body bgcolor="#871F1F" text="#FFFFFF" link="#000000" vlink="#000000" alink="#000000">
<title>ANISEED</title>
<center>
<?php
	
    $comment=$_POST["comment"];
	
	$mol=strtoupper( $_POST["molecule"] );
	 $mol=split(":",$mol);
	$receptor=strtoupper( $_POST["receptor"] );
	 $receptor=split(":",$receptor);
	$target=strtoupper( $_POST["target"] );
	 $target=split(":",$target);
	$cascade=strtoupper( $_POST["cascade"] );
	 $cascade=split(":",$cascade);
//Reference 1	
	$pmid=$_POST["pmid"];
	$paper=$_POST["paper"];
	$title=$_POST["title"];
	$author=$_POST["author"];
	$url=$_POST["url"];
	$comment2=$_POST["comment2"];
	
//Reference 2
	$pmid2=$_POST["pmid2"];
	$paper2=$_POST["paper2"];
	$title2=$_POST["title2"];
	$author2=$_POST["author2"];
	$url2=$_POST["url2"];
	$comment22=$_POST["comment22"];

//Reference 3
	$pmid3=$_POST["pmid3"];
	$paper3=$_POST["paper3"];
	$title3=$_POST["title3"];
	$author3=$_POST["author3"];
	$url3=$_POST["url3"];
	$comment23=$_POST["comment23"];

//Reference 4
	$pmid4=$_POST["pmid4"];
	$paper4=$_POST["paper4"];
	$title4=$_POST["title4"];
	$author4=$_POST["author4"];
	$url4=$_POST["url4"];
	$comment24=$_POST["comment24"];





	$ifate=$_POST["ifate"];
	$dfate=$_POST["dfate"];
	
	$incell= $_POST["region0"];
	$cell= $_POST["region1"];
	
	
	if( (strlen($incell)<=0)and(strlen($cell)<=0) ){
	    print "Error: missing parameter";
		exit;
	}else{

	    //bio idé
		
		$requete="insert into \"BIO_ENTITY\" (\"BIO_ID\") values(nextval('\"S_BIO_ID\"'))";
		pg_query($c,"begin");
		
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		
		
		//get the last bio_id value
		/*En PostgreSQL un champ se nomme lastValue, nous allons l'utiliser*/
		//$requete="SELECT S_BIO_ID.CURRVAL as key FROM DUAL";
		$requete="SELECT currval('\"S_BIO_ID\"')";
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		if($row3=pg_fetch_array($stmt3)){
		    $clef=$row3["currval"];
		
		}else{
			print "ERROR: can't save induction data (2)";
			exit;
		}
		
		//induction
		$requete="insert into \"INDUCTION\" (\"INDUCTOR_ID\", \"INDUCED_ID\", \"DEFAULT_FATE\", \"COMMENTS\", \"INDUCTION_ID\") values($incell, $cell, '$dfate', '$comment', $clef)";
		
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		
	
		//induced fates
		for($m=0;$m<count($ifate);++$m){
		    $fate=$ifate[$m];
			
			$requete="insert into \"INDUCTION_FATE\" (\"INDUCTION_ID\", \"FATE\") values($clef, '$fate')";
			
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			
		}
		
		//mol involved
		//extracellular ligant
		for($m=0;$m<count($mol);++$m){
		    $gene=$mol[$m];

			if(strlen($gene)<=0)
			    continue;
			
			$requete="SELECT \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$gene'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if($row3=pg_fetch_array($stmt3)){
				$gene_id = $row3["GENE_ID"];
			}else{
				//OCIRollback($connection);
				print "ERROR: One of your gene references does not exists in the database";
				exit;
			}
			
			$requete="insert into \"INDUCTION_MOL\" (\"INDUCTION_ID\", \"MOL_ID\", \"TYPE\") values($clef, $gene_id, 'ligand')";
			print $requete;
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

			/*if(! OCIExecute($stmt3, OCI_DEFAULT) ){
				OCIRollback($connection);
				print "ERROR: can't save induction data (5)";
				exit;
			}*/
		}
		//receptor
		for($m=0;$m<count($receptor);++$m){
		    $gene=$receptor[$m];

			if(strlen($gene)<=0)
			    continue;
			
			$requete="SELECT \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$gene'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if($row3=pg_fetch_array($stmt3)){
				$gene_id = $row3["GENE_ID"];
			}else{
				//OCIRollback($connection);
				print "ERROR: One of your gene references does not exists in the database";
				exit;
			}
			
			$requete="insert into \"INDUCTION_MOL\" (\"INDUCTION_ID\", \"MOL_ID\", \"TYPE\") values($clef, $gene_id, 'receptor')";

			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			

		}
		//intracellular cascade
		for($m=0;$m<count($cascade);++$m){
		    $gene=$cascade[$m];

			if(strlen($gene)<=0)
			    continue;
			
			$requete="SELECT \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$gene'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if($row3=pg_fetch_array($stmt3)){
				$gene_id = $row3["GENE_ID"];
			}else{
				//OCIRollback($connection);
				print "ERROR: One of your gene references does not exists in the database";
				exit;
			}
			
			$requete="insert into \"INDUCTION_MOL\" (\"INDUCTION_ID\", \"MOL_ID\", \"TYPE\") values($clef, $gene_id, 'cascade')";
			
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			
		}
		//transcriptional target
		for($m=0;$m<count($target);++$m){
		    $gene=$target[$m];

			if(strlen($gene)<=0)
			    continue;
			
			$requete="SELECT \"GENE_ID\" from \"GENE\" where upper(\"NAME\")='$gene'";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if($row3=pg_fetch_array($stmt3)){
				$gene_id = $row3["GENE_ID"];
			}else{
				//OCIRollback($connection);
				print "ERROR: One of your gene references does not exists in the database";
				exit;
			}
			
			$requete="insert into \"INDUCTION_MOL\" (\"INDUCTION_ID\", \"MOL_ID\", \"TYPE\") values($clef, $gene_id, 'target')";
			$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			
		}
		
		
		
		//reference 1
		
		if (strlen($pmid.$title.$paper.$author.$url.$comment2)>0)
		{
		$requete="insert into \"REFERENCE\" (\"BIO_ID\", \"PUBMED_ID\", \"TITLE\", \"PAPER\", \"AUTHORS\", \"URL\", \"COMMENTS\") values($clef, '$pmid', '$title', '$paper', '$author', '$url', '$comment2')";
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		}
		
		//reference 2

		if (strlen($pmid2.$title2.$paper2.$author2.$url2.$comment22)>0)
		{
		$requete="insert into \"REFERENCE\" (\"BIO_ID\", \"PUBMED_ID\", \"TITLE\", \"PAPER\", \"AUTHORS\", \"URL\", \"COMMENTS\") values($clef, '$pmid2', '$title2', '$paper2', '$author2', '$url2', '$comment22')";
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		}

		//reference 3

		if (strlen($pmid3.$title3.$paper3.$author3.$url3.$comment23)>0)
		{
		$requete="insert into \"REFERENCE\" (\"BIO_ID\", \"PUBMED_ID\", \"TITLE\", \"PAPER\", \"AUTHORS\", \"URL\", \"COMMENTS\") values($clef, '$pmid3', '$title3', '$paper3', '$author3', '$url3', '$comment23')";
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		}

		//reference 4

		if (strlen($pmid4.$title4.$paper4.$author4.$url4.$comment24)>0)
		{
		$requete="insert into \"REFERENCE\" (\"BIO_ID\", \"PUBMED_ID\", \"TITLE\", \"PAPER\", \"AUTHORS\", \"URL\", \"COMMENTS\") values($clef, '$pmid4', '$title4', '$paper4', '$author4', '$url4', '$comment24')";
		$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		}





		
		pg_query($c,"commit");
		print "Data successfully entered";
	}
?>
<br><br><br>
<input type="button" name="OK" onClick="javascript: self.close();" value=" OK ">
</center>
</body>
</html>