<?php

function show_Children_1 ($fam, $gen, $new_gen, $tab)
{
$tab2 = array();
$nb_child = 0;
$nb_enfants = 0;
	if($fam!=-1)
	{
		$sReqFam="and id_family=$fam";
	}
	else
	{
		$sReqFam="";
	}
	global $bSessionOk;

	$i=0;
	while ($i<count($tab))
	{
		// Children
		$nb_enfants = 1;
		//$gen++;
		$parent=$tab[$i]['ind'];
		$num_up=$tab[$i]['num'];
		$old_conjoint = "";
		$sql_query = "select oxg_individuals.id_individual as id_ind, oxg_individuals.first_name, sex, oxg_patronyms.libelle, id_father, id_mother from oxg_individuals, oxg_patronyms where (id_father='$parent' or id_mother='$parent') and oxg_patronyms.id_patronym=oxg_individuals.id_patronym $sReqFam ";
		if($bSessionOk==false)
		{
			$sql_query.= " order by right(date_naissance,4) ";
		}
		$result_ind = mysql_query( $sql_query ) or mysql_die();
		while($row_ind = mysql_fetch_array($result_ind))
		{
			if ($new_gen==1) {echo "<BR><B>".decode_lng_text('LIST_GEN')." $gen</B><BR>";$new_gen=0;}
				if($parent!=$row_ind['id_father'])
					{
						$id_conjoint=$row_ind['id_father'];
					}
					if($parent!=$row_ind['id_mother'])
					{
						$id_conjoint=$row_ind['id_mother'];
					}
					if($old_conjoint!=$id_conjoint)
					{
						$old_conjoint=$id_conjoint;
					}
					if ($row_ind['sex']==1)
					{
						$article=" ".decode_lng_text('LIST_DAU')." ";
						$flag="female.png";
					}
					else if ($row_ind['sex']==0)
					{
						$article=" ".decode_lng_text('LIST_SON')." ";
						$flag="male.png";
					}
					else
					{
						$article=" ".decode_lng_text('LIST_SON')." ";
						$flag="";
					}
					if ($num_up==0) {$affiche = $nb_enfants;} else {$affiche = $num_up.".".$nb_enfants;}
					$nb_child = $nb_child + 1;
					$nb_enfants++;
					if($flag!="")
					{
						echo "<IMG SRC=$flag>";
					}
					echo affiche_lien_individu($fam, $row_ind['id_ind'],true,true)."<I>".$article.affiche_lien_individu($fam,$parent,true,false)." ".decode_lng_text('GEDTAG_AND')." ".affiche_lien_individu($fam,$id_conjoint,true,false)."</I><BR>";
					$tab2[] = array('num' => $affiche, 'ind' => $row_ind['id_ind']);
		}
		$i++;
	}
	return $tab2;
}


function show_Children_2 ($parent, $fam, $tab1, $tab2, $gen, $num_up, $gen_limit)
{
	global $bSessionOk;
	if($fam!=-1)
	{
		$sReqFam="and id_family=$fam";
	}
	else
	{
		$sReqFam="";
	}
	// Children
	$nb_enfants = 1;
	$gen++;
	$old_conjoint = "";
	$sql_query = "select oxg_individuals.id_individual as id_ind, oxg_individuals.first_name, sex, oxg_patronyms.libelle, id_father, id_mother from oxg_individuals, oxg_patronyms where (id_father='$parent' or id_mother='$parent') and oxg_patronyms.id_patronym=oxg_individuals.id_patronym $sReqFam order by id_father, id_mother";
	if($bSessionOk==false)
	{
		$sql_query.= ", right(date_naissance,4) ";
	}
	$result_ind = mysql_query( $sql_query ) or mysql_die();
	$tab1 = $tab1.$tab2;
	$nb_child = 0;
	while($row_ind = mysql_fetch_array($result_ind))
	{
			if($parent!=$row_ind['id_father'])
		{
			$id_conjoint=$row_ind['id_father'];
		}
		if($parent!=$row_ind['id_mother'])
		{
			$id_conjoint=$row_ind['id_mother'];
		}
		if($old_conjoint!=$id_conjoint)
		{
			echo $tab1.affiche_lien_individu($fam, $parent,true,true);
			echo $tab1."<I> ".decode_lng_text("DETAIL_IND_WITH")." ".affiche_lien_individu($fam, $id_conjoint,true,true)."</I>";
			$old_conjoint=$id_conjoint;
		}
		if ($row_ind['sex']==1) { $flag="female.png"; }
		if ($row_ind['sex']==0) { $flag="male.png"; }

		if ($num_up=="") {$affiche = $nb_enfants;} else {$affiche = $num_up.".".$nb_enfants;}
		$nb_child = $nb_child + 1;
		echo $tab1.$affiche." --> <IMG SRC=$flag> ".affiche_lien_individu($fam, $row_ind['id_ind'], true, true)." </dd>";

	//boucle sur les enfants
	if ($gen < $gen_limit)
		{
		show_children_2 ($row_ind['id_ind'], $fam, $tab1, $tab2, $gen, $affiche, $gen_limit);
		}
		$nb_enfants++;
	}
}


function show_Children_3 ($fam, $gen, $new_gen, $tab)
{
$tab2 = array();
	global $bSessionOk;
	if($fam!=-1)
	{
		$sReqFam="and id_family=$fam";
	}
	else
	{
		$sReqFam="";
	}
$i=0;
while ($i<count($tab)) {
	// Children
	$nb_enfants = 1;
	//$gen++;
	$parent=$tab[$i]['ind'];
	$num_up=$tab[$i]['num'];
	$old_conjoint = "";
	$sql_query = "select oxg_individuals.id_individual as id_ind, oxg_individuals.first_name, sex, oxg_patronyms.libelle, id_father, id_mother from oxg_individuals, oxg_patronyms where (id_father='$parent' or id_mother='$parent') and oxg_patronyms.id_patronym=oxg_individuals.id_patronym $sReqFam";
	if($bSessionOk==false)
	{
		$sql_query.= " order by right(date_naissance,4)";
	}
	$result_ind = mysql_query( $sql_query ) or mysql_die();
	$nb_child = 0;
	while($row_ind = mysql_fetch_array($result_ind))
	{
	if ($new_gen==1) {echo "<TR><TD><BR><B>".decode_lng_text('LIST_GEN')." $gen</B><BR></TD></TR>";$new_gen=0;}
		if($parent!=$row_ind['id_father'])
			{
				$id_conjoint=$row_ind['id_father'];
			}
			if($parent!=$row_ind['id_mother'])
			{
				$id_conjoint=$row_ind['id_mother'];
			}
			if($old_conjoint!=$id_conjoint)
			{
			$old_conjoint=$id_conjoint;
			}
			if ($row_ind['sex']==1) { $article=" ".decode_lng_text('LIST_DAU')." "; $flag="female.png"; }
			if ($row_ind['sex']==0) { $article=" ".decode_lng_text('LIST_SON')." "; $flag="male.png"; }
	
			if ($num_up==0) {$affiche = $nb_enfants;} else {$affiche = $num_up.".".$nb_enfants;}
			$nb_child = $nb_child + 1;
			$nb_enfants++;
			echo "<TR><TD align=right>".$affiche."</TD><TD><IMG SRC=$flag> ".affiche_ligne($fam, $row_ind['id_ind'])."<TD>".$article.affiche_lien_individu($fam,$parent,true,false)." ".decode_lng_text('GEDTAG_AND')." ".affiche_lien_individu($fam,$id_conjoint,true,false)."</TR>";
			$tab2[] = array('num' => $affiche, 'ind' => $row_ind['id_ind']);
		}
	$i++;
	}
return $tab2;
}


function affiche_ligne($id_family, $id_individual)
{
	global	$bSosa, $bSessionOk;

	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}
	
	$sInd="";
	
	$sReq = "select * from oxg_individuals, oxg_patronyms ";
	$sReq .= "where id_individual='$id_individual' ";
	$sReq .= "and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$sReq .= $sReqFam;

	$result = mysql_query( $sReq ) or mysql_die();
	if($row = mysql_fetch_array($result));
	{
		if ($row['contemporary']==0 && ($bSessionOk==false || $bSessionOk==""))
		{
			$sInd.=decode_lng_text('PRIVATE_MSG');
			$sInd.="</A></TD>";
			if($bSosa==true && $row['sosa']!='-1')
			{
				$sInd=$sInd." <strong><font color=red>$row[sosa]</font></strong>";
			}
			$sInd.="<TD>";
			$sInd.="<CENTER>-</CENTER>";
			$sInd.="</TD><TD>";
			$sInd.="<CENTER>-</CENTER>";
			$sInd.="</TD>";
		
		}
		else
		{
			$sInd.=affiche_lien_individu($id_family, $id_individual, True, False);
			$sInd.="<TD>";
				if($row['date_naissance']!="")
				{
					$sInd.="<CENTER>$row[date_naissance]</CENTER>";
				}
				else
				{
					$sInd.="<CENTER>?</CENTER>";
				}
			$sInd.="</TD><TD>";
				if($row['date_deces']!="")
				{
					$sInd.="<CENTER>$row[date_deces]</CENTER>";
				}
				else
				{
					if (substr($row['date_naissance'],-4)<1910)
					{$sInd.="<CENTER>?</CENTER>";} else
					{$sInd.="<CENTER>-</CENTER>";}
				}
			$sInd.="</TD>";
		}
	}
	return $sInd;
}


	//début du script
	$sPage  = "desc";
	require("admin/§lib_inc.php");

	$INDIVIDU=isset($_REQUEST["INDIVIDU"])?$_REQUEST["INDIVIDU"] : 0; 
	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 

	if($FAMILY!=-1)
	{
		$sReqFam="and id_family=$FAMILY";
	}
	else
	{
		$sReqFam="";
	}
	if($bSessionOk==false)
	{
		$sReqCont="and contemporary=1";
		$result_ind = mysql_query( "select right(date_naissance,4) as year1 from oxg_individuals where id_individual='$INDIVIDU' $sReqFam");
		while($row_ind = mysql_fetch_array($result_ind))
		{
			$year1 = $row_ind['year1'];
		}
	}
	else
	{
		$sReqCont="";
	}

//	set_time_limit(0);
	
	flush();
	
	$sql_query = "select * from oxg_individuals where id_individual='$INDIVIDU' $sReqFam";
	$result_ind = mysql_query( $sql_query ) or mysql_die();
	$row_ind = mysql_fetch_array($result_ind);

	$sql_query="select * from oxg_patronyms where id_patronym='$row_ind[id_patronym]'";
	$result_patro = mysql_query( $sql_query ) or mysql_die();
	$row_patro = mysql_fetch_array($result_patro);

	$sTitle = $row_ind['first_name']." ".$row_patro['libelle'];
	
	//if($row_ind['sosa']!=-1)
	//{
	//	$sTitle.=" <font color=red>".$row_ind['sosa']."</font>";
	//}

	require("§menu.php");
	require("§progress_bar.php");


// recup variable du formulaire
	$gen_limit = @$_GET['gen_limit'];
	$ind = @$_GET['INDIVIDU'];
	$fam = @$_GET['FAMILY'];
	$affichage = @$_GET['affichage'];
	if  ($affichage=="") {$affichage=1;}
	$INDIVIDU=isset($_REQUEST["INDIVIDU"])?$_REQUEST["INDIVIDU"] : 0; 
	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : 0;

echo "<div id=panel>";
echo "<form action=\"\" method=get>";
echo "<input type=hidden name=INDIVIDU value=\"$ind\">";
echo "<input type=hidden name=FAMILY value=\"$fam\">";
echo "<center>";
echo "<table border=1><tr>";
echo "<td>".decode_lng_text('TREE_NBGEN')." ";
echo  "<input type=text name=gen_limit value=\"$gen_limit\" MAXLENGTH=2 SIZE=2></td>";
echo "<td>".decode_lng_text('LIST_SHOW')." ";
if ($affichage==1) {echo "<select name=affichage><OPTION value='1'>".decode_lng_text('LIST_LIST')."_1</OPTION><OPTION value='2'>".decode_lng_text('LIST_LIST')."_2</OPTION><OPTION value='3'>".decode_lng_text('LIST_CHAR')."</OPTION></select></td>";}
if ($affichage==2) {echo "<select name=affichage><OPTION value='2'>".decode_lng_text('LIST_LIST')."_2</OPTION><OPTION value='1'>".decode_lng_text('LIST_LIST')."_1</OPTION><OPTION value='3'>".decode_lng_text('LIST_CHAR')."</OPTION></select></td>";}
if ($affichage==3) {echo "<select name=affichage><OPTION value='3'>".decode_lng_text('LIST_CHAR')."</OPTION><OPTION value='1'>".decode_lng_text('LIST_LIST')."_1</OPTION><OPTION value='2'>".decode_lng_text('LIST_LIST')."_2</OPTION></select></td>";}
echo "<td><input type=submit></td>";
echo "</tr></table></form>";
echo "</center>";
echo "</div>";

if  ($gen_limit=="") {$gen_limit=99;}

	echo "<br>";
	echo "<H3>";
	echo decode_lng_text('LIST_DESC_TITLE');
	echo "</H3>";

	$tab1 = "";
	$tab2 = "<dd>";
	$gen = 0;
	$nb_child = 0;
	$num_up = "";



//affichage mode liste1
if ($affichage==1)
{
	$num_up = 0;
	$gen=1;
	$tab = array ();
	$result = array ();
	echo "<BR><B>".decode_lng_text('LIST_GEN')." $gen</B><BR>";
	echo affiche_lien_individu($FAMILY, $INDIVIDU,true,true)."<BR>";
	$tab[] = array('num'=>$num_up,'ind'=>$INDIVIDU);
	while ($gen<$gen_limit)
	{
		$gen++;
		$new_gen=1;
		$result=show_children_1 ($FAMILY, $gen, $new_gen, $tab);
		$tab=$result;
	}
}


// Display list2 mode
if ($affichage==2) { show_children_2 ($INDIVIDU, $FAMILY, $tab1, $tab2, $gen, $num_up, $gen_limit); }


// Display table mode
if ($affichage==3)
{
	$num_up = 0;
	echo "<TABLE border=1>";
	$gen=1;
	$tab = array ();
	$result = array ();
	echo "<TD><B></TD><TD><B>".decode_lng_text('GEDTAG_NAME')."</B></TD><TD><B>".decode_lng_text('LIST_BIRTH_DATE')."</B></TD><TD><B>".decode_lng_text('LIST_DEATH_DATE')."</B></TD><TD><B>".decode_lng_text('LIST_PARENT')."</B></TD></TR>";
	echo "<TR><TD><BR><B>".decode_lng_text('LIST_GEN')." $gen</B><BR></TD></TR>";
	echo "<TR><TD></TD><TD>".affiche_ligne($FAMILY, $INDIVIDU)."</TR>";
	$tab[] = array('num'=>$num_up,'ind'=>$INDIVIDU);
	while ($gen<$gen_limit)
	{
		$gen++;
		$new_gen=1;
		$result=show_children_3 ($FAMILY, $gen, $new_gen, $tab);
		$tab=$result;
	}
	echo "</TABLE>";
}

	affiche_pied_page(true);

?>


<html>
<script type="text/javascript">
document.getElementById('progress_bar').style.display="none"
</script>
</html>
