<?php
class FamillesController extends Controller{

	function index(){
//$_SESSION['ind_affiche']=1;
		$_SESSION['s_groupe']='users';
		$_SESSION['s_page']= 'family';
		$_SESSION['s_vue']='index';				

	
		$_SESSION['t_familles'] = array();

		$this->loadModel('Familles');
		
		$table = ' oxg_family';
		$this->Familles->table = $table;
		
		$result=$this->Familles->find(array(
				'fields'     => 'id_family, name'
		));

		foreach($result as $row) {
//$_SESSION['ind_affiche']=1;				
			$table = ' oxg_individuals';
			$this->Familles->table = $table;
			
			$condition  = "id_family=".$row->id_family;
		
			$result_cpt=$this->Familles->find(array(
					'fields'     => 'count(*) as somme',
					'conditions'  => $condition
			));

			foreach ($result_cpt as $row_cpt) {
				$_SESSION['t_familles'][$row->id_family] = array('nom_famille'=>$row->name, 'id_famille'=>$row->id_family, 'nbre_famille'=>$row_cpt->somme);
			}

		}

	} // fin de fonction index

	/*
	* Permet d'installer un numéro de famille
	*/
	function chg_famille(){	
		
		$_SESSION['FAMILY'] = $this->request->params[0];
		$_SESSION['s_ReqFamille'] = " and id_family=".$_SESSION['FAMILY'];
		$_SESSION['t_stats_accueil'] = array();
		$_SESSION['b_init'] = 0;
		
		$this->redirect('Accueil/index');
	
	} // fin de fonction chg_famille
	
	
	function sosa(){
		$_SESSION['s_groupe']='users';
		$_SESSION['s_page']= 'family';
		$_SESSION['s_vue']='sosa_update';
		
		$patronymes = array();
		$personnes = array();
		$_SESSION['t_personnes'] = array();

		
		if ( $this->request->params[0] == 0)  {
//$_SESSION['ind_affiche']=1;	
			$_SESSION['s_changed_family'] = $this->request->params[1];
			
			$this->loadModel('Familles');
			
			$table = ' oxg_patronyms, oxg_individuals';
			$this->Familles->table = $table;
			
			$conditions  = "oxg_patronyms.id_patronym=oxg_individuals.id_patronym";
			$conditions .= " and libelle<>'X' and libelle<>'?' and id_father<>'-1' and id_mother<>'-1'";
			$conditions .= "and id_family='".$_SESSION['s_changed_family']."'";
			
			$result=$this->Familles->find(array(
					'fields'  	=> '*',
					'conditions' => $conditions,
					'group' 	=> "libelle"
			));

			foreach($result as $k => $row) {
				
				$patronymes[]  = array('patro_id'=>$row->id_patronym, 'patro_libelle'=>$row->libelle);
			}
			
		$_SESSION['t_patronymes'] = $patronymes;
		
		//var_dump($_SESSION['t_patronymes']);
		}


	$this->render('sosa');
		
	} // fin de fonction sosa

	
	function liste_asc(){
		$_SESSION['s_page']= 'family';
		$_SESSION['s_vue']='liste_asc';
//$_SESSION['ind_affiche']=1;	
		$max_results = 35;	// nombre of ligne par page
;

		if($this->request->params[0]) {				
			$limite = $this->request->params[0];
		}
		else {
			$limite=0;
		}
		$limite_param=$limite;
		$limite=intval($limite*$max_results);
		$from = (( ($limite_param+1) * $max_results) - $max_results);
//var_dump($this->request->params[0])	; 
// var_dump($from);  
// var_dump($max_results);

		$individus = array();
	
		$this->loadModel('Familles');
		
		$table = 'oxg_individuals, oxg_patronyms';
		$this->Familles->table = $table;
		
		$conditions  = "oxg_individuals.sosa>=1";
		$conditions .= " and oxg_patronyms.id_patronym=oxg_individuals.id_patronym";
		$conditions .= $_SESSION['s_ReqFamille'];
		
		// $result_individus = $this->Familles->find(array(
							// 'conditions' => $conditions
							// ));
		// $total_individus = count($result_individus)	;				
//$_SESSION['ind_affiche']=1;								
		$people=$this->Familles->find(array(
				'fields'  	 => '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, id_father, id_mother, contemporary',
				'conditions' => $conditions,
				'order' 	 => "sosa"
//					,
//					'limit'      => $from.",".$max_results
		));

		foreach($people as $k => $row) {
				$nom_individu=array();
				$nom_individu = array( 'sosa'=>$row->id_sosa, 'id_individual'=>$row->id_individual, 'first_name'=>$row->first_name, 'libelle'=>$row->libelle, 'date_naissance'=>$row->naidat, 'date_deces'=>$row->deces, 'sex'=>$row->sex, 'id_father'=>$row->id_father, 'id_mother'=>$row->id_mother,'contemporary'=>$row->contemporary );

				$individus [] = $nom_individu;

			}
		$_SESSION['t_individus'] =  $individus;
//		$_SESSION['s_total_individus'] = $total_individus;
		$this->render('liste_asc');
}
} // fin de la classe FamillesController
?>