<?php
    include("connection.php");
    include("frames_header.php");
?>
<form name="formulaire" target="_blank" action="lineage-delete3.php" method="post">
<?php
    $id=$_POST["region"];
	$id=$id[0];
    $dev="";
	$couleur=array();
	print "<center>";
	
	$requete="select B.\"NAME\" as \"DEV\", A.\"NAME\", A.\"DEV_STAGE_ID\" from \"ANAT_ENTITY\" A, \"DEV_STAGE\" B where A.\"ANAT_ID\"='$id' and A.\"DEV_STAGE_ID\"=B.\"DEV_STAGE_ID\"";
    $stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	if ($row=pg_fetch_array($stmt)){
	    $dev=$row["DEV_STAGE_ID"];
	    print "<h2><strong><em><u>".$row["NAME"]." &nbsp;(".$row["DEV"].") lineage</u></em></strong></h2><br>";
	}
	
	print '<br>';
	
	print ' <font color="#999999">Select the <u>daughter cells</u> you want to remove from this line.<br>mother cells are removed when every daughter cells are deleted.</font>';
	print '<table border="1" cellspacing="0" cellpadding="0"><tr>';
	
	$count=0;
	$doublons=(array) null;//when a structure has several parents -> avoid doublons
	//search for cell precursors

/*Hierarchie*/
function select_recursif_descendant($conn,$start,$res)
{
global $doublons;
global $mocell;
global $couleur;
global $daughtername;
global $dev2;
global $dev;
global $devname;
global $lastdev;
global $count;
/*ON ECRIT SA REQUETE*/

if ($start!='') 
$requete="SELECT distinct * FROM \"LINEAGE\" WHERE  \"DAUGHTER_CELL\"=$start";
else return;


$stmt=pg_query($conn,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while ($row=pg_fetch_array($stmt))
	{
	$res=select_recursif_descendant($conn,"'".$row["MOTHER_CELL"]."'",$res);	

	
	    if(! in_array($row["DAUGHTER_CELL"],$doublons))
	    	$doublons[count($doublons)]=$row["DAUGHTER_CELL"];
		else
		    continue;
	
        ++$count;
		
		$mocell=$row["MOTHER_CELL"];
		
		if(! in_array($mocell,$couleur))
			if(strlen($couleur[$mocell])<=0)
		    	$couleur[$mocell]=rgb2html(rand(0,255),rand(0,255),rand(0,255));
		if(! in_array($row["DAUGHTER_CELL"],$couleur))
			if(strlen($couleur[$row["DAUGHTER_CELL"]])<=0)
		    	$couleur[$row["DAUGHTER_CELL"]]=rgb2html(rand(0,255),rand(0,255),rand(0,255));

		
		
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));
		if ($row2=pg_fetch_array($stmt2)){
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font color="#FF9900">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}
		
		if($dev2==$lastdev){
		
            print '<br><font size="+2" color="'.$couleur[$mocell].'">&#8226;</font><a href="short_hierarchy.php?region='.$row["DAUGHTER_CELL"].'&dev='.$dev2.'">'.$daughtername.'</a><font size="+2" color="'.$couleur[$row["DAUGHTER_CELL"]].'">&#8226;</font>';
		}else{
		    $lastdev=$dev2;
			print '</td><td align="left" >&nbsp;'.$devname.'&nbsp;<br>____________________<br><font size="+2" color="'.$couleur[$mocell].'">&#8226;</font><a href="short_hierarchy.php?region='.$row["DAUGHTER_CELL"].'&dev='.$dev2.'">'.$daughtername.'</a><font size="+2" color="'.$couleur[$row["DAUGHTER_CELL"]].'">&#8226;</font>';
		}
		//print '<td align="left" >&nbsp;'.$devname.'&nbsp;<br>___________________<br><a href="short_hierarchy.php?region='.OCIResult($stmt,"DAUGHTER_CELL").'&dev='.$dev2.'">'.$daughtername."</a></td>";

	}
return $res;
}

$res=select_recursif_descendant($c,$id,$res);


///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
	print '<td>';
	$lastdev="";
	$doublons=(array) null;//not sure it is usefull here
	//$lastmother="";
	//$couleur=array();

//search for daughter cell
/*Hierarchie*/
function select_recursif_descendant2($conn,$start,$res)
{
global $doublons;
global $mocell;
global $couleur;
global $daughtername;
global $dev2;
global $dev;
global $devname;
global $lastdev;
global $count;
/*ON ECRIT SA REQUETE*/
print $start;
if ($start!='') 
$requete="SELECT distinct * FROM \"LINEAGE\" WHERE  \"MOTHER_CELL\"=$start";
else return;


$stmt=pg_query($conn,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while ($row=pg_fetch_array($stmt))
	{
	
	    if(! in_array($row["DAUGHTER_CELL"],$doublons))
	    	$doublons[count($doublons)]=$row["DAUGHTER_CELL"];
		else
		    continue;
	
        ++$count;
		$mocell=$row["MOTHER_CELL"];
////////
		if(! in_array($mocell,$couleur))
			if(strlen($couleur[$mocell])<=0)
		    	$couleur[$mocell]=rgb2html(rand(0,255),rand(0,255),rand(0,255));
		if(! in_array($row["DAUGHTER_CELL"],$couleur))
			if(strlen($couleur[$row["DAUGHTER_CELL"]])<=0)
		    	$couleur[$row["DAUGHTER_CELL"]]=rgb2html(rand(0,255),rand(0,255),rand(0,255));

////////
		$requete2= "SELECT a.\"NAME\", a.\"DEV_STAGE_ID\", b.\"NAME\" as \"DEV\" FROM \"ANAT_ENTITY\" a, \"DEV_STAGE\" b where a.\"ANAT_ID\"='".$row["DAUGHTER_CELL"]."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));
		if ($row2=pg_fetch_array($stmt2)){
		    $daughtername=$row2["NAME"];
			$dev2=$row2["DEV_STAGE_ID"];
			if($dev2==$dev)
			    $devname='<strong><font color="#FF9900">'.$row2["DEV"].'</font></strong>';
			else
			    $devname=$row2["DEV"];
		}
		
		if($dev2==$lastdev){
		
		   // if(  ($mocell!=$lastmother) ) //and($mothername != $daughtername)  )
		   //    print "<br>";
        
            print '<br>'. '<font size="+2" color="'.$couleur[$mocell].'">&#8226;</font>' . '<input type="checkbox" name="anat[]" value="'.$mocell.'--'.$row["DAUGHTER_CELL"].'">' .'<a href="short_hierarchy.php?region='.$row["DAUGHTER_CELL"].'&dev='.$dev2.'">'.$daughtername.'</a>'. '<font size="+2" color="'.$couleur[$row["DAUGHTER_CELL"]].'">&#8226;</font>';
			//<font color="'.$couleur[$mocell].'">&#8226;</font>
			/*
			if(  $mocell==$lastmother ){ //detect asymetric division
				$requete= "SELECT daughter_cell FROM CIONA_DESIGN.lineage where mother_cell=";
				$stmt2 = OCIParse($connection, $requete);
				OCIExecute($stmt2);
				if (OCIFetch($stmt2)){
				}
			}
			*/
		
		}else{
		    $lastdev=$dev2;
			print '</td><td align="left">&nbsp;'.$devname.'&nbsp;<br>___________________<br>'. '<font size="+2" color="'.$couleur[$mocell].'">&#8226;</font>'.  '<input type="checkbox" name="anat[]" value="'.$mocell."--".$row["DAUGHTER_CELL"].'">'  .'<a href="short_hierarchy.php?region='.$row["DAUGHTER_CELL"].'&dev='.$dev2.'">'.$daughtername.'</a>'. '<font size="+2" color="'.$couleur[$row["DAUGHTER_CELL"]].'">&#8226;</font>';
		}
		
		/*
		if($mocell!=$lastmother)
			$lastmother=$mocell;
		*/
	$res=select_recursif_descendant2($conn,"'".$row["DAUGHTER_CELL"]."'",$res);	
		
	}
return $res;
}

$res=select_recursif_descendant2($c,$id,$res);



	
	print '</td></tr></table>';
    

	if($count==0)
	    print "<strong>No match</strong>";
	else
	    print '<input name="submit" type="image" src="pictures/button-submit.jpg" onClick="submit">';
	print "<center>";



function rgb2html($r,$g,$b)
{
  // Le str_pad permet de remplir avec des 0
  // parce que sinon rgb2html(Array(0,255,255)) retournerai #0ffff<=manque un 0 !
  return "#" . str_pad(dechex( ($r<<16)|($g<<8)|$b ), 6, "0", STR_PAD_LEFT);
}

?>
</form>

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