<?php

function TagAsContemporary($fam, $ind){

	global $count;
	$result_ind = $_SESSION['dbh']->query( "select first_name, id_patronym, contemporary from oxg_individuals where id_individual=$ind and id_family=$fam and date_deces is NULL and contemporary=1");
	$row_ind = $result_ind->fetch_array(MYSQLI_ASSOC);
	if ($row_ind) {
		$result_patro = $_SESSION['dbh']->query( "select libelle from oxg_patronyms where id_patronym=$row_ind[id_patronym]");
		$row_patro = $result_patro->fetch_array(MYSQLI_ASSOC);
		echo "Update to contemporary => ".$row_ind['first_name']." ".$row_patro['libelle']." : OK<BR>";
		$result = $_SESSION['dbh']->query( "UPDATE oxg_individuals SET contemporary=0 WHERE id_individual=$ind and id_family=$fam and date_deces=\"\" and contemporary=1");
		$count+=1;
	}
	//$result = $_SESSION['dbh']->query( "UPDATE oxg_individuals SET contemporary=0 WHERE id_individual=$ind and id_family=$fam and date_deces=\"\" and contemporary=1");
	//if ($result) {$count+=1;}
}

function Find_Child($fam, $id_parent){
	global $count;
	$result_child = $_SESSION['dbh']->query( "select id_father, id_mother, sex, id_individual as id_child from oxg_individuals where (id_father=$id_parent or id_mother=$id_parent) and contemporary=1 and id_family=$fam" );
	while($row_child = $result_child ->fetch_array(MYSQLI_ASSOC))
		{
		TagAsContemporary($fam, $row_child['id_child']);  //on tag l'enfant
		if ($row_child['id_father']==$id_parent and $row_child['id_mother']!="-1")  //si on connait le père
			{ TagAsContemporary($fam, $row_child['id_mother']); }					//-> on tag la mère
		if ($row_child['id_mother']==$id_parent and $row_child['id_father']!="-1")  //si on connait la mère
			{ TagAsContemporary($fam, $row_child['id_father']); }					//-> on tag le père
		Find_Child ($fam, $row_child['id_child']);	//on recherche les enfants des enfants
		}
}

	require("lib_inc.php");
	echo("<center><h1>");
	echo decode_lng_text("FILTERING_TITLE");
	echo("</h1></center>");

		
	// recup variable du formulaire
	$choice = @$_GET['choice'];
	$limit = @$_GET['limit'];
	if ($limit=="") {$date = time(); $year = date("Y",$date); $limit=$year-101;}
	echo '<br>limite '.$limit;
	// tableau du filtrage	
	echo "<div id=panel>";
	echo "<br/>";
	echo "<br/>";
	echo "<form action=\"\" method=get>";
	echo "<center>";
	echo "<table border=0><tr><td>";
	echo "<input type=radio name=choice value='public'>".decode_lng_text('FILTERING_PUBLIC')."<BR>";
    echo "<input type='radio' name=choice value='private'>".decode_lng_text('FILTERING_PRIVATE')." > ";
	echo "<input type=text name=limit value=\"$limit\" MAXLENGTH=4 SIZE=4></td>\n";
	echo "<td><input type=submit value=".decode_lng_text('PRIVATE_ENTER')."></td></tr></table></form>";
	echo "</center>";
	echo "</div>";		
		
	echo  'choix= '.$choice;
	if ($choice=="") {
		echo "<BR>".decode_lng_text('FILTERING_BEGIN1')."<BR>";
		echo decode_lng_text('FILTERING_BEGIN2')."<BR>";
		echo decode_lng_text('FILTERING_BEGIN3');
		}
		
	if ($choice=="public") {
		//mise en public de tous des individus
		$result = $_SESSION['dbh']->query( "UPDATE oxg_individuals SET contemporary=1 WHERE 1");
		if ($result)
			{ echo "=> ".decode_lng_text('FILTERING_END1')." <=";}
		}
	$count = 0;
	
	//mise en privé des individus > année de réf
	if ($choice=="private") {
		$id_type_evnt_date="";
		$result = $_SESSION['dbh']->query( "select id_type_event from oxg_events_type WHERE code='DATE' ");
		$row_evnt=$result->fetch_array(MYSQLI_ASSOC);
		if ($row_evnt) { $id_type_evnt_date = $row_evnt['id_type_event'];}
		$id_type_evnt_marr="";
		$result = $_SESSION['dbh']->query( "select id_type_event from oxg_events_type WHERE code='MARR'");
		$row_evnt=$result->fetch_array(MYSQLI_ASSOC);		
		if($row_evnt) { $id_type_evnt_marr = $row_evnt['id_type_event']; }

		//recherche des naissances contemporaines
		$result = $_SESSION['dbh']->query("select id_individual, date_naissance, id_family from oxg_individuals where right(date_naissance,4)>$limit-1");
		echo '<br>';
		echo 'Il y a '. $result->num_rows. ' contemporain(s) dans la table individuals </br>';
		while ($row = $result->fetch_array(MYSQLI_ASSOC))
			{
			$FAMILY=$row['id_family'];
			$sReqFam="and id_family=$FAMILY";
			if (substr($row['date_naissance'],-4)>$limit) {
			TagAsContemporary($FAMILY, $row['id_individual']); }
			Find_Child($FAMILY, $row['id_individual']);  //recherche des enfants
			}
		//recherche des mariages contemporains
		$result = $_SESSION['dbh']->query( "select libelle, id_father, type, id_event, id_family from oxg_events where type='$id_type_evnt_date' and left(libelle,4)>$limit-1");

		echo 'Il y a '. $result->num_rows. ' entrée(s) dans la base de données : </br>';
		
		while($row = $result->fetch_array(MYSQLI_ASSOC))
			{
			$FAMILY=$row['id_family'];
			$sReqFam="and id_family=$FAMILY";
			$result_marr = $_SESSION['dbh']->query( "select id_father, info2 as id_mother from oxg_events where id_event=$row[id_father] and type='$id_type_evnt_marr' $sReqFam");
			if ($row_marr = $result_marr->fetch_array(MYSQLI_ASSOC))
				{
				TagAsContemporary($FAMILY, $row_marr['id_father']);
				TagAsContemporary($FAMILY, $row_marr['id_mother']);
				Find_Child($FAMILY, $row_marr['id_mother']);  //recherche des enfants
				}
			}
	if ($count>0)
		{
		echo "<BR>$count ".decode_lng_text('FILTERING_COUNT');
		}
	else
		{
		echo "<BR>".decode_lng_text('FILTERING_END2');
		}
	}
	
	
	echo "<BR><BR>";
	affiche_pied_page_admin(true);
?>

