<?php
class ArbresController extends Controller{
	
	function arbre_individu() {
		$_SESSION['s_page'] = "indi_ascend_tree";
		$_SESSION['s_vue']  = 'arbres';
		
		
		//$phase = isset($this->request->params[0]) ? 1 : 0;
		$type_arbre = $this->request->params[0]; 
		$individual = $this->request->params[1];

		$this->loadModel('Outils');
		$this->loadModel('Arbres');
		
	// acquisition des choix dans le formulaire
	if ($type_arbre == 'H') {
		//$nbgen = 6;
		//$nbgen= = $this->request->data->nbgen;

		$nbgen = isset($this->request->data->nbgen) ? $this->request->data->nbgen : 4 ;
		$_SESSION['s_nbgen'] = $nbgen;
	} 
	else {
		//$nbgen = 3;
		$nbgen = isset($this->request->data->nbgen) ? $this->request->data->nbgen : 4 ;
		$_SESSION['s_nbgen'] = $nbgen;
	}
	
	$table = 'oxg_individuals';
	$this->Arbres->table = $table;

	$condition  = "id_individual=".$individual;
	$condition .= $_SESSION['s_ReqFamille'];
	
	$result=$this->Arbres->find(array(
			'fields'     => '*',
			'conditions' => $condition
	));
	foreach ($result as $row) {
		$_SESSION['arbre_individu'] ['sexe'] = $row->sex;
	}
	
	$_SESSION['arbre_individu'] ['id'] = $individual;
	
//echo 'famille= '.$_SESSION['FAMILY'];

	$_SESSION['t_parents'] = array();
	$parents = array();
	$individus = array();
	
	$retour = $this->Outils->rechercheParents($individual);
	$individus[] = array( 'id'=>$retour ['id_father'], 'fin'=>'');
	$individus[] = array( 'id'=>$retour ['id_mother'], 'fin'=>'');

	$_SESSION['t_parents'] ['Father'] = $retour ['id_father'];
	$_SESSION['t_parents'] ['Mother'] = $retour ['id_mother'];
	
	$_SESSION['s_Title'] = $this->Outils->affiche_lien_individu($individual,true,false,false,false,false);
	
	// var_dump($parents) ;echo "</br>";
	// var_dump($individus); echo "</br>";

	for ($gen=1; $gen<$nbgen;$gen++) {

		$deb=str_repeat("G",$gen); // on créee le niveau de grand-parenté
		
			foreach  ($individus as $k=>$v) {

				$id = $v[id];

				$retour = $this->Outils->rechercheParents($id);
				$finale = $k%2 == 0 ? 1 : 2; // vérication pair ou impair - pair=père impair=mère

				$finF = $v[fin].$finale;
				$finM = $v[fin].$finale;
				$Father = $deb."Father".$finF;
				$Mother = $deb."Mother".$finM;
				$parents[] = array( 'id'=>$retour ['id_father'], 'fin'=>$finF);
				$parents[] = array( 'id'=>$retour ['id_mother'], 'fin'=>$finM);

				$_SESSION['t_parents'] [$Father] = $retour ['id_father'];
				$_SESSION['t_parents'] [$Mother] = $retour ['id_mother'];
			}
	
		$individus=$parents;
		$parents=array();

	}
// var_dump ($_SESSION['t_parents']);

	if ($type_arbre == "V") {
		$sPage  = "indi_ascend_tree_v";
		$this->render('arbre_individu_v');
	}
	else {
		$sPage  = "indi_ascend_tree_h";
		$this->render('arbre_individu');
	}
	
		
	} //fin de fonction arbre_individu		


	
} // fin de classe