Skip to content

Commit 81153ee

Browse files
committed
Tobai_GeoStoreSwitcher v1.1.0
1 parent 142e508 commit 81153ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1621
-122
lines changed

Block/Adminhtml/System/Config/Form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright © 2015 ToBai. All rights reserved.
3+
* Copyright © 2016 ToBai. All rights reserved.
44
*/
55
namespace Tobai\GeoStoreSwitcher\Block\Adminhtml\System\Config;
66

Helper/Config/AppState.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Helper\Config;
6+
7+
use Magento\Framework\App\Area;
8+
use Magento\Framework\Exception\LocalizedException;
9+
10+
class AppState
11+
{
12+
/**
13+
* @var \Magento\Framework\App\State
14+
*/
15+
protected $state;
16+
17+
/**
18+
* @param \Magento\Framework\App\State $state
19+
*/
20+
public function __construct(\Magento\Framework\App\State $state) {
21+
$this->state = $state;
22+
}
23+
24+
/**
25+
* @return bool
26+
*/
27+
public function isFrontendArea()
28+
{
29+
try {
30+
if ($this->state->getAreaCode() == Area::AREA_ADMINHTML) {
31+
return false;
32+
}
33+
} catch (LocalizedException $e) {
34+
/* Area is not initialized. Do nothing. */
35+
return false;
36+
}
37+
return true;
38+
}
39+
}

Helper/Config/Request.php

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Helper\Config;
6+
7+
class Request
8+
{
9+
/**
10+
* @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
11+
*/
12+
protected $remoteAddress;
13+
14+
/**
15+
* @var \Magento\Framework\HTTP\Header
16+
*/
17+
protected $httpHeader;
18+
19+
/**
20+
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
21+
* @param \Magento\Framework\HTTP\Header $httpHeader
22+
*/
23+
public function __construct(
24+
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress,
25+
\Magento\Framework\HTTP\Header $httpHeader
26+
) {
27+
$this->remoteAddress = $remoteAddress;
28+
$this->httpHeader = $httpHeader;
29+
}
30+
31+
/**
32+
* @param array $whiteIps
33+
* @return bool
34+
*/
35+
public function isCurrentIp($whiteIps)
36+
{
37+
$remoteIp = $this->remoteAddress->getRemoteAddress();
38+
return !empty($whiteIps) && !empty($remoteIp) && array_search($remoteIp, $whiteIps) !== false;
39+
}
40+
41+
/**
42+
* @param string $uaRegex
43+
* @return bool
44+
*/
45+
public function isCurrentUserAgent($uaRegex)
46+
{
47+
return $uaRegex && @preg_match($uaRegex, $this->httpHeader->getHttpUserAgent());
48+
}
49+
}

Model/Config/Backend/Regexp.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config\Backend;
6+
7+
class Regexp extends \Magento\Framework\App\Config\Value
8+
{
9+
/**
10+
* @var \Magento\Framework\Message\ManagerInterface
11+
*/
12+
protected $messageManager;
13+
14+
/**
15+
* @param \Magento\Framework\Model\Context $context
16+
* @param \Magento\Framework\Registry $registry
17+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
18+
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
19+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
20+
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
21+
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
22+
* @param array $data
23+
*/
24+
public function __construct(
25+
\Magento\Framework\Model\Context $context,
26+
\Magento\Framework\Registry $registry,
27+
\Magento\Framework\App\Config\ScopeConfigInterface $config,
28+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
29+
\Magento\Framework\Message\ManagerInterface $messageManager,
30+
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
31+
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
32+
array $data = []
33+
) {
34+
$this->messageManager = $messageManager;
35+
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
36+
}
37+
38+
39+
/**
40+
* @return $this
41+
* @throws \Magento\Framework\Exception\LocalizedException
42+
*/
43+
public function beforeSave()
44+
{
45+
if (!empty($this->getValue()) && !$this->_isRegexp($this->getValue())) {
46+
$this->messageManager->addNotice(
47+
__('Invalid regular expression: %value', ['value' => $this->getValue()])
48+
);
49+
$this->setValue(null);
50+
}
51+
return parent::beforeSave();
52+
}
53+
54+
/**
55+
* @param string $expression
56+
* @return bool
57+
* @throws \Magento\Framework\Exception\LocalizedException on invalid regular expression
58+
*/
59+
protected function _isRegexp($expression)
60+
{
61+
return @preg_match($expression, '') !== false;
62+
}
63+
}

Model/Config/General.php

+72-15
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,95 @@
11
<?php
22
/**
3-
* Copyright © 2015 ToBai. All rights reserved.
3+
* Copyright © 2016 ToBai. All rights reserved.
44
*/
55
namespace Tobai\GeoStoreSwitcher\Model\Config;
66

7-
use Magento\Framework\App\Config\ScopeConfigInterface;
7+
use Magento\Store\Api\Data\StoreInterface;
88

99
class General
1010
{
1111
/**
12-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
12+
* @var \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig
1313
*/
1414
protected $scopeConfig;
1515

1616
/**
17-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
17+
* @var \Tobai\GeoStoreSwitcher\Helper\Config\AppState
18+
*/
19+
protected $appStateHelper;
20+
21+
/**
22+
* @var \Tobai\GeoStoreSwitcher\Helper\Config\Request
23+
*/
24+
protected $requestHelper;
25+
26+
/**
27+
* @param \Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig
28+
* @param \Tobai\GeoStoreSwitcher\Helper\Config\AppState $appStateHelper
29+
* @param \Tobai\GeoStoreSwitcher\Helper\Config\Request $requestHelper
1830
*/
1931
public function __construct(
20-
ScopeConfigInterface $scopeConfig
32+
\Tobai\GeoStoreSwitcher\Model\Config\ScopeConfig $scopeConfig,
33+
\Tobai\GeoStoreSwitcher\Helper\Config\AppState $appStateHelper,
34+
\Tobai\GeoStoreSwitcher\Helper\Config\Request $requestHelper
2135
) {
2236
$this->scopeConfig = $scopeConfig;
37+
$this->appStateHelper = $appStateHelper;
38+
$this->requestHelper = $requestHelper;
39+
}
40+
41+
/**
42+
* @param \Magento\Store\Api\Data\StoreInterface $store
43+
* @return $this
44+
*/
45+
public function setOriginStore(StoreInterface $store)
46+
{
47+
$this->scopeConfig->setOriginStore($store);
48+
return $this;
49+
}
50+
51+
/**
52+
* @return bool
53+
*/
54+
public function isAvailable()
55+
{
56+
return $this->appStateHelper->isFrontendArea()
57+
&& !$this->requestHelper->isCurrentIp($this->getWhiteIps())
58+
&& !$this->requestHelper->isCurrentUserAgent($this->getWhiteUa())
59+
&& $this->isActive();
2360
}
2461

2562
/**
2663
* @return bool
2764
*/
2865
public function isActive()
2966
{
30-
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/active');
67+
return $this->scopeConfig->getFrontendStoreOrBackendValue('tobai_geo_store_switcher/general/active');
68+
}
69+
70+
/**
71+
* @return array
72+
*/
73+
public function getWhiteIps()
74+
{
75+
$whiteIps = $this->scopeConfig->getStoreValue('tobai_geo_store_switcher/general/white_ips');
76+
return !empty($whiteIps) ? preg_split('#\s*,\s*#', $whiteIps, null, PREG_SPLIT_NO_EMPTY) : [];
77+
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function getWhiteUa()
83+
{
84+
return $this->scopeConfig->getStoreValue('tobai_geo_store_switcher/general/white_ua');
3185
}
3286

3387
/**
3488
* @return bool
3589
*/
3690
public function isOverwriteDefault()
3791
{
38-
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/overwrite_default');
92+
return (bool)$this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/overwrite_default');
3993
}
4094

4195
/**
@@ -44,7 +98,7 @@ public function isOverwriteDefault()
4498
public function getDefaultStore()
4599
{
46100
return $this->isOverwriteDefault()
47-
? $this->scopeConfig->getValue('tobai_geo_store_switcher/general/default_store')
101+
? $this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/default_store')
48102
: false;
49103
}
50104

@@ -53,23 +107,24 @@ public function getDefaultStore()
53107
*/
54108
public function isMappingSore()
55109
{
56-
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/mapping_sore');
110+
return (bool)$this->scopeConfig->getWebsiteValue('tobai_geo_store_switcher/general/mapping_sore');
57111
}
58112

59113
/**
60114
* @return bool
61115
*/
62116
public function isCountries()
63117
{
64-
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/by_countries');
118+
return $this->isActive()
119+
&& $this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/by_countries');
65120
}
66121

67122
/**
68123
* @return array
69124
*/
70125
public function getCountryList()
71126
{
72-
$countriesData = $this->scopeConfig->getValue('tobai_geo_store_switcher/general/country_list');
127+
$countriesData = $this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/country_list');
73128
$countries = $this->isCountries() && !empty($countriesData) ? explode(',', $countriesData) : [];
74129
return $countries;
75130
}
@@ -80,15 +135,17 @@ public function getCountryList()
80135
*/
81136
public function getCountryStore($countryCode)
82137
{
83-
return $this->scopeConfig->getValue("tobai_geo_store_switcher/{$countryCode}/store");
138+
return $this->scopeConfig->getWebsiteValue("tobai_geo_store_switcher/{$countryCode}/store");
84139
}
85140

86141
/**
87142
* @return int
88143
*/
89144
public function getGroupCount()
90145
{
91-
return (int)$this->scopeConfig->getValue('tobai_geo_store_switcher/general/by_groups');
146+
return $this->isActive()
147+
? (int)$this->scopeConfig->getFrontendWebsiteOrBackendValue('tobai_geo_store_switcher/general/by_groups')
148+
: 0;
92149
}
93150

94151
/**
@@ -97,7 +154,7 @@ public function getGroupCount()
97154
*/
98155
public function getGroupCountryList($group)
99156
{
100-
$countriesData = $this->scopeConfig->getValue("tobai_geo_store_switcher/group_{$group}/country_list");
157+
$countriesData = $this->scopeConfig->getFrontendWebsiteOrBackendValue("tobai_geo_store_switcher/group_{$group}/country_list");
101158
$countries = !empty($countriesData) ? explode(',', $countriesData) : [];
102159
return $countries;
103160
}
@@ -108,6 +165,6 @@ public function getGroupCountryList($group)
108165
*/
109166
public function getGroupStore($group)
110167
{
111-
return $this->scopeConfig->getValue("tobai_geo_store_switcher/group_{$group}/store");
168+
return $this->scopeConfig->getWebsiteValue("tobai_geo_store_switcher/group_{$group}/store");
112169
}
113170
}

0 commit comments

Comments
 (0)