<?php

class gestion_gplace {
	
	var $secret_key = "";
	
	function gestion_gplace($key)
	{
		$this->secret_key = $key;
	}
	
	
	function get_gplace($gplace_id)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$data = array();
		
		$sql = "SELECT *
			FROM point_interet
			WHERE pointinteret_gplaceid = '".$db->sql_inject($gplace_id)."'";
		$query = $db->sql_query($sql);
		
		if($result_gplace = $db->sql_fetchrow($query))
		{
			if($result_gplace["pointinteret_lat"] == "" || $result_gplace["pointinteret_lng"] == "")
			{
				$rsc = curl_init("https://maps.googleapis.com/maps/api/place/details/json?key=".$this->secret_key."&placeid=".$gplace_id);
				curl_setopt($rsc, CURLOPT_RETURNTRANSFER, true);
				$json_gplace = curl_exec($rsc);
				curl_close($rsc);
				
				$result_gplace2 = json_decode($json_gplace);
				
				$sql = "UPDATE point_interet
					SET pointinteret_lat = '".$db->sql_inject($result_gplace2->result->geometry->location->lat)."',
						pointinteret_lng = '".$db->sql_inject($result_gplace2->result->geometry->location->lng)."'
					WHERE pointinteret_gplaceid = '".$db->sql_inject($gplace_id)."'";
				$db->sql_query($sql);
				
				$result_gplace["pointinteret_lat"] = $result_gplace2->result->geometry->location->lat;
				$result_gplace["pointinteret_lng"] = $result_gplace2->result->geometry->location->lng;
			}
			
			
			$data = array("gplace_name"=>$result_gplace["pointinteret_name"],"gplace_id"=>$gplace_id, "glplace_adresse"=>$result_gplace["pointinteret_adress"],
				"gplace_cp"=>$result_gplace["pointinteret_cp"], "gplace_ville"=>$result_gplace["pointinteret_ville"],
				"gplace_tel"=>$result_gplace["pointinteret_tel"], "gplace_website"=>$result_gplace["pointinteret_website"],
				"gplace_types"=>explode(",",$result_gplace["pointinteret_type"]),
				"photo_ref"=>$result_gplace["pointinteret_photo"], "gplace_lat"=>$result_gplace["pointinteret_lat"], "gplace_lng"=>$result_gplace["pointinteret_lng"]);
		}
		else{
			$rsc = curl_init("https://maps.googleapis.com/maps/api/place/details/json?key=".$this->secret_key."&placeid=".$gplace_id);
			curl_setopt($rsc, CURLOPT_RETURNTRANSFER, true);
			$json_gplace = curl_exec($rsc);
			curl_close($rsc);
			
			$result_gplace = json_decode($json_gplace);
			
			$adresse = "";
			$nb_rue = "";
			$nom_rue = "";
			$ville = "";
			$cp = "";
			$ouverture = "";
			
			if(!isset($result_gplace->error_message))
			{
				foreach($result_gplace->result->address_components as $adr_cmpnt)
				{
					if($adr_cmpnt->types[0] == "street_number")
					{
						$nb_rue = $adr_cmpnt->long_name;
					}
					
					if($adr_cmpnt->types[0] == "route")
					{
						$nom_rue = $adr_cmpnt->long_name;
					}
					
					if($adr_cmpnt->types[0] == "locality")
					{
						$ville = $adr_cmpnt->long_name;
					}
					
					if($adr_cmpnt->types[0] == "postal_code")
					{
						$cp = $adr_cmpnt->long_name;
					}
				}
				
				$adresse = $nb_rue." ".$nom_rue;
				
				if(isset($result_gplace->result->opening_hours))
				{
					if($result_gplace->result->opening_hours->open_now)
					{
						$ouverture = "Ouvert";
					}
					else{
						$ouverture = "Fermé";
					}
				}
				
				
				$photo_ref = "";
				
				if(isset($result_gplace->result->photos))
				{
					$photo_ref = $result_gplace->result->photos[0]->photo_reference;
				}
				
				
				$sql = "INSERT INTO point_interet
					(pointinteret_type,
					pointinteret_gplaceid,
					pointinteret_name,
					pointinteret_cp,
					pointinteret_ville,
					pointinteret_adress,
					pointinteret_website,
					pointinteret_tel,
					pointinteret_photo,
					pointinteret_lat,
					pointinteret_lng)
					VALUES
					('".$db->sql_inject(implode(",",$result_gplace->result->types))."',
					'".$db->sql_inject($gplace_id)."',
					'".$db->sql_inject($result_gplace->result->name)."',
					'".$db->sql_inject($cp)."',
					'".$db->sql_inject($ville)."',
					'".$db->sql_inject($nb_rue." ".$nom_rue)."',
					'".$db->sql_inject($result_gplace->result->website)."',
					'".$db->sql_inject($result_gplace->result->formatted_phone_number)."',
					'".$db->sql_inject($photo_ref)."',
					'".$db->sql_inject($result_gplace->result->geometry->location->lat)."',
					'".$db->sql_inject($result_gplace->result->geometry->location->lng)."')";
				$db->sql_query($sql);
				
				
				$data = array("gplace_name"=>$result_gplace->result->name,"gplace_id"=>$result_gplace->result->place_id, "glplace_adresse"=>$nb_rue." ".$nom_rue,
							"gplace_cp"=>$cp, "gplace_ville"=>$ville,
							"gplace_tel"=>$result_gplace->result->formatted_phone_number, "gplace_ouverture"=>$ouverture, "gplace_website"=>$result_gplace->result->website,
							"gplace_types"=>$result_gplace->result->types, "photo_ref"=>$photo_ref,
							"gplace_lat"=>$result_gplace->result->geometry->location->lat, "gplace_lng"=>$result_gplace->result->geometry->location->lng);
			}
			else{
				$data = array("error"=>$result_gplace->error_message);
			}
		}
		
		return $data;
	}
	
	
	function get_point_interet($pointinteret_id)
	{
		global $requete,$db,$template,$tpl,$convert,$widget;
		
		$data = array();
		
		$sql = "SELECT *
			FROM point_interet
			WHERE pointinteret_id = '".$db->sql_inject($pointinteret_id)."'";
		$query = $db->sql_query($sql);
		
		if($result = $db->sql_fetchrow($query))
		{
			$data = array("gplace_name"=>$result["pointinteret_name"],"pointinteret_id"=>$result["pointinteret_id"],
				"gplace_types"=>explode(",",$result["pointinteret_type"]));
		}
		else{
			$data = array("error"=>"no_result");
		}
		
		return $data;
	}
	
	
}

?>