<?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="";

print "PATH=$path<br>";

//$path="sv_24c.txt";
$path="./biometry/l112e2CI_VOL.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)!=3 ){
			    print "<br><br><br> <center>WARNING: your file does not seems to be an ANISEED biometry report <br>
				use the 3D software to export the biometry report file with volume and surface</center> <br><br><br>";
				include("frames_footer.php");
				return;
			}
			
			$cell=$data[0];
			$surf=$data[1];
			$vol =$data[2];
			
			print $cell."-".$surf."-".$vol."<br>\n";
			
			if($stage==""){
			    print "<br><br><br> <center>WARNING: your file does not seems to be an ANISEED biometry report <br>
				use the 3D software to export the biometry report file with volume and surface</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;
			}
				
			if( $test=='n' ){
/*Debut de la transaction d'insert*/ 
pg_query($c,"begin"); 
				$requete= "SELECT \"CELL\", \"AV\", \"VOLUME\", \"SURFACE\" from \"BIOMETRY\" where \"CELL\"='$cellid'";
				$stmt = pg_query($c,$requete) or die("<strong>Error </strong> ".pg_last_error($c));
				if( $row=pg_fetch_array($stmt)){
					$oldav=$row["AV"];
					$oldvol=$row["VOLUME"];
					$oldsurf=$row["SURFACE"];
					
					//update
					if($av=='n'){
						$requete= "update \"BIOMETRY\" set \"VOLUME\"='$vol', \"SURFACE\"='$surf', \"AV\"='1' where \"CELL\"='$cellid'";
						
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
					}else{
					    $oldvol = round( (($oldvol*$oldav)  + $vol) /($oldav+1) );
						$oldsurf= round( (($oldsurf*$oldav) + $surf)/($oldav+1) );
					    $oldav += 1;
						
						$requete= "update \"BIOMETRY\" set \"VOLUME\"='$oldvol', \"SURFACE\"='$oldsurf', \"AV\"='$oldav' where \"CELL\"='$cellid'";	
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));

					}
					
				}else{
                    //insert
					$requete= "insert into \"BIOMETRY\" (\"CELL\",\"VOLUME\",\"SURFACE\",\"AV\") values ('$cellid','$vol','$surf','1')";
					$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					
				}
				//$committed = OCICommit($connection);
/*Commit de la transaction*/
pg_query($c,"commit");
				$cell="";
				$surf="";
				$vol="";
				
			}
			
		}
		
	}
    print "stage: $stage";
    print "<br>-- Ok --<br>";
?>

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