<?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
/*
    $and="";
	if( isset($_POST["and"]) )
		$and=$_POST["and"];
*/
	$region=array();
	if( isset($_POST["regionlist"]) ){
	    $region=explode("@",$_POST["regionlist"]);
	}
	

	if($_POST["supinf"]==0)
		$supinf=">";
	else
		$supinf="<";

	$surf=$_POST["surf"];
	



	if($_POST["supinf2"]==0)
		$supinf2=">";
	else
		$supinf2="<";

	$dist=$_POST["dist"];
	



	$species="";
	if(isset($_POST["species"]))
	    $species=$_POST["species"];
	
	$celllist="";
	if(isset($_POST["celllist"]))
		$celllist=$_POST["celllist"];

	
	$requete="( ";
	for($i=0;$i<count($region);++$i){
	    $anat_id=$region[$i];
		//print "=>>"+$anat_id;
	    $requete.="(SELECT \"CELL_A\", \"CELL_B\", \"CONTACT\", \"DISTANCE\" FROM \"NEIGHBOR\" where ((\"CELL_A\"=$anat_id) or (\"CELL_B\"=$anat_id))and(\"CONTACT\" $supinf= $surf)and(\"DISTANCE\" $supinf2= $dist))";
	    
		if($i<count($region)-1)
			//if($and=="n")
				$requete.=" UNION ";
			/*
			else
				$requete.=" INTERSECT ";
			*/
	}
    $requete.=" )";

//print $requete;

/*
	if( strlen($celllist)>0){
	print $celllist;
		$celllist = substr($celllist, 0, strlen($celllist)-1);
		$celllist = ereg_replace(":","','",$celllist);

		$requete.="intersect select cell_a, cell_b, contact, distance FROM CIONA_DESIGN.neighbor where cell_a in('".$celllist."') or cell_b in('".$celllist."')";
	}
print "$requete";
*/
	
	

	$stmt = pg_query($c,$requete);
	
	
    $tabcelllist=array();
	if( strlen($celllist)>0){
	    print $celllist;
		$celllist = substr($celllist, 0, strlen($celllist)-1);
		$tabcelllist=split(":",$celllist);
	}
	
	
	
	$count=0;
	$celllist="";

	while( $row=pg_fetch_array($stmt) ){
	    
	    $cell1=$row["CELL_A"];
		$cell2=$row["CELL_B"];
        if(in_array($cell1,$region)){
		    $cell1=$row["CELL_B"];
			$cell2=$row["CELL_A"];
		}

        if((count($tabcelllist)>0)and(! in_array($cell1,$tabcelllist)))
		    continue;

        $requete="SELECT \"NAME\", \"DEV_STAGE_ID\" FROM \"ANAT_ENTITY\" where \"ANAT_ID\"='".$cell1."'";
		$stmt2 = pg_query($c,$requete);
		if( $row2=pg_fetch_array($stmt2)){
		    ++$count;
	print "ANATOMICAL_ENTITY\t".$row2["NAME"]."\t";
         
		}
		
        $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\"='".$cell2."' and a.\"DEV_STAGE_ID\"=b.\"DEV_STAGE_ID\"";
		$stmt2 = pg_query($c,$requete);
		if( $row2=pg_fetch_array($stmt2) ){
		print "NEIGHBOUR\t".$row2["NAME"]."\tDISTANCE\t".$row["DISTANCE"]."\tCONTACT\t".$row["CONTACT"]."\tDEV_STAGE\t".$row2["DEV"]."\tSPECIES\t".$row2["SPECIES"]."\n";
			
		}
		
		
		
		if($count>500){
			//OCICancel($stmt);
			pg_cancel_query($c);
			break;
		}
	    $celllist.= $cell1.":";  //liste les cell id pour le rafinage.
	}
	

	if($count==0)
		print "NO_MATCH_FOUND\n//\n";
	

	
	
	



?>