<?php
/*
* fonctions de recherche des liens sur les individus
*/

function affiche_lien_individu($id_family, $id_individual, $bAffichePatronyme=true, $bAfficheDate=false, $bAfficheSurnom=false)
{
	// fonction transférée de lib_inc.php (ce programme est le lieu d'utilisation unique) 
//echo 'on est dans affiche... - family '.$id_family.' indiv '. $id_individual.'<br/>';
	$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));
*/

		$table = 'oxg_individuals, oxg_patronyms';
		$this->Accueil->table = $table;
		$condition  = 'id_individual='.$id_individual;
		$condition .= ' and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ';
		$condition .= $_SESSION['s_ReqFamille'];
		$field= 'first_name, libelle';
		if($_SESSION['b_Sosa']==true) {
			$field .= ', sosa';
		}
		$field .= ', date_naissance, date_deces, contemporary ';
		$result = $this->Accueil->find(array(
			'fields'     => $field,
			'conditions'  => $condition
		));	
//var_dump($row);
	foreach($result as $k => $row) {

		$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;
		}
		if($bAfficheSurnom==true)
		{
			$sInd.=$this->display_nickname($id_individual, $_SESSION['s_ReqFamille'], "GIVN", "NICK", $row->sex);
		}
		$sInd.="</a>";
		if($_SESSION['b_Sosa']==true && $row->sosa!='-1')
		{
			$sInd.=" <strong><font color=red>$row->sosa</font></strong>";
		}
		
		if($bAfficheDate==true && ($row->date_naissance!="" || $row->date_deces!=""))
		{
			$sInd.=" (";
			if($row->date_naissance!="")
			{
				$sInd.=decode_date_eng($row->date_naissance);
			}
			else
			{
				$sInd.="?";
			}
			if($row->date_deces!="")
			{
				$sInd.=" - ";
				$sInd.=decode_date_eng($row->date_deces);
			}
			$sInd.=")";
		}
	}
	if ($row->first_name=="" AND $row->libelle=="")
	{
		return "?";
	}
	else
	{	
		if (($_SESSION['b_connecte']==false || $bSessionOk=="") and $row->contemporary==0)
		{
			return decode_lng_text('PRIVATE_MSG');
		}
		else
		{
			return $sInd;
		}
	}
}

function display_nickname($id_individual, $sReqFam, $sParentEvntCode, $sEvntCode, $sSex)
{
	// fonction transférée de lib_inc.php (ce programme est le lieu d'utilisation unique) 
	$sResult="";
	$sNickname=$this->get_event($id_individual, $sReqFam, "GIVN", "NICK");
	if($sNickname!="")
	{
		if($sSex==1)	// Female
		{
			$sResult=" dite ".$sNickname;
		}
		else
		{
			$sResult=" dit ".$sNickname;
		}
	}
	return $sResult;
}

function get_event($id_individual, $sReqFam, $sParentEvntCode, $sEvntCode)
{
		// fonction transférée de lib_inc.php (ce programme est le lieu d'utilisation unique) 
	global $sql_query;
/*
	$sql_query  = "select id_type_event from oxg_events_type where code='$sParentEvntCode'";
	$result_ind = mysql_query( $sql_query ) or mysql_die();
	$row_ind = mysql_fetch_array($result_ind);
*/
	
	$table = 'oxg_events_type';
	$this->Accueil->table = $table;
	$condition = 'code='. $sParentEvntCode;
	$result_ind=$this->Accueil->find(array(
			'fields'     => 'id_type_event',
			'conditions'  => $condition
	));
	
	$sId_type_event=$result_ind->id_type_event;
	if($sId_type_event=="")
	{
		return "";
	}
	
	// $sql_query  = "select id_event from oxg_events where type_father=0 and type=$sId_type_event and id_father=$id_individual $sReqFam";
	// $result_ind = mysql_query( $sql_query ) or mysql_die();
	// $row_ind = mysql_fetch_array($result_ind);
	
	$table = 'oxg_events';
	$this->Accueil->table = $table;
	$condition  = 'type_father=0 and type= '.$sId_type_event.' and id_father= '.$id_individual;
	$condition .= $sReqFam;
	$result_ind=$this->Accueil->find(array(
			'fields'     => 'id_event',
			'conditions'  => $condition
	));
	
	$sId_event=$result_ind->id_event;
	if($sId_event=="")
	{
		return "";
	}
	
	// $sql_query  = "select id_type_event from oxg_events_type where code='$sEvntCode'";
	// $result_ind = mysql_query( $sql_query ) or mysql_die();
	// $row_ind = mysql_fetch_array($result_ind);
		
	$table = 'oxg_events_type';
	$this->Accueil->table = $table;
	$condition = 'code='. $sEvntCode;
	$result_ind=$this->Accueil->find(array(
			'fields'     => 'id_type_event',
			'conditions'  => $condition
	));


	$sId_type_event=$result_ind->id_type_event;
	if($sId_type_event=="")
	{
		return "";
	}
	
	// $sql_query  = "select libelle from oxg_events where type_father=1 and type=$sId_type_event and id_father=$sId_event $sReqFam";
	// $result_ind = mysql_query( $sql_query ) or mysql_die();
	$row_ind = mysql_fetch_array($result_ind);
			
	$table = 'oxg_events_type';
	$this->Accueil->table = $table;
	$condition = 'type_father=1 and type= '.$sId_type_event.' and id_father= '.$sId_event;
	$condition .= $sReqFam;
	$result_ind=$this->Accueil->find(array(
			'fields'     => 'libelle',
			'conditions'  => $condition
	));

	$sLibelle=$result_ind->libelle;
	
	return $sLibelle;
}

function donne_ind_evnt($id_family, $id_evnt) {	
//echo 'on y est - people event '.$id_evnt.'<br/>';
	$table = 'oxg_events';
	$this->Accueil->table = $table;	
	// fonction transférée de lib_inc.php (ce programme est le lieu d'utilisation unique) 
	$condition  = ' id_event= '.$id_evnt;
	$condition .= $_SESSION['s_ReqFamille'];

	$row_ind = $this->Accueil->findFirst(array(
			'fields'     => 'id_father, type_father',
			'conditions'  => $condition
	));
	//var_dump($row_ind);
//	echo $id_evnt.': '.$row_ind->id_father.' /'.$row_ind->type_father.'<br/>';

	if($row_ind->type_father==0) 
	{
		$sRes=$row_ind->id_father;
	}
	else
	{
		$sRes = $this->donne_ind_evnt($id_family, $row_ind->id_father);
	}
//	echo 'sIndividual '.$sRes.'<br/>';
	return $sRes;
	
}
?>