<?php
class CartesController extends Controller{

	function index(){

		$_SESSION['s_groupe']='patronymes';
		$_SESSION['s_page']='places_map';
		$_SESSION['stats'] = array();
		$_SESSION['t_zones']=array();
	
		$PAYS= $this->request->params[0];
		
		$this->loadModel('Cartes');

//$_SESSION['ind_affiche']=1;
		$table = 'oxg_countries';
		$this->Cartes->table = $table;
		
		$condition  = "id_country=".$PAYS;
	
		$result_info_pays=$this->Cartes->find(array(
				'fields'      => '*',
				'conditions'  => $condition
			));
		foreach ($result_info_pays as $k=>$row_info_pays) {
			$extension = strtolower($row_info_pays->extension);
			$sTitle = $row_info_pays->libelle;
		}
		
	// recherche des événements survenus dans le pays choisi		
		$table = 'oxg_events_type';
		$this->Cartes->table = $table;
		
		$condition  = "code='PLAC'";
		
		$row_event=$this->Cartes->findFirst(array(
				'fields'      => 'id_type_event',
				'conditions'  => $condition
			));
			
		if($row_evnt) {
			$id_type_evnt_plac = $row_evnt->id_type_event;
		}	
		
		//stat du nombre d'événements
		$table = 'oxg_events2';
		$this->Cartes->table = $table;
		
		$condition   = "id_country=".$PAYS;
		$condition  .= " and type not in('45','99','125','129', '148', '156', '163', '167','179')";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result=$this->Cartes->find(array(
				'fields'      => 'COUNT(*) as somme',
				'conditions'  => $condition
			));
		$nb_ind_pays = 	$result[0]->somme;

		$condition   = "1=1";
		$condition  .= " and type not in('45','99','125','129', '148', '156', '163', '167','179')";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result=$this->Cartes->find(array(
				'fields'      => 'COUNT(*) as somme',
				'conditions'  => $condition
			));
		$nb_ind_total =	$result[0]->somme;
		//$prc_ind_pays = number_format(($nb_ind_pays*100)/$nb_ind_total,1);
		$prc_ind_pays = number_format(($nb_ind_pays*100)/$nb_ind_total,1);

		
		//totaux des naissances, décès et mariages
		
		$event= array('type=51','type=77','type=115');
		
		for ($i=0;$i<3;$i++) {
			
			$condition   = $event[$i];
			$condition .= $_SESSION['s_ReqFamille'];
			
			$result=$this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
				));
				
			switch ($i) {
				case 0:
					$nb_birth_total = $result[0]->somme;
					break;
					
				case 1:
					$nb_death_total = $result[0]->somme;
					break;

				case 2:
					$nb_wedding_total = $result[0]->somme;
					break;
			}
		}
		
		//total des individus de la famille
		$table = 'oxg_individuals';
		$this->Cartes->table = $table;
		
		$condition  = "1=1";
		$condition .= $_SESSION['s_ReqFamille'];
	
		$result=$this->Cartes->find(array(
				'fields'      => 'COUNT(*) as somme',
				'conditions'  => $condition
		));
		
		$nb_ind_total = $result[0]->somme;		
		
		//stat du nombre de naissances	
		if($_SESSION['b_connecte']==false) {
			$table = 'oxg_individuals';
			$this->Cartes->table = $table;
			
			$condition  = "id_pays_naissance=".$PAYS;
			$condition .= $_SESSION['s_ReqFamille'];
		
			$result=$this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
			));
			
			$nb_birth_ind = $result[0]->somme;	
			$prc_birth_ind = number_format(($nb_birth_ind*100)/$nb_ind_total,1);
			$prc_birth = number_format(($nb_birth_ind*100)/$nb_birth_total,1);
		}
		
	//stat du nombre de décès
		if($_SESSION['b_connecte']==false) {
			$table = 'oxg_individuals';
			$this->Cartes->table = $table;
			
			$condition  = "id_pays_deces=".$PAYS;
			$condition .= $_SESSION['s_ReqFamille'];
		
			$result=$this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
			));
			
			$nb_death_ind = $result[0]->somme;	
			$prc_death_ind = number_format(($nb_death_ind*100)/$nb_ind_total,1);
			$prc_death = number_format(($nb_death_ind*100)/$nb_death_total,1);
		}
	//stat du nombre des mariages
		$nb_wedding_ind=0;
		$table = 'oxg_events';
		$this->Cartes->table = $table;
		
		$condition  = "type=115";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result=$this->Cartes->find(array(
				'fields'      => 'id_event',
				'conditions'  => $condition
		));
			
		foreach ($result as $k=>$row) {
	
			$table = 'oxg_events ';
			$this->Cartes->table = $table;
			
			$condition  = "type=134 and id_father='".$row->id_event."' and info1=".$PAYS;
			$condition .= $_SESSION['s_ReqFamille'];
		
			$result_wedding=$this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
			));
			
			if($result_wedding[0]->somme) {
				$nb_wedding_ind = $nb_wedding_ind + 1;
			}
		}	
		
	$prc_wedding = number_format(($nb_wedding_ind*100)/$nb_wedding_total,1);
	
	// preparation du mapping
			
			$table = 'oxg_zones';
			$this->Cartes->table = $table;
			
			$condition  = "id_country=".$PAYS." and id_zone<>-1";
		
			$result_zones=$this->Cartes->find(array(
					'fields'      => 'libelle, code, id_zone, pos_remp_x, pos_remp_y, coord_zone',
					'conditions'  => $condition
			));
//var_dump($result_zones);
			foreach($result_zones as $k=>$row_zones) {
				
				$table = 'oxg_events';
				$this->Cartes->table = $table;
					
				$condition  = "info1=".$PAYS." and info2=".$row_zones->id_zone;
				
				
				$result_info_zone = $this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
				));
				foreach($result_info_zone as $l=>$row_info_zone) {
						
						if($row_zones->coord_zone=='') {
							$pX=$row_zones->pos_remp_x -7;
							$pY=$row_zones->pos_remp_y -7;
							$coords = "COORDS=$pX,$pY";
							$pX=$row_zones->pos_remp_x +7;
							$pY=$row_zones->pos_remp_y -7;
							$coords .=",$pX,$pY";
							$pX=$row_zones->pos_remp_x +7;
							$pY=$row_zones->pos_remp_y +7;
							$coords .= ",$pX,$pY";
							$pX=$row_zones->pos_remp_x -7;
							$pY=$row_zones->pos_remp_y +7;
							$coords .= ",$pX,$pY";
						}
						else {
							$coords= $row_zones->coord_zone;
						}
						
						$_SESSION['t_zones'] [] = array('pays'=>$PAYS, 'id_zone'=>$row_zone->id_zone, 'id_dept'=>$row_zones->code,'coord_zone'=>$coords, 'libelle'=>$row_zones->libelle,'somme'=>$row_info_zone->somme);
					}
			}
	
			
	$_SESSION['stats'] = array('libelle'=>$sTitle, 'nb_ind_pays'=>$nb_ind_pays, 'prc_ind_pays'=>$prc_ind_pays, 'nb_birth'=>$nb_birth_ind, 'prc_birth_ind'=> $prc_birth_ind, 'prc_birth'=>$prc_birth, 
								'nb_wedding'=>$nb_wedding_ind, 'prc_wedding'=>$prc_wedding, 'nb_death'=>$nb_death_ind, 'prc_death'=>$prc_death, 'prc_death_ind'=>$prc_death_ind );
	
	// var_dump($_SESSION['stats']);
	//var_dump($_SESSION['t_zones']);
	
	} // fin de la fonction index
	function carte_img($PAYS) {
	
	// Loading an extension based on OS
	if (!extension_loaded('gd'))
	{
	   if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
	   {
//	       dl('php_gd.dll');
	       dl('php_gd2.dll');
	   }
	   else
	   {
//	       dl('gd.so');
	       dl('gd2.so');
		}
	}
	
	// Or, the PHP_SHLIB_SUFFIX constant is available as of PHP 4.3.0
	if (!extension_loaded('gd'))
	{
		$prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
//		dl($prefix . 'gd2.' . PHP_SHLIB_SUFFIX);
		dl($prefix . 'gd.' . PHP_SHLIB_SUFFIX);
	}

	$UTMNorthing=0;
	$UTMEasting=0;

	//$PAYS=isset($this->request->params[0])?$this->request->params[0] : 0; 
	
		$this->loadModel('Cartes');
	
		$table = 'oxg_zones, oxg_events';
		$this->Cartes->table = $table;
		
		$condition  = "oxg_zones.id_country=".$PAYS." and info1=".$PAYS." and oxg_zones.id_zone=info2 and id_zone<>-1 ";
		$condition .= $_SESSION['s_ReqFamille'];
		$result=$this->Cartes->find(array(
				'fields'      => 'COUNT(*) as somme',
				'conditions'  => $condition
			));
		$nb_ind_total = $result[0]->somme;
		
		// $result = mysql_query( "select COUNT(*) as somme from oxg_zones, oxg_events where oxg_zones.id_country=$PAYS and info1=$PAYS and oxg_zones.id_zone=info2 and id_zone<>-1 $sReqFam");
	// $row = mysql_fetch_array($result);
	// $nb_ind_total = $row['somme'];
	
	if($nb_ind_total==0) {
		$nb_ind_total=1;
	}

	// $result_info_pays = mysql_query( "select * from oxg_countries where id_country='$PAYS'");
	// $row_info_pays = mysql_fetch_array($result_info_pays);
	// $extension = strtolower($row_info_pays['extension']);
	
		$table = 'oxg_countries';
		$this->Cartes->table = $table;
		
		$condition  = "id_country=".$PAYS;
	
		$result_info_pays=$this->Cartes->find(array(
				'fields'      => '*',
				'conditions'  => $condition
			));
		foreach ($result_info_pays as $k=>$row_info_pays) {
			$extension = strtolower($row_info_pays->extension);
			$sTitle = $row_info_pays->libelle;
			$coef = $row_info_pays->coef;
		}
//	header("Content-type: image/jpeg");
//	$im = imagecreatefromjpeg("map$PAYS.jpg");
//	header("Content-type: image/png");
	$im = imagecreatefrompng(WEBROOT."/images/map/".$extension.".png");
	

	//couleur des zones
	
	// $result_zones = mysql_query( "select libelle, code, id_zone, pos_remp_x, pos_remp_y, coord_zone from oxg_zones, oxg_flashlist where country='$PAYS' and id_country=country and id_zone=zone $sReqFam GROUP BY id_zone");
	// if(mysql_num_rows($result_zones)!=0)
	// {
		 // while($row_zones = mysql_fetch_array($result_zones))
		// {
			// $result_info_zone = mysql_query( "select COUNT(*) as somme from oxg_events where info1=$PAYS and info2=$row_zones[id_zone] $sReqFam");
			// while($row_info_zone = mysql_fetch_array($result_info_zone))
			// {
				// $orange = imagecolorallocate($im, 245, 245, 200-(($row_info_zone['somme']*200)/$nb_ind_total));
				// ImageFill($im, $row_zones['pos_remp_x'], $row_zones['pos_remp_y'], $orange);
			// }
		// }
	// }
	
	//couleur des zones
		$table = 'oxg_zones, oxg_flashlist';
		$this->Cartes->table = $table;
		
		$condition  = "country=".$PAYS." and id_country=country and id_zone=zone";
		$condition .= $_SESSION['s_ReqFamille'];
		
		$result_zones=$this->Cartes->find(array(
				'fields'      => 'libelle, code, id_zone, pos_remp_x, pos_remp_y, coord_zone',
				'conditions'  => $condition,
				'group'       => "id_zone"
			));
			
		foreach ($result_zones as $k=>$row_zones) { 
			$table = 'oxg_events';
			$this->Cartes->table = $table;
			
			$condition  = "info1=".$PAYS." and info2=".$row_zones->id_zone;
			$condition .= $_SESSION['s_ReqFamille'];
			
			$result_info_zone = $this->Cartes->find(array(
					'fields'      => 'COUNT(*) as somme',
					'conditions'  => $condition
				));
				
			foreach($result_info_zone as $l=>$row_info_zone) {
				$orange = imagecolorallocate($im, 255, 255, 220-(($row_info_zone->somme*200)/$nb_ind_total));
				//$orange = imagecolorallocate($im, 245,220-(($row_info_zone->somme*110)/$nb_ind_total),245);
				ImageFill($im, floatval($row_zones->pos_remp_x), floatval($row_zones->pos_remp_y), $orange);
			}
		}
	
		$sTypeCarte=substr($coef, 0, 1);
		if($sTypeCarte=="#")
		{
			list($ulPosX, $ulPosY, $ulTailleX, $ulTailleY) = split( ';', substr($coef, 1));
			$imgSize = getimagesize(WEBROOT."/images/map/".$extension.".png"); 
			$ulTailleImgX=$imgSize[0];
			$ulTailleImgY=$imgSize[1];
		}
		else
		{
			list( $dBaseLong, $dYlat, $dXlat, $dYlong, $dXlong) = split( ';', $coef);
		}

	//affichage des croix sur chacun des lieux
	//$result_pays = mysql_query( "select longitude, latitude from oxg_cities where id_country='$PAYS'");
	// $result_pays = mysql_query("select longitude, latitude from oxg_cities, oxg_flashlist where country='$PAYS' and id_country=country and id_city=city $sReqFam GROUP BY id_city");
	// if(mysql_num_rows($result_pays)!=0)
	// {
		// while($row = mysql_fetch_array($result_pays))
			
		// {
			
			$table = 'oxg_cities, oxg_flashlist';
			$this->Cartes->table = $table;
			
			$condition  = "country=".$PAYS." and id_country=country and id_zone=zone and id_city=city";
			$condition .= $_SESSION['s_ReqFamille'];
			
			$result_pays = $this->Cartes->find(array(
					'fields'      => 'longitude, latitude',
					'conditions'  => $condition,
					'group'       => "id_city"
				));
//	var_dump($result_pays)			;
			foreach($result_pays as $l=>$row_pays) {
			
				if($sTypeCarte=="#")
				{
					if($row_pays->latitude >0)
					{
						$y=((($row_pays->latitude +90)*$ulTailleY)/180)-$ulPosY;
						$y=$ulTailleImgY-$y;
					}
					else
					{
						$y=(((($row_pays->latitude +90)*$ulTailleY)/180)-$ulPosY);
						$y=$ulTailleImgY-$y;
					}
					$x=((($row_pays->longitude +180)*$ulTailleX)/360)-$ulPosX;
				}
				else
				{
					$coordonnees = self::LatLong_to_UTM($dBaseLong, $row_pays->latitude, $row_pays->longitude);
					$UTMEasting  = $coordonnees[0];
					$UTMNorthing = $coordonnees[1];
					$x=$dXlat+$dYlat*$UTMEasting;
					$y=$dXlong+$dYlong*$UTMNorthing;
				}

				$rouge = imagecolorallocate($im, 200, 10, 10);
				imagesetpixel($im, $x, $y, $rouge);
				imagesetpixel($im, $x+1, $y, $rouge);
				imagesetpixel($im, $x-1, $y, $rouge);
				imagesetpixel($im, $x, $y+1, $rouge);
				imagesetpixel($im, $x, $y-1, $rouge);
			}

	imagepng($im, WEBROOT."/images/map/map_".$_SESSION['FAMILY']."_".$PAYS."_cities.png");
	imagedestroy($im);

	} // fin de la fonction carte_img
	
	
	function LatLong_to_UTM($dLam, $dLat, $Lon) {
		$UTMNorthing=0;
		$UTMEasting=0;
		
		$PI=3.14159265;

		// Perform initialization
		$A = 6378206.4;
		$B = 6356584.8;
		$M0 = 0.9996;
		$E2 = ($A * $A - $B * $B) / ($A * $A);
		$EPS = ($A * $A - $B * $B) / ($B * $B);
		$E4 = $E2 * $E2;
		$E6 = $E4 * $E2;
		$E8 = $E6 * $E2;
		$AE = $A * (1.0 - $E2);
		$A0 = $AE * (1.0 + (3.0 / 4) * $E2 + (45.0 / 64.0) * $E4 + (175.0 / 256.0) * $E6 + (11025.0 / 16384.0) * $E8);
		$A1 = - $AE / 2.0 * ((3.0 / 4.0) * $E2 + (15.0 / 16.0) * $E4 + (525.0 / 512.0) * $E6 + (2205.0 / 2048.0) * $E8);
		$A2 = $AE / 4.0 * ((15.0 / 64.0) * $E4 + (105.0 / 256.0) * $E6 + (2205.0 / 4096.0) * $E8);
		$Err = - $AE / 6.0 * ((35.0 / 512.0) * $E6 + (315.0 / 2048.0) * $E8);

		// Following give UTM Zone number
		$Izone = floor((180.0 - $dLam) / 6.0 + 1.0);
		$Lam0 = ((183.0-6.0*$Izone)*$PI)/180.0;

		// Start conversion part of function
		$D = $Lam0 - ($Lon*$PI)/180.0;
		$D2 = $D * $D;
		$D3 = $D2 * $D;
		$D4 = $D3 * $D;
		$D5 = $D4 * $D;
		$D6 = $D5 * $D;
		$D7 = $D6 * $D;
		$D8 = $D7 * $D;

		$S = sin(($dLat*$PI)/180.0);
		$C = cos(($dLat*$PI)/180.0);
		$T = $S / $C;
		$T2 = $T * $T;
		$T4 = $T2 * $T2;
		$T6 = $T4 * $T2;
		$C2 = $C * $C;
		$C3 = $C2 * $C;
		$C4 = $C3 * $C;
		$C5 = $C4 * $C;
		$C6 = $C5 * $C;
		$C7 = $C6 * $C;

		$N2 = $EPS * $C * $C;
		$N4 = $N2 * $N2;
		$N = $A / sqrt(1.0 - $E2 * $S * $S);
		$AA0 = $N * $C;
		$AA1 = $N * $C3 * (1.0 - $T2 + $N2);
		$AA2 = $N * $C5 * (5.0 - 18.0 * $T2 + $T4 + 14.0 * $N2 - 58.0 * $T2 * $N2);
		$AA3 = $N * $S * $C7 * (61.0 - 479.0 * $T2 + 179.0 * $T4 - $T6);
		$Lat2=($dLat*$PI)/180.0;
		$B0 = $A0 * $Lat2 + $A1 * sin(2.0 * $Lat2) + $A2 * sin(4.0 * $Lat2) + $Err * sin(6.0 * $Lat2);
		$B1 = $N * $S * $C;
		$B2 = $N * $S * $C3 * (5.0 - $T2 + 9.0 * $N2 + 4.0 * $N4);
		$B3 = $N * $S * $C5 * (61.0 - 58.0 * $T2 + $T4 + 270.0 * $N2 - 33.0 * $T2 * $N2);
		$B4 = $N * $S * $C7 * (1385.0 - 3111.0 * $T2 + 543.0 * $T4 - $T6);

		$UTMEasting = $M0 * ($AA0 * $D + $AA1 * $D3 / 6.0 + $AA2 * $D5 / 120.0 + $AA3 * $D7 / 5040.0) + 500000.0;
		$UTMNorthing = $M0 * ($B0 + $B1 * $D2 / 2.0 + $B2 * $D4 / 24.0 + $B3 * $D6 / 720.0 + $B4 * $D8 / 40320.0);
		return array($UTMEasting,$UTMNorthing);
	} // fin de la fonction LatLong_to_UTM


} // fin de classe CartesController