<?php
class DatesController extends Controller{


	function index(){

		$_SESSION['s_page'] = "dates";
		$_SESSION['s_vue']  = 'index';
		
		$this->loadModel('Dates');
		// $sReq="select min(left(devnt,4)) ANNEE from oxg_events2 where devnt<>'' $sReqFam";
		// $result = mysql_query( $sReq );
		// $row = mysql_fetch_array($result);
		// $anneee_min=$row['ANNEE'];
		
	/*
	* recherche de l'année mini
	*/		
		$table = 'oxg_events2';
		$this->Dates->table = $table;
		
		$condition  = "devnt<>'' ";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result=$this->Dates->find(array(
				'fields'     => 'min(left(devnt,4))  as ANNEE ',
				'conditions'  => $condition
		));

		foreach($result as $k => $row) {
			$anneee_min = $row->ANNEE;
		}

	
	/*
	* recherche de l'année maxi
	*/		
	
		$table = 'oxg_events2';
		$this->Dates->table = $table;
		
		$condition  = "devnt<>'' and left(devnt,4)<'9999' ";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result=$this->Dates->find(array(
				'fields'     => 'max(left(devnt,4)) as ANNEE ',
				'conditions'  => $condition
		));
		
		foreach($result as $k => $row) {
			$anneee_max = $row->ANNEE;
		}
		
		$anneee_max+=1;
		$nb_dates=($anneee_max-$anneee_min)/(($anneee_max-$anneee_min)/10);
		
		$tbAnneeCnt=array();
		$iMaxCnt=0;
		for($iAnnee=$anneee_min;$iAnnee<$anneee_max;$iAnnee+=$nb_dates)
		{
			$iAnneeFin=$iAnnee+$nb_dates;

//$_SESSION['ind_affiche']=1;			
			$table = 'oxg_events2';
			$this->Dates->table = $table;
			
			$condition  = "devnt<>'' and left(devnt,4)>='".$iAnnee."' and left(devnt,4)<'".$iAnneeFin."' ";
			$condition .= $_SESSION['s_ReqFamille'];
			
			$result=$this->Dates->find(array(
					'fields'     => 'COUNT(*) as somme ',
					'conditions'  => $condition
			));

			foreach($result as $k => $row) {
				$somme = $row->somme;
			}

	/*
	*		Construire le tableau à afficher 
	*/		
			$tbAnneeCnt[]=$somme;

			if($somme>$iMaxCnt)
			{
				$iMaxCnt=$somme;
			}
		}
		
		$_SESSION['s_repart_event'] = array();
		
		$iCptLig=0;
		for($iAnnee=$anneee_min;$iAnnee<$anneee_max;$iAnnee+=$nb_dates)
		{
			$iAnneeFin=$iAnnee+$nb_dates-1;
			$iSomme=$tbAnneeCnt[$iCptLig++];
			$iAnneeDep=ceil($iAnnee);
			$iAnneeFin=ceil($iAnneeFin);

			if($iSomme>0)
			{
				$iTaille=(($iSomme/$iMaxCnt)*1000);
			}
			$_SESSION['s_repart_event'][] =array('somme'=>$iSomme, 'anneeDep'=>$iAnneeDep, 'anneeFin'=>$iAnneeFin, 'taille'=>$iTaille);
		}
//var_dump($_SESSION['s_repart_event'])	;
		
		$this->render('index'); 
		
	} // fin de fonction index

/*
*  liste les évenements de la période sus revue
*/		
	function detail_date() {
		

		$_SESSION['s_page'] = 'date_zoom';
		$_SESSION['s_vue']  = 'detail_date';

		// récupération des dates entrées dans le formulaire
		// $sBeginDate=isset($_REQUEST["DATE_BEG"])?$_REQUEST["DATE_BEG"] : 0; 
		// $sEndDate=isset($_REQUEST["DATE_END"])?$_REQUEST["DATE_END"] : 0;
		
		// récupération des paramètres d'appel

		$sBeginDate  = isset($this->request->data->DATE_BEG)?$this->request->data->DATE_BEG : $this->request->params[0]; 
		$sEndDate	 = isset($this->request->data->DATE_END)?$this->request->data->DATE_END : $this->request->params[1]; 
	
		$page = isset($this->request->params[2])?$this->request->params[2] : 1; 
		// $_SESSION['s_BeginDate'] = $sBeginDate;
		// $_SESSION['s_EndDate']   = $sEndDate;
		// $_SESSION['s_Page']      = $page;
		
		$this->loadModel('Dates');
		
		// installation des règles de validation
		$this->Dates->validate = array(
			'DATE_BEG' => array(
				'R0'=>array(
					'rule' => 'notEmpty',
					'message' => decode_lng_text('DATE_START_R0')
				),
				'R1'=>array(
					'rule' => '[0-9]+',
					'message' => decode_lng_text('DATE_START_R1')
				),
				'R2' => array(
					'rule' =>  'min_4',
					'message' => decode_lng_text('DATE_START_R2')
				 ),
				'R3' => array(
					'rule' =>  'max_4',
					'message' => decode_lng_text('DATE_START_R3')
				 )
			),
			'DATE_END' => array(
				'R0'=>array(
					'rule' => 'notEmpty',
					'message' => decode_lng_text('DATE_END_R0')
				),
				'R1'=>array(
					'rule' => '[0-9]+',
					'message' => decode_lng_text('DATE_END_R1')
				),
				'R2' => array(
					'rule' =>  'min_4',
					'message' => decode_lng_text('DATE_END_R2')
				 ),
				'R3' => array(
					'rule' =>  'max_4',
					'message' => decode_lng_text('DATE_END_R3')
				 ),
				 'R4' => array(
					'rule' =>  "sup-eg_".$sBeginDate,
					'message' => decode_lng_text('DATE_END_R4')
				 )
			)
		);
		
	// raz du tableau des ereurs de validation
		$this->Form->errors	= array();	
		
		
	
	if($this->Dates->validates($this->request->data)) { 
//$_SESSION['ind_affiche']=1;				
		$id_type_evnt_plac="";

		$table = 'oxg_events_type';
		$this->Dates->table = $table;	

		$condition  = " code='DATE'";
		$row_evnt=$this->Dates->findFirst(array(
			'fields'     => 'id_type_event',
			'conditions' => $condition
		));
		
		if($row_evnt) {
			$id_type_evnt_date = $row_evnt->id_type_event;
		}

//$_SESSION['ind_affiche']=1;				
			$table = 'oxg_events2';
			$this->Dates->table = $table;
			
			$condition  = "devnt<>'' and left(devnt,4)>='".$sBeginDate."' and left(devnt,4)<='".$sEndDate."' ";
			$condition .= $_SESSION['s_ReqFamille'];
			
			$result=$this->Dates->find(array(
					'fields'      => ' count(*) as somme ',
					'conditions'  => $condition
			));

			foreach($result as $k => $row) {
				$nb_res_max = $row->somme;
			}
	//echo 	$nb_res_max;	
//$_SESSION['ind_affiche']=1;	
			$table = 'oxg_events2';
			$this->Dates->table = $table;
			
			$condition  = "devnt<>'' and left(devnt,4)>='".$sBeginDate."' and left(devnt,4)<='".$sEndDate."' ";
			$condition .= $_SESSION['s_ReqFamille'];
			
			// if ($limite_param!=-1) {
				// $limit = "$nb_res_par_page, $limite"; 
			// }
			$result_evnt=$this->Dates->find(array(
					'fields'      => ' type, id_event, devnt, sDtEvnt, id_individu, oxg_events2.id_family',
					'conditions'  => $condition
			));

		$cpt=1;	
		
		//affichage du tableau final selon choix
			
		foreach ($result_evnt as $k => $row_evnt) {
			$compagne="";
			if ($row_evnt->type==51) { $acte=decode_lng_text("GEDTAG_BIRT")." ".decode_lng_text("GEDTAG_OF"); }
			if ($row_evnt->type==115) { $acte=decode_lng_text("GEDTAG_MARR")." ".decode_lng_text("GEDTAG_BTW"); $compagne = $row_evnt->id_event;}
			if ($row_evnt->type==82) { $acte=decode_lng_text("GEDTAG_DIV")." ".decode_lng_text("GEDTAG_BTW"); $compagne = $row_evnt->id_event;}
			if ($row_evnt->type==77) { $acte=decode_lng_text("GEDTAG_DEAT")." ".decode_lng_text("GEDTAG_OF"); }
			if ($row_evnt->type==55) { $acte=decode_lng_text("GEDTAG_BURI")." ".decode_lng_text("GEDTAG_OF"); }
			if ($row_evnt->type!=51 AND $row_evnt->type!=115 AND $row_evnt->type!=82 AND $row_evnt->type!=77 AND $row_evnt->type!=55) { $acte=decode_lng_text("GEDTAG_EVEN")." ";}
		
			$date1 = $row_evnt->devnt;
			$date1 = str_replace("-0", "-", $date1);
			$date1 = str_replace("-", "/", $date1);
			$date2 = $row_evnt->sDtEvnt;
			$date = $date1.":".$date2;
			$timestamp = mktime($row_evnt->devnt);
			
			$result_out[] = array('date'=>$date,'timestamp'=> $timestamp, 'acte'=>$acte, 'id_family'=>$row_evnt->id_family, 'id_ind'=>$row_evnt->id_individu, 'nom_indiv'=>'', 'compagne'=>$compagne, 'nom_compagne'=>'');

			if (count($result_out)!=0) {
				foreach ($result_out as $key => $row_out)
					{
						$timestamp_[$key]  = $row_out['timestamp'];
					}

				array_multisort($timestamp_, SORT_NUMERIC, SORT_ASC, $result_out);
							
			}
		}
		/*
		*	 Recherche des noms des individus et de leurs compagnes
		*/	
//$_SESSION['ind_affiche']=1;

	//	if ($result_out) { $_SESSION['result_out']=''; return true;}
		$this->loadModel('Outils');

		foreach ($result_out as $key => $row_out) {

			$id_ind=$result_out[$key]['id_ind'];
			$id_fam=$result_out[$key]['id_family'];
		
			$table = 'oxg_individuals';
			$this->Dates->table = $table;	
		
			$condition = "id_individual=". $row_out['id_ind'] .' '.$_SESSION['s_ReqContemporain'].' '." and id_family=".$id_fam;
			
			$result_cont=$this->Dates->findFirst(array(
				'fields'     => '*',
				'conditions' => $condition
			));

				$result_out[$key]['nom_indiv'] =  $this->Outils->affiche_lien_individu($id_ind, true, false);

				$compagne = $row_out['compagne'];
				if ($compagne != "") {
//$_SESSION['ind_affiche']=1;	
					$table = 'oxg_events';
					$this->Dates->table = $table;	
					
					$condition = " id_event=".$compagne." and id_family=".$id_fam;
					
					$req=$this->Dates->findFirst(array(
						'fields'     => 'info2',
						'conditions' => $condition
					));
					
					$compagne = $req->info2;
//$_SESSION['ind_affiche']=1;							
					$table = 'oxg_individuals';
					$this->Dates->table = $table;	
					
					$condition = 'id_individual='.$compagne.' '. $_SESSION['s_ReqContemporain'].' and id_family='.$id_fam;
					
					$req=$this->Dates->findFirst(array(
						'fields'     => '*',
						'conditions' => $condition
					));
					
					if ($req) {
						$result_out[$key]['nom_compagne'] = $this->Outils->affiche_lien_individu($compagne, true, false);
					}
					else
					{
						$result_out[$key]['nom_compagne'] = ' X';
					}
				}
			}
		$_SESSION['t_result_out'] = $result_out;
// var_dump($_SESSION['t_result_out']);
	} // fin du validate=ok
			
		$this->render('detail_date');
			
	} //fin de fonction detail_date		
	
} // fin de classe