Skip to content

Commit

Permalink
Compatible Osc & GeoIP
Browse files Browse the repository at this point in the history
  • Loading branch information
haitv282 committed Jan 31, 2019
1 parent 4f6675a commit 6300a53
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
47 changes: 27 additions & 20 deletions Helper/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Address extends Data

/**
* Address constructor.
*
* @param Context $context
* @param ObjectManagerInterface $objectManager
* @param StoreManagerInterface $storeManager
Expand All @@ -71,11 +72,10 @@ public function __construct(
DirectoryList $directoryList,
Resolver $localeResolver,
Region $regionModel
)
{
$this->_directoryList = $directoryList;
) {
$this->_directoryList = $directoryList;
$this->_localeResolver = $localeResolver;
$this->_regionModel = $regionModel;
$this->_regionModel = $regionModel;

parent::__construct($context, $objectManager, $storeManager);
}
Expand All @@ -93,7 +93,7 @@ public function checkHasLibrary()
return false;
}

$folder = scandir($path, true);
$folder = scandir($path, true);
$pathFile = $path . '/' . $folder[0] . '/GeoLite2-City.mmdb';
if (!file_exists($pathFile)) {
return false;
Expand All @@ -103,33 +103,40 @@ public function checkHasLibrary()
}

/**
* @param null $storeId
*
* @return array
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function getGeoIpData()
public function getGeoIpData($storeId = null)
{
$libPath = $this->checkHasLibrary();

if ($this->getConfigValue('geoip/general/enable') && $libPath && class_exists('GeoIp2\Database\Reader')) {
try {
$geoIp = new \GeoIp2\Database\Reader($libPath, $this->getLocales());

try {
$libPath = $this->checkHasLibrary();
if ($this->isEnabled($storeId) && $libPath && class_exists('GeoIp2\Database\Reader')) {
$geoIp = new \GeoIp2\Database\Reader($libPath, $this->getLocales());
$record = $geoIp->city($this->_request->getParam('fakeIp', null) ?: $this->getIpAddress());
// $record = $geoIp->city($this->getIpAddress());

$geoIpData = [
'city' => $record->city->name,
'country_id' => $record->country->isoCode,
'postcode' => $record->postal->code
];
} catch (\Exception $e) {

if ($record->mostSpecificSubdivision) {
$code = $record->mostSpecificSubdivision->isoCode;
if ($regionId = $this->_regionModel->loadByCode($code, $record->country->isoCode)->getId()) {
$geoIpData['region_id'] = $regionId;
} else {
$geoIpData['region'] = $record->mostSpecificSubdivision->name;
}
}
} else {
$geoIpData = [];
}

return $geoIpData;
} catch (\Exception $e) {
$geoIpData = [];
}

return [];
return $geoIpData;
}

/**
Expand All @@ -149,7 +156,7 @@ public function getIpAddress()
}

$ipArr = explode(',', $ip);
$ip = $ipArr[count($ipArr) - 1];
$ip = $ipArr[count($ipArr) - 1];

return trim($ip);
}
Expand All @@ -159,7 +166,7 @@ public function getIpAddress()
*/
protected function getLocales()
{
$locale = $this->_localeResolver->getLocale();
$locale = $this->_localeResolver->getLocale();
$language = substr($locale, 0, 2) ? substr($locale, 0, 2) : 'en';

$locales = [$language];
Expand Down
4 changes: 2 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="geoip" translate="label comment" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
<label>GeoIp</label>
<label>GeoIP</label>
<tab>mageplaza</tab>
<resource>Mageplaza_GeoIP::configuration</resource>
<group id="general" translate="label comment" sortOrder="10" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
<label>GeoIP Configuration</label>
<field id="enable" translate="label comment" sortOrder="1" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
<field id="enabled" translate="label comment" sortOrder="1" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Enable GeoIP</label>
<comment>Please download library before enabling GeoIP</comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
Expand Down

0 comments on commit 6300a53

Please sign in to comment.