<?php

	$sPage="index";
	$sTitle="";

	require("admin/§lib_inc.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="";
	}

	require("§menu.php");	

	// Vérification du nombre de famille si aucune n'est sélectionnée
	if($FAMILY==-1)
	{
		$sql_query = "select count(*) as somme from oxg_family";
		$result = mysql_query($sql_query) or mysql_die();
		$row = mysql_fetch_array($result);
		$nb_fam = $row['somme'];
		if ($nb_fam==1)
		{
			$sql_query = "select id_family from oxg_family";
			$result = mysql_query($sql_query) or mysql_die();
			$row = mysql_fetch_array($result);
			$FAMILY=$row['id_family'];
		}
	}

	// Affichage du titre si 1 famille est sélectionnée
	if($sReqFam!="" || $sTitreGeneral=="")
	{
		$sql_query = "select name libelle from oxg_family where 1=1 $sReqFam";
		$resFam = mysql_query($sql_query) or mysql_die();
		$sTitle = mysql_fetch_array($resFam);
		$sTitle = decode_lng_text("INDEX_TITLE")." $sTitle[libelle]";
	}
	else
	{
		$sTitle=$sTitreGeneral;
	}

	// Calcul de la zone la plus présente
	$sql_query = "select id_country, id_zone, count(*) as somme "
					." from oxg_events2 "
					." where id_zone is not null "
					." and id_country is not null $sReqFam "
					." group by id_country, id_zone "
					." order by somme desc "
					." limit 1";
	$req = mysql_query($sql_query) or mysql_die();
	$place = mysql_fetch_array($req);

	// Calcul des 5 lieux les plus présents
	$places = array();
	$sql_query = "select id_country, count(*) somme from oxg_events2 where id_country is not null $sReqFam group by id_country order by somme desc LIMIT 5";
	$req=mysql_query($sql_query) or mysql_die();
	$nbre = 0;
	while($row = mysql_fetch_array($req))
	{
		$places[] = array('id_country'=>$row['id_country']);
		$nbre += 1;
	}

	// Calcul de la personne la plus ancienne
	$sql_query = "select id_event, devnt from oxg_events2 where devnt is not null $sReqFam order by devnt limit 1";
	$req=mysql_query($sql_query) or mysql_die();
	$people = mysql_fetch_array($req);
	$birth_date=substr($people['devnt'], 0, 4);
	$sIndividual=donne_ind_evnt($FAMILY, $people['id_event']);
	$individual=affiche_lien_individu($FAMILY, $sIndividual);
	$lblDate=substr($people['devnt'], 0, 4);
	$sIndividu=donne_ind_evnt($FAMILY, $people['id_event']);
	$sIndividu=affiche_lien_individu($FAMILY, $sIndividu);
	
	// Calcul du nombre de personnes
	$result = mysql_query( "select count(*) as SOMME from oxg_individuals where 1=1 $sReqFam");
	$row = mysql_fetch_array($result);
	$ind = $row['SOMME'];

	// Calcul du nombre de patronymes
	$result = mysql_query( "select distinct id_patronym from oxg_individuals where 1=1 $sReqFam");
	$patro = mysql_num_rows($result);

	// Calcul du nombre de couples, d'enfants et la moyenne par couple
	$result = mysql_query("select count(*) as somme from oxg_individuals where id_father!='-1' $sReqFam or id_mother!='-1' $sReqFam GROUP BY id_father, id_mother");
	$fam = 0;
	$child = 0;
	while ($row = mysql_fetch_array($result))
	{	$fam += 1;
		$child += $row['somme'];
	}
	if($fam != 0)
	{
		$child = round($child/$fam,2);
	}
	else
	{
		$child = 0;
	}

	$result = mysql_query( "select COUNT(*) as somme from oxg_events2 where id_country is not null");
	$lieux = 0;
	while ($row = mysql_fetch_array($result)) { $lieux += 1; }

	$sReq="select birth, birth_d, birth_p, wedding, wedding_d, wedding_p, death, death_d, death_p, event, event_d, event_p from oxg_stat_index where id_family=$FAMILY";
	$result = mysql_query( $sReq );
	$row = mysql_fetch_array($result);

	$birth =	$row['birth'];
	$birth_d =	$row['birth_d'];
	$birth_p =	$row['birth_p'];

	$wedding = 	$row['wedding'];
	$wedding_d =	$row['wedding_d'];
	$wedding_p =	$row['wedding_p'];

	$death =	$row['death'];
	$death_d =	$row['death_d'];
	$death_p =	$row['death_p'];

	$event =	$row['event'];
	$event_d =	$row['event_d'];
	$event_p =	$row['event_p'];

	$other =	$event - $birth - $wedding - $death;
	$other_d =	$event_d - $birth_d - $wedding_d - $death_d;
	$other_p =	$event_p - $birth_p - $wedding_p - $death_p;

	$sql_query = "select value from oxg_param where name='MENU'";
	$result = mysql_query($sql_query) or mysql_die();
	if($row = mysql_fetch_array($result))
	{
		if($row['value']=="BLUE_TOP")
		{
			echo "<table>";
			echo "<tbody><tr><td bgcolor='#425a74' valign='top' width='25%'><br>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='100%'>";
			echo "<tbody><tr><td></td></tr><tr align='left'><div style='text-align: left;'>";
			echo "<font color='#e5e5e5' face='helvetica'><b>".decode_lng_text('INDEX_IND')."</b></font><br>";
			echo "</tr></tbody></table>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='98%'>";
			echo "<tbody><tr><td bgcolor='#8888ee' width='100%'><font color='#e5e5e5'>";
			echo "<a href='§patro_full.php?FAMILY=$FAMILY'>".decode_lng_text('INDEX_LNK_PATRO')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§flashlist.php?FAMILY=$FAMILY'>".decode_lng_text('FLASH_TITLE')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§top10.php?FAMILY=$FAMILY'>".decode_lng_text('INDEX_LINK_STATS')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§recherche.php?FAMILY=$FAMILY'>".decode_lng_text('INDEX_LNK_SEARCH')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§couplesearch.php?FAMILY=$FAMILY'>".decode_lng_text('SEARCH_COUPLE')."</a></font><br>";
			echo "</td></tr></tbody></table>";

			echo "<font color='#e5e5e5' face='helvetica'><b>".decode_lng_text('INDEX_LNK_PLACES')."</b></font><br></div>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='98%'>";
			echo "<tbody><tr><td bgcolor='#8888ee' width='100%'>";
			echo "<font color='#e5e5e5'><a href='§idx_lieux.php?FAMILY=$FAMILY&amp;event=birth'>".decode_lng_text('GEDTAG_BIRT')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§idx_lieux.php?FAMILY=$FAMILY&amp;event=wedding'>".decode_lng_text('GEDTAG_MARR')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§idx_lieux.php?FAMILY=$FAMILY&amp;event=death'>".decode_lng_text('GEDTAG_DEAT')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§idx_lieux.php?FAMILY=$FAMILY&amp;event=other'>".decode_lng_text('PATRO_OTHER')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§idx_lieux.php?FAMILY=$FAMILY'>".decode_lng_text('IDX_LIEUX_ALL')."</a></font><br>";
			echo "</td></tr></tbody></table>";

			echo "<font color='#e5e5e5' face='helvetica'><b>".decode_lng_text('GEDTAG_DATE')."</b></font><br>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='98%'>";
			echo "<tbody><tr><td bgcolor='#8888ee' width='100%'>";
			echo "<font color='#e5e5e5'><a href='§idx_dates.php?FAMILY=$FAMILY'>".decode_lng_text('INDEX_EVENT')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§idx_calendar.php?FAMILY=$FAMILY'>".decode_lng_text('CALENDAR_INDEX')."</a></font><br>";
			echo "<font color='#e5e5e5'><a href='§birthday.php?FAMILY=$FAMILY'>".decode_lng_text('BIRTHDAY_INDEX')."</a></font><br>";
			echo "</td></tr></tbody></table>";

			echo "<font color='#e5e5e5' face='helvetica'><b>".decode_lng_text('INDEX_EVENT')."</b></font><br>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='98%'>";
			echo "<tbody><tr><td bgcolor='#8888ee' width='100%'>";
			echo "<font color='#e5e5e5'><a href='§EventSearch.php?FAMILY=$FAMILY'>".decode_lng_text('INDEX_LNK_SEARCH')."</a></font><br>";
			echo "</td></tr></tbody></table>";

			echo "<font color='#e5e5e5' face='helvetica'><b>".decode_lng_text('IDX_LIEUX_MAP')."</b></font><br>";
			echo "<table border='0' cellpadding='2' cellspacing='0' width='98%'>";
			echo "<tbody><tr><td bgcolor='#8888ee' width='100%'>";
			$i = 0;
			while ($i < $nbre)
			{
				echo "<font color='#e5e5e5'><a href='§carte.php?FAMILY=".$FAMILY."&PAYS=".$places[$i]['id_country']."'>".GetCountryName($places[$i]['id_country'])."</a></font><br>";
				$i += 1;
			}
			echo "</td></tr></tbody></table>";
			echo "<br></td></tr></tbody></table>";
			echo "<td valign='top' width='75%'><br />";
		}
	}
	echo "<center><h1>$sTitle</h1></center>";
	echo "<BR><CENTER>";

	// SOSA number 1
	if($FAMILY<>'-1' and $bSosa==true)
	{
		$persoon = mysql_fetch_array(mysql_query( "select id_individual, first_name, libelle from oxg_individuals, oxg_patronyms where sosa='1' and oxg_individuals.id_patronym=oxg_patronyms.id_patronym $sReqFam"));
		echo "<P>".decode_lng_text("INDEX_SOSA1");
		echo " <A HREF='§detail_individu.php?INDIVIDU=$persoon[id_individual]&FAMILY=$FAMILY'>$persoon[first_name] $persoon[libelle]</a> ";
		echo decode_lng_text("INDEX_SOSA2")." <A HREF='§sosa.php?FAMILY=$FAMILY'>".decode_lng_text('LIST_ASC_TITLE')."</A><BR><BR>";
	}
	echo decode_lng_text("INDEX_BASED1")." ".GetZoneName($place['id_country'],$place['id_zone']).decode_lng_text("INDEX_BASED2")." ".GetCountryName($place['id_country']).".<BR>";
	echo decode_lng_text("INDEX_EARLIEST1")." $sIndividu ".decode_lng_text("INDEX_EARLIEST2")." $lblDate.<BR>";
	echo decode_lng_text('INDEX_RESUME1')." $ind ".decode_lng_text('INDEX_RESUME2')." $fam ".decode_lng_text('INDEX_RESUME3')." $patro ".decode_lng_text('INDEX_RESUME4')." $lieux ".decode_lng_text('INDEX_RESUME5')."<br />";
	echo decode_lng_text('INDEX_RESUME6')." $fam ".decode_lng_text('INDEX_RESUME7')." $wedding ".decode_lng_text('INDEX_RESUME8')." $child ".decode_lng_text('INDEX_RESUME9')."<br /></center>";
	echo "</CENTER><BR>";

	echo "<CENTER><TABLE border=1><TR bgcolor=lightblue align=center>";
	echo "<TH style='width: 30%;'><center>".decode_lng_text('INDEX_EVENT')."</TH>";
	echo "<TH style='width: 110px;'><center>".decode_lng_text('STATS_NB')."</TH>";
	echo "<TH style='width: 110px;'><center>".decode_lng_text('GEDTAG_DATE')."</TH>";
	echo "<TH style='width: 110px;'><center>".decode_lng_text('INDEX_LNK_PLACES')."</TH></TR>";
	echo "<TR BGCOLOR=''><TD><center>".decode_lng_text('GEDTAG_BIRT')."/".decode_lng_text('GEDTAG_BAPM')."</TD><TD><center>$birth</TD><TD><center>$birth_d</TD><TD><center>$birth_p</TD></TR>";
	echo "<TR BGCOLOR=''><TD><center>".decode_lng_text('GEDTAG_MARR')."</TD><TD><center>$wedding</TD><TD><center>$wedding_d</TD><TD><center>$wedding_p</TD></TR>";
	echo "<TR BGCOLOR=''><TD><center>".decode_lng_text('GEDTAG_DEAT')."</TD><TD><center>$death</TD><TD><center>$death_d</TD><TD><center>$death_p</TD></TR>";
	echo "<TR BGCOLOR=''><TD><center>".decode_lng_text('PATRO_OTHER')."</TD><TD><center>$other</TD><TD><center>$other_d</TD><TD><center>$other_p</TD></TR>";
	echo "<TR BGCOLOR=''><TD><center>".decode_lng_text('STATS_TOTAL')."</TD><TD><center>$event</TD><TD><center>$event_d</TD><TD><center>$event_p</TD></TR>";
	echo "</TABLE></CENTER><br>";

function GetCountryName($id_country)
{
	$sqlResult=mysql_query($sql_query="select libelle from oxg_countries where id_country=$id_country") or mysql_die();
	$reqResult=mysql_fetch_array($sqlResult);
	return $reqResult[libelle];
}

function GetZoneName($id_country, $id_zone)
{
	$sqlResult=mysql_query($sql_query="select libelle from oxg_zones where id_country=$id_country and id_zone=$id_zone") or mysql_die();
	$reqResult=mysql_fetch_array($sqlResult);
	return $reqResult[libelle];
}
	
	affiche_pied_page(false);
?>