<?php

/*
*  appel des fonctions pour remplir les select du formulaire de recherches par événement
*  en fonction des sélections successives
*/

	session_start();
	
	$DS= DIRECTORY_SEPARATOR;
	$ROOT=dirname(dirname(dirname(dirname(__FILE__))));
	define('BASE_URL',dirname(dirname(dirname(dirname($_SERVER['SCRIPT_NAME']))))); 

	require $ROOT.$DS.'webroot'.$DS.'includes'.$DS.'lib_inc.php';
	//require $ROOT.$DS.'core'.$DS.'functions.php'; 
	require $ROOT.$DS.'core'.$DS.'Router.php'; 
	require $ROOT.$DS.'config'.$DS.'conf.php';

	require $ROOT.$DS.'core'.$DS.'Model.php'; 
	$Recherches = new Model();
	//var_dump($_GET);
	 // gestion de l'affichage des zones
	if(isset($_GET['country']) && !isset($_GET['zone'])) {
			$country = $_GET['country'];
			
			$table = 'oxg_events2, oxg_zones ';
			$condition =  "oxg_events2.id_country=oxg_zones.id_country and oxg_events2.id_zone=oxg_zones.id_zone and oxg_zones.id_country=".$_GET['country'];
			$condition .= " and oxg_events2.id_family=".$_SESSION['FAMILY'];
			
			$Recherches->table = $table;
			$result=	$Recherches->find(array(
				'fields'     => "oxg_zones.id_zone, oxg_zones.libelle as libelle, count(*) as decompte",  
				'conditions' => $condition,
				'group'      => "oxg_zones.id_zone, oxg_zones.libelle"
				));
				$contenu_select = "<option value='All'>".display_lng_text('EVNT_ALL')."</option>";	
				//var_dump($result);
				foreach($result as $k => $row)	{
					$contenu_select .= "<option value='".$row->id_zone."'>".utf8_encode($row->libelle)."<i> (".$row->decompte.")</i></option>";
				}
				
				echo $contenu_select;

	 }
	 // gestion de l'affichage des villes
	 if(isset($_GET['country']) && isset($_GET['zone']) && !isset($_GET['event'])) {
		  
			$country = $_GET['country'];
			$zone = $_GET['zone'];
			$event = $_GET['event'];
			$start = $_GET['start'];
			$end = $_GET['end'];
			
			$table = 'oxg_events2, oxg_cities';
			$condition =  "oxg_events2.id_country=oxg_cities.id_country and oxg_events2.id_zone=oxg_cities.id_zone and oxg_events2.id_city=oxg_cities.id_city and oxg_cities.id_country=".$country." and oxg_cities.id_zone=".$zone;
			$condition .= " and oxg_events2.id_family=".$_SESSION['FAMILY'];

			$Recherches->table = $table;
			$result=	$Recherches->find(array(
				'fields'     => "oxg_cities.id_city, oxg_cities.libelle as libelle, count(*) as decompte",  
				'conditions' => $condition,
				'group'      => "oxg_cities.id_city, oxg_cities.libelle"
				));
				$contenu_select = "<option value='All'>".display_lng_text('EVNT_ALL_F')."</option>";
				
				foreach($result as $k => $row)	{
					$contenu_select .= "<option value='".$row->id_city."'>".utf8_encode($row->libelle)."<i> (".$row->decompte.")</i></option>";
				}
				
				echo $contenu_select; 
			
	  }
	   // gestion de l'affichage des événements
	  if (isset($_GET['country']) && isset($_GET['zone']) && isset($_GET['event']))  {
			// require $ROOT.$DS.'webroot'.$DS.'includes'.$DS.'lbl_inc.php';
			// require $ROOT.$DS.'webroot'.$DS.'includes'.$DS.'lib_inc.php';
			require $ROOT.$DS.'core'.$DS.'Request.php'; 
			require $ROOT.$DS.'model'.$DS.'Outils.php';
			$Outils = new Outils;
			$Request = new Request;

	// installation des règles de validation
			$Recherches->validate = array(

				'date_begin' => array(
					'R_'=>array(
						'rule' => 'Empty',
						'message' => ''
					),
					'R0' => array(
						'rule' =>  'min_4',
						'message' => decode_lng_text('SEARCH_INDIV_START_R0')
					 ),
					'R1' => array(
						'rule' =>  'max_4',
						'message' => decode_lng_text('SEARCH_INDIV_START_R1')
					 ),
					 'R2'=>array(
						'rule' => '(1|2)([0-9])+',
						'message' => decode_lng_text('SEARCH_INDIV_START_R2')
					)
				),
				'date_end' => array(
					'R_'=>array(
						'rule' => 'Empty',
						'message' => ''
					),
					'R0' => array(
						'rule' =>  'min_4',
						'message' => decode_lng_text('SEARCH_INDIV_END_R0')
					 ),
					'R1' => array(
						'rule' =>  'max_4',
						'message' => decode_lng_text('SEARCH_INDIV_END_R1')
					 ),
					 'R2'=>array(
						'rule' => '(1|2)([0-9])+',
						'message' => decode_lng_text('SEARCH_INDIV_END_R2')
					)
				)
			);
			
			$country = $_GET['country'];
			$zone = $_GET['zone'];
			$city = $_GET['city'];
			$event = $_GET['event'];
			$start_evnt = $_GET['start'];
			$end_evnt = $_GET['end'];
	//var_dump($Request->data);
	// mise en réserve pour ré-affichage 
		//	if($Recherches->validates($Request->data)) {
			// $_SESSION['s_rech_country_evnt'] = $country;
			// $_SESSION['s_rech_zone_evnt'] = $zone;
			// $_SESSION['s_rech_city_evnt'] = $city;
			// $_SESSION['s_rech_event'] = $event;
			// $_SESSION['s_rech_start_evnt'] = $start_evnt;
			// $_SESSION['s_rech_end_evnt'] = $end_evnt;

			$contenu_select = array();
					
			$table = 'oxg_events2, oxg_individuals, oxg_events_type';
			
			$condition  = "oxg_events2.id_family=".$_SESSION['FAMILY'];
			if($_SESSION['b_connecte']==false) {
					$condition .= $_SESSION['s_ReqContemporain'];
			}
			switch ($event) {
				
				case 'All':
					$condition .= " and oxg_events2.type IN('47','51','55','77','82','112','115','125','129','163','167')";
					break;
				default:
					$condition .= " and oxg_events2.type =".$event;
					break;
			}
			
			$condition .= " and oxg_events_type.id_type_event = oxg_events2.type";
			$condition .=  " and oxg_individuals.id_individual = oxg_events2.id_individu";
			$condition .=  " and oxg_individuals.id_family = ".$_SESSION['FAMILY'];
			if ($country !='All') {
				$condition .= " and oxg_events2.id_country=".$country;
			}
			if ($zone !='All') {
				$condition .= " and oxg_events2.id_zone=".$zone;
			}	
			if ($city !='All') {
				$condition .= " and oxg_events2.id_city=".$city;
			}

			if ($start_evnt<>"") { $condition .= " and left(oxg_events2.devnt,4)>=".intval($start_evnt); }
			if ($end_evnt<>"")   { $condition .= " and left(oxg_events2.devnt,4)+1>1 and left(oxg_events2.devnt,4)<=".intval($end_evnt); }
//	$_SESSION['ind_affiche']=1;
			
			$Recherches->table = $table;
			$result =	$Recherches->find(array(
				'fields'     => "oxg_events2.id_family as family, oxg_events2.id_individu as individu, oxg_events2.id_country as country, oxg_events2.id_zone as zone, oxg_events2.id_city as city, oxg_events2.type as type_event, 
									oxg_events2.id_event as id_event, oxg_events_type.code as code_event, oxg_events2.sPlace as place, oxg_events2.sDtEvnt as date_evnt, oxg_individuals.id_father as id_father, oxg_individuals.id_mother as id_mother",  
				'conditions' => $condition,
				'group'      => "oxg_events2.id_individu, oxg_events2.type",
				'order'      => "individu Asc, oxg_events2.type ASC"
				));
				$contenu_select = "<table border=1 width=100% cellspacing=0 style='font-weight:normal;'>". 					
									"<th>".display_lng_text('GEDTAG_NAME')."</th>".
									"<th>".utf8_encode(display_lng_text('EVNT_EVNT'))."</th>".
									"<th>".display_lng_text('GEDTAG_DATE')."</th>".
									"<th>".display_lng_text('GEDTAG_PLAC')."</th>".
									"<th>".utf8_encode(display_lng_text('DETAIL_IND_FATHER'))."</th>".
									"<th>".utf8_encode(display_lng_text('DETAIL_IND_MOTHER'))."</th>".
									"<th>".display_lng_text('EVNT_SPS')."</th>"."$$";
									
				foreach($result as $k => $row)	{
					$nom_conjoint = '';
					if ($row->type_event == '115') {
						$table = 'oxg_events2, oxg_events';
						$Recherches->table = $table;
						$condition = "oxg_events2.id_individu = '".$row->individu."' and oxg_events2.id_family = oxg_events.id_family and oxg_events2.id_event = oxg_events.id_event and oxg_events.type = '115'";
//$_SESSION['ind_affiche']=1;						
						$result_conjoint =	$Recherches->find(array(
							'fields'     => "oxg_events.info2 as id_conjoint",
							'conditions' => $condition
						));
		
						$id_conjoint = $result_conjoint[0]->id_conjoint;
						$nom_conjoint = utf8_encode($Outils->affiche_lien_individu($id_conjoint,true,true,true,false));
					}
					$nom_individu = utf8_encode($Outils->affiche_lien_individu($row->individu,true));
					$nom_father = utf8_encode($Outils->affiche_lien_individu($row->id_father,true,true,true,false));
					$nom_mother = utf8_encode($Outils->affiche_lien_individu($row->id_mother,true,true,true,false));
					$contenu_select .= "<tr><td>".$nom_individu."</td><td>".utf8_encode(display_lng_text('GEDTAG_'.$row->code_event))."</td><td>".$row->date_evnt."</td><td>".utf8_encode($row->place)."</td><td>".$nom_father."</td><td>".$nom_mother." </td><td>".$nom_conjoint." </td></tr>"."$$";
					//array('individu'=>$row->individu, 'country'=>$row->country, 'zone'=>$row->zone, 'city'=>$row->city, 'date_evnt'=>$row->date_evnt, 'libelle_evnt'=>$row->libelle_event,
							// 'place'=>utf8_encode($row->place), 'id_father'=>$row->id_father, 'id_mother'=>$row->id_mother);
					//$contenu_select[] = $row;
				}
				//echo 'k = '.$k.' count '.count($contenu_select);
				//$contenu_select[] = "</table>"; // est positionné dns le js
//var_dump($result);
				//echo json_encode($contenu_select);
				echo $contenu_select;
	  }
	  

?>