<?php

function dessiner_trait(&$nb,$img,$alt)
	{
	global $epaisseur;
   global $w1;

	if ($nb!=0)
		{
		$w=$nb*$w1;
		echo "<td colspan=$nb width=$w>";
		echo "<img src=\"$img\" alt=\"$alt\" border=\"0\" width=\"$w\" height=\"$epaisseur\">";
		echo "</td>";
	   echo chr(13);
		echo chr(10);
		$nb=0;
		}
	}
	

	$sPage  = "indi_ascend_tree_v";
	require("admin/§lib_inc.php");
	include("§fonction.php");

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

	if($bSessionOk==false)
	{
		$sReqCont="and contemporary=1";
	}
	else
	{
		$sReqCont="";
	}

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

	$result_ind = mysql_query( "select * from oxg_individuals where id_individual='$INDIVIDU' $sReqFam");
	$row_ind = mysql_fetch_array($result_ind);

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

	$sTitle = $row_ind['first_name']." ".$row_patro['libelle'];

	//require("menu.php");
?>
<style type="text/css">
a:hover
	{  color: #004900; background-color: #FFFF00; text-decoration: underline overline
	}
font.f1
	{  background-color: #FFFF55; text-decoration: none
	}

a 	{  color: §3§; text-decoration: underline
	}

body {
	font-family: Verdana, Arial, Helvetica, sans-serif; color:§3§;
	font-size: 12px;
	font-weight: normal;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
	line-height: 20px;
	background: §2§;
	background-image: url(§1§);
}

h1 {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: large;
	color: §3§;
	font-variant: small-caps;
}	
h2 {
	font-family: Verdana, Arial, Helvetica, sans-serif; 
	font-size: small; color: §3§; 
	}
h3 {
	font-size: small
	}
h4 {  font-family: Verdana, Arial, Helvetica, sans-serif; 
	font-size: medium; font-style: normal; font-weight: normal
	}
h6 {
	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
	font-size: 10px;
	font-weight: lighter;
}
	
table {
	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
	font-size: x-small;
	}
td {
	font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
	font-size: 12px
}

table.t1	{
	border:0px; width=100%
}

td.c1	{
	border-top:0px; width=3%; 
	border-right:0px;
	border-left:0px;
	border-bottom:0px;
}

td.c2	{
	border-top:0px; width=3%;
	border-right:2px groove black;
	border-left:0px solid black;
	border-bottom:2px groove black;
	
}

td.c3	{ 
	border-top:0px; width=3%;
	border-right:0px dashed black;
	border-left:2px groove black;
	border-bottom:2px groove black;
}

td.c4	{
	border-top:0px; width=3%;
	border-right: 1px groove black;
	border-left: 0px solid black;
	border-bottom: 0px groove black;
}

td.c5	{
	border-top:0px; width=3%;
	border-right:0px dashed black;
	border-left:1px groove black;
	border-bottom:0px groove black;
}
</style>
<?php
//	set_time_limit(0);
	
	flush();

	$affichage = @$_GET['affichage'];
	$gen = @$_GET['gen'];
	$ind = @$_GET['INDIVIDU'];
	$fam = @$_GET['FAMILY'];
	if ($affichage=="") {$affichage = "2";}
	if ($ttprenom=="") {$ttprenom = "2";}
	if ($gen=="") { $gen = "4"; }
	
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 "<table border=1><tr>";
echo "<td>".decode_lng_text('TREE_NBGEN')." : ";
if ($gen==3) {echo "<select name=gen><OPTION value='3'>3</OPTION><OPTION value='4'>4</OPTION><OPTION value='5'>5</OPTION><OPTION value='6'>6</OPTION><OPTION value='7'>7</OPTION></select></td>";}
if ($gen==4) {echo "<select name=gen><OPTION value='4'>4</OPTION><OPTION value='3'>3</OPTION><OPTION value='5'>5</OPTION><OPTION value='6'>6</OPTION><OPTION value='7'>7</OPTION></select></td>";}
if ($gen==5) {echo "<select name=gen><OPTION value='5'>5</OPTION><OPTION value='3'>3</OPTION><OPTION value='4'>4</OPTION><OPTION value='6'>6</OPTION><OPTION value='7'>7</OPTION></select></td>";}
if ($gen==6) {echo "<select name=gen><OPTION value='6'>6</OPTION><OPTION value='3'>3</OPTION><OPTION value='4'>4</OPTION><OPTION value='5'>5</OPTION><OPTION value='7'>7</OPTION></select></td>";}
if ($gen==7) {echo "<select name=gen><OPTION value='7'>7</OPTION><OPTION value='3'>3</OPTION><OPTION value='4'>4</OPTION><OPTION value='5'>5</OPTION><OPTION value='6'>6</OPTION></select></td>";}
echo "<td>".decode_lng_text('TREE_AFI')." : ";
if ($affichage==1) {echo "<select name=affichage><OPTION value='1'>".decode_lng_text('TREE_AFF1')."</OPTION><OPTION value='2'>".decode_lng_text('TREE_AFF2')."</OPTION><OPTION value='3'>".decode_lng_text('TREE_AFF3')."</OPTION></select>";}
if ($affichage==2) {echo "<select name=affichage><OPTION value='2'>".decode_lng_text('TREE_AFF2')."</OPTION><OPTION value='1'>".decode_lng_text('TREE_AFF1')."</OPTION><OPTION value='3'>".decode_lng_text('TREE_AFF3')."</OPTION></select>";}
if ($affichage==3) {echo "<select name=affichage><OPTION value='3'>".decode_lng_text('TREE_AFF3')."</OPTION><OPTION value='1'>".decode_lng_text('TREE_AFF1')."</OPTION><OPTION value='2'>".decode_lng_text('TREE_AFF2')."</OPTION></select>";}
echo "<BR>";
echo "<td><input type=submit value=".decode_lng_text('PRIVATE_ENTER')."></td>";
echo "</tr></table></form>";
echo "</center>";
echo "</div>";
	
	
	$result_ind = mysql_query( "select * from oxg_individuals where id_individual='$INDIVIDU' $sReqFam");
	$row_ind = mysql_fetch_array($result_ind);
	$id_father=$row_ind['id_father'];
	$id_mother=$row_ind['id_mother'];


//présence du père
	if($id_father!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_father' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_gfather1=$row_par['id_father'];
			$id_gmother1=$row_par['id_mother'];
		}
	}
	else
	{
		$id_gfather1=-1;
		$id_gmother1=-1;
	}


//présence de la mère
	if($id_mother!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_mother' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_gfather2=$row_par['id_father'];
			$id_gmother2=$row_par['id_mother'];
		}
	}
	else
	{
		$id_gfather2=-1;
		$id_gmother2=-1;
	}


//présence grand-parents paternels
	if($id_gfather1!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_gfather1' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_ggfather11=$row_par['id_father'];
			$id_ggmother11=$row_par['id_mother'];
		}
	}
	else
	{
		$id_ggfather11=-1;
		$id_ggmother11=-1;
	}
	if($id_gmother1!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_gmother1' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_ggfather12=$row_par['id_father'];
			$id_ggmother12=$row_par['id_mother'];
		}
	}
	else
	{
		$id_ggfather12=-1;
		$id_ggmother12=-1;
	}


//présence grand-parents maternels
	if($id_gfather2!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_gfather2' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_ggfather21=$row_par['id_father'];
			$id_ggmother21=$row_par['id_mother'];
		}
	}
	else
	{
		$id_ggfather21=-1;
		$id_ggmother21=-1;
	}
	if($id_gmother2!=-1)
	{
		$result_par = mysql_query( "select id_father, id_mother from oxg_individuals where id_individual='$id_gmother2' $sReqCont $sReqFam");
		if($row_par = mysql_fetch_array($result_par))
		{
			$id_ggfather22=$row_par['id_father'];
			$id_ggmother22=$row_par['id_mother'];
		}
	}
	else
	{
		$id_ggfather22=-1;
		$id_ggmother22=-1;
	}


//affichage des arrières grand-parents paternels
	if($id_ggfather11!=-1)
	{
		$sGGFather11=affiche_lien_individu($FAMILY, $id_ggfather11);
		$sGGFather11.="<BR>";
		$sGGFather11.=affiche_lien_arbre($FAMILY, $id_ggfather11);
	}
	else
	{
		$sGGFather11="";
	}
	if($id_ggmother11!=-1)
	{
		$sGGMother11=affiche_lien_individu($FAMILY, $id_ggmother11);
		$sGGMother11.="<BR>";
		$sGGMother11.=affiche_lien_arbre($FAMILY, $id_ggmother11);
	}
	else
	{
		$sGGMother11="";
	}
	if($id_ggfather12!=-1)
	{
		$sGGFather12=affiche_lien_individu($FAMILY, $id_ggfather12);
		$sGGFather12.="<BR>";
		$sGGFather12.=affiche_lien_arbre($FAMILY, $id_ggfather12);
	}
	else
	{
		$sGGFather12="";
	}
	if($id_ggmother12!=-1)
	{
		$sGGMother12=affiche_lien_individu($FAMILY, $id_ggmother12);
		$sGGMother12.="<BR>";
		$sGGMother12.=affiche_lien_arbre($FAMILY, $id_ggmother12);
	}
	else
	{
		$sGGMother12="";
	}


//affichage des arrières grand-parents paternels
	if($id_ggfather21!=-1)
	{
		$sGGFather21=affiche_lien_individu($FAMILY, $id_ggfather21);
		$sGGFather21.="<BR>";
		$sGGFather21.=affiche_lien_arbre($FAMILY, $id_ggfather21);
	}
	else
	{
		$sGGFather21="";
	}
	if($id_ggmother21!=-1)
	{
		$sGGMother21=affiche_lien_individu($FAMILY, $id_ggmother21);
		$sGGMother21.="<BR>";
		$sGGMother21.=affiche_lien_arbre($FAMILY, $id_ggmother21);
	}
	else
	{
		$sGGMother21="";
	}
	if($id_ggfather22!=-1)
	{
		$sGGFather22=affiche_lien_individu($FAMILY, $id_ggfather22);
		$sGGFather22.="<BR>";
		$sGGFather22.=affiche_lien_arbre($FAMILY, $id_ggfather22);
	}
	else
	{
		$sGGFather22="";
	}
	if($id_ggmother22!=-1)
	{
		$sGGMother22=affiche_lien_individu($FAMILY, $id_ggmother22);
		$sGGMother22.="<BR>";
		$sGGMother22.=affiche_lien_arbre($FAMILY, $id_ggmother22);
	}
	else
	{
		$sGGMother22="";
	}


//affichage des grand-parents
	if($id_gfather1!=-1)
	{
		$sGFather1=affiche_lien_individu($FAMILY, $id_gfather1);
		$sGFather1.="<BR>";
		$sGFather1.=affiche_lien_arbre($FAMILY, $id_gfather1);
	}
	else
	{
		$sGFather1="";
	}
	if($id_gmother1!=-1)
	{
		$sGMother1=affiche_lien_individu($FAMILY, $id_gmother1);
		$sGMother1.="<BR>";
		$sGMother1.=affiche_lien_arbre($FAMILY, $id_gmother1);
	}
	else
	{
		$sGMother1="";
	}
	if($id_gfather2!=-1)
	{
		$sGFather2=affiche_lien_individu($FAMILY, $id_gfather2);
		$sGFather2.="<BR>";
		$sGFather2.=affiche_lien_arbre($FAMILY, $id_gfather2);
	}
	else
	{
		$sGFather2="";
	}
	if($id_gmother2!=-1)
	{
		$sGMother2=affiche_lien_individu($FAMILY, $id_gmother2);
		$sGMother2.="<BR>";
		$sGMother2.=affiche_lien_arbre($FAMILY, $id_gmother2);
	}
	else
	{
		$sGMother2="";
	}


//affichage des parents
	if($id_father!=-1)
	{
		$sFather = affiche_lien_individu($FAMILY, $id_father);
		$sFather.= "<BR>";
		$sFather.= affiche_lien_arbre($FAMILY, $id_father);
	}
	else
	{
		$sFather="";
	}
	if($id_mother!=-1)
	{
		$sMother = affiche_lien_individu($FAMILY, $id_mother);
		$sMother.= "<BR>";
		$sMother.= affiche_lien_arbre($FAMILY, $id_mother);
	}
	else
	{
		$sMother="";
	}

/****************************
 gen : nombre de générations
 nl : nombre de lignes
 nc : nombre de colonnes
 lg : ligne en cours
 cl : colonne en cours
****************************/
$color="black";
$nc=pow(2,$gen-1)*8-2;
$nl=$gen;
$epaisseur=2;
$sTailleBloc = 16;
$w1 = $sTailleBloc;
$w2 = 2*$w1;
$w6 = 6*$w1;
$w8 = 8*$w1;


// initialisation du tableau des ascendants
for ($lg=1;$lg<=$nl;$lg++)
	{
	for ($cl=1;$cl<=$nc;$cl++)
		{
		$tb[$lg][$cl]=0;
		}
	}

$niv=$nl-1;
echo $nc;
echo count($tb);
// calcul du tableau des ascendants
for ($lg=1;$lg<=$nl;$lg++)
	{
	$pos=1;
	$sosa1=$SosaCourant*pow(2,$niv);
	$sosa2=($SosaCourant+1)*pow(2,$niv);

	for ($ns=$sosa1;$ns<$sosa2;$ns++)
		{
		if (sosa_equivalent($ns))
			{
			$res=calculer_position($tb,$lg,$nc,$ns);

			if ($res==0) decaler_position($tb,$lg,$nc,$ns,$pos);
			else $pos=$res;

			$tb[$lg][$pos]=$ns;
			$pos=$pos+8;
			}
		}

	$niv=$niv-1;
	}

	
	
// recalcul des limites du tableau des ascendants
$pos=0;
$ld=1;

for ($l=1;$l<=$nl;$l++)
	{
	if ($pos==0) $ld=$l;
	
	for ($c=$nc;$c>0;$c--)
		{
		if ($tb[$l][$c])
			{
			if ($pos<($c+5)) $pos=$c+5;
			$c=0;
			}
		}
	}
	
$nc=$pos;	

?>

<br>

<!-- affichage de l'arbre -->
<table align="center" border="0" cellpadding="0" cellspacing="0">

<?php
	// ligne vide de cadrage
	echo "<!-- Cadrillage de $nc cellule(s) -->";
   echo chr(13);
   echo chr(10);

	echo "<tr>";
   echo chr(13);
   echo chr(10);
   
   for ($cl=1;$cl<=$nc;$cl++)
   	{
		echo "<td width=$w1>";
		echo "<img src=\"pixel.gif\" border=\"0\" width=\"$w1\" height=\"2\">";
 		echo "</td>";
	   echo chr(13);
	   echo chr(10);
   	}

	echo "</tr>";
   echo chr(13);
   echo chr(13);
   echo chr(10);

	// boucle sur les générations
	for ($lg=$ld;$lg<=$nl;$lg++)
		{
	   $esp=0;  

		$gen=$nl-$lg;
		$sosa1=$SosaCourant*pow(2,$gen);
		$sosa2=($SosaCourant+1)*pow(2,$gen)-1;
		$gen++;

		echo "<!-- Génération: $gen - Sosa de $sosa1 à $sosa2 -->";
	   echo chr(13);
	   echo chr(10);
		
		echo "<tr>";
	   echo chr(13);
	   echo chr(10);

		// ligne d'ascendants
		for ($cl=1;$cl<=$nc;$cl++)
			{
			$v=$tb[$lg][$cl];

			if ($v!=0)
				{
				dessiner_trait($esp,"pixel.gif","");
				echo "<td colspan=\"6\" width=\"$w6\">";
			   echo chr(13);
			   echo chr(10);

		   	echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" bordercolor=\"$color\" bordercolordark=\"$color\" bordercolorlight=\"$color\">";
			   echo chr(13);
			   echo chr(10);

				echo "<tr><td class=\"arbre\" width=\"$w6\">";
				$fiche = chercher_individu($v);
						
				if ($fiche)
					{
					$titre="";
					$date=format_date($fiche->datenaissance);
         		if ($date<>"" || $fiche->lieunaissance<>"") $titre="$titre ° $date $fiche->lieunaissance ";
         		$date=format_date($fiche->datedeces);
         		
         		if ($date<>"" || $fiche->lieudeces<>"")
         			{
         			if (strlen($titre)>0) $titre="$titre\n";
         			$titre="$titre + $date $fiche->lieudeces ";
         			}
						
   				echo "<a href=\"$url2?lng=$lng&pm=$v.$ng\" title=\"$titre\"> Sosa: $v</a> <br>";
   				$prenom=$fiche->prenom;
   				$pos=strpos($prenom," ");
   				if ($pos<>0) $prenom=substr($prenom,0,$pos);
   				echo "$prenom <br>";
   				echo "$fiche->nom";
   				}

				echo "</td></tr>";
			   echo chr(13);
			   echo chr(10);

				echo "</table>";
			   echo chr(13);
			   echo chr(10);
			   
				echo "</td>";
			   echo chr(13);
			   echo chr(10);
			   
				$cl+=5;
				}
			else $esp++;
			}

		dessiner_trait($esp,"pixel.gif","");
		
		echo "</tr>";
	   echo chr(13);
	   echo chr(13);
	   echo chr(10);

		// Dessin des traits entre deux lignes d'ascendants
		if ($lg<$nl)
			{
			/***************************************
			* tth : tableau de traits hauts
			* ttb : tableau de traits bas
			* sh  : sosa haut
			* sb  : sosa bas
			***************************************/
			// initialisation des tableaux de traits
			for ($cl=1;$cl<=$nc;$cl++) 
				{
				$tth[$cl]=0;
				$ttb[$cl]=0;
				}

			// calcul des tableaux de traits
			for ($cl=1;$cl<=$nc;$cl++)
				{
				$sh=$tb[$lg][$cl];
				$sb=$tb[$lg+1][$cl];			

				// Trait supérieur (père ou mère)
				$tth[$cl+2]=$sh;
				
				// Trait inférieur (enfant)
				if ($sh==0 || $sh==pere($sb) || $sh==mere($sb)) $ttb[$cl+2]=$sb;
				}

		   // première ligne de traits verticaux (haut)
		   $esp=0;

			echo "<!-- Traits verticaux -->";
		   echo chr(13);
		   echo chr(10);

			echo "<tr>";
   		echo chr(13);
   		echo chr(10);

			for ($cl=1;$cl<=$nc;$cl++)
				{
				if ($tth[$cl]!=0)
					{
					dessiner_trait($esp, "pixel.gif", "");
	   			echo "<td align=\"right\" valign=\"top\" width=\"$w1\">";
					echo "<img src=\"blue.gif\" border=\"0\" width=\"$epaisseur\" height=\"20\">";
	   			echo "</td>";
	   			echo chr(13);
		   		echo chr(10);
					}
				else $esp++;
				}

			dessiner_trait($esp,"pixel.gif","");
			
			echo "</tr>";
		   echo chr(13);
   		echo chr(13);
   		echo chr(10);

			// ligne de traits horizontaux (intermédiaire)
			$ft=0;
			$esp=0;

			echo "<!-- Traits horizontaux -->";
		   echo chr(13);
		   echo chr(10);

			echo "<tr>";
   		echo chr(13);
   		echo chr(10);

			for ($cl=1;$cl<=$nc;$cl++)
				{
				$sh=$tth[$cl];
				$sb=$ttb[$cl];			

				if ($sh!=0)
					{
					$union=chercher_union($sh);
					$alt="";

		         if ($union)
		            {
		            $date=format_date($union->dateunion);
		            $alt=" x $date $union->lieuunion ";
		            }
											
					if ($ft==0)
						{
						// Début de trait
						dessiner_trait($esp,"pixel.gif","");
	   				echo "<td align=\"right\" width=\"$w1\">";
						echo "<img src=\"blue.gif\" alt=\"$alt\" border=\"0\" width=\"$epaisseur\" height=\"$epaisseur\">";
	   				echo "</td>";	   					   				
						}
					else
						{
						// Fin de trait
						dessiner_trait($esp,"blue.gif","$alt");
	   				echo "<td width=\"$w1\">";
						echo "<img src=\"blue.gif\" alt=\"$alt\" border=\"0\" width=\"$w1\" height=\"$epaisseur\">";
	   				echo "</td>";
						}
	
		   		echo chr(13);
   				echo chr(10);

	   			if ($sb==0 || ($sh!=0 && $sh!=pere($sb) && $sh!=mere($sb))) $ft=1-$ft;
					}
				else $esp++;
				}

			dessiner_trait($esp,"pixel.gif","");
			
			echo "</tr>";
   		echo chr(13);
   		echo chr(13);
   		echo chr(10);

			// dernière ligne de traits verticaux (bas)
		   $esp=0;
		   $ft1=0;
		   $ft2=0;

			echo "<!-- Traits verticaux -->";
		   echo chr(13);
		   echo chr(10);
		   
			echo "<tr>";
   		echo chr(13);
   		echo chr(10);

			for ($cl=1;$cl<=$nc;$cl++)
				{
				$sh=$tth[$cl];
				$sb=$ttb[$cl];			

				if ($sh!=0)
					{
	   			if ($sb==0 || ($sh!=pere($sb) && $sh!=mere($sb))) $ft1=1-$ft1;
	   			if ($ft1!=0) $ft2=1;
	   			}
				  														
				if ($sb!=0 && ($ft2!=0 || $sh==pere($sb) || $sh==mere($sb)))
					{
					$ft2=0;
					dessiner_trait($esp,"pixel.gif","");
	   			echo "<td align=\"right\" valign=\"top\" width=\"$w1\">";
					echo "<img src=\"blue.gif\" border=\"0\" width=\"$epaisseur\" height=\"20\">";
	   			echo "</td>";
	   			echo chr(13);
		   		echo chr(10);		   		
					}
				else $esp++;
				}

			dessiner_trait($esp,"pixel.gif","");
			
			echo "</tr>";
   		echo chr(13);
   		echo chr(13);
   		echo chr(10);
			}
		}
?>
</table>

<?	



	affiche_pied_page(true);
?>