<?php

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

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	$COUNTRY=isset($_REQUEST["PAYS"])?$_REQUEST["PAYS"] : 0; 
	$ZONE=isset($_REQUEST["ZONE"])?$_REQUEST["ZONE"] : 0; 
	$event=isset($_REQUEST["event"])?$_REQUEST["event"] : "all";
	
	$sTitle = decode_lng_text("DETAIL_ZONE_TITLE");
	$result = mysql_query( $sql_query="select oxg_countries.id_country, oxg_countries.libelle from oxg_countries where oxg_countries.id_country=$COUNTRY") or mysql_die();
	if($row = mysql_fetch_array($result))
	{
		$sTitle .= " : ".$row['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();
	
	
	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_zone.php?PAYS=$COUNTRY&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_zone.php?PAYS=$COUNTRY&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_zone.php?PAYS=$COUNTRY&FAMILY=$FAMILY&event=death'>".decode_lng_text('GEDTAG_DEAT')."</A></TD>";
	}
	
	if ($event=="other")
	{
		echo "<TD><B>".decode_lng_text('PATRO_OTHER')."</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_zone.php?PAYS=$COUNTRY&FAMILY=$FAMILY&event=other'>".decode_lng_text('PATRO_OTHER')."</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_zone.php?PAYS=$COUNTRY&FAMILY=$FAMILY&event=all'>".decode_lng_text('IDX_LIEUX_ALL')."</A></TD>";
	}

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


	$result = mysql_query( $sql_query="select oxg_countries.id_country, oxg_countries.libelle from oxg_countries where oxg_countries.id_country=$COUNTRY")  or mysql_die();
	if($row = mysql_fetch_array($result))
	{
		//echo "<H2>$row[libelle]</H2><BR>";

		// Unknown places of the country
		$sql_query  = "select count(*) as CNT ";
		$sql_query .= "from oxg_events2 ";
		$sql_query .= "where id_country=$row[id_country] and id_zone=-1 and id_city=-1 ";
		$sql_query .= $sReqFam;
		$sql_query .= $sReqFiltre;

		$result_ind = mysql_query( $sql_query ) or mysql_die();
		if($row_ind = mysql_fetch_array($result_ind))
		{
			if($row_ind['CNT']!="0")
			{
				echo "<p><a href='§detail_ind_zone.php?COUNTRY=$row[id_country]&ZONE=-1&CITY=-1&FAMILY=$FAMILY&event=$event'>";
				display_lng_text("DETAIL_ZONE_UNKNOWN_CNTRY");
				echo " ($row_ind[CNT])";
				echo "</a></p>";
			}
		}

		
		$result_pays = mysql_query( $sql_query="select id_zone, COUNT(*) as somme from oxg_events2 where id_country=$row[id_country] and id_country<>-1 $sReqFiltre $sReqFam group by id_zone order by id_zone") or mysql_die();
		
		while($row_pays = mysql_fetch_array($result_pays))
		{
			if($row_pays["id_zone"]!="")
			{
				$result_zone = mysql_query( $sql_query="select oxg_zones.code, oxg_zones.id_zone, oxg_zones.libelle from oxg_zones where oxg_zones.id_country=$row[id_country] and oxg_zones.id_zone=$row_pays[id_zone]") or mysql_die();
				$row_zone = mysql_fetch_array($result_zone);
				if ($row_zone['libelle']!="")
				{
					echo "<p><a name=".$row_zone['id_zone']." id=".$row_zone['id_zone']."></a></p>";
					echo "<H3>$row_zone[code] $row_zone[libelle] ($row_pays[somme])  : <a href='§rechercheLieuX.php?FAMILY=$FAMILY&COUNTRY=$row[id_country]&ZONE=$row_pays[id_zone]'>".decode_lng_text('INDEX_LNK_SEARCH')."</a></H3>";
				}
			
				if(!function_exists('mysqli_connect'))
				{
					mysql_query($sql_query="SET OPTION SQL_BIG_SELECTS=1") or mysql_die();
				}

				$sql_query  = "select oxg_cities.code, oxg_cities.id_city, oxg_cities.libelle, count(*) as somme ";
				$sql_query .= "from oxg_cities, oxg_events2 ";
				$sql_query .= "where oxg_events2.id_country=$row[id_country] and oxg_events2.id_zone=$row_pays[id_zone] ";
				$sql_query .= "and oxg_cities.id_zone=$row_pays[id_zone] and oxg_cities.id_country=$row[id_country] ";
				$sql_query .= "and oxg_cities.id_city=oxg_events2.id_city ";
				$sql_query .= $sReqFam;
				$sql_query .= $sReqFiltre;
				$sql_query .= "group by oxg_cities.code, oxg_cities.id_city, oxg_cities.libelle ";
				$sql_query .= "order by oxg_cities.libelle, oxg_cities.code ";

				$result_ville = mysql_query( $sql_query ) or mysql_die();
				while($row_ville = mysql_fetch_array($result_ville))
				{
					echo "<p><a href='§detail_ind_zone.php?COUNTRY=$row[id_country]&ZONE=$row_pays[id_zone]&CITY=$row_ville[id_city]&FAMILY=$FAMILY&event=$event'>$row_ville[libelle] $row_ville[code] ($row_ville[somme])</a> <a href='§rechercheLieuX.php?FAMILY=$FAMILY&COUNTRY=$row[id_country]&ZONE=$row_pays[id_zone]&CITY=$row_ville[id_city]'>".decode_lng_text('INDEX_LNK_SEARCH')."</a></p>";
				}

				// Unknown places of the zone	
				$sql_query  = "select count(*) as CNT ";
				$sql_query .= "from oxg_events2 ";
				$sql_query .= "where id_country=$row[id_country] and id_zone=$row_pays[id_zone] and id_zone<>-1 and (id_city='-1' or id_city is null) $sReqCont ";
				$sql_query .= $sReqFam;
				$sql_query .= $sReqFiltre;
				$result_ind = mysql_query( $sql_query ) or mysql_die();
				if($row_ind = mysql_fetch_array($result_ind))
				{
					if($row_ind['CNT']!="0")
					{
						echo "<p><a href='§detail_ind_zone.php?COUNTRY=$row[id_country]&ZONE=$row_pays[id_zone]&CITY=-1&FAMILY=$FAMILY&event=$event'>";
						display_lng_text("DETAIL_ZONE_UNKNOWN");
						echo " ($row_ind[CNT])";
						echo "</a></p>";
					}
				}
			}
			
			flush();
		}
	}
	affiche_pied_page(true);
?>