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

*/
	//connection
	include("connection.php");
//Title before frames_header
print '<title>ANISEED v3.0 - Lineage result</title>';

    include("frames_header.php");
?>

<center>
<?php
    $and="";
	if( isset($_POST["and"]) )
		$and=$_POST["and"];

	$anc="";
	if( isset($_POST["ance"]) )
	    $anc=$_POST["ance"];
	
	$desc="";
	if( isset($_POST["desc"]) )
	    $desc=$_POST["desc"];

	if(  (strlen($anc)<=0) and (strlen($desc)<=0)  ){
	    print '<center><h2><strong>Please check either ancestor, descendant or both.</strong></h2></center>';
		include("frames_footer.php");
		return;
	}


	$region=array();
	if( isset($_POST["region"]) ){
	    $region=$_POST["region"];
	}
	
	if(  count($region)<=0  ){
	    print '<center><h2><strong>Please select anatomical parts.</strong></h2></center>';
		include("frames_footer.php");
		return;
	}
	
	

	$species="";
	if(isset($_POST["species"]))
	    $species=$_POST["species"];
	
	$celllist="";
	if(isset($_POST["celllist"]))
		$celllist=$_POST["celllist"];
	
	if(strlen($celllist)>0)
		print "<center><H2><font color=\"#FF6600\">Refining subset mode</font></H2></center>";
	
	
	$requete="SELECT \"DAUGHTER_CELL\" FROM \"LINEAGE\", \"ANAT_ENTITY\", \"DEV_STAGE\" where \"ANAT_ENTITY\".\"DEV_STAGE_ID\"=\"DEV_STAGE\".\"DEV_STAGE_ID\" AND \"DAUGHTER_CELL\"=\"ANAT_ID\" AND \"DAUGHTER_CELL\" IN (";


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

/* VARIANTE DANS LA REQUETE POUR LA CONNEXION ET LE RESULTAT DU SELECT */

$IN="";
function select_recursif($conn,$start,$res)
{
global $IN;


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

$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	//$IN.=$row["DAUGHTER_CELL"].",";
	$IN.="(".$requeteIN.") UNION ";
	$res=select_recursif($conn,"'".$row["MOTHER_CELL"]."'",$res);
	}
return $res;
}



$IN2="";
function select_recursif_descendant2($conn,$start,$res)
{
global $IN2;

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

$stmt=pg_query($conn,$requete);
while ($row=pg_fetch_array($stmt))
	{
	//$IN2.=$row["DAUGHTER_CELL"].",";
	$IN2.="(".$requeteIN2.") UNION ";
	$res=select_recursif_descendant2($conn,"'".$row["DAUGHTER_CELL"]."'",$res);
	}
return $res;
}


/* FIN DE LA SIMULATION */



	for($i=0;$i<count($region);++$i){
	    $anat_id=$region[$i];

	//print "=======>".$anat_id."<br>";

	    $requete.="( ";
	    if(strlen($anc)>0){
			/*ICI*/
		$IN="";
		$res=select_recursif($c,$anat_id,$res);
		//$IN=substr($IN,0,strlen($IN)-1);



		    /*$requete.="SELECT daughter_cell FROM CIONA_DESIGN.lineage  CONNECT BY daughter_cell = PRIOR mother_cell START WITH daughter_cell='$anat_id'";*/
			$requete.=$IN;


			if(strlen($desc)>0){
			    //$requete.=" UNION ";
			}
		}
		if(strlen($desc)>0){
			/*ICI AUSSI*/

		$IN2="";
		$res=select_recursif_descendant2($c,$anat_id,$res);
		


			/*$requete.="SELECT daughter_cell FROM CIONA_DESIGN.lineage  CONNECT BY mother_cell = PRIOR daughter_cell START WITH mother_cell='$anat_id'";*/

		$requete.=$IN2;


		}
//Rajout
//Pk ? Pcq: Dans le cas ou IN et IN2 sont vides, Postgres ne peut pas faire des requetes avec des IN () 
//la requete plante et une erreur arrive
// (du fait egalement qu'on fait un substr pour avoir une requete niquel, et qu'on mange le IN
if ((strlen($IN)==0) && (strlen($IN2)==0))
	{
	print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
	include ("frames_footer.php");
	return;
	}


		$requete=substr($requete,0,strlen($requete)-6);
		
		$requete.=" )";
		
		if($i<count($region)-1)
			if($and=="n")
				$requete.=" UNION ";
			else
				$requete.=" INTERSECT ";
	}
	$requete.="  )";
	
		
	if( strlen($celllist)>0){
		$celllist = substr($celllist, 0, strlen($celllist)-1);
		$celllist = ereg_replace(":","','",$celllist);

		$requete.=" AND (\"DAUGHTER_CELL\" in ('".$celllist."'))";
	}
	
	//$requete.=" order by \"DEV_STAGE_ID\"";
	$requete.=" order by \"DEV_PERCENT\"";	
	
	$stmt = pg_query($c,$requete);


	print "<font color=\"#D64242\" size=\"3\"><b>Matching records</b></font><br><br>";
	print '<table width="99%" border="0" cellspacing="0" cellpadding="0">';
	print '<tr align="center" valign="middle"><td><font color="#456898" size="2"><b>Anatomical entity</b></font></td><td><font color="#456898" size="2"><b>Developmental stage</b></font></td><td><font color="#456898" size="2"><b>Species</b></font></td></tr>';
	$count=0;
	$celllist="";
$doublon=(array) null;//moche mais evite un bug Oracle
$nn=0;
	while( $row=pg_fetch_array($stmt) ){
		if(in_array($row["DAUGHTER_CELL"], $doublon))
			continue;
		$doublon[$nn++]=$row["DAUGHTER_CELL"];

        $requete="SELECT a.\"SPECIES\", 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($c,$requete);
		if( $row2=pg_fetch_array($stmt2) ){
		
			++$count;
			print '<tr align="center" valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
			print '<tr align="center" valign="top"> <td><a href="short_hierarchy.php?region='.$row["DAUGHTER_CELL"].'&dev='.$row2["DEV_STAGE_ID"].'"><font color="#000000" size="-1">'.$row2["NAME"].'</font></a></td> <td><font color="#656565" size="-1">'.$row2["DEV"].'</font></td> <td><font color="#656565" size="-1">'.$row2["SPECIES"].'</font></td> </tr>';
		
		}
		
		
		
		if($count>500){
			//OCICancel($stmt);
			pg_cancel_query($c);
			break;
		}
	    $celllist.= $row["DAUGHTER_CELL"].":";  //liste les cell id pour le rafinage.
	}
	print '<tr valign="top"><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td><td><hr size="1"></td></tr>';
	print '</table>';
	
	if($count==0)
		print "<br><br><center><h2><strong>No match</strong></h2></center><br><br>";
	else
		print "<br><center>$count result(s)</center><br><br>";

	
	if($count>500)
		print "...<br>More than 500 results has been returned.<br>Please refine your query";
	
	



	if(substr_count($celllist,":")<1000){
		print '<form name="formulaire3" action="" method="post">';
		print '<input type="hidden" value="'.$celllist.'" name="celllist">';
		print'&nbsp;&nbsp;&nbsp;<input type="image" src="pictures/button-subset.jpg" onClick="javascript: window.document.formulaire3.action= window.document.formulaire3.redirect.value; submit;">';
		
		/*print '<br>
			   <select name="redirect" size="1">
				   <option value="biometry-search.php">Biometry</option>
				   <option value="neighbour-search.php">Neighbours</option>
				   <option value="fate-search.php">Fate</option>
				   <option value="lineage-search.php">Lineage</option>
				   <option value="division-search.php">Cleavage inequality</option>
				   <option value="insitu-search.php">In situ</option>
				   <option value="ID.php">Export ID list</option>
			   </select>';*/
		include ("refine_anatomy.php");
			   
		print '</form>';
    }else{print '<font size="-1" color="#999999">Refine mode not available for set > 1000</font>';}

?>
</center>

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