<?php

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

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

	require("§menu.php");

	// recup variable du formulaire
	$ordre = @$_GET['ordre'];
	if ($ordre=="") {$ordre="asc";}
	$nbre = @$_GET['nbre'];
	if ($nbre=="") {$nbre=15;}
		
	//$sTitle = decode_lng_text("STATS_TITLE");
	if ($ordre == "asc") {$sTitle=decode_lng_text("STATS_ARTICLE")." ".$nbre." ".decode_lng_text("STATS_TOP15_YW");}
	if ($ordre == "desc") {$sTitle=decode_lng_text("STATS_ARTICLE")." ".$nbre." ".decode_lng_text("STATS_TOP15_OW");}
	
	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 

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

	//set_time_limit(0);
	
	flush();
	
	//création de la table temporaire pour y stocker toutes les dates
	$table_temp = array();
	
	$id_type_evnt_marr=0;
	$row_evnt = mysql_fetch_array(mysql_query( "select id_type_event from oxg_events_type WHERE code='MARR'"));
	$id_type_evnt_marr = $row_evnt['id_type_event'];

	$result = mysql_query( "select id_event, id_father, info2, id_family from oxg_events where type='$id_type_evnt_marr' $sReqFam");
	while ($row = mysql_fetch_array($result))
	{
		$weddingdate = "";
		$weddingyear = "";
		$birthdate = "";
		$wedding_age = "";
		$sReqFam2 = "and id_family=".$row['id_family'];
		// Recherche date du mariage
		$result_date = mysql_query( "select libelle from oxg_events where type='76' and id_father=$row[id_event] $sReqFam2");
		if($row_date = mysql_fetch_array($result_date))
		{
			$weddingdate = $row_date['libelle'];
			$weddingyear = substr($weddingdate, strlen($weddingdate)-4);
		}
		// Recherche age du marié
		$result_ind = mysql_query( "select sex, id_family, id_individual, date_naissance from oxg_individuals WHERE date_naissance!=\"\" and id_individual=$row[id_father] $sReqCont $sReqFam2");
		if ($row_ind = mysql_fetch_array($result_ind))
		{
			$birthdateTable=preg_split('/ /', $row_ind['date_naissance'], -1);
			$birthdate=$birthdateTable[sizeof($birthdateTable)-1];
			// Remplissage de la table temporaire
			if ($weddingyear>$birthdate)
			{
				$wedding_age = $weddingyear - $birthdate;
				$table_temp[] = array($wedding_age, $row_ind['sex'], $row_ind['id_family'], $row_ind['id_individual'], $weddingdate);
			}
		}
		$birthdate = "";
		$wedding_age = "";			
		// Recherche age du conjoint
		$result_ind = mysql_query( "select sex, id_family, id_individual, date_naissance from oxg_individuals WHERE date_naissance!=\"\" and id_individual=$row[info2] $sReqCont $sReqFam2");
		if ($row_ind = mysql_fetch_array($result_ind))
		{
			$birthdateTable=preg_split('/ /', $row_ind['date_naissance'], -1);
			$birthdate=$birthdateTable[sizeof($birthdateTable)-1];
			// Remplissage de la table temporaire
			if ($weddingyear>$birthdate)
			{
				$wedding_age = $weddingyear - $birthdate;
				$table_temp[] = array($wedding_age, $row_ind['sex'], $row_ind['id_family'], $row_ind['id_individual'], $weddingdate);
			}
		}
	}
	
	// Transformation des lignes en colonnes pour le tri avec array_multisort
	foreach ($table_temp as $key => $row)
	{
		$age_[$key]  = $row[0];
		$sexe_[$key] = $row[1];
		$fam_[$key] = $row[2];
		$id_[$key] = $row[3];
		$date_[$key] = $row[4];
	}
	if ($ordre=="asc") { array_multisort($age_, SORT_ASC, $date_, SORT_ASC, $table_temp);}
	if ($ordre=="desc") { array_multisort($age_, SORT_DESC, $date_, SORT_ASC, $table_temp);}

	
	echo "<BR>";
	//création tableau de sortie
	echo "<table>";
	for ($i=0; $i<$nbre; $i++)
	{
		$age_ind = $table_temp[$i][0];
		$sexe_ind = $table_temp[$i][1];
		$fam_ind = $table_temp[$i][2];
		$id_ind = $table_temp[$i][3];
		$date = $table_temp[$i][4];
		
		if ($fam_ind!="" and $id_ind!="" and $date!="" and $date!="?")
		{
			if ($sexe_ind==1) { $flag="female.png"; }
			if ($sexe_ind==0) { $flag="male.png"; }
			echo "<tr><th align=right>".decode_date($date)."<td><IMG SRC=$flag> ".affiche_lien_individu($fam_ind, $id_ind)." ".decode_lng_text('STATS_OLD_1')." ".$age_ind." ".decode_lng_text('STATS_OLD_2')."</td></tr>";
		}
	}
	echo "</table>";
	echo "<BR>";

	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>Les mariés les plus ";
	if ($ordre=="asc") {echo "<select name=ordre><OPTION value='asc'>"."jeunes"."</OPTION><OPTION value='desc'>"."âgés"."</OPTION></select></td>";}
	if ($ordre=="desc") {echo "<select name=ordre><OPTION value='desc'>"."âgés"."</OPTION><OPTION value='asc'>"."jeunes"."</OPTION></select></td>";}
	echo "<td>".decode_lng_text('STATS_TOP15_DATE_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);
?>