<?php

	require("§lib_inc.php");

	set_time_limit(0);
	
	flush();

	//on vide la base avant d'insérer de nouvelles lignes
	mysqli_query($_SESSION['dbh'],"truncate table oxg_stat_index");

	//stat pour chacune des familles
	$result = mysqli_query($_SESSION['dbh'], "select id_family from oxg_family where 1=1") or mysqli_die();
	while($row = mysqli_fetch_array($result))
	{
		$fml=$row['id_family'];
		$sReqFam="and id_family=$fml";
		create_stat($fml, $sReqFam);
		update_stat($fml, $sReqFam, "birth", "51,47,48,64");
		update_stat($fml, $sReqFam, "death", "77,55");
		update_stat($fml, $sReqFam, "wedding", "115");
	}

	//stat globale pour l'ensemble des familles
	$fml=-1;
	$sReqFam="";
	create_stat($fml, $sReqFam);
	update_stat($fml, $sReqFam, "birth", "51,47,48,64");
	update_stat($fml, $sReqFam, "death", "77,55");
	update_stat($fml, $sReqFam, "wedding", "115");

function create_stat($fml,$sReqFam)
{
	mysqli_query($_SESSION['dbh'],"INSERT INTO oxg_stat_index (id_family) VALUES ($fml)") or mysqli_die();
	$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'";
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set event=(select count(*) from oxg_events2 where 1=1 $sRexclusion $sReqFam) where id_family=$fml") or mysqli_die();
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set event_d=(select count(*) from oxg_events2 where devnt<>'0000-00-00' $sRexclusion $sReqFam) where id_family=$fml") or mysqli_die();
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set event_p=(select count(*) from oxg_events2 where id_country<>-1 $sRexclusion $sReqFam) where id_family=$fml") or mysqli_die();
}

function update_stat($fml, $sReqFam, $evt_name, $evt_type)
{
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set $evt_name=(select count(*) from oxg_events2 where oxg_events2.type in ($evt_type) $sReqFam) where id_family=$fml") or mysqli_die();
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set ".$evt_name."_d=(select count(*) from oxg_events2 where oxg_events2.type in ($evt_type) and devnt<>'0000-00-00' $sReqFam) where id_family=$fml") or mysqli_die();
	mysqli_query($_SESSION['dbh'],"update oxg_stat_index set ".$evt_name."_p=(select count(*) from oxg_events2 where oxg_events2.type in ($evt_type) and id_country<>-1 $sReqFam) where id_family=$fml") or mysqli_die();
}

?>

<P>Table oxg_stat_index effectué
<?php
$CREAT=isset($_REQUEST["CREAT"])?$_REQUEST["CREAT"] : "";
if($CREAT=="AUTO")
{
	echo ("<p>");
	display_lng_text("ADM_DATA");
?>
	<SCRIPT LANGUAGE="JavaScript">
	<!--
	setTimeout("top.location.href = '§cal.php?CREAT=AUTO'",1);
	//-->
	</SCRIPT>
<?php
}
else
{
affiche_pied_page_admin(true);
}
?>
</body></html>