123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Config locale allowed currencies backend
- */
- namespace Magento\Config\Model\Config\Backend;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- /**
- * @api
- * @since 100.0.2
- */
- class Locale extends \Magento\Framework\App\Config\Value
- {
- /**
- * @var \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory
- */
- protected $_configsFactory;
- /**
- * @var \Magento\Store\Model\WebsiteFactory
- */
- protected $_websiteFactory;
- /**
- * @var \Magento\Store\Model\StoreFactory
- */
- protected $_storeFactory;
- /**
- * @var \Magento\Framework\Locale\CurrencyInterface
- */
- protected $_localeCurrency;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
- * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
- * @param \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configsFactory
- * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
- * @param \Magento\Store\Model\StoreFactory $storeFactory
- * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- *
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __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 = []
- ) {
- $this->_configsFactory = $configsFactory;
- $this->_websiteFactory = $websiteFactory;
- $this->_storeFactory = $storeFactory;
- $this->_localeCurrency = $localeCurrency;
- parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
- }
- /**
- * @return $this
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function afterSave()
- {
- /** @var $collection \Magento\Config\Model\ResourceModel\Config\Data\Collection */
- $collection = $this->_configsFactory->create();
- $collection->addPathFilter('currency/options');
- $values = explode(',', $this->getValue());
- $exceptions = [];
- foreach ($collection as $data) {
- $match = false;
- $scopeName = __('Default scope');
- if (preg_match('/(base|default)$/', $data->getPath(), $match)) {
- if (!in_array($data->getValue(), $values)) {
- $currencyName = $this->_localeCurrency->getCurrency($data->getValue())->getName();
- if ($match[1] == 'base') {
- $fieldName = __('Base currency');
- } else {
- $fieldName = __('Display default currency');
- }
- switch ($data->getScope()) {
- case ScopeConfigInterface::SCOPE_TYPE_DEFAULT:
- $scopeName = __('Default scope');
- break;
- case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
- /** @var $website \Magento\Store\Model\Website */
- $website = $this->_websiteFactory->create();
- $websiteName = $website->load($data->getScopeId())->getName();
- $scopeName = __('website(%1) scope', $websiteName);
- break;
- case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
- /** @var $store \Magento\Store\Model\Store */
- $store = $this->_storeFactory->create();
- $storeName = $store->load($data->getScopeId())->getName();
- $scopeName = __('store(%1) scope', $storeName);
- break;
- }
- $exceptions[] = __('Currency "%1" is used as %2 in %3.', $currencyName, $fieldName, $scopeName);
- }
- }
- }
- if ($exceptions) {
- throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions)));
- }
- return parent::afterSave();
- }
- }
|