<?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
	if($_POST["mvol"]==0)
		$mvol=">";
	else
		$mvol="<";
	$vol=$_POST["vol"];

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




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

	if($_POST["mcyl"]==0)
		$mcyl=">";
	else
		$mcyl="<";
	$cyl=$_POST["cyl"];
	
	if($_POST["melong"]==0)
		$melong=">";
	else
		$melong="<";
	$elong=$_POST["elong"];
	
	if($_POST["mflat"]==0)
		$mflat=">";
	else
		$mflat="<";
	$flat=$_POST["flat"];
	
	if($_POST["mcomp"]==0)
		$mcomp=">";
	else
		$mcomp="<";
	$comp=$_POST["comp"];
	
	if($_POST["mocup"]==0)
		$mocup=">";
	else
		$mocup="<";
	$ocup=$_POST["ocup"];
	
	if($_POST["ment"]==0)
		$ment=">";
	else
		$ment="<";
	$ent=$_POST["ent"];
	
	if($_POST["msv"]==0)
		$msv=">";
	else
		$msv="<";
	$sv=$_POST["sv"];
	
	
	
	
	
	
	
	$devstage=array();
	if(isset($_POST["devstagelist"]))
	    //$devstage=$_POST["devstage"];
		$devstage=explode("@",$_POST["devstagelist"]);
		
		
	
	$species="";
	if(isset($_POST["species"]))
	    $species=$_POST["species"];
	
	$celllist="";
	if(isset($_POST["celllist"]))
		$celllist=$_POST["celllist"];
	
		
	
	//search for cells		
	$requete="SELECT distinct a.\"CELL\", a.\"VOLUME\", a.\"SURFACE\", d.\"SPHERICITY\", d.\"CYLINDRICITY\", d.\"ELONGATION\", d.\"FLATNESS\", d.\"COMPLEXITY\", d.\"OCCUPENCY\", d.\"ENTROPY\", d.\"SV\", b.\"NAME\", b.\"SPECIES\", b.\"DEV_STAGE_ID\", c.\"NAME\" as \"DEV\" FROM \"BIOMETRY\" a, \"ANAT_ENTITY\" b, \"DEV_STAGE\" c, \"BIOMETRY_SHAPE\" d WHERE (a.\"CELL\"=b.\"ANAT_ID\") and (b.\"DEV_STAGE_ID\"=c.\"DEV_STAGE_ID\") and (d.\"CELL\"=b.\"ANAT_ID\")";
		

	if( $species>0 )
		$requete.=" AND (c.\"SPECIES\" ='$species') ";
	
	if( count($devstage)>0 ){
		if (strlen($devstage[0])>0)
	    {$devstage=join("','",$devstage);
		$requete.=" AND (b.\"DEV_STAGE_ID\" in ('".$devstage."')) ";}
	}
	
	if( $vol>0 )
		$requete.=" AND (a.\"VOLUME\"".$mvol.$vol.") ";
		
	if( $surf>0 )
		$requete.=" AND (a.\"SURFACE\"".$msurf.$surf.") ";
	
	
	
	if( $sphe>0 )
		$requete.=" AND (d.\"SPHERICITY\"".$msphe.$sphe.") ";
	
	if( $cyl>0 )
		$requete.=" AND (d.\"CYLINDRICITY\"".$mcyl.$cyl.") ";
		
	if( $elong>0 )
		$requete.=" AND (d.\"ELONGATION\"".$melong.$elong.") ";
		
	if( $flat>0 )
		$requete.=" AND (d.\"FLATNESS\"".$mflat.$flat.") ";
		
	if( $comp>0 )
		$requete.=" AND (d.\"COMPLEXITY\"".$mcomp.$comp.") ";
		
	if( $ocup>0 )
		$requete.=" AND (d.\"OCCUPENCY\"".$mocup.$ocup.") ";
		
	if( $ent>0 )
		$requete.=" AND (d.\"ENTROPY\"".$ment.$ent.") ";
		
	if( $sv>0 )
		$requete.=" AND (d.\"SV\"".$msv.$sv.") ";
		
	
	if( strlen($celllist)>0){
		$celllist = substr($celllist, 0, strlen($celllist)-1);
		$celllist = ereg_replace(":","','",$celllist);

		$requete.=" AND (a.\"CELL\" in('".$celllist."'))";
	}
	
	$requete.="order by b.\"DEV_STAGE_ID\"";
//print "$requete";
		
	$stmt = pg_query($c,$requete);



	
	$count=0;
	$celllist="";
	while($row=pg_fetch_array($stmt) ){
		
		++$count;
		
		print "ANATOMICAL_ENTITY\t".$row["NAME"]."\n";
		print "DEV_STAGE\t".$row["DEV"]."\n";
		print "VOLUME\t".$row["VOLUME"]."\n";
		print "SURFACE\t".$row["SURFACE"]."\n";
		print "SPHERICITY\t".$row["SPHERICITY"]."\n";
		print "CYLINDRICITY\t".$row["CYLINDRICITY"]."\n";
		print "ELONGATION\t".$row["ELONGATION"]."\n";
		print "FLATNESS\t".$row["FLATNESS"]."\n";
		print "COMPLEXITY\t".$row["COMPLEXITY"]."\n";
		print "OCCUPENCY\t".$row["OCCUPENCY"]."\n";
		print "ENTROPY\t".$row["ENTROPY"]."\n";
		print "SV\t".$row["SV"]."\n";
		print "SPECIES\t".$row["SPECIES"]."\n";
		print "//\n";
		
		
		if($count>500){
			//OCICancel($stmt);
			//pg_cancel_query($c);
			break;
		}
		
	    $celllist.= $row["CELL"].":";  //liste les cell id pour le rafinage.
		
	}
	
	
	if($count==0)
		print "NO_MATCH_FOUND\n//\n";
	
	
	

?>