<?php

function update_sosa($id_ind,$id_sosa)
{
	global $sReqFam;
	$sql = mysql_query("select id_father, id_mother from oxg_individuals where id_individual=$id_ind $sReqFam");
	while ($people = mysql_fetch_array($sql))
		{
		$sosa_father = ($id_sosa*2);
		$sosa_mother = ($id_sosa*2)+1;
		$id_father = $people[id_father];
		$id_mother = $people[id_mother];
		if ($id_father!=-1) $result = mysql_query( "UPDATE oxg_individuals SET sosa=$sosa_father WHERE id_individual=$id_father and sosa=-1 $sReqFam");
		if ($id_mother!=-1) $result = mysql_query( "UPDATE oxg_individuals SET sosa=$sosa_mother WHERE id_individual=$id_mother and sosa=-1 $sReqFam");
		if ($id_father!=-1) update_sosa ($id_father, $sosa_father);
		if ($id_mother!=-1) update_sosa ($id_mother, $sosa_mother);
		}
}

function affiche_lien_individu_bis($id_family, $id_individual, $bAffichePatronyme=true, $bAfficheDate=false)
{
	global	$bSosa, $bSessionOk;

	if($id_family!=-1)
	{
		$sReqFam="and id_family=$id_family";
	}
	else
	{
		$sReqFam="";
	}
	if($bSessionOk==false)
	{
		$sReqCont=" and contemporary=1 ";
	}
	else
	{
		$sReqCont="";
		//$sReqCont=" and contemporary=1 ";
	}
	
	$sInd="";
	
	$sReq  = "select first_name, libelle ";
	if($bSosa==true)
	{
		$sReq  .= ", sosa ";
	}
	$sReq .= ", date_naissance, date_deces, contemporary ";
	$sReq .= " 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));
	{
		$sInd.="<A HREF='§detail_individu.php?INDIVIDU=$id_individual&FAMILY=$id_family'>";
		if($row['first_name']=="")
		{
			$sInd.="?";
		}
		else
		{
			$sInd.=$row['first_name'];
		}
		if($bAffichePatronyme==true)
		{
			$sInd.=" $row[libelle]";
		}
		$sInd.="</A>";
		
		if($bAfficheDate==true && ($row['date_naissance']!="" || $row['date_deces']!=""))
		{
			$sInd.=" (";
			if($row['date_naissance']!="")
			{
				$sInd.="$row[date_naissance]";
			}
			else
			{
				$sInd.="?";
			}
			if($row['date_deces']!="")
			{
				$sInd.=" - ";
				$sInd.="$row[date_deces]";
			}
			$sInd.=")";
		}
	}
	if ($row['first_name']=="" AND $row['libelle']=="")
	{
		return "?";
	}
	else
	{	
		if ($bSessionOk==false and $row['contemporary']==0)
		{return decode_lng_text('PRIVATE_MSG');}
		else
		{return $sInd;}
	}
}  //fin  function affiche_lien_individu_bis

	$sPage="sosa";
	require("admin/§lib_inc.php");
	require("admin/§lib_inc2.php");
	$sTitle=decode_lng_text("SOSA_TITLE");

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1; 
	$name = $_GET['name'];
	
	require("§menu.php");
	require("§progress_bar.php");

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

	if(!isset($_GET['limite']))
	{
		$limite = 0;
	}
	else
	{
		$limite = $_GET['limite'];
	}

	
if ($FAMILY=="-1")
	{
	echo "<br>".decode_lng_text('SOSA_SELECT')."<br>";
	}
else
	{
	
if (isset($_SESSION['privilege']) and $_SESSION['privilege']=="admin")
	{
	echo "<div id=panel>";
	echo "<form action=\"\" method=get>";
	echo "<input type=hidden name=FAMILY value=\"$FAMILY\">";
	echo "<center>";
	echo "<table border=1><tr>";
	echo "<td>".decode_lng_text('SOSA_MODIFY');

	$link = $_SERVER["SCRIPT_NAME"];
	$link = $link."?FAMILY=".$FAMILY;

	//Liste des patronymes
	echo "<select name=name onChange='window.location.href=this.value'>";
	if ($name!=0)
		{
		$request = "select libelle from oxg_patronyms where id_patronym=$name";
		$sql = mysql_query($request);
		$patro = mysql_fetch_array($sql);
		echo "<OPTION value=".$link."&name=".$patro[id_patronym].">".$patro[libelle]."</OPTION>";
		}
	echo "<OPTION value=".$link."&name=0>".decode_lng_text('SOSA_PATRO')."</OPTION>";
	$request = "select * from oxg_patronyms, oxg_individuals";
	$request = $request ." where oxg_patronyms.id_patronym=oxg_individuals.id_patronym";
	$request = $request ." and libelle<>'X' and libelle<>'?'";
	$request = $request ." and id_father<>'-1' and id_mother<>'-1'";
	$request = $request ." $sReqFam";
	$request = $request ." GROUP by libelle";
	$sql = mysql_query($request);
	while ($patro = mysql_fetch_array($sql))
		{
		echo "<OPTION value=".$link."&name=".$patro[id_patronym].">".$patro[libelle]."</OPTION>";
		}
	echo "</select>";

	//Liste des personnes
	echo "<select name=sosa onChange='window.location.href=this.value'>";
	echo "<OPTION value=".$link."&sosa=0>".decode_lng_text('SOSA_PERSON')."</OPTION>";
	//echo "<OPTION value='-1'>supprimer tous les numéro sosa</OPTION>";
	$request = "select * from oxg_individuals, oxg_patronyms";
	$request = $request ." where 1=1 and oxg_patronyms.id_patronym=oxg_individuals.id_patronym";
	$request = $request ." and oxg_individuals.id_patronym=$name";
	$request = $request ." $sReqFam $sReqCont ORDER by libelle, first_name";
	$sql = mysql_query($request);
	while ($ind = mysql_fetch_array($sql))
		{
		echo "<OPTION value=".$link."&sosa=".$ind[id_individual].">".affiche_lien_individu_bis($FAMILY, $ind[id_individual],true,true)."</OPTION>";
		}
	echo "</select></td></tr>";
	echo "<td><A HREF='".$link."&sosa=-1'>".decode_lng_text('SOSA_DELETE')."</a></td>";
	echo "</table></form>";
	echo "</center>";
	echo "</div>";
	}


	//Recherche de la présence des numéro sosa
	$sql = mysql_query("select * from oxg_individuals, oxg_patronyms where sosa=1 and oxg_patronyms.id_patronym=oxg_individuals.id_patronym $sReqFam");
	if ($row = mysql_fetch_array($sql))
		{
		echo "<br>".decode_lng_text('SOSA_MAIN')."<br>";
		}
	else
		{
		echo "<br>".decode_lng_text('SOSA_NOSOSA')."<br>";
		}
	
$sosa = @$_GET['sosa'];
if ($sosa!=0 and $sosa!=$row['id_individual'])
{
	//mise blanc des numéro sosa existant
	$result = mysql_query( "UPDATE oxg_individuals SET sosa='-1' WHERE id_family=$FAMILY");
	if ($sosa!=-1)
	{
		//création des nouveaux numéro sosa
		$result = mysql_query( "UPDATE oxg_individuals SET sosa=1 WHERE id_family=$FAMILY and id_individual=$sosa ");
		update_sosa ($sosa, 1);
	}
}

	//Affichage des ascendants
	$max_results = 35;	// numbre of pages
	$limite_param=$limite;
	$limite=intval($limite*$max_results);
	$from = (( ($limite_param+1) * $max_results) - $max_results);
	$sql = mysql_query("select sosa as id_sosa, oxg_individuals.id_individual as id_individual, first_name, oxg_patronyms.libelle, oxg_individuals.date_naissance as naidat, oxg_individuals.date_deces as deces, sex, contemporary from oxg_individuals, oxg_patronyms where oxg_individuals.sosa>=1 and oxg_patronyms.id_patronym=oxg_individuals.id_patronym $sReqFam ORDER BY sosa LIMIT $from, $max_results");
	mysql_query("SET OPTION SQL_BIG_SELECTS=1") or die(": FAILED");
	$max=0;
	$OldGene=0;
	$generation=0;
	while($row = mysql_fetch_array($sql))
	{
		$temp=$row["id_sosa"];
		if($max<=$temp)
		{
			$generation=1;
			while($temp>1)
			{
				$temp = $temp - ($temp%2);
				$generation++;
				$temp = $temp/2;
			}
			$max = pow(2,$generation);
			echo "<br>";
	 		echo "<b><i>";
			display_lng_text("SOSA_GENERATION");
	 		echo " ".$generation."</b></i> :<br>";
	 		echo "<b><i>________________</b></i><br>";
	 		echo "<br>";
		}
		// Build your formatted results here.
		if ($bSessionOk==false and $row['contemporary']==0)
			{
			echo "$row[id_sosa]&nbsp;";
			echo decode_lng_text('PRIVATE_MSG');
			}
		else
			{
			echo "<a href=§detail_individu.php?INDIVIDU=$row[id_individual]&FAMILY=$FAMILY>$row[id_sosa]</a>&nbsp;";
			echo "$row[first_name]&nbsp;$row[libelle]";
			if($row["naidat"]<>"")
				{
				echo " ";
				if($row["sex"]==1)
					{
					display_lng_text("SOSA_BIRTH_F");
					}
				else
					{
					display_lng_text("SOSA_BIRTH_M");
					}
				echo " <b>$row[naidat]</b>";
				}
			if($row["deces"]<>"")
				{
				echo " ";
				if($row["sex"]==1)
					{
					display_lng_text("SOSA_DEATH_F");
					}
				else
					{
					display_lng_text("SOSA_DEATH_M");
					}
				echo " <b>$row[deces]</b>";
				}
			}
		echo "<br>";
	}
	
	echo "<p>&nbsp;</p><center>";
	// Figure out the total number of results in DB:
	$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM oxg_individuals WHERE oxg_individuals.sosa >=1 $sReqFam"),0);
	$total_pages = ceil($total_results / $max_results);
	// on recupere la page + les parametres
	$lien = $_SERVER['REQUEST_URI'];
	// on retire le parametre limit
	$pos = strpos($lien,'&limite=');
	if ($pos > 0) { $lien = substr($lien,0,$pos); }
	$nb_aff_page = 9;
	aff_numeros_pages($max_results,$nb_aff_page,$total_results,$lien,$limite_param);
	//aff_numeros_pages($nb_res_par_page,$nb_aff_page,$nb_res_max,$lien,$limite_param);
	echo "</center>";
	}

	affiche_pied_page(true);
?>

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