<?
/*
    ANISEED, NISEED System and Database
    Copyright (C) 2007 Olivier Tassy, Fabrice Daian, Patrick Lemaire

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
include("connection.php");
?>
<?
header ("Content-type: image/png");


ini_set("memory_limit","128M");


/* AJOUT */

  $id=$_GET["idanat"];

   $dev="";
	$lastdev="";
	
	
	$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);
	if ($row=pg_fetch_array($stmt)){
	    $dev=$row["DEV_STAGE_ID"];
	    
	}
	
	
	
	
	
	
	$count=0;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	$lastdev="";
	//$lastmother="";

	$count=0;
	$doublons=(array) null;//when a structure has several parents -> avoid doublons
	//search for cell precursors



$term=(array) null;
$term2=(array) null;

function select_recursif_descendant_2($conn,$start,$res,$nivo,$doubl)
{
global $doublons;
global $lastdev;
global $count;
global $dev;
global $term;
global $term2;

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

}
else return;

$stmt=pg_query($conn,$requete);

//Rajout
if (pg_num_rows($stmt)!=0) $nivo++;//print "Nz";
if (pg_num_rows($stmt)==0) $nivo--;//print "Ze";



while ($row=pg_fetch_array($stmt))
	{
	
	$res=select_recursif_descendant_2($conn,$row["MOTHER_CELL"],$res,$nivo,$doubl);
	

	if ($doubl=='yes')
	{

	    if(! in_array($row["DAUGHTER_CELL"],$doublons))
	    	{
		$doublons[count($doublons)]=$row["DAUGHTER_CELL"];
		
		//Rjout
		$mothercell=$row["MOTHER_CELL"];
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mothercell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}
        ++$count;
		$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);
		
		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"];
		}
		}
		else continue;
		
		
	}
	else
	{
		//Rjout
		$mothercell=$row["MOTHER_CELL"];
		$requete3= "SELECT a.\"NAME\" FROM \"ANAT_ENTITY\" a where a.\"ANAT_ID\"='$mothercell' ";
		$stmt3 = pg_query($conn,$requete3);
		if ($row3=pg_fetch_array($stmt3)) {
		    $mothername=$row3["NAME"];
			
		}
        ++$count;
		$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);
		
		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"];
		}

	}

$test="SELECT \"ANAT_ID\" FROM \"ANAT_ENTITY\" where \"CHILD_OF\"='".$row["DAUGHTER_CELL"]."'";
$st=pg_query($conn,$test);
//if (pg_num_rows($st)==0)
//{	
if ($doubl=='yes') $term[$nivo].=$row["MOTHER_CELL"].">".$row["DAUGHTER_CELL"]."@";
if ($doubl=='no') $term2[$nivo].=$row["MOTHER_CELL"].">".$row["DAUGHTER_CELL"]."@";
//}

//
	}
return $res;
}


$res=select_recursif_descendant_2($c,$id,$res,0,'yes');
$res=select_recursif_descendant_2($c,$id,$res,0,'no');

//On a $term dans l'ordre inverse, on va le remettre dans l'ordre:
$oterm=(array) null;
$oterm2=(array) null;

for ($a=count($term);$a>=0;$a--) $oterm[count($oterm)]=$term[$a];
for ($a=count($term2);$a>=0;$a--) $oterm2[count($oterm2)]=$term2[$a];


/* FIN AJOUT */

/* INTERFACAGE */

/*$table=$recap;
$table2=$recap2;*/
$recap=$oterm;
$recap2=$oterm2;
$stade=$dev;
/*$recap=(array) null;
$recap2=(array) null;*/

/* FIN INTERFACAGE*/

/*
$table=$_POST["tableau"];
$table2=$_POST["tableau2"];
$stade=$_POST["stade"];

$recap=explode("$",$table);
$recap2=explode("$",$table2);
*/



function getPere($string)
{
$tmp=explode(">",$string);
return $tmp[0];
}

function getFils($string)
{
$tmp=explode(">",$string);
$fils=substr($tmp[1],0,strlen($tmp[1]));
return $fils;
}

//Nouveau pour le dessin de fleche
function arrow($im, $x1, $y1, $x2, $y2, $alength, $awidth, $color) {

   $distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));

   $dx = $x2 + ($x1 - $x2) * $alength / $distance;
   $dy = $y2 + ($y1 - $y2) * $alength / $distance;

   $k = $awidth / $alength;

   $x2o = $x2 - $dx;
   $y2o = $dy - $y2;

   $x3 = $y2o * $k + $dx;
   $y3 = $x2o * $k + $dy;

   $x4 = $dx - $y2o * $k;
   $y4 = $dy - $x2o * $k;

   imageline($im, $x1, $y1, $dx, $dy, $color);
   imageline($im, $x3, $y3, $x4, $y4, $color);
   imageline($im, $x3, $y3, $x2, $y2, $color);
   imageline($im, $x2, $y2, $x4, $y4, $color);

}

function html2rgb($color)
{
  // gestion du #...
  if (substr($color,0,1)=="#") $color=substr($color,1,6);

  $tablo[0] = hexdec(substr($color, 0, 2));
  $tablo[1] = hexdec(substr($color, 2, 2));
  $tablo[2] = hexdec(substr($color, 4, 2));
  return $tablo;
}


//Step 1: Calculer la taille maximale du tableau
$string="";
$max=-1;
for ($i=0;$i<=count($recap);$i++){
$tmp=explode("@",$recap[$i]);
if (count($tmp)>$max) $max=count($tmp);
}

$tailleMax=$max-1; //ca commence a 0


//$image = imagecreate(10000,5000);

$image=imagecreate($max*140+20*$max,(90*(count($recap)-1)+60*count($recap)));

//$rgb=html2rgb("505F70");
$rgb=html2rgb("f7fbfe");

$aniseed=imagecolorallocate($image,$rgb[0],$rgb[1],$rgb[2]);
$orange = imagecolorallocate($image, 255, 128, 0);
$rouge=imagecolorallocate($image,255,0,0);
$bleu = imagecolorallocate($image, 0, 0, 255);
$bleuclair = imagecolorallocate($image, 156, 227, 254);

$noir = imagecolorallocate($image, 0, 0, 0);

$blanc = imagecolorallocate($image, 255, 255, 255);

//imagestring($image, 4, 35, 15, "blabla", $blanc);

//ImageLine ($image, 30, 30, 120, 120, $noir);

/*
for ($a=0;$a<count($recap);$a++)
{
$x1=$a*100;
for ($b=0;$b<$tailleMax;$b++)
{
$y1=$b*80;
ImageRectangle ($image, $x1, $y1, $x1+80, $y1+40, $noir);
}
}
*/

$box=(array) null;
$k=0;

$requete="SELECT \"NAME\" from \"DEV_STAGE\" where \"SPECIES\"='Ciona intestinalis' order by \"DEV_PERCENT\" ";
$stmt=pg_query($c,$requete);

while ($row=pg_fetch_array($stmt))
{
$devel[count($devel)]=$row["NAME"];
}

//imagestring($image, 3, 0, 0,$recap[1] , $noir);

for ($a=-1;$a<count($recap)-2;$a++)
{
$tmp=explode("@",$recap[$a+1]);
$len=count($tmp)-1;

$y1=($a+1)*150; //espace vertical entre les cellules
//80//150

//Pour l'affichage du stade de developpement
Imagerectangle($image,0,$y1,140,$y1+60,$noir);
/*if ($a+3==$stade) imagefill ($image, 1, $y1+1, $orange );
else imagefill ($image, 1, $y1+1, $blanc );*/
imagefill ($image, 1, $y1+1, $blanc );
$stadereq=$devel[$a+2];
//imagestring($image, 3, round((19-strlen($stadereq))/2)*8, $y1+20, $stadereq , $noir);



for ($b=0;$b<$len;$b++)
{

$x1=$b*160+150; //espace horizontal entre les cellules //+150 pour le decalage pour mettre le stade de developpement
//100
//ImageRectangle ($image, $x1, $y1, $x1+80, $y1+40, $noir);
ImageRectangle ($image, $x1, $y1, $x1+140, $y1+60, $noir);
//imagestring($image, 4, 35, 15, count($recap), $blanc);
//Calcul des centres
$Cx1=($x1+($x1+140))/2;$Cy1=$y1;
//ImageSetPixel ($image, $Cx1, $Cy1, $rouge);

$Cx2=($x1+($x1+140))/2;$Cy2=$y1+60;
//ImageSetPixel ($image, $Cx2, $Cy2, $rouge);


$name=$tmp[$b];
//imagestring($image, 4, 35, 15*($a+$b), $tmp[$b], $blanc);
$size=4;
/*if (strlen(getFils($name))>15) $size--;
if (strlen(getFils($name))>20) $size--;*/

if ($b==0)
{
$r="SELECT a.\"NAME\",a.\"DEV_STAGE_ID\" from \"DEV_STAGE\" a,\"ANAT_ENTITY\" b where b.\"ANAT_ID\"='".getFils($name)."' and b.\"DEV_STAGE_ID\"=a.\"DEV_STAGE_ID\"";
$stmt=pg_query($c,$r);
$row=pg_fetch_array($stmt);
$stadereq=$row["NAME"];
$cur=$row["DEV_STAGE_ID"];

if ($cur==$stade) imagefill ($image, 1, $y1+1, $orange );

imagestring($image, 3, round((19-strlen($stadereq))/2)*8, $y1+20, $stadereq , $noir);



}

$r="SELECT \"NAME\" from \"ANAT_ENTITY\" where \"ANAT_ID\"='".getFils($name)."'";
$stmt=pg_query($c,$r);
$row=pg_fetch_array($stmt);
$fils=$row["NAME"];



if (strlen($fils)<=17) {

imagestring($image, $size, $x1+round((17-strlen($fils))/2)*8, $y1+20, $fils, $noir);
}
	if (strlen($fils)>34)
	{
	imagestring($image, $size, $x1, $y1, substr($fils,0,17)."-" , $noir);
	imagestring($image, $size, $x1, $y1+15, substr($fils,17,17)."-" , $noir);
	imagestring($image, $size, $x1, $y1+30, substr($fils,34,strlen($fils)) , $noir);
	}

else{
if ((strlen($fils)>17)&&(strlen($fils)<=34)) {
	imagestring($image, $size, $x1, $y1+10, substr($fils,0,17)."-" , $noir);
	imagestring($image, $size, $x1, $y1+10+15, substr($fils,17,strlen($fils)) , $noir);

	}
}
$fils=getFils($name);
$pere=getPere($name);
$box[$k]="$a;$pere;$fils;$x1;$y1;$x2;$y2;$Cx1;$Cy1;$Cx2;$Cy2";
$k++;
}

}


//Nouveau sur le recap2
$box2=(array) null;
$kk=0;

for ($a=0;$a<=count($recap2)-1;$a++)
{
$tmp=explode("@",$recap2[$a+1]);
$len=count($tmp)-1;

for ($b=0;$b<$len;$b++)
{

$name=$tmp[$b];

$fils=getFils($name);
$pere=getPere($name);
//$box2[$kk]="$a;$pere;$fils";
$box2[$kk]="$a;$pere>$fils@";
$kk++;
}

}




$vertex=(array) null;
$v=0;

 for ($a=0;$a<$k-1;$a++)
 {
 $tmp=explode(";",$box[$a]);
 $stade=$tmp[0];
 $pere=$tmp[1];
 $fils=$tmp[2];
 	for ($b=$a;$b<$k-1;$b++)
	{
	if ($a!=$b)
	{
	$tmp2=explode(";",$box[$b]);
	$stade2=$tmp2[0];
	$pere2=$tmp2[1];	
	$fils2=$tmp2[2];
	
	if ($stade2==($stade+1))
	{
	if ($pere2==$fils)
		{
		// on a trouvé une filiation on trace la ligne
		$Cx2=$tmp[9];	//pour le centre bas du pere
		$Cy2=$tmp[10];
		
		$CCx1=$tmp2[7];	//pour le centre haut du fils
		$CCy1=$tmp2[8];

 		
		//arrow($image, $Cx2, $Cy2, $CCx1, $CCy1, 10, 2, $noir);
		arrow($image, $CCx1, $CCy1, $Cx2, $Cy2, 10, 2, $noir);
		//Ajout de l'arete
		$vertex[$v]="$stade2;$pere2>$fils2@";
		$v++;	

		}
	}
	}
	}
 }

//Nouveau pour le recap2

//On parcour le recap2 a la recherche des noeuds non présents dans le recap1


/*imagestring($image, 4, 35, 15, $table, $blanc);
imagestring($image, 4, 35, 30, $table2, $blanc);*/
/*
for ($a=0;$a<count($vertex);$a++)
{
imagestring($image, 4, 35, 30*$a, $vertex[$a], $blanc);
}*/


/*On calcule les aretes manquantes*/
$bool=false;
$inList=(array) null;
for ($a=0;$a<count($box2);$a++)
{

for ($b=0;$b<count($vertex);$b++)
	{
	
	if ($box2[$a]==$vertex[$b]) {$trouve=true;break;}
	
	}
if ($trouve==false)
{
$inList[count($inList)]=$box2[$a];
}
if ($trouve==true) $trouve=false;
}



$arraybox=(array) null;


for ($a=0;$a<count($inList);$a++)
{


$tmp=explode(";",$inList[$a]);
//on recup le stade, le pere
$stade=$tmp[0];
$pere=getPere($tmp[1]);
$fils=substr(getFils($tmp[1]),0,strlen(getFils($tmp[1]))-1);
//Maintenant on cherche dans box qu'elle la boite qui a pour fils le pere au stade -1

for ($b=0;$b<count($box);$b++)
{
$tmpbox=explode(";",$box[$b]);
//on recup le satde et le fils
$stadebox=$tmpbox[0];
$filsbox=$tmpbox[2];


if ($stadebox==($stade-1))
	{
	//imagestring($image, 4, 35, 30*$b, "ici", $blanc);	
	if ($filsbox==$pere)
		{
		//imagestring($image, 4, 35, 30*$b, $b."=".$box[$b], $blanc);	
		//$arraybox[count($arraybox)]=$b;
		$Cx2=$tmpbox[9];	//pour le centre bas du pere
		$Cy2=$tmpbox[10];
		break;
		}
	}

}

for ($b=0;$b<count($box);$b++)
{
$tmpbox=explode(";",$box[$b]);
//on recup le satde et le fils
$stadebox=$tmpbox[0];
$perebox=$tmpbox[1];
$filsbox=$tmpbox[2];

if ($stadebox==($stade))
	{
	//imagestring($image, 4, 35, 30*$b, "ici", $blanc);	
	if ($filsbox==$fils)
		{
		//imagestring($image, 4, 35, 30*$b, $b."=".$box[$b], $blanc);	
		//$arraybox[count($arraybox)]=$b;
		$CCx1=$tmpbox[7];	//pour le centre haut du fils
		$CCy1=$tmpbox[8];
		break;
		}
	}

}

//toujours dans la boucle, on relie les boites
//ImageLine ($image, $Cx2, $Cy2, $CCx1, $CCy1, $noir);
//On fait le test pour enlever les fleches aberrantes ...
//Controle en dessous imagestring si different que 90 pb
//imagestring($image, 4, 35, 50*$a+$b*10, $CCy1-$Cy2, $rouge);	

//Ratio des espaces verticaux entre 2 cellules doit etre egal a 90 suivant la taille et
// l'espacement entre les boites distance entre le centre fils bas et le centre pere bas
if (abs($CCy1-$Cy2)==90) 
{
//arrow($image, $Cx2, $Cy2, $CCx1, $CCy1, 10, 2, $noir);
arrow($image, $CCx1, $CCy1, $Cx2, $Cy2, 10, 2, $noir);
}



}






//ImageRectangle ($image, $x1, $y1, $x2, $y1+40, $noir);

imagepng($image);
?> 