Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/lib/djgeocoder.php |
<?php
/**
* @package DJ-Classifieds
* @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
* @license http://www.gnu.org/licenses GNU/GPL
* @author url: http://dj-extensions.com
* @author email contact@dj-extensions.com
*/
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djtheme.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djregion.php');
require_once(JPATH_ROOT.'/administrator/components/com_djclassifieds/lib/djimage.php');
class DJClassifiedsGeocode
{
static private $url = "https://maps.google.com/maps/api/geocode/json";
public static function getLocation($address, $region_id = null)
{
$app = JFactory::getApplication();
$par = JComponentHelper::getParams('com_djclassifieds');
if($par->get('map_provider', 'google') == 'none'){
return false;
}
if($par->get('geolocation', '1') == '0'){
return false;
}
$geo_address = $region_id ? self::getGeolocAddress($region_id, $address) : $address;
JPluginHelper::importPlugin('djclassifieds');
$use_gm = 1;
$lat_lng = $app->triggerEvent('onGeocoderGetLocation', array (&$use_gm, &$geo_address));
$lat_lng = isset($lat_lng[0]) && is_array($lat_lng[0]) ? $lat_lng[0] : $lat_lng;
if(empty($geo_address)){
return false;
}
if($use_gm){
$url = self::$url."?address=".urlencode($geo_address);
$params = JComponentHelper::getParams( 'com_djclassifieds' );
if($params->get('map_api_key','')){
$url .= '&key='.trim($params->get('map_api_key',''));
}
$resp_json = self::curl_file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']='OK' && isset($resp['results'][0])){
$lat_lng = $resp['results'][0]['geometry']['location'];
}elseif(isset($resp['error_message'])){
$app->enqueueMessage($resp['error_message'], 'error');
return false;
}
}
if(empty($lat_lng) && $region_id){
$lat_lng = self::getRegionLatLon($region_id);
}
return $lat_lng;
}
public static function getLocationPostCode($post_code, $address = '', $region_id = null)
{
$app = JFactory::getApplication();
$par = JComponentHelper::getParams('com_djclassifieds');
if($par->get('map_provider', 'google') == 'none'){
return false;
}
if($par->get('geolocation', '1') == '0'){
return false;
}
$geo_address = $region_id ? self::getGeolocAddress($region_id, $address) : $address;
JPluginHelper::importPlugin('djclassifieds');
$use_gm = 1;
$lat_lng = $app->triggerEvent('onGeocoderGetLocationPostCode', array (&$use_gm, &$post_code, &$geo_address));
$lat_lng = isset($lat_lng[0]) && is_array($lat_lng[0]) ? $lat_lng[0] : $lat_lng;
if(!$lat_lng){ // backward compatibility for older map plugins not supporting 'onGeocoderGetLocationPostCode' event
$lat_lng = self::getLocation($geo_address.', '.$post_code);
}
if(empty($post_code)){
return false;
}
if($use_gm){
$url_zip = '';
if($geo_address){
$url_zip = '?address='.urlencode($geo_address);
$url = self::$url.$url_zip."&components=postal_code:".urlencode($post_code);
}else{
$url = self::$url."?components=postal_code:".urlencode($post_code);
}
$params = JComponentHelper::getParams('com_djclassifieds');
if($params->get('map_api_key','')){
$url .= '&key='.trim($params->get('map_api_key',''));
}
$resp_json = self::curl_file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']='OK' && isset($resp['results'][0])){
return $resp['results'][0]['geometry']['location'];
}elseif(!empty($resp['error_message'])){
JFactory::getApplication()->enqueueMessage($resp['error_message'], 'error');
return false;
}else{
return self::getLocation($geo_address.', '.$post_code);
}
}
return $lat_lng;
}
public static function getRegionLatLon($region_id)
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__djcf_regions WHERE id=".$region_id;
$db->setQuery($query);
$reg = $db->loadObject();
if($reg && floatval($reg->latitude) && floatval($reg->longitude)){
return array('lat' => $reg->latitude, 'lng' => $reg->longitude);
}
}
public static function getAddressLatLon($lat_lng)
{
$app = JFactory::getApplication();
$par = JComponentHelper::getParams('com_djclassifieds');
if($par->get('map_provider', 'google') == 'none'){
return false;
}
if($par->get('geolocation', '1') == '0'){
return false;
}
JPluginHelper::importPlugin( 'djclassifieds' );
$use_gm = 1;
$user_address = $app->triggerEvent('onGeocoderGetAddressLatLon', array (&$use_gm, &$lat_lng));
$user_address = isset($user_address[0]) ? $user_address[0] : $user_address;
if(isset($user_address['message'])){ // error message
JFactory::getApplication()->enqueueMessage($user_address['message'], 'error');
return false;
}
if($use_gm){
$url = self::$url."?latlng=".urlencode($lat_lng);
$params = JComponentHelper::getParams( 'com_djclassifieds' );
if($params->get('map_api_key','')){
$url .= '&key='.$params->get('map_api_key','');
}else{
return false;
}
$resp_json = self::curl_file_get_contents($url);
$resp = json_decode($resp_json, true);
if($resp['status']='OK' && isset($resp['results'][0])){
$user_address = $resp['results'][0]['formatted_address'];
return $user_address;
}else{
JFactory::getApplication()->enqueueMessage($resp['error_message'], 'error');
return false;
}
}
return $user_address;
}
static private function curl_file_get_contents($URL)
{
DJClassifiedsGeocode::serverFnCheck();
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
$contents = curl_exec($c);
curl_close($c);
if($contents){
return $contents;
}else{
return false;
}
}
static function getMarkerIconImg($par, $cat_id = null)
{
$cat_icon = self::getCategoryMarkerIconImg($cat_id);
if($cat_icon){
return $cat_icon;
}elseif($par->get('map_marker_icon') && JFile::exists(JPATH_ROOT.'/'.DJClassifiedsImage::decodeMediaPath($par->get('map_marker_icon')))){
return DJClassifiedsImage::decodeMediaPath($par->get('map_marker_icon'));
}else if(JFile::exists(JPATH_ROOT.'/'.DJClassifiedsTheme::getImgAssetPath('djcf_gmicon.png'))){
return DJClassifiedsTheme::getImgAssetPath('djcf_gmicon.png');
}
}
static function getCategoryMarkerIconImg($cat_id)
{
if($cat_id){
$db = JFactory::getDBO();
$query = "SELECT map_marker_icon FROM #__djcf_categories WHERE id=".$cat_id;
$db->setQuery($query);
$map_marker_icon = $db->loadResult();
if($map_marker_icon && JFile::exists(JPATH_ROOT.'/'.DJClassifiedsImage::decodeMediaPath($map_marker_icon))){
return DJClassifiedsImage::decodeMediaPath($map_marker_icon);
}
}
}
static function getGeolocAddress($region_id, $address = null)
{
$address_arr = array();
if($region_id){
$reg_path = DJClassifiedsRegion::getParentPath($region_id);
foreach($reg_path as $reg){
if($reg->country || $reg->city){
$address_arr[] = $reg->name;
}
}
}
$address_arr = array_reverse($address_arr);
if($address){
$address_arr[] = $address;
}
$address = implode(', ', $address_arr);
return $address;
}
static function serverFnCheck($fn_arr = array('curl_version', 'curl_exec'))
{
foreach($fn_arr as $fn){
if(!function_exists($fn)){
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_DJCLASSIFIEDS_SERVER_FUNCTION_NOT_FOUND', 'curl_exec'), 'error');
return false;
}
}
return true;
}
}