<?php

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

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

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

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

	if ($date=='b') {$sTitle=decode_lng_text("STATS_ARTICLE")." $nbre ".decode_lng_text("STATS_TOP15_B"); $date_="date_naissance";}
	if ($date=='d') {$sTitle=decode_lng_text("STATS_ARTICLE")." $nbre ".decode_lng_text("STATS_TOP15_D"); $date_="date_deces";}
	if ($date=='w') {$sTitle=decode_lng_text("STATS_ARTICLE")." $nbre ".decode_lng_text("STATS_TOP15_M");}

	require("§menu.php");

	$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();

	echo "<table border=0>";

	if ($date=='w')
	{
		$id_type_evnt_plac="";
		$row_evnt = mysql_fetch_array(mysql_query( "select id_type_event from oxg_events_type WHERE code='DATE'"));
		if($row_evnt) { $id_type_evnt_date = $row_evnt['id_type_event']; }
		$cpt=0;
		$result_t = mysql_query( "select id_family, devnt, id_individu, id_event from oxg_events2 where type=115 and left(devnt,4)>1 $sReqFam ORDER by devnt DESC");
		while ($row_t = mysql_fetch_array($result_t))
		{
			if($cpt<$nbre)
			{
				$fam = $row_t['id_family'];
				$id_ind = $row_t['id_individu'];
				$fam_ind = $row_t['id_family'];
				$result_ind = mysql_query("select contemporary from oxg_individuals where id_individual='$id_ind' $sReqFam");
				$row_ind = mysql_fetch_array($result_ind);
				$result_comp = mysql_query("select contemporary, info2 from oxg_individuals, oxg_events where oxg_events.id_event=$row_t[id_event] and oxg_individuals.id_individual=oxg_events.info2 and oxg_events.id_family=$fam and oxg_individuals.id_family=$fam");
				$row_comp = mysql_fetch_array($result_comp);
				$test = $row_ind['contemporary'] + $row_comp['contemporary'];
				if(isset($_SESSION['privilege']))
				{
					$test = 1;
				}
				if ($test > 0)
				{
					$date1 = $row_t['devnt'];
					$date1 = str_replace("-0", "-", $date1);
					$date1 = str_replace("-", "/", $date1);
					$date2 = $row_t['sDtEvnt'];
					$weddingdate = $date1.":".$date2;
					echo "<tr><th align=right>".decode_date($weddingdate)."</th>";
					$acte=decode_lng_text("GEDTAG_BTW")." ";
					echo "<td>".$acte;
					$compagne=" ".decode_lng_text("GEDTAG_AND")." ".affiche_lien_individu($FAMILY, $row_comp['info2'], true, false);
					echo "".affiche_lien_individu($FAMILY, $id_ind).$compagne;	
					echo "</td></tr>";
					$cpt=$cpt+1;
				}
			}
		}
	}
	else  //if $date=='b' or $date=='d'
	{
		$result_ind = mysql_query( $sql_query="select * from oxg_individuals where $date_!='' $sReqFam $sReqCont ORDER by right($date_,4) DESC LIMIT $nbre" ) 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 ($date=='b')
			{
				if ($row_ind['sex']==1) { $article=decode_lng_text("SOSA_BIRTH_F"); $flag="female.png"; }
				if ($row_ind['sex']==0) { $article=decode_lng_text("SOSA_BIRTH_M"); $flag="male.png"; }
			}
			if ($date=='d')
			{
				$old = "";
				if ($row_ind['sex']==1) { $article=decode_lng_text("SOSA_DEATH_F"); $flag="female.png"; }
				if ($row_ind['sex']==0) { $article=decode_lng_text("SOSA_DEATH_M"); $flag="male.png"; }
				if ($row_ind['date_naissance']!='')
				{
				$old = (substr($row_ind['date_deces'],-4))-(substr($row_ind['date_naissance'],-4));
				$old = " ".decode_lng_text("STATS_OLD_1")." $old ".decode_lng_text("STATS_OLD_2");
				}
			}
			echo "  <IMG SRC=$flag> <i> ";

			if ( $row_ind[$date_]==substr($row_ind[$date_],0,4) )
			{
				$article .= " ".decode_lng_text('STATS_ARTICLE_DATE2');
			}
			else
			{
				$article .= " ".decode_lng_text('STATS_ARTICLE_DATE1');
			}
			$id_ind = $row_ind['id_individual'];
			$fam_ind = $row_ind['id_family'];
			$result = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual=$id_ind $sReqCont $sReqFam" ) or mysql_die();
			if($row = mysql_fetch_array($result))
			{	
				echo affiche_lien_individu($fam_ind, $id_ind);
				echo " ".$article." $row_ind[$date_]</i>";
			}
			else
			{
			display_lng_text('PRIVATE_MSG');	
			}
			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_DATE_C')." ";
	if ($date=='b') {echo "<select name=date><OPTION value='b'>".decode_lng_text('GEDTAG_BIRT')."</OPTION><OPTION value='d'>".decode_lng_text('GEDTAG_DEAT')."</OPTION><OPTION value='w'>".decode_lng_text('GEDTAG_MARR')."</OPTION></select></td>";}
	if ($date=='d') {echo "<select name=date><OPTION value='d'>".decode_lng_text('GEDTAG_DEAT')."</OPTION><OPTION value='b'>".decode_lng_text('GEDTAG_BIRT')."</OPTION><OPTION value='w'>".decode_lng_text('GEDTAG_MARR')."</OPTION></select></td>";}
	if ($date=='w') {echo "<select name=date><OPTION value='w'>".decode_lng_text('GEDTAG_MARR')."</OPTION><OPTION value='b'>".decode_lng_text('GEDTAG_BIRT')."</OPTION><OPTION value='d'>".decode_lng_text('GEDTAG_DEAT')."</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);

?>
