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


<?php

	if (!$handle = gzopen("ontologies.gz", "wb9")) {
	   echo "Impossible d'ouvrir le fichier";
	   exit;
	}
	
	
$species="";
$requete= "select \"NAME\" from \"SPECIES\"";
$stmt55 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while($row55=pg_fetch_array($stmt55)){
	$species=$row55["NAME"];
	
	

	gzwrite($handle,"##$species anatomical ontologies\n");
	
	$count=0;
    $requete= "select \"DEV_STAGE_ID\", \"NAME\", \"SPECIES\" from \"DEV_STAGE\" where \"SPECIES\" = '$species' order by \"DEV_PERCENT\"";
	$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while($row=pg_fetch_array($stmt)){
	    ++$count;
		$dev=$row["DEV_STAGE_ID"];
		if (gzwrite($handle,"\n\n#".$row["SPECIES"]."   ".$row["NAME"]."\n") === FALSE) {
			echo "Error: Impossible d'�rire dans le fichier!";
			exit;
		}
		

		/*Requete hierarchique*/
	/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */

/* VARIANTE DANS LA REQUETE POUR LA CONNECTION ET LE RESULTAT DU SELECT */
/* VARIANTE SUR LE PASSAGE EN PARAMETRE DU NIVEAU COURANT DE RECURSIVITE POUR L'AFFICHAGE DES BARRES VERTICALES */


$nivo=0;


function select_recursif_descendant_2($conn,$start,$res,$nivo)
{
global $dev;

/*ON ECRIT SA REQUETE*/

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

$stmt2=pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));


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

while($row2=pg_fetch_array($stmt2))
	{

			for($a=0;$a<$nivo-1;++$a){
				if( $nivo>1 )
					if (gzwrite($handle,'  |  ') === FALSE) {
						echo "Error: Impossible d'�rire dans le fichier!";
						exit;
				    }
			}
			if (gzwrite($handle,$row2["NAME"]."\n") === FALSE) {
				echo "Error: Impossible d'�rire dans le fichier!";
				exit;
			}
		

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

	
	}

return $res;
}

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



	}
}	
	
	gzclose($handle);
	print "Ok. $count ontologies written on ontologies.gz";
?> 


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