<?php

	$sPage="recherche";
	$sTitle="";

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

	$sTitle = decode_lng_text("STATS_ARTICLE")." $nbre ".decode_lng_text("STATS_TOP15_LML");
	require("§menu.php");

	// Form data
	$nbre = @$_GET['nbre'];
	if($nbre=="")
	{
		$nbre=25;
	}

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -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();

	$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'];
	}	

	echo "<table border=0>";

	$sql_query="select id_individual, oxg_individuals.id_family, first_name, id_patronym, date_naissance, date_deces, sex, sosa ";
	$sql_query.=", DATE_FORMAT(adddate(e_birth.devnt,datediff(CURDATE(),e_death.devnt)),'%Y-%m-%d') AS php_date_naissance ";
	$sql_query.=", DATE_FORMAT(adddate(e_death.devnt,datediff(CURDATE(),e_death.devnt)),'%Y-%m-%d') AS php_date_deces ";
	$sql_query.="from oxg_individuals, oxg_events2 e_birth, oxg_events2 e_death ";
	$sql_query.="where oxg_individuals.id_individual=e_birth.id_individu ";
	$sql_query.="and oxg_individuals.id_individual=e_death.id_individu ";
	$sql_query.="and oxg_individuals.id_family=e_birth.id_family ";
	$sql_query.="and oxg_individuals.id_family=e_death.id_family ";
	$sql_query.="and e_birth.devnt is not null and date_naissance<>\"\" ";
	$sql_query.="and e_death.devnt is not null and date_deces<>\"\" ";
	$sql_query.="$sReqCont $sReqFam ";
	$sql_query.="and e_birth.type=$id_type_evnt_birth ";
	$sql_query.="and e_death.type=$id_type_evnt_death ";
	$sql_query.="ORDER by datediff(e_death.devnt,e_birth.devnt) desc ";
	$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)
		{
			$age=decode_lng_text("SOSA_DEATH_F")." ".decode_lng_text("STATS_OLD_1");
			$flag="female.png";
		}
		else if ($row_ind['sex']==0)
		{
			$age=decode_lng_text("SOSA_DEATH_M")." ".decode_lng_text("STATS_OLD_1");
			$flag="male.png";
		}

		echo "  <IMG SRC=$flag> ".affiche_lien_individu($row_ind['id_family'], $row_ind['id_individual'], true, true);

		// Display age
		echo " (".$age." ".dateDiff(strtotime($row_ind["php_date_deces"]) , 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);
	@mysql_close($dbh);
?>