<?php
	//connection
	include("connection.php");
    include("frames_header.php");
?>

<?php
    //ignore_user_abort(true);
    $test=$_POST["test"];
	if($test!='y'){
	    $test='n';
	}else print "test mode<br>";
	
    $av=$_POST["av"];
	if($av!='y')
	    $av='n';

    $path=$_POST["filepath"];
	$stage="";
	
//TEMPO
//$path="./76c_neigh.txt";
$path="./biometry/vl112e1CI_CONTACT.txt";

///////



	$file = fopen ("$path", "r");
	while ($line = fgets($file)){
		$line=rtrim($line);
		if( ereg("^#devstage=([0-9]+)", $line, $regs) ){
            $stage=$regs[1];
		}
		else if( ! ereg("^#", $line) ){
		    
		    $data=split("\t",$line);
			
			if( count($data)!=6 ){
			    print "<br><br><br> <center>WARNING: your file does not seems to be an ANISEED distance & contact report <br>
				use the 3D software to export the distance & contact report file</center> <br><br><br>";
				include("frames_footer.php");
				return;
			}
			
			$cell  =$data[0];
			$cell2  =$data[1];
			$dist   =$data[2];
			$contact=$data[5];
			
			print $cell."-".$cell2."-".$dist."-".$contact."<br>\n";
			
			if($stage==""){
			    print "<br><br><br> <center>WARNING: your file does not seems to be an ANISEED distance & contact report <br>
				use the 3D software to export the distance & contact report file</center><br>
				ERROR: unable to find the developmental stage ID
				<br><br><br>";
				include("frames_footer.php");
				return;
			}
			
			
			$requete= "SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$cell' and \"DEV_STAGE_ID\"='$stage'";
			$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if( $row=pg_fetch_array($stmt) ){
				$cellid=$row["ANAT_ID"];
			}else{
				print "Error: cell anatomical id not found for $cell";
				return;
			}
			
			$requete= "SELECT \"ANAT_ID\" from \"ANAT_ENTITY\" where \"NAME\"='$cell2' and \"DEV_STAGE_ID\"='$stage'";
			$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			if( $row=pg_fetch_array($stmt) ){
				$cellid2=$row["ANAT_ID"];
			}else{
				print "Error: cell anatomical id not found for $cell";
				return;
			}
			
			
			
			if( $test=='n' ){
/*Debut de la transaction*/
pg_query($c,"begin");
				$requete= "SELECT \"CELL_A\", \"CELL_B\", \"AV\", \"DISTANCE\", \"CONTACT\" from \"NEIGHBOR\" where (\"CELL_A\"='$cellid' and \"CELL_B\"='$cellid2')or(\"CELL_A\"='$cellid2' and \"CELL_B\"='$cellid')";
				$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				if( $row=pg_fetch_array($stmt) ){
					$oldav      =$row["AV"];
					$olddist    =$row["DISTANCE"];
					$oldcontact =$row["CONTACT"];
					
					//update
					if($av=='n'){
						$requete= "update \"NEIGHBOR\" set \"DISTANCE\"='$dist', \"CONTACT\"='$contact', \"AV\"='1' where (\"CELL_A\"='$cellid' and \"CELL_B\"='$cellid2')or(\"CELL_A\"='$cellid2' and \"CELL_B\"='$cellid')";
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
					}else{
					    $olddist   = round( (($olddist*$oldav)    + $dist)   /($oldav+1) );
						$oldcontact= round( (($oldcontact*$oldav) + $contact)/($oldav+1) );
					    $oldav += 1;
						
						$requete= "update \"NEIGHBOR\" set \"DISTANCE\"='$olddist', \"CONTACT\"='$oldcontact', \"AV\"='$oldav' where (\"CELL_A\"='$cellid' and \"CELL_B\"='$cellid2')or(\"CELL_A\"='$cellid2' and \"CELL_B\"='$cellid')";	
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
					}
					
				}else{
                    //insert
					$requete= "insert into \"NEIGHBOR\" (\"CELL_A\", \"CELL_B\", \"DISTANCE\", \"CONTACT\", \"AV\") values ('$cellid', '$cellid2', '$dist', '$contact', '1')";
					$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

				}
	
/*Commit de la transaction*/
pg_query($c,"commit");

				$cell="";
				$cell2="";
				$dist="";
				$contact="";
				
			}
			
		}
		
	}
    print "stage: $stage";
    print "<br>-- Ok --<br>";
?>

<?php
    include("frames_footer.php");
?>