<?php
class IndividusController extends Controller{

	public $sId_Event_Type_Marb;
	public $sId_Event_Type_Marc;
	public $sId_Event_Type_Mar;
	public $sId_Event_Type_Div;		
	public $sId_Event_Type_Union;		
	public $sId_Event_Type_Date;		
	public $sId_Event_Type_Birth;
	public $sId_Event_Type_Baptism;		
	public $sId_Event_Type_Death;		
	public $sId_Event_Type_Burial;	
	
function detail_individu() {


		$_SESSION['s_page'] = "individus";
		$_SESSION['s_vue']  = 'detail_individu';


		$sTitle = decode_lng_text("IDX_DATES_TITLE");
		
		$INDIVIDU  = isset($this->request->params[0])?$this->request->params[0] : 0; 

	// initialisation dutableau des données générales de l'individu principal		
		$commun= array();

		
		$this->loadModel('Outils');
		$this->loadModel('Individus');
		
//$_SESSION['ind_affiche']=1;	
		$table = 'oxg_patronyms, oxg_individuals';
		$this->Individus->table = $table;
		
		$condition  = "oxg_individuals.id_individual='".$INDIVIDU."' and oxg_patronyms.id_patronym=oxg_individuals.id_patronym";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_ind=$this->Individus->find(array(
				'fields'     => '*, oxg_patronyms.libelle as libelle',
				'conditions'  => $condition
		));
		
		foreach($result_ind as $row_ind) {
			
			substr($row_ind->date_naissance,-4);

			$sNickname=$this->Outils->get_event($INDIVIDU, $_SESSION['s_ReqFamille'], "GIVN", "NICK");

			$sTitle = decode_lng_text('IND_FICHE')." ".$row_ind->first_name." ".$row_ind->libelle;
			$commun['individu']   = $INDIVIDU;
			$commun['name']       =  $row_ind->libelle;
			$commun['first_name'] = $row_ind->first_name;
			$commun['libelle']    = $row_ind->libelle;
			$commun['father']	  = $row_ind->id_father;
			$commun['mother']	  = $row_ind->id_mother;
			$commun['year1'] 	  = substr($row_ind->date_naissance,-4);
			$commun['titre']      = self::load_title($INDIVIDU);		
			$commun['prof']       = self::load_prof($INDIVIDU);
			$event 		          = self::load_common_events($INDIVIDU);
		} // fin foreach recherche infos communes de l'individu principal
	
	if($sNickname!="")
	{
		if($row_ind->sex==1)	// Female
		{
			$sTitle.=" dite ".$sNickname;
			$sNickname_plus = " dit ".$sNickname;
		}
		else
		{
			$sTitle.=" dit ".$sNickname;
			$sNickname_plus = " dit ".$sNickname;
		}
		
		$commun['nickname']= $sNickname_plus;

	}
	$sTitle = decode_lng_text('IND_FICHE')." ".$row_ind->first_name." ".$row_ind->libelle." ".$sNickname_plus;
	
	if(isset($row_ind->sosa) && $row_ind->sosa!=-1)
	{
		$sTitle.=" <font color=red>".$row_ind->sosa."</font>";
	}

	$_SESSION['s_Title'] = $sTitle;
	
	$sId_Event_Type_Marb    = $this->Outils->get_id_event_type("MARB");
	$sId_Event_Type_Div     = $this->Outils->get_id_event_type("DIV");
	$sId_Event_Type_Mar     = $this->Outils->get_id_event_type("MARR");
	$sId_Event_Type_Union   = $this->Outils->get_id_event_type("UNION");
	$sId_Event_Type_Date    = $this->Outils->get_id_event_type("DATE");
	$sId_Event_Type_Birth   = $this->Outils->get_id_event_type("BIRT");
	$sId_Event_Type_Baptism = $this->Outils->get_id_event_type("CHR"); // Heredis utilise CHR pour désigner les baptemes en général
	$sId_Event_Type_Death   = $this->Outils->get_id_event_type("DEAT");
	$sId_Event_Type_Burial  = $this->Outils->get_id_event_type("BURI");
	$_SESSION['event_type'] = array('Marb'=>$sId_Event_Type_Marb, 'Marc'=>$sId_Event_Type_Marc,'Div'=>$sId_Event_Type_Div,'Mar'=>$sId_Event_Type_Mar,
									'Union'=>$sId_Event_Type_Union, 'Date'=>$sId_Event_Type_Date, 'Birth'=>$sId_Event_Type_Birth, 
									'Bapm'=>$sId_Event_Type_Baptism, 'Deat'=>$sId_Event_Type_Death, 'Buri'=>$sId_Event_Type_Burial);

//Vérification si accès zone privée
//$_SESSION['ind_affiche']=1;		
	$_SESSION['infos_confidentielles'] = 0;
	$table = 'oxg_individuals';
	$this->Individus->table = $table;
	
	$condition  = "id_individual='".$INDIVIDU."'";
	$condition .= $_SESSION['s_ReqFamille'];
	
//$condition .= $_SESSION['s_ReqContemporain'];

	
	$result_ind=$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
	));
	
//	if (!$result_ind) { $_SESSION['infos_confidentielles'] = true ; return true; }
	if ($result_ind[0]->contemporary==0 and $_SESSION['b_connecte'] != true) { 
		$_SESSION['infos_confidentielles'] = true ; 
		return true;
	}	
	
	foreach($result_ind as $row_ind) {

	//Affichage des liens vers l'arbre vertical et horizontal
	$commun['lien-arbres'] = $this->Outils->affiche_lien_arbre($INDIVIDU);
	
	// vérification si descendance 
	
	// $req_ = "select count(*) as SOMME from oxg_individuals where (id_father='$INDIVIDU' or id_mother='$INDIVIDU') $sReqFam";
	// $result_ = mysql_query( $req_ ) or mysql_die();
	// $row_ = mysql_fetch_array($result_);
//$_SESSION['ind_affiche']=1;		
	$table = 'oxg_individuals';
	$this->Individus->table = $table;
	
	$condition  = " (id_father ='".$INDIVIDU."' or id_mother ='".$INDIVIDU."')";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result_ind=$this->Individus->find(array(
			'fields'     => 'count(*) as SOMME',
			'conditions'  => $condition
	));
	foreach($result_ind as $row_ind) {
		$commun['descendance']	= $row_ind->SOMME != 0 ? 1 : 0;
		
	}

	// Display birth
	if (isset($event[$sId_Event_Type_Birth]['id_event'])==true)
	{
		$note = self::load_note($event[$sId_Event_Type_Birth]['id_event']);
		$event[$sId_Event_Type_Birth]['note']=$note;
		$country = $event[$sId_Event_Type_Birth]['id_country'];
		$zone = $event[$sId_Event_Type_Birth]['id_zone'];
		$city = $event[$sId_Event_Type_Birth]['id_city'];
		if(isset($event[$sId_Event_Type_Birth]['place']))
		{
			$place = self::decode_place($event[$sId_Event_Type_Birth]['place']);
		}
		else
		{
			$place="";
		}
		if($place=="") {
			$event[$sId_Event_Type_Birth]['place']=self::affiche_lieu($country,$zone,$city,$place);
		}
	}

		// Display Baptism
	if (isset($event[$sId_Event_Type_Baptism]['id_event']))
	{
		$note = self::load_note($event[$sId_Event_Type_Baptism]['id_event']);
		echo $note;
		$event[$sId_Event_Type_Baptism]['note']=$note;
		$country = $event[$sId_Event_Type_Baptism]['id_country'];
		$zone = $event[$sId_Event_Type_Baptism]['id_zone'];
		$city = $event[$sId_Event_Type_Baptism]['id_city'];
		if(isset($event[$sId_Event_Type_Baptism]['place']))
		{
			$place = self::decode_place($event[$sId_Event_Type_Baptism]['place']);
		}
		else
		{
			$place="";
		}
		if($place=="") {
			$event[$sId_Event_Type_Baptism]['place']=self::affiche_lieu($country,$zone,$city,$place);
		}
	}
	// Display death
	if (isset($event[$sId_Event_Type_Death]['id_event']))
	{
		
		$note = self::load_note($event[$sId_Event_Type_Death]['id_event']);
		$event[$sId_Event_Type_Death]['note']=$note;
		$country = $event[$sId_Event_Type_Death]['id_country'];
		$zone = $event[$sId_Event_Type_Death]['id_zone'];
		$city = $event[$sId_Event_Type_Death]['id_city'];
		if(isset($event[$sId_Event_Type_Death]['place']))
		{
			$place = self::decode_place($event[$sId_Event_Type_Death]['place']);
		}
		else
		{
			$place="";
		}
		if($place=="") {
			$event[$sId_Event_Type_Death]['place']=self::affiche_lieu($country,$zone,$city,$place);
		}
		
	    $year2 = substr($event[$sId_Event_Type_Death]['date2'],-4);		
		$event[$sId_Event_Type_Death]['age']=self::load_age($commun['year1'], $year2);
	}
	// Display Burial
	if (isset($event[$sId_Event_Type_Burial]['id_event']))
	{
		$note = self::load_note($event[$sId_Event_Type_Burial]['id_event']);
		$event[$sId_Event_Type_Burial]['note']=$note;
		$country = $event[$sId_Event_Type_Burial]['id_country'];
		$zone = $event[$sId_Event_Type_Burial]['id_zone'];
		$city = $event[$sId_Event_Type_Burial]['id_city'];
		if(isset($event[$sId_Event_Type_Burial]['place']))
		{
			$place = self::decode_place($event[$sId_Event_Type_Burial]['place']);
		}
		else
		{
			$place="";
		}
		if($place=="") {
			$event[$sId_Event_Type_Burial]['place']=self::affiche_lieu($country,$zone,$city,$place);
		}
	}

	// Display parents table	
		$commun['father']=$this->Outils->affiche_lien_individu($commun['father'],true,true);

		$commun['mother']=$this->Outils->affiche_lien_individu($commun['mother'],true,true);
		
		// sauvegarde des infos communes pour les fonctions utilisées pour la suite
		$_SESSION['t_commun'] = $commun;
	
			
	// Display union table
	
//$_SESSION['ind_affiche']=1;	
		$condition_union="";
	
		$array_conjoints = array();
		$array_conjoints_mar = array();
		$array_infos_union = array();
		
		$table = 'oxg_events';
		$this->Individus->table = $table;
		
		$condition  = " id_father='".$INDIVIDU."' and type_father=0 and type=".$sId_Event_Type_Union;
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_conjoints=$this->Individus->find(array(
				'fields'     => 'info1 ',
				'conditions'  => $condition
		));
//var_dump($result_conjoints)	;	
		foreach($result_conjoints as $row_conjoints) {
			

		if($condition_union!="")
		{
			$condition_union.= " or ";
		}
		$condition_union.="((oxg_events_mar.id_father=".$INDIVIDU." and oxg_events_mar.info2=".$row_conjoints->info1.") or (oxg_events_mar.id_father=".$row_conjoints->info1." and oxg_events_mar.info2=".$INDIVIDU.")) ";
		array_push($array_conjoints, $row_conjoints->info1);
	}
//var_dump($array_conjoints)	;	
	if($condition_union!="") {
//$_SESSION['ind_affiche']=1;	
		$table = 'oxg_events as oxg_events_mar, oxg_events as oxg_events_dat';
		$this->Individus->table = $table;

		$condition  = "(".$condition_union.")";
		$condition .= " and (oxg_events_mar.type_father=0 ";
		$condition .= " and oxg_events_mar.type=".$sId_Event_Type_Mar.")";
		$condition .= " and (oxg_events_dat.id_father=oxg_events_mar.id_event ";
		$condition .= " and oxg_events_dat.type=".$sId_Event_Type_Date;
		$condition .= " and oxg_events_dat.type_father=1 )";
		$condition .= " and oxg_events_mar.id_family=".$_SESSION['FAMILY'];
		
//$_SESSION['ind_affiche']=1;	
		
		$result_fam=$this->Individus->find(array(
			'fields'      => 'oxg_events_mar.info2, oxg_events_mar.id_father',
			'conditions'  => $condition,
			'order'       => 'oxg_events_dat.libelle'
		));

	}
	else {

		$table = 'oxg_events';
		$this->Individus->table = $table;
		$condition = "id_father=".$INDIVIDU." and type_father=0 and oxg_events.type=".$sId_Event_Type_Union;
		$condition .= $_SESSION['s_ReqFamille'];

		$result_fam=$this->Individus->find(array(
			'fields'     => 'info1 as info2, id_father',
			'conditions'  => $condition
		));
	}	
//var_dump($result_fam)	;	

	foreach($result_fam as $row_fam) {
		$id_conjoint=$row_fam->info2;
		$array_infos_union=array();
		
		if($id_conjoint==$INDIVIDU)
		{
			$id_conjoint=$row_fam->id_father;
		}
		$event2 = self::load_event_union($INDIVIDU, $id_conjoint);
		$_SESSION['t_event2']=$event2;
		$_SESSION['t_commun'] = $commun;
		$compagne= $this->Outils->affiche_lien_individu($id_conjoint,true,true);
		
		$enfants=self::affichage_tableau_union($id_conjoint);
	
		$array_infos_union = array('id_conjoint'=>$id_conjoint, 'compagne'=>$compagne, 'event2'=>$event2, 'enfants'=>$enfants);
		array_push($array_conjoints_mar, $array_infos_union);

	}

 // evenements relation incomplètement identifiée
	foreach ($array_conjoints as $conjoint)
	{
		if(in_array($conjoint, $array_conjoints_mar)==false)
		{

			$enfants=self::affichage_tableau_union($conjoint);

		}
	}	

	/*
	*  Display children if joint is known
	*  ajouter les enfants à l'union concernée
	*/
//$_SESSION['ind_affiche']=1;	
		$id_conjoint = -1;
		$table = 'oxg_individuals';
		$this->Individus->table = $table;

		$condition  = " (id_father='".$INDIVIDU."' and id_mother='".$id_conjoint."'  OR id_father='".$id_conjoint."' and id_mother='".$INDIVIDU."')";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_child=$this->Individus->find(array(
				'fields'     => 'id_individual, sex',
				'conditions'  => $condition
		));

		$nbre_child = $_SESSION['Req_Count'];

		if ($nbre_child > 0) {
			$enfants=array();
			foreach($result_child as $row_child) {
				$nom_enfant= $this->Outils->affiche_lien_individu($row_child->id_individual, true, true);
				$enfants_union = array('sex_child'=>$row_child->sex, 'nom_enfant'=>$nom_enfant);
				array_push($enfants, $enfants_union);
			}
		$array_infos_union = array('id_conjoint'=>-1, 'compagne'=>'?', 'event2'=>'', 'enfants'=>$enfants);
		array_push($array_conjoints_mar, $array_infos_union);
//var_dump($array_conjoints_mar);
		 }

	// Affichage des autres évènements de la personne
		$table = 'oxg_events2, oxg_events_type';
		$this->Individus->table = $table;

		$condition  = " id_individu='".$INDIVIDU."' and oxg_events_type.code not in ('GIVN','MARR','UNION','BIRT','BURI','CHR','DEAT','NOTE','OCCU') and oxg_events2.type=oxg_events_type.id_type_event";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_evt=$this->Individus->find(array(
				'fields'     => '*',
				'conditions' => $condition,
				'order'      => "left(devnt,4)"
		));
//var_dump($result_evt);
		foreach ($result_evt as $row_evt) {

			$note = self::load_note($row_evt->id_event);
			$event_divers[$row_evt->id_event]['note']=$note;
			$country = $row_evt->id_country;
			$zone = $row_evt->id_zone;
			$city = $row_evt->id_city;
			$event_divers[$row_evt->id_event]['type'] = $row_evt->type;
			$event_divers[$row_evt->id_event]['code'] = $row_evt->code;
			$event_divers[$row_evt->id_event]['libelle'] = $row_evt->libelle;
			$event_divers[$row_evt->id_event]['country'] = $row_evt->id_country;
			$event_divers[$row_evt->id_event]['zone'] = $row_evt->id_zone;
			$event_divers[$row_evt->id_event]['city'] = $row_evt->id_city;
			
			$year2 = substr($row_evt->devnt,0,4);
			$date1 = $row_evt->devnt;
			$date1 = str_replace("-0", "-", $date1);
			$date1 = str_replace("-", "/", $date1);
			$date2 = $row_evt->sDtEvnt;
			$date = $date1.":".$date2;
			$event_divers[$row_evt->id_event]['date']=$date;
			if ($date != ":") {
				$event_divers[$row_evt->id_event]['date']=decode_date($date);
			}

			if($row_evt->code !='')	{

			}
			else {	
//$_SESSION['ind_affiche']=1;			
				$table = 'oxg_events';
				$this->Individus->table = $table;

				$condition  = "type=169 and id_father=".$row_evt->id_event;
				$condition .= $_SESSION['s_ReqFamille'];
				
				$result_event=$this->Individus->find(array(
						'fields'     => 'libelle',
						'conditions' => $condition
				));
				foreach($result_event as $row_event) {	
					$sLibelleType=$row_event->libelle;
					$event_divers[$row_evt->id_event]['libelle']=$row_event->libelle;
					if ($sLibelleType = '' )  {
						$event_divers[$row_evt->id_event]['libelle']='Divers';
					}			
				}
			}

			if($country=='-1' && $zone=='-1' && $city=='-1' && $date==':') {
				if($row_evt->code=='ASSO')
				{
					$event_divers[$row_evt->id_event]['nom_individu']=affiche_lien_individu($row_evt->libelle,true,true);
				}
				else
				{
					$event_divers[$row_evt->id_event]['libelle']=$row_evt->libelle;
				}
				self::display_evnt_tree($row_evt->id_event, $event_divers);
			
			}
			else
			{
				$place="";
				if(isset($row_evt->sPlace))
				{
					$place = self::decode_place($row_evt->sPlace);
					$event_divers[$row_evt->id_event]['place']=$place;
				}

				if($place=="") {
					$event_divers[$row_evt->id_event]['place']=self::affiche_lieu($country,$zone,$city,$place);
				}	
			
				$event_divers[$row_evt->id_event]['age']=self::load_age($commun['year1'], $year2);
			}

		} // fin du foreach sur row_evt

	// Affichage des notes sur la personne
	$_SESSION['notes'] = '';		
	$_SESSION['notes'] = self::load_note ($INDIVIDU, 0);
	
	//Affichage des photos de la personne
	$photos=array();
	
	$table = 'oxg_events';
	$this->Individus->table = $table;

	$condition  = "id_father=".$INDIVIDU." and (type=".$sId_Event_Type_Birth." or type=128 or type=96) and type_father=0";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Individus->find(array(
			'fields'     => 'libelle, id_event, type',
			'conditions' => $condition
	));
	
	foreach($result as $row) {	
	if($row->type==96) {
		$photos['$row->id_event']['libelle']= $row->libelle;
//			echo "<img src='$row[libelle]' title='$row[libelle]'><br>";
		}
		else
		{
			$table = 'oxg_events';
			$this->Individus->table = $table;

			$condition  = "id_father=".$row->id_event." and type=96 and type_father=1";
			$condition .= $_SESSION['s_ReqFamille'];
	
			$result2=$this->Individus->find(array(
					'fields'     => 'libelle',
					'conditions' => $condition,
					'order' 	 => 'id_event'
			));
			
			foreach($result2 as $row2) {	
				$photos['$row->id_event']['libelle']= $row2->libelle;
			//	echo "<img src='$row2[libelle]' title='$row2[libelle]'><br>";
			}
		}
	} // fin lecture photos	
	

	// transmision des infos de travail pour l'affichage
	$_SESSION['t_common_events'] = $event;
	$_SESSION['t_conjoints_mar'] = $array_conjoints_mar;
	$_SESSION['t_event_divers'] = $event_divers;
	$_SESSION['t_common_photos'] = $photos; 
	
	//$this->layout = 'modal';
	$this->render('detail_individu'); 
	
	}	 // fin du foreach sur row_ind
	
} // fin de fonction detail_individu	

/*
*    Préparation de la liste d'ascendance pour l'individu sélectionné
*/
function asc_individu() {
	/*
	* préparation du nom complet entête de la liste d'ascendance
	*/
		$_SESSION['s_page'] = "asc";
		$_SESSION['s_vue']  = 'asc_individu';
		
	
	$Individu =  isset($this->request->params[0])?$this->request->params[0] : 0;
	$_SESSION['s_individu'] = $Individu;
	
	$this->loadModel('Outils');
	$this->loadModel('Individus');
	
	$table = 'oxg_individuals, oxg_patronyms';
	$this->Individus->table = $table;

	$condition  = "id_individual=".$Individu." and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result_ind=$this->Individus->find(array(
			'fields'     => '*, libelle',
			'conditions' => $condition
	));
	foreach ($result_ind as $row_ind) {
		$_SESSION['s_Title'] = $row_ind->first_name." ". $row_ind->libelle;
	}


	// recup variables du formulaire
	$gen_limit = $this->request->data->gen_limit;
	if  ($gen_limit=="") {$gen_limit=5;}
	$affichage = $this->request->data->affichage;
	if  ($affichage=="") {$affichage=1;}
	$_SESSION['s_affichage'] = $affichage;
	$_SESSION['s_gen_limit'] = $gen_limit;


//$_SESSION['ind_affiche']=1;	
	$table = 'oxg_individuals, oxg_patronyms';
	$this->Individus->table = $table;

	$condition  = "id_individual=".$Individu." and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result_ind=$this->Individus->find(array(
			'fields'     => 'first_name, libelle, sosa, date_naissance, date_deces',
			'conditions' => $condition
	));
	foreach ($result_ind as $row_ind) {
		$_SESSION['s_Title'] = $row_ind->first_name." ". $row_ind->libelle;
	}
	
	$all_count=1;
	$new_tab = Array();
	$new_tab[] = $Individu;
	$G=1;
	for ($L=2; $L<$gen_limit+1; $L++)
	{
	$new_gen = 0;
		if (count($new_tab)>0) {
			$tab = Array();
			$tab = $new_tab;
//var_dump($tab);
			$new_tab = Array();
			$count = count($tab);
			for ($i = 0; $i < $count; $i++) {
				$ind=$tab[$i];
//$_SESSION['ind_affiche']=1;					
				$table = 'oxg_individuals';
				$this->Individus->table = $table;

				$condition  = "id_individual=".$ind;
				$condition .= $_SESSION['s_ReqFamille'];
				
				$result=$this->Individus->find(array(
						'fields'     => '*',
						'conditions' => $condition
				));
				foreach ($result as $row) {
					$father=$row->id_father;
					$mother=$row->id_mother;
					$nom_pere='';
					$nom_mere='';
					$relation_pere='';
					$relation_mere='';
					
					if($new_gen==0 and ($father!="-1" or $mother!="-1")) { $new_gen=1; $G++;}
					if($father!="-1" and $father!="") {
						if ($affichage==1) {						
							$nom_pere = $this->Outils->affiche_lien_individu($father,true,true);
						}
						else{
							$nom_pere = $this->Outils->affiche_ligne_individu($father);
						}						
						$relation_pere=$this->Outils->affiche_lien_individu($ind,true,false);
						$new_tab[]=$father;
						}
					if($mother!="-1" and $mother!="") {
						if ($affichage==1) {
							$nom_mere = $this->Outils->affiche_lien_individu($mother,true,true); 
						}
						else {
							$nom_mere = $this->Outils->affiche_ligne_individu($mother); 
						}
						$relation_mere=$this->Outils->affiche_lien_individu($ind,true,false);
						$new_tab[]=$mother;
						}
						
				$ligne_individus[]= array('generation'=>$L, 'id_individual'=>$row->id_individual, 'naissance'=>$row->date_naissance, 'deces'=>$row->date_deces, 'id_father'=>$father, 'nom_pere'=>$nom_pere, 'relation_pere'=>$relation_pere,'id_mother'=>$mother, 'nom_mere'=>$nom_mere, 'relation_mere'=>$relation_mere);
				}
				
			}
		}
		$all_count=$all_count+count($new_tab);
		$new_gen=0;

		}
	//var_dump($individus);
	
	$_SESSION['t_ligne_individus']= $ligne_individus;
	
	$this->render('asc_individu'); 
}

function desc_individu() {
	/*
	* préparation du nom complet entête de la liste d'ascendance
	*/
//$_SESSION['ind_affiche']=1;		
	$_SESSION['s_page'] = "desc";
	$_SESSION['s_vue']  = 'desc_individu';

	$INDIVIDU =  isset($this->request->params[0])?$this->request->params[0] : 0;
	$_SESSION['s_individu'] = $INDIVIDU;
	
	$this->loadModel('Outils');
	$this->loadModel('Individus');
	
	$table = 'oxg_individuals, oxg_patronyms';
	$this->Individus->table = $table;

	$condition  = "id_individual=".$INDIVIDU." and oxg_individuals.id_patronym=oxg_patronyms.id_patronym ";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result_ind=$this->Individus->find(array(
			'fields'     => '*, libelle',
			'conditions' => $condition
	));
	foreach ($result_ind as $row_ind) {
		$_SESSION['s_Title'] = $row_ind->first_name." ". $row_ind->libelle;
	}
	
	// recup variables du formulaire
	$gen_limit = $this->request->data->gen_limit;
	if  ($gen_limit=="") {$gen_limit=5;}
	$affichage = $this->request->data->affichage;
	if  ($affichage=="") {$affichage=1;}
	$_SESSION['s_affichage'] = $affichage;
	$_SESSION['s_gen_limit'] = $gen_limit;

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

	// début de la préparation des données à afficher
	$tab1 = "";
	$tab2 = "<dd>";
	$gen = 0;
	$nb_child = 0;
	$num_up = "";
	$_SESSION['t_ligne_individus']=array();
	
	//affichage mode liste1
	// switch ($affichage) {
		// case 1:
		// case 2:
			$num_up = 0;
			$gen=1;
			$tab = array ();
			$result = array ();
			$nom_principal = $this->Outils->affiche_lien_individu($INDIVIDU,true,true);
			$tab[] = array('num'=>$num_up,'ind'=>$INDIVIDU);
		
			while ($gen<$gen_limit) {
				$gen++;
				$new_gen=1;
				$result=$this->Outils->show_children($gen, $new_gen, $tab);

				for ($j=0; $j< count($_SESSION['return_ligne']); $j++) {
					$_SESSION['t_ligne_individus'][]=$_SESSION['return_ligne'][$j];
				}

				$tab=$result;
			}

			// case 3:
			
			// break;
		
		// }
		
	/* 
	*  préparation finale de l'affichage type 2
	*
	*  reconstitution des couples avec ajout du champ 'nom_conjoint'
	*
	*/
	if ($affichage==2) {

		foreach ($_SESSION['t_ligne_individus'] as $key => $row_ind) {
			
			$num_abboville[$key]  = $row_ind['affiche'];
		}
		// //Tri des données par numerotation Abbovile (affiche) croissante
		array_multisort($num_abboville, SORT_DESC,  $_SESSION['t_ligne_individus'] );

		$reserve_niveau='';

		foreach ($_SESSION['t_ligne_individus'] as $k =>$row) {
			$niveau=substr($_SESSION['t_ligne_individus'][$k]['affiche'],0,-2);
			$niveau = $niveau!=false ? $niveau : 0;

			if ($reserve_niveau=='') {$reserve_niveau = $niveau;}
			if($niveau!= $reserve_niveau && $_SESSION['t_ligne_individus'][$k]['affiche'] == $reserve_niveau ) {
					$_SESSION['t_ligne_individus'][$k]['nom_conjoint'] = $_SESSION['t_ligne_individus'][$k-1]['nom_mere'];	
			}	
			$reserve_niveau = $niveau;

		}

		array_multisort($num_abboville, SORT_ASC,  $_SESSION['t_ligne_individus']);
	}
	
		$this->render('desc_individu'); 
		
} // fin de la fonction desc_individu

/*
* Fonctions utilitaires des fonctions de préparation de la classe Individus
*/

function affiche_lieu($country, $zone, $city, $sFullPlaceName) {
//$_SESSION['ind_affiche']=1;		
	if($_SESSION['b_UserPlaceNames']==true and $sFullPlaceName!="")
	{
		return $sFullPlaceName;
	}
	elseif (($country=='' || $country=-1) || ($zone=='' || $zone=-1) || ($city=='' || $city=-1))
	{
		return '';
	}
	else
	{
		$strVille = "";
		$strZone = "";
		$strPays = "";
		$strNote = "";
		$libelle="";

		$table = 'oxg_countries';
		$this->Individus->table = $table;
		
		$condition  = "id_country=".$country;
			
		$result_pays=$this->Individus->find(array(
				'fields'     => 'libelle',
				'conditions'  => $condition
		));
		
		foreach($result_pays as $row_pays) {
			if ($row_pays['libelle']!="")	{ $strPays = strtoupper($row_pays['libelle']); }
		}
//$_SESSION['ind_affiche']=1;	
		$table = 'oxg_cities';
		$this->Individus->table = $table;
		
		$condition  = "id_country=".$country." and id_zone=".$zone."and id_city=".$city;
			
		$result_ville=$this->Individus->find(array(
				'fields'     => 'libelle',
				'conditions'  => $condition
		));
		
		foreach($result_ville as $row_ville) {
			if ($row_ville['libelle']!="")	{ $strVille = $row_ville['libelle'].", "; }
		}
		
		$table = 'oxg_zones';
		$this->Individus->table = $table;
		
		$condition  = "id_country=".$country." and id_zone=".$zone;
			
		$result_zone=$this->Individus->find(array(
				'fields'     => 'libelle',
				'conditions'  => $condition
		));
		
		foreach($result_zone as $row_zone) {
			if ($row_zone['libelle']!="")	{ $strZone = $row_zone['libelle'].", "; }
		}
		
		if ($sFullPlaceName!="")	{ $strNote = " (".$sFullPlaceName.")"; }
		return $strVille.$strZone.$strPays.$strNote;

	}
}

function decode_place($place)
{
	if($_SESSION['b_UserPlaceNames']==false)
	{
		if (substr($place,-1)==")")
		{
			$place = substr($place, strpos($place, "(")+1, -1);
		}
	}
	return $place;
}

function load_common_events($ind) {
//$_SESSION['ind_affiche']=1;	
	$sId_Event_Type_Marb    = $_SESSION['event_type']['Marb'];
	$sId_Event_Type_Marc    = $_SESSION['event_type']['Marc'];
	$sId_Event_Type_Mar     = $_SESSION['event_type']['Mar'];
	$sId_Event_Type_Div     = $_SESSION['event_type']['Div'];		
	$sId_Event_Type_Union   = $_SESSION['event_type']['Union'];		
	$sId_Event_Type_Date    = $_SESSION['event_type']['Date'];		
	$sId_Event_Type_Birth   = $_SESSION['event_type']['Birth'];
	$sId_Event_Type_Baptism = $_SESSION['event_type']['Bapm'];		
	$sId_Event_Type_Death   = $_SESSION['event_type']['Deat'];		
	$sId_Event_Type_Burial  = $_SESSION['event_type']['Buri'];	
	
	$event = array();

    $libelle="";

	$table = 'oxg_events2';
	$this->Individus->table = $table;
	
	$condition  = "id_individu=".$ind." and type in ('".$sId_Event_Type_Birth."', '".$sId_Event_Type_Baptism."', '".$sId_Event_Type_Death."', '".$sId_Event_Type_Burial."')";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
	));
	
	foreach($result as $row_event) {

		$type = $row_event->type;
		$event[$type] = array(
		
			'id_family'  => $row_event->id_family,
			'id_father'  => $row_event->id_individu,
			'id_event'   => $row_event->id_event,
			'type'       => $row_event->type,
			'libelle'    => $row_event->libelle,
			'date1'      => $row_event->devnt,
			'date2'      => $row_event->sDtEvnt,
			'age'		 => '',
			'note' 		 => '',
			'place'      => $row_event->sPlace,
			'id_country' => $row_event->id_country,
			'id_zone'    => $row_event->id_zone,
			'id_city'    => $row_event->id_city,
//			'info1'      => $row_event->info1,
//			'info2'      => $row_event->info2,
//			'info3'      => $row_event->info3
			);
	}
	return $event;
}

function load_event_union($ind, $conj)
{
//$_SESSION['ind_affiche']=1;
	$event = array();
	// récupération des infos communes
	$commun=$_SESSION['t_commun'];
		
		$table = 'oxg_events, oxg_events_type';
		$this->Individus->table = $table;
		$condition = "(id_father='".$ind."' and info2='".$conj."' OR info2='".$ind."' and id_father='".$conj."') and type_father=0 and oxg_events_type.id_type_event=oxg_events.type ";
		$condition .= $_SESSION['s_ReqFamille'];

		$result_event =$this->Individus->find(array(
			'fields'     => '*, oxg_events_type.code',
			'conditions'  => $condition
		));

	
	foreach($result_event  as $row_event) {
//$_SESSION['ind_affiche']=1;
		$table = 'oxg_events2';
		$this->Individus->table = $table;
		$condition = "id_event=".$row_event->id_event;
		$condition .= $_SESSION['s_ReqFamille'];

		$result_evt =$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
		));
	    
		foreach($result_evt  as $row_evt) {		
		
			$type = $row_event->type;
			$date1 = str_replace("-0", "-",$row_evt->devnt);
			$date1 = str_replace("-", "/", $row_evt->devnt);
			$date2 = $row_evt->sDtEvnt;
			$date = $date1.":".$date2;
	
			$event[$type] = array(
				'id_family' => $row_event->id_family,
				'id_conjoint' => $row_event->id_individu,
				'id_event' => $row_event->id_event,
				'type' => $row_event->type,
				'libelle' => $row_event->libelle,
				'date1' => $row_evt->devnt,
				'date2' => $row_evt->sDtEvnt,
				'date'  => decode_date($date),
				'age'	=> '',				
				'place' => $row_evt->sPlace,
				'id_country' => $row_evt->id_country,
				'id_zone' => $row_evt->id_zone,
				'id_city' => $row_evt->id_city,
				'note' => '',
				'info1' => $row_event->info1,
				'info2' => $row_event->info2,
				'info3' => $row_event->info3
				);
				$year2 = substr($event[$type]['date2'],-4);	
				$event[$type]['age']=self::load_age($commun['year1'], $year2);
				
		}
	}
//	var_dump($event);
	return $event;
}

function load_age($year1, $year2)
{
	if ($year2!="" and $year1!="")
		{
			if ($year2 == $year1)
			{
				$strAge = " < 1 ".decode_lng_text("STATS_OLD_2");
			}
			else if ($year2 > $year1)
				{
					$age = $year2 - $year1;
					$strAge = " ".decode_lng_text("STATS_OLD_1")." ".$age." ".decode_lng_text("STATS_OLD_2");
				}
				else
				{
					$strAge = " date error";
				}
		return $strAge;
		}	
}

function toggle_panel($texte)
{
	echo "<div id='mypanel' class='ddpanel'>";
	echo "<div id='mypanelcontent' class='ddpanelcontent'>";
	echo "<p style='padding:10px'>";
	echo $texte;
	echo "</p>";
	echo "<br style='clear: left' />";
	echo "</div>";
	echo "<div id='mypaneltab' class='ddpaneltab'>";
	echo "<a href='#'><span>More...</span></a>";
	echo "</div>";
	echo "</div>";
}

function load_note($idfather, $type_father=1) {

	$libelle="";
	$table = 'oxg_events';
	$this->Individus->table = $table;
	
	$condition  = "id_father=".$idfather." and type=125 and type_father=".$type_father;
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Individus->find(array(
			'fields'     => 'libelle, id_event ',
			'conditions'  => $condition
	));
	
	foreach($result as $row) {
		$libelle = $row->libelle;
//$_SESSION['ind_affiche']=1;
		$table = 'oxg_events';
		$this->Individus->table = $table;
		
		$condition  = "id_father=".$row->id_event." and type in (68,71) and type_father=1";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result2=$this->Individus->find(array(
				'fields'     => 'libelle, type ',
				'conditions' => $condition,
				'order'		 => 'id_event'
		));
		
		foreach($result2 as $row2) {
			if ($row2->type==68) { $libelle = $libelle.$row2->libelle; }
			if ($row2->type==71) { $libelle = $libelle."<br>".$row2->libelle; }
		}
	}
	return $libelle;
}

function load_title($ind) {
	
    $libelle="";

	$table = 'oxg_events';
	$this->Individus->table = $table;
	
	$condition  = "id_father=".$ind." and type=167 and type_father=0 ";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
	));
	
	foreach($result as $row) {
		$libelle = $row->libelle;
	}
	return $libelle;
}

function load_prof($ind) {
	
	$libelle="";

	$table = 'oxg_events';
	$this->Individus->table = $table;
	
	$condition  = "id_father=".$ind." and type=129 and type_father=0 ";
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
	));
	
	foreach($result as $row) {
		$libelle = $row->libelle;
	}
	return $libelle;
}

function balloon($code, $text)
{
	if ($text)
	{
		echo "<div id='balloon$code' class='balloonstyle' style='width: 550px; background-color: lightyellow'>";
		echo $text;
		echo "</div>";
		$plus = " <a rel='balloon$code'>(".decode_lng_text('EVNT_MORE')."...)</a>";
	}
	else
	{
		$plus = "";	
	}
	return $plus;
}

function display_evnt_tree($id_event, $events_divers)
{
		$table = 'oxg_events, oxg_events_type';
		$this->Individus->table = $table;
		$condition = "id_father=".$id_event." and type_father=1 and oxg_events.type=oxg_events_type.id_type_event";
		$condition .= $_SESSION['s_ReqFamille'];

		$result_event =$this->Individus->find(array(
			'fields'     => '*',
			'conditions'  => $condition
		));
 
		foreach($result_event  as $row_event) {		
		
		$event_divers[$row_evt->type]['code'] = $row_event->code;

		$event_divers[$row_evt->type]['libelle']=$row_event->libelle;
		
		self::display_evnt_tree($row_event->id_event, $events_divers);
	}
}

function affichage_tableau_union($id_conjoint) {

	$sId_Event_Type_Marb    = $_SESSION['event_type']['Marb'];
	$sId_Event_Type_Marc    = $_SESSION['event_type']['Marc'];
	$sId_Event_Type_Mar     = $_SESSION['event_type']['Mar'];
	$sId_Event_Type_Div     = $_SESSION['event_type']['Div'];		
	$sId_Event_Type_Union   = $_SESSION['event_type']['Union'];		
	$sId_Event_Type_Date    = $_SESSION['event_type']['Date'];		
	$sId_Event_Type_Birth   = $_SESSION['event_type']['Birth'];
	$sId_Event_Type_Baptism = $_SESSION['event_type']['Bapm'];		
	$sId_Event_Type_Death   = $_SESSION['event_type']['Deat'];		
	$sId_Event_Type_Burial  = $_SESSION['event_type']['Buri'];
	
	$commun=$_SESSION['t_commun'];
	
	if($id_conjoint!=-1)
	{

		$event2=$_SESSION['t_event2'];

		//Affichage publication des bans
		if (isset($event2[$sId_Event_Type_Marb]['id_event'])==true)
		{
		
			$note = self::load_note($event2[$sId_Event_Type_Marb]['id_event']);
			$event2[$sId_Event_Type_Marb]['note']=$note;
			$country = $event2[$sId_Event_Type_Marb]['id_country'];
			$zone = $event2[$sId_Event_Type_Marb]['id_zone'];
			$city = $event2[$sId_Event_Type_Marb]['id_city'];
			if(isset($event2[$sId_Event_Type_Marb]['place']))
			{
				$place = self::decode_place($event2[$sId_Event_Type_Marb]['place']);
			}
			else
			{
				$place="";
			}
			if($place=="") {
				$event2[$sId_Event_Type_Marb]['place']=self::affiche_lieu($country,$zone,$city,$place);
			}
			
			$year2 = substr($event2[$sId_Event_Type_Marb]['date2'],-4);		
		//	$event2[$sId_Event_Type_Marb]['age']=self::load_age($year1, $year2);
			// $date1 = $event2[$sId_Event_Type_Marb]['date1'];
			// $date1 = str_replace("-0", "-", $date1);
			// $date1 = str_replace("-", "/", $date1);
			// $date2 = $event2[$sId_Event_Type_Marb]['date2'];
			// $date = $date1.":".$date2;
			// $event2[$sId_Event_Type_Marb]['date']=decode_date($date);
		}
		//Affichage contrat de mariage
		if (isset($event2[$sId_Event_Type_Marc]['id_event'])==true)
		{
			$note = self::load_note($event2[$sId_Event_Type_Marc]['id_event']);
			$event2[$sId_Event_Type_Marc]['note']=$note;
			$country = $event2[$sId_Event_Type_Marc]['id_country'];
			$zone = $event2[$sId_Event_Type_Marc]['id_zone'];
			$city = $event2[$sId_Event_Type_Marc]['id_city'];
			if(isset($event2[$sId_Event_Type_Marc]['place']))
			{
				$place = self::decode_place($event2[$sId_Event_Type_Marc]['place']);
			}
			else
			{
				$place="";
			}
			if($place=="") {
				$event2[$sId_Event_Type_Marc]['place']=self::affiche_lieu($country,$zone,$city,$place);
			}
			
			$year2 = substr($event2[$sId_Event_Type_Marc]['date2'],-4);		
		//	$event2[$sId_Event_Type_Marc]['age']=self::load_age($year1, $year2);
			// $date1 = $event2[$sId_Event_Type_Marc]['date1'];
			// $date1 = str_replace("-0", "-", $date1);
			// $date1 = str_replace("-", "/", $date1);
			// $date2 = $event2[$sId_Event_Type_Marc]['date2'];
			// $date = $date1.":".$date2;
			//$event2[$sId_Event_Type_Marc]['date']=decode_date($date);
		
			
		}
		//Affichage mariage
		if (isset($event2[$sId_Event_Type_Mar]['id_event'])==true)
		{
			$note = self::load_note($event2[$sId_Event_Type_Mar]['id_event']);
			$event2[$sId_Event_Type_Mar]['note']=$note;
			$country = $event2[$sId_Event_Type_Mar]['id_country'];
			$zone = $event2[$sId_Event_Type_Mar]['id_zone'];
			$city = $event2[$sId_Event_Type_Mar]['id_city'];
			if(isset($event2[$sId_Event_Type_Mar]['place']))
			{
				$place = self::decode_place($event2[$sId_Event_Type_Mar]['place']);
			}
			else
			{
				$place="";
			}
			if($place=="") {
				$event2[$sId_Event_Type_Mar]['place']=self::affiche_lieu($country,$zone,$city,$place);
			}

		}
		//Affichage divorce
		if (isset($event2[$sId_Event_Type_Div]['id_event'])==true)
		{
			$note = self::load_note($event2[$sId_Event_Type_Div]['id_event']);
			$event2[$sId_Event_Type_Div]['note']=$note;
			$country = $event2[$sId_Event_Type_Div]['id_country'];
			$zone = $event2[$sId_Event_Type_Div]['id_zone'];
			$city = $event2[$sId_Event_Type_Div]['id_city'];
			if(isset($event2[$sId_Event_Type_Div]['place']))
			{
				$place = self::decode_place($event2[$sId_Event_Type_Div]['place']);
			}
			else
			{
				$place="";
			}
			if($place=="") {
				$event2[$sId_Event_Type_Div]['place']=self::affiche_lieu($country,$zone,$city,$place);
			}
			
			
		}

		//Affichage du ou des enfants de cette union
//$_SESSION['ind_affiche']=1;
		$table = 'oxg_individuals';
		$this->Individus->table = $table;

		$condition  = " (id_father='".$commun['individu']."' and id_mother='".$id_conjoint."' ) OR (id_father='".$id_conjoint."' and id_mother='".$commun['individu']."')";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_child=$this->Individus->find(array(
				'fields'     => 'id_individual, sex',
				'conditions' => $condition,
				'order'		=> "right(date_naissance,4)"
		));

		$nbre_child = $_SESSION['Req_Count'];

		if ($nbre_child > 0) {
			$enfants=array();
			foreach($result_child as $row_child) {
				$nom_enfant= $this->Outils->affiche_lien_individu($row_child->id_individual, true, true);
				$enfants_union = array('sex_child'=>$row_child->sex, 'nom_enfant'=>$nom_enfant);
				array_push($enfants, $enfants_union);
			}
		 }

		return $enfants;
	}		

}



}// fin de classe