Skip to content

Commit 142e508

Browse files
committed
Tobai_GeoStoreSwitcher v1.0.0
0 parents  commit 142e508

Some content is hidden

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

46 files changed

+3444
-0
lines changed
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Block\Adminhtml\System\Config;
6+
7+
class Form extends \Magento\Config\Block\System\Config\Form
8+
{
9+
/**
10+
* @var \Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface
11+
*/
12+
protected $groupGenerator;
13+
14+
/**
15+
* @param \Magento\Backend\Block\Template\Context $context
16+
* @param \Magento\Framework\Registry $registry
17+
* @param \Magento\Framework\Data\FormFactory $formFactory
18+
* @param \Magento\Config\Model\Config\Factory $configFactory
19+
* @param \Magento\Config\Model\Config\Structure $configStructure
20+
* @param \Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory
21+
* @param \Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory
22+
* @param \Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface $groupGenerator
23+
* @param array $data
24+
*/
25+
public function __construct(
26+
\Magento\Backend\Block\Template\Context $context,
27+
\Magento\Framework\Registry $registry,
28+
\Magento\Framework\Data\FormFactory $formFactory,
29+
\Magento\Config\Model\Config\Factory $configFactory,
30+
\Magento\Config\Model\Config\Structure $configStructure,
31+
\Magento\Config\Block\System\Config\Form\Fieldset\Factory $fieldsetFactory,
32+
\Magento\Config\Block\System\Config\Form\Field\Factory $fieldFactory,
33+
\Tobai\GeoStoreSwitcher\Model\Config\System\GroupGeneratorInterface $groupGenerator,
34+
array $data = []
35+
) {
36+
$this->groupGenerator = $groupGenerator;
37+
parent::__construct(
38+
$context,
39+
$registry,
40+
$formFactory,
41+
$configFactory,
42+
$configStructure,
43+
$fieldsetFactory,
44+
$fieldFactory,
45+
$data
46+
);
47+
}
48+
49+
/**
50+
* Initialize form
51+
*
52+
* @return $this
53+
*/
54+
public function initForm()
55+
{
56+
$this->_initObjects();
57+
58+
/** @var \Magento\Framework\Data\Form $form */
59+
$form = $this->_formFactory->create();
60+
/** @var $section \Magento\Config\Model\Config\Structure\Element\Section */
61+
$section = $this->_configStructure->getElement($this->getSectionCode());
62+
if ($section && $section->isVisible($this->getWebsiteCode(), $this->getStoreCode())) {
63+
foreach ($section->getChildren() as $group) {
64+
$this->_initGroup($group, $section, $form);
65+
}
66+
$sortOrder = isset($group) && $group->getAttribute('sortOrder') ? $group->getAttribute('sortOrder') : 1;
67+
$sortOrder++;
68+
foreach ($this->groupGenerator->generate($sortOrder) as $group) {
69+
$this->_initGroup($group, $section, $form);
70+
}
71+
}
72+
73+
$this->setForm($form);
74+
return $this;
75+
}
76+
}

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 ToBai
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Model/Config/General.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config;
6+
7+
use Magento\Framework\App\Config\ScopeConfigInterface;
8+
9+
class General
10+
{
11+
/**
12+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
13+
*/
14+
protected $scopeConfig;
15+
16+
/**
17+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
18+
*/
19+
public function __construct(
20+
ScopeConfigInterface $scopeConfig
21+
) {
22+
$this->scopeConfig = $scopeConfig;
23+
}
24+
25+
/**
26+
* @return bool
27+
*/
28+
public function isActive()
29+
{
30+
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/active');
31+
}
32+
33+
/**
34+
* @return bool
35+
*/
36+
public function isOverwriteDefault()
37+
{
38+
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/overwrite_default');
39+
}
40+
41+
/**
42+
* @return string|bool
43+
*/
44+
public function getDefaultStore()
45+
{
46+
return $this->isOverwriteDefault()
47+
? $this->scopeConfig->getValue('tobai_geo_store_switcher/general/default_store')
48+
: false;
49+
}
50+
51+
/**
52+
* @return bool
53+
*/
54+
public function isMappingSore()
55+
{
56+
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/mapping_sore');
57+
}
58+
59+
/**
60+
* @return bool
61+
*/
62+
public function isCountries()
63+
{
64+
return $this->scopeConfig->isSetFlag('tobai_geo_store_switcher/general/by_countries');
65+
}
66+
67+
/**
68+
* @return array
69+
*/
70+
public function getCountryList()
71+
{
72+
$countriesData = $this->scopeConfig->getValue('tobai_geo_store_switcher/general/country_list');
73+
$countries = $this->isCountries() && !empty($countriesData) ? explode(',', $countriesData) : [];
74+
return $countries;
75+
}
76+
77+
/**
78+
* @param string $countryCode
79+
* @return string
80+
*/
81+
public function getCountryStore($countryCode)
82+
{
83+
return $this->scopeConfig->getValue("tobai_geo_store_switcher/{$countryCode}/store");
84+
}
85+
86+
/**
87+
* @return int
88+
*/
89+
public function getGroupCount()
90+
{
91+
return (int)$this->scopeConfig->getValue('tobai_geo_store_switcher/general/by_groups');
92+
}
93+
94+
/**
95+
* @param int $group
96+
* @return array
97+
*/
98+
public function getGroupCountryList($group)
99+
{
100+
$countriesData = $this->scopeConfig->getValue("tobai_geo_store_switcher/group_{$group}/country_list");
101+
$countries = !empty($countriesData) ? explode(',', $countriesData) : [];
102+
return $countries;
103+
}
104+
105+
/**
106+
* @param int $group
107+
* @return string
108+
*/
109+
public function getGroupStore($group)
110+
{
111+
return $this->scopeConfig->getValue("tobai_geo_store_switcher/group_{$group}/store");
112+
}
113+
}

Model/Config/Source/Website.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config\Source;
6+
7+
use Magento\Store;
8+
9+
class Website implements \Magento\Framework\Option\ArrayInterface
10+
{
11+
/**
12+
* @var \Magento\Store\Model\System\Store
13+
*/
14+
protected $store;
15+
16+
/**
17+
* @param \Magento\Store\Model\System\Store $store
18+
*/
19+
public function __construct(
20+
Store\Model\System\Store $store
21+
) {
22+
$this->store = $store;
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
public function toOptionArray()
29+
{
30+
return $this->store->getWebsiteValuesForForm(true, false);
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 ToBai. All rights reserved.
4+
*/
5+
namespace Tobai\GeoStoreSwitcher\Model\Config\System\GroupGenerator;
6+
7+
use Magento\Config\Model\Config\Structure\Element\FlyweightFactory;
8+
use Magento\Directory\Model\CountryFactory;
9+
use Tobai\GeoStoreSwitcher\Model;
10+
use Tobai\GeoStoreSwitcher\Model\Config\System;
11+
12+
class Country extends System\GroupGeneratorAbstract implements System\GroupGeneratorInterface
13+
{
14+
/**
15+
* @var \Magento\Directory\Model\CountryFactory
16+
*/
17+
protected $countryFactory;
18+
19+
/**
20+
* @param \Magento\Config\Model\Config\Structure\Element\FlyweightFactory $flyweightFactory
21+
* @param \Tobai\GeoStoreSwitcher\Model\Config\General $generalConfig
22+
* @param \Magento\Directory\Model\CountryFactory $countryFactory
23+
*/
24+
public function __construct(
25+
FlyweightFactory $flyweightFactory,
26+
Model\Config\General $generalConfig,
27+
CountryFactory $countryFactory
28+
) {
29+
$this->countryFactory = $countryFactory;
30+
parent::__construct($flyweightFactory, $generalConfig);
31+
}
32+
33+
34+
/**
35+
* @param int $sortOrder
36+
* @return array
37+
*/
38+
public function generate(&$sortOrder = 1)
39+
{
40+
$countries = $this->generalConfig->getCountryList();
41+
$countriesGroups = [];
42+
foreach ($countries as $countryCode) {
43+
$groupData = [
44+
'_elementType' => 'group',
45+
'id' => $countryCode,
46+
'label' => $this->getCountry($countryCode)->getName(),
47+
'path' => 'tobai_geo_store_switcher',
48+
'showInDefault' => '1',
49+
'showInStore' => '0',
50+
'showInWebsite' => '0',
51+
'sortOrder' => $sortOrder++,
52+
'type' => 'text',
53+
'children' => [
54+
'store' => [
55+
'_elementType' => 'field',
56+
'id' => 'store',
57+
'label' => (string)__('Set Store View'),
58+
'path' => 'tobai_geo_store_switcher/' . $countryCode,
59+
'showInDefault' => '1',
60+
'showInStore' => '0',
61+
'showInWebsite' => '0',
62+
'sortOrder' => '1',
63+
'source_model' => 'Magento\Store\Model\System\Store',
64+
'type' => 'select',
65+
'depends' => [
66+
'fields' => [
67+
'active' => [
68+
'_elementType' => 'field',
69+
'id' => 'tobai_geo_store_switcher/general/active',
70+
'value' => '1',
71+
'dependPath' => [
72+
'tobai_geo_store_switcher',
73+
'general',
74+
'active'
75+
]
76+
]
77+
]
78+
]
79+
]
80+
]
81+
];
82+
$countriesGroups[$countryCode] = $this->createGroup($groupData);
83+
}
84+
return $countriesGroups;
85+
}
86+
87+
/**
88+
* @param string $countryCode
89+
* @return \Magento\Directory\Model\Country
90+
*/
91+
protected function getCountry($countryCode)
92+
{
93+
return $this->countryFactory->create()->loadByCode($countryCode);
94+
}
95+
}

0 commit comments

Comments
 (0)