<?php
/*
    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/>.

*/
header('Content-Type: text/plain');
include("connection.php");
?>
<?php
    $region="";
	$dev="";
    if(isset($_GET["region"]))
	    $region = $_GET["region"];
    if(isset($_GET["dev"]))
		$dev = $_GET["dev"];
	
	
	if( (strlen($dev)<=0)or(ereg("___",$dev)) ){
	    print "NO_MATCH_FOUND\n";
	    print "//\n";
	    return;
	}
	
	

	

	$requete= "SELECT \"NAME\", \"SPECIES\" FROM \"DEV_STAGE\" WHERE \"DEV_STAGE_ID\"= $dev";
    $stmt = pg_query($c,$requete);
    if( $row=pg_fetch_array($stmt)){
		print "SPECIES\t".$row["SPECIES"]."\n";
		print "DEV_STAGE\t".$row["NAME"]."\n";
		
print "TREE\n";
$nivo=0;
function select_recursif_descendant_2($conn,$start,$res,$nivo)
{
global $dev;
//global $level;
global $region;
//global $nivo;
/*ON ECRIT SA REQUETE*/

//print "la";
if ($start!='')
{
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$dev." and  \"CHILD_OF\"= $start order by \"NAME\"";
//print $start;
$level++;
}
else {
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"DEV_STAGE_ID\"= ".$dev." and  \"CHILD_OF\" is null order by \"NAME\"";
//$level=0;
}

$stmt2=pg_query($conn,$requete2);

//if (pg_num_rows($stmt2)==0) {$level--;}

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

while($row2=pg_fetch_array($stmt2))
	{
//$res=select_recursif_descendant_2($conn,$row2["ANAT_ID"],$res);
	
			for($a=0;$a<$nivo-1;++$a){
				if( $nivo>1 )
					print "|";
					
			}
	
			
			if( $row2["TYPE"]=='cell' ){
				print "CELL\t";
				//print "CELLULE";
				
				
				}
			else if( $row2["TYPE"]=='organism' ){
				print "ORGANISM\t";
				//print "ORGANISME";
				}
			else	{
				print "REGION\t";
				//print "REGION";
				
				}
			
			
			
				print "NAME\t".$row2["NAME"]."\tID\t".$row2["ANAT_ID"];
				
			

		print "\n";


	
	$res=select_recursif_descendant_2($conn,$row2["ANAT_ID"],$res,$nivo);

	
	}

return $res;
}

$res=select_recursif_descendant_2($c,null,$res,$nivo);



/* FIN DE LA SIMULATION */

}else{ print "NO_MATCH_FOUND\n//\n"; }
?>