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

<?php
$lines=0;

$id=$_GET["embryoid"];
   $test="n";
	
	print '<center><table width="80%" border="0" cellspacing="0" cellpadding="0">';
	print '<tr><td>Cell A</td><td>Cell B</td><td>Vol A</td><td>Vol B</td><td>Ratio</td></tr>';
	print '<tr><td><hr></td><td><hr></td><td><hr></td><td><hr></td><td><hr></td></tr>';
  	
  	
	/*$requete= "SELECT DISTINCT \"MOTHER_CELL\" from \"LINEAGE\" where \"DAUGHTER_CELL\" in (
		select \"ANAT_ID\" from \"ANAT_ENTITY\" where \"DEV_STAGE_ID\"=(
		select \"DEV_STAGE_ID\" from \"EMBRYO\" where \"EMBRYO_ID\"='$id'
		)
	)";*/

	$requete= "SELECT DISTINCT \"MOTHER_CELL\" from \"LINEAGE\" ";
	$stmt = pg_query($c,$requete);
	while( $row=pg_fetch_array($stmt) ){
	    $tab=(array) null;
		$tab2=(array) null;
	    $tab3=(array) null;
		
		$count=0;
	    $moid=$row["MOTHER_CELL"];

  		$requete= "select a.\"DEV_STAGE_ID\", a.\"ANAT_ID\", a.\"NAME\" from \"ANAT_ENTITY\" a, \"LINEAGE\" c where a.\"ANAT_ID\"=c.\"DAUGHTER_CELL\" and a.\"TYPE\"='cell' and c.\"MOTHER_CELL\"=$moid";
		$stmt2 = pg_query($c,$requete);
		while( $row2=pg_fetch_array($stmt2) ){	
			$tab[$count]=$row2["ANAT_ID"];
			$tab2[$count]=$row2["NAME"];
			$tab3[$count]="(".$row2["DEV_STAGE_ID"].")";
			$count++;
		}
		
		$moid="";
		
		
		if((count($tab2)<=2)and(count($tab2)>0)){
		    $requete= "select \"NAME\", \"VOLUME\", \"ANAT_ID\", \"DEV_STAGE_ID\" from \"ANAT_ENTITY\", \"EMBRYO_BIOMETRY\" where \"ANAT_ID\"=\"CELL\" and \"NAME\" ='".$tab2[0]."' and \"EMBRYO_ID\"='$id'";
			$stmt2 = pg_query($c,$requete);
			$vol1=0;
			$dev1="";
			$id1="";
			$nam1="";
			while( $row2=pg_fetch_array($stmt2) ){
				$nam1=$row2["NAME"];
				$id1=$row2["ANAT_ID"];
				$vol1=$row2["VOLUME"];
				$dev1=$row2["DEV_STAGE_ID"];
				
				$count2=0;
				$requete= "select \"NAME\", \"VOLUME\", \"ANAT_ID\" from \"ANAT_ENTITY\", \"EMBRYO_BIOMETRY\" where \"ANAT_ID\"=\"CELL\" and \"NAME\" ='".$tab2[1]."' and \"DEV_STAGE_ID\"='".$dev1."' and \"EMBRYO_ID\"='$id'";
				$stmt3 = pg_query($c,$requete);
				$vol2=0;
				$id2="";
				$nam2="";
				while( $row3=pg_fetch_array($stmt3) ){
				    ++$count2;
					$nam2=$row3["NAME"];
					$id2=$row3["ANAT_ID"];
					$vol2=$row3["VOLUME"];
					
				}

				if($count2==1){ //only one sister cell
					$ratio=0;
					if($vol1 >= $vol2){
						$ratio= round(100-(($vol2*100)/$vol1));
					}else{
						$ratio= round(100-(($vol1*100)/$vol2));
					}
					
					print "<tr> <td>$dev1 - $nam1 ($id1)</td> <td>$nam2 ($id2)</td> <td>$vol1</td> <td>$vol2</td> <td>$ratio</td> </tr>";
					++$lines;
					
					
					//transfert data into the database
					if( ($test=='n') ){
						$requete= "SELECT * from \"EMBRYO_DIVISION\" where \"EMBRYO_ID\"='$id' and (((\"CELL1\"='$id1') and (\"CELL2\"='$id2'))or((\"CELL2\"='$id1') and (\"CELL1\"='$id2')))";
						$stmt4 = pg_query($c,$requete);
pg_query($c,"begin");
						if( $row4=pg_fetch_array($stmt4) ){
// 							print "============>UPDATE<br>";
							//a previous record exists -> update
						
							$requete= "update \"EMBRYO_DIVISION\" set \"RATIO\"=$ratio where \"EMBRYO_ID\"='$id' and (((\"CELL1\"='$id1') and (\"CELL2\"='$id2'))or((\"CELL2\"='$id1') and (\"CELL1\"='$id2')))";
//  							print $requete."<br>";
							$stmt3 = pg_query($c,$requete);
							if(! $stmt3 ){
								pg_query($c,"rollback");
								print "ERROR while transfering data";
								return;
							}
						
						}else{
						
							//insert
							$requete= "insert into \"EMBRYO_DIVISION\" (\"CELL1\",\"CELL2\",\"RATIO\",\"EMBRYO_ID\") values ('$id1','$id2','$ratio','$id')";
							$stmt3 = pg_query($c,$requete);
							if(! $stmt3 ){
								pg_query($c,"rollback");
								print "ERROR while transfering data";
								return;
							}

						}
pg_query($c,"commit");
					}
					$ratio="";
					
					
					
				}
				
				
			}
		
		}
		
		
					
    }
    print '</table></center>';
	print $lines." entries";
    print "<br><br>-- Ok --<br>";
	
?>
<SCRIPT LANGUAGE="JavaScript"> javascript:opener.location.reload(); self.close(); </script> 

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