<?php

	$sPage  = "places_ind";
	require("admin/§lib_inc.php");

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	//$sTitle = decode_lng_text("detail_ind_zone_TITLE");

	$COUNTRY=isset($_REQUEST["COUNTRY"])?$_REQUEST["COUNTRY"] : -1; 
	$ZONE=isset($_REQUEST["ZONE"])?$_REQUEST["ZONE"] : -1; 
	$CITY=isset($_REQUEST["CITY"])?$_REQUEST["CITY"] : -1; 
	$event=isset($_REQUEST["event"])?$_REQUEST["event"] : "all";

	$result_ville = mysql_query( $sql_query="select libelle from oxg_cities where id_city=$CITY and id_zone=$ZONE and id_country=$COUNTRY" ) or mysql_die();
	$ville = mysql_fetch_array($result_ville);
	$result_pays = mysql_query( $sql_query="select libelle from oxg_countries where id_country=$COUNTRY" ) or mysql_die();
	$pays = mysql_fetch_array($result_pays);

	$sTitle = $ville['libelle']." (".$pays['libelle'].")";

	require("§menu.php");

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

	//set_time_limit(0);
	
	flush();

	$sReqFiltre = "";
	echo "<div id=panel><center><table>";
	echo "<TD>".decode_lng_text('STATS_TOP15_DATE_C')." :</TD>";
	if ($event=="")
	{
		$event="all";
	}

	if ($event=="birth")
	{
		echo "<TD><B>".decode_lng_text('GEDTAG_BIRT')."</B></TD>";
		$sReqFiltre = " and type='51' ";
	}
	else
	{
		echo "<TD><A HREF='§detail_ind_zone.php?COUNTRY=$COUNTRY&ZONE=$ZONE&CITY=$CITY&FAMILY=$FAMILY&event=birth'>".decode_lng_text('GEDTAG_BIRT')."</A></TD>";
	}

	if ($event=="wedding")
	{
		echo "<TD><B>".decode_lng_text('GEDTAG_MARR')."</B></TD>";
		$sReqFiltre = " and type='115' ";
	}
	else
	{
		echo "<TD><A HREF='§detail_ind_zone.php?COUNTRY=$COUNTRY&ZONE=$ZONE&CITY=$CITY&FAMILY=$FAMILY&event=wedding'>".decode_lng_text('GEDTAG_MARR')."</A></TD>";
	}
	
	if ($event=="death")
	{
		echo "<TD><B>".decode_lng_text('GEDTAG_DEAT')."</B></TD>";
		$sReqFiltre = " and type='77' ";
	}
	else
	{
		echo "<TD><A HREF='§detail_ind_zone.php?COUNTRY=$COUNTRY&ZONE=$ZONE&CITY=$CITY&FAMILY=$FAMILY&event=death'>".decode_lng_text('GEDTAG_DEAT')."</A></TD>";
	}
	
	if ($event=="other")
	{
		echo "<TD><B>"."autre"."</B></TD>";
		$sRexclusion = "and type<>'45' and type<>'99' and type<>'125' and type<>'129' and type<>'148' and type<>'156' and type<>'163' and type<>'167' and type<>'179'";
		$sReqFiltre = " and type<>'51' and type<>'77' and type<>'115' $sRexclusion ";
	}
	else
	{
		echo "<TD><A HREF='§detail_ind_zone.php?COUNTRY=$COUNTRY&ZONE=$ZONE&CITY=$CITY&FAMILY=$FAMILY&event=other'>"."autre"."</A></TD>";
	}

	if ($event=="all")
	{
		echo "<TD><B>".decode_lng_text('IDX_LIEUX_ALL')."</B></TD>";
		$sRexclusion = "and type<>'45' and type<>'99' and type<>'125' and type<>'129' and type<>'148' and type<>'156' and type<>'163' and type<>'167' and type<>'179'";
		$sReqFiltre = $sRexclusion;
	}
	else
	{
		echo "<TD><A HREF='§detail_ind_zone.php?COUNTRY=$COUNTRY&ZONE=$ZONE&CITY=$CITY&FAMILY=$FAMILY&event=all'>".decode_lng_text('IDX_LIEUX_ALL')."</A></TD>";
	}

	echo "</table></center></div><BR>";
	

	$id_type_evnt_plac="";
	$result_evnt = mysql_query( $sql_query="select id_type_event from oxg_events_type WHERE code='PLAC'") or mysql_die();
	$row_evnt = mysql_fetch_array($result_evnt);
	if($row_evnt)
	{
		$id_type_evnt_plac = $row_evnt['id_type_event'];
	}

	$result_out = array ();
	
	//$sql_query  = "select id_father, id_family, type, id_country, id_zone, date ";
	$sql_query  = "select type, id_event, devnt, sDtEvnt, id_individu, oxg_events2.id_family ";
	$sql_query .= "from oxg_events2 ";
	if($bSessionOk==false)
	{
		$sql_query .= ", oxg_individuals ";
	}
	$sql_query .= "where oxg_events2.id_country=$COUNTRY and oxg_events2.id_zone=$ZONE and (oxg_events2.id_city=$CITY or (oxg_events2.id_city is null and $CITY=-1))";
	if($FAMILY!=-1)
	{
		$sql_query .= " and oxg_events2.id_family=$FAMILY ";
		if($bSessionOk==false)
		{
			$sql_query .= " and oxg_individuals.id_family=$FAMILY ";
		}
	}
	$sql_query .= $sReqFiltre;
	if($bSessionOk==false)
	{
		$sql_query .= " and oxg_events2.id_family=oxg_individuals.id_family ";
		$sql_query .= " and oxg_events2.id_individu=oxg_individuals.id_individual ";
		$sql_query .= " and contemporary=1 ";
	}
//	$sql_query .= "order by oxg_patronyms.libelle, oxg_events.first_name ";
	$result_evnt = mysql_query( $sql_query ) or mysql_die();
	while($row_evnt = mysql_fetch_array($result_evnt, MYSQL_ASSOC))
	{
		//$sql_query = "select id_father, type, id_event, info2 from oxg_events where id_event=$row_evnt[id_father] and id_family=$row_evnt[id_family]";
		//$result_ind = mysql_query( $sql_query );
		//$row_ind = mysql_fetch_array($result_ind);
		$compagne="";
		if ($row_evnt["type"]==51) { $acte=decode_lng_text("GEDTAG_BIRT")." ".decode_lng_text("GEDTAG_OF"); }
		if ($row_evnt["type"]==115) { $acte=decode_lng_text("GEDTAG_MARR")." ".decode_lng_text("GEDTAG_BTW"); $compagne = $row_evnt["id_event"];}
		if ($row_evnt["type"]==82) { $acte=decode_lng_text("GEDTAG_DIV")." ".decode_lng_text("GEDTAG_BTW"); $compagne = $row_evnt["id_event"];}
		if ($row_evnt["type"]==77) { $acte=decode_lng_text("GEDTAG_DEAT")." ".decode_lng_text("GEDTAG_OF"); }
		if ($row_evnt["type"]==55) { $acte=decode_lng_text("GEDTAG_BURI")." ".decode_lng_text("GEDTAG_OF"); }
		if ($row_evnt["type"]!=51 AND $row_evnt["type"]!=115 AND $row_evnt["type"]!=82 AND $row_evnt["type"]!=77 AND $row_evnt["type"]!=55) { $acte=decode_lng_text("GEDTAG_EVEN")." ";}

		//$sql_query2 = "select libelle from oxg_events where id_father=$row_evnt[id_event] and type='76' and id_family=$row_evnt[id_family]";
		//$result_date = mysql_query( $sql_query2 );
		//$row_date = mysql_fetch_array($result_date);

		$year = intval(substr($row_evnt["devnt"],0,4))+1000;
		$month = intval(substr(substr($row_evnt["devnt"],0,7),-2))+100;
		$date1 = $row_evnt["devnt"];
		$date1 = str_replace("-0", "-", $date1);
		$date1 = str_replace("-", "/", $date1);
		$date2 = $row_evnt["sDtEvnt"];
		$date = $date1.":".$date2;
		if ($month<110)
		{
			$day = substr(substr($row_evnt["devnt"],0,9),-2)+100;
		}
		else
		{
			$day = substr(substr($row_evnt["devnt"],0,10),-2)+100;
		}
		$result_out[] = array('year'=>$year, 'month'=>$month, 'day'=>$day, 'acte'=>$acte, 'id_family'=>$row_evnt["id_family"], 'id_father'=>$row_evnt["id_individu"], 'compagne'=>$compagne, 'date'=>$date);
	}

	//affichage du résultat
	if (count($result_out)==0)
	{
		echo "<BR><BR><BR>".decode_lng_text("BIRTHDAY_NONE");
	}
	else
	{
		//transformation des lignes en colonnes pour le tri avec array_multisort
		foreach ($result_out as $key => $row_out)
		{
		    $year_[$key]  = $row_out['year'];
		    $month_[$key] = $row_out['month'];
		    $day_[$key] = $row_out['day'];
		}
		array_multisort($year_, SORT_NUMERIC, SORT_ASC, $result_out,
				$month_, SORT_NUMERIC, SORT_ASC, $result_out,
					$day_, SORT_NUMERIC, SORT_ASC, $result_out );
		echo "<table>";
		for ($i=0; $i<count($result_out); $i++)
		{
			// Affichage de la personne
			$id_ind=$result_out[$i]['id_father'];
			$id_fam=$result_out[$i]['id_family'];
			$result_cont = mysql_query( $sql_query="select * from oxg_individuals where id_individual=$id_ind $sReqCont and id_family=$id_fam") or mysql_die();
			if ($row_cont = mysql_fetch_array($result_cont))
			{
				echo "<tr><th align=right>".decode_date($result_out[$i]['date'])."</th>";
				echo "<td>".$result_out[$i]['acte']." </th>";
				echo affiche_lien_individu($id_fam, $id_ind, true, false);
				
				//affichage du conjoint
				$compagne = $result_out[$i]['compagne'];
				if ($compagne != "")
				{
					$req = "select info2 from oxg_events where id_event=$compagne and id_family=$id_fam";
					$res = mysql_query($sql_query=$req) or mysql_die();
					$row = mysql_fetch_array($res);
					$compagne = $row['info2'];
					$result_cont = mysql_query( $sql_query="select * from oxg_individuals where id_individual=$compagne $sReqCont and id_family=$id_fam") or mysql_die();
					if ($row_cont = mysql_fetch_array($result_cont))
					{
						echo " ".decode_lng_text("GEDTAG_AND")." ".affiche_lien_individu($id_fam, $compagne, true, false);
					}
					else
					{
						echo " ".decode_lng_text("GEDTAG_AND")." X";
					}
				}
			}
			else
			{
				echo "<tr><th></th><td></th>";
				echo affiche_lien_individu($id_fam, $id_ind, true, false);
			}
		}	
		echo "</table>";
	}
	
	affiche_pied_page(true);
?>