<?php

	$sPage="recherche";
	$sTitle="";
	$sLimitAge="110";	// Age limit displayed

	require("admin/§lib_inc.php");
	require("admin/§lib_inc2.php");

	$sTitle = decode_lng_text("STATS_ARTICLE")." $nbre ".decode_lng_text("STATS_TOP15_OLP");

	require("§menu.php");

	// recup variable du formulaire
	$nbre = @$_GET['nbre'];
	if ($nbre=="") {$nbre=25;}

	$INDIVIDU=isset($_REQUEST["INDIVIDU"])?$_REQUEST["INDIVIDU"] : 0; 
	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	$LANG=isset($_REQUEST["LANG"])?$_REQUEST["LANG"] : -1; 

	if($FAMILY!=-1)
	{
		$sReqFam="and oxg_individuals.id_family=$FAMILY";
	}
	else
	{
		$sReqFam="";
	}
	if($bSessionOk==false)
	{
		$sReqCont="and contemporary=1";
	}
	else
	{
		$sReqCont="";
	}

	//set_time_limit(0);
	
	flush();

	echo "<table border=0>";
	$date = time();
	$annee = date("Y",$date);
	
	$row_evnt = mysql_fetch_array(mysql_query( "select id_type_event from oxg_events_type WHERE code='BIRT'"));
	if($row_evnt)
	{
		$id_type_evnt_birth = $row_evnt['id_type_event'];
	}	

	$row_evnt = mysql_fetch_array(mysql_query( "select id_type_event from oxg_events_type WHERE code='DEAT'"));
	if($row_evnt)
	{
		$id_type_evnt_death = $row_evnt['id_type_event'];
	}	

	$sql_query="select id_individual, oxg_individuals.id_family, first_name, id_patronym, date_naissance, date_deces, sex, sosa ";
	$sql_query.=", DATE_FORMAT(e_birth.devnt,'%Y-%m-%d') AS php_date_naissance ";
	$sql_query.=", DATE_FORMAT(CURDATE(),'%Y-%m-%d') AS php_date_jour ";
	$sql_query.="from oxg_individuals, oxg_events2 e_birth ";
	$sql_query.="where oxg_individuals.id_individual=e_birth.id_individu ";
	$sql_query.="and oxg_individuals.id_family=e_birth.id_family ";
	$sql_query.="and e_birth.devnt is not null and date_naissance<>\"\" and date_deces=\"\" ";
	$sql_query.="$sReqCont $sReqFam ";
	$sql_query.="and e_birth.type=$id_type_evnt_birth ";
	$sql_query.="and TIMESTAMPDIFF(YEAR,e_birth.devnt,CURDATE())<$sLimitAge ";	
	$sql_query.="ORDER by e_birth.devnt asc ";
	$sql_query.="LIMIT $nbre";

	$result_ind = mysql_query($sql_query) or mysql_die();
	while($row_ind = mysql_fetch_array($result_ind))
	{
		// on recupere le patronyme
		$id_patronym = $row_ind['id_patronym'];
		$req_nom = "SELECT libelle FROM oxg_patronyms WHERE id_patronym='$id_patronym'";
		$result_nom = mysql_query($req_nom);
		$row_nom = mysql_fetch_array($result_nom);

		echo "<tr><td>";

		if ($row_ind['sex']==1) { $article=decode_lng_text("SOSA_BIRTH_F"); $age=decode_lng_text("SOSA_AGED_F"); $flag="female.png"; }
		if ($row_ind['sex']==0) { $article=decode_lng_text("SOSA_BIRTH_M"); $age=decode_lng_text("SOSA_AGED_M"); $flag="male.png"; }
		$birthdate = $row_ind["date_naissance"];
		if (substr($row_ind["date_naissance"],0,6)=="autour")
		{
			$article = $article." ".decode_lng_text('STATS_ARTICLE_DATE3');
			$birthdate = substr($row_ind["date_naissance"],10-strlen($row_ind["date_naissance"]));
		}
		else
		{
			if ( $row_ind["date_naissance"]==substr($row_ind["date_naissance"],0,4) )
			{
				$article = $article." ".decode_lng_text('STATS_ARTICLE_DATE2');
			}
			else
			{
				$article = $article." ".decode_lng_text('STATS_ARTICLE_DATE1');
			}
		}

		echo "  <IMG SRC=$flag> ";
		echo "<a href=\"§detail_individu.php?INDIVIDU=$row_ind[id_individual]&FAMILY=$row_ind[id_family]\"> $row_ind[first_name] $row_nom[libelle]</a>";
		echo "<i> ".$article." ".$birthdate;
		if($bSosa==true)
		{
			$sSosa=$row_ind["sosa"];
			if($sSosa!="-1")
			{
				echo " <font color=red>".$sSosa."</font>";
			}
		}
		echo "</i>";
		echo " (".$age." ".dateDiff(strtotime($row_ind["php_date_jour"]) , strtotime($row_ind["php_date_naissance"])).")";
		echo "</td></tr>\n";
	}

	flush();
	
	echo "</table>";

	echo "<div id=panel>";
	// tableau du filtrage
	echo "<br/>";
	echo "<br/>";
	echo "<form action=\"\" method=get>";
	echo "<input type=hidden name=FAMILY value=\"$FAMILY\">";
	echo "<center>";
	echo "<table border=1><tr>";
	echo "<td>".decode_lng_text('STATS_TOP15_P_L')." ";
	echo "<input type=text name=nbre value=\"$nbre\" MAXLENGTH=3 SIZE=3></td>\n<td>";
	echo "<td><input type=submit value=".decode_lng_text('PRIVATE_ENTER')."></td></tr></table></form>";
	echo "</center>";
	echo "</div>";
	
	affiche_pied_page(true);
?>