<?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="";
	$fates=array();

    if(isset($_GET["region"]))
		$region = $_GET["region"];
	if(isset($_GET["dev"]))
		$dev = $_GET["dev"];
	if(isset($_GET["fate"]))
		$fates=explode("@",$_GET["fate"]);
	
	
	if( (strlen($dev)<=0)or(ereg("___",$dev)) ){
	    print "NO_MATCH_FOUND\n//\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";
		
$nivo=0;

function select_recursif_descendant_2($conn,$start,$res,$nivo)
{
global $fates;
global $region;
global $dev;
/*ON ECRIT SA REQUETE*/


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


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


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

while($row=pg_fetch_array($stmt))
	{
	
					
	        
			for($a=0;$a<$nivo-1;++$a){
				if( $nivo>1 )
					print '|';
			}
			
			if( $row["TYPE"]=='cell' )
				print "CELL\t";
			else if( $row["TYPE"]=='organism' )
				print "ORGANISM\t";
			else
				print "REGION\t";
			
		
			print "NAME\t".$row["NAME"]."\tID\t".$row["ANAT_ID"]."\n";

			
			    //recup des fates deja definis
				$requete2= "SELECT \"KEYWORDS\" FROM \"FATE\" WHERE \"ANAT_ID\"= ".$row["ANAT_ID"];
				$stmt2 = pg_query($conn,$requete2);
				$fkw = (array) null;
				$a=0;
				while($row2=pg_fetch_array($stmt2)){
				    $fkw[$a++]=$row2["KEYWORDS"];
				}
			
			
			
			//generation des checkbox pour les differents fates
            for($a=0;$a<count($fates);++$a){
			    //print "<td align=\"center\"><input name=\"f[]\" type=\"checkbox\" value=\"".$fates[$a]."--".OCIResult($stmt, "ANAT_ID")."\"";
				if( in_array($fates[$a], $fkw) )
					{
					for($b=0;$b<$nivo-1;++$b){
				if( $nivo>1 )
					print '|';
			}
				    print "FATE\t".$fates[$a]."\n";
					}
				
			}
		    
		
		

	




	$res=select_recursif_descendant_2($conn,$row["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"; }
?>