Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Locale.php
Go to the documentation of this file.
1 <?php
11 
13 
18 class Locale extends \Magento\Framework\App\Config\Value
19 {
23  protected $_configsFactory;
24 
28  protected $_websiteFactory;
29 
33  protected $_storeFactory;
34 
38  protected $_localeCurrency;
39 
55  public function __construct(
56  \Magento\Framework\Model\Context $context,
57  \Magento\Framework\Registry $registry,
59  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
60  \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configsFactory,
61  \Magento\Store\Model\WebsiteFactory $websiteFactory,
62  \Magento\Store\Model\StoreFactory $storeFactory,
63  \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
64  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
65  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
66  array $data = []
67  ) {
68  $this->_configsFactory = $configsFactory;
69  $this->_websiteFactory = $websiteFactory;
70  $this->_storeFactory = $storeFactory;
71  $this->_localeCurrency = $localeCurrency;
72  parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
73  }
74 
79  public function afterSave()
80  {
82  $collection = $this->_configsFactory->create();
83  $collection->addPathFilter('currency/options');
84 
85  $values = explode(',', $this->getValue());
86  $exceptions = [];
87 
88  foreach ($collection as $data) {
89  $match = false;
90  $scopeName = __('Default scope');
91 
92  if (preg_match('/(base|default)$/', $data->getPath(), $match)) {
93  if (!in_array($data->getValue(), $values)) {
94  $currencyName = $this->_localeCurrency->getCurrency($data->getValue())->getName();
95  if ($match[1] == 'base') {
96  $fieldName = __('Base currency');
97  } else {
98  $fieldName = __('Display default currency');
99  }
100 
101  switch ($data->getScope()) {
103  $scopeName = __('Default scope');
104  break;
105 
106  case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
108  $website = $this->_websiteFactory->create();
109  $websiteName = $website->load($data->getScopeId())->getName();
110  $scopeName = __('website(%1) scope', $websiteName);
111  break;
112 
113  case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
115  $store = $this->_storeFactory->create();
116  $storeName = $store->load($data->getScopeId())->getName();
117  $scopeName = __('store(%1) scope', $storeName);
118  break;
119  }
120 
121  $exceptions[] = __('Currency "%1" is used as %2 in %3.', $currencyName, $fieldName, $scopeName);
122  }
123  }
124  }
125  if ($exceptions) {
126  throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions)));
127  }
128 
129  return parent::afterSave();
130  }
131 }
$config
Definition: fraud_order.php:17
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configsFactory, \Magento\Store\Model\WebsiteFactory $websiteFactory, \Magento\Store\Model\StoreFactory $storeFactory, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Locale.php:55
$storeName
Definition: logo.phtml:13