Locale.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Config locale allowed currencies backend
  8. */
  9. namespace Magento\Config\Model\Config\Backend;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Locale extends \Magento\Framework\App\Config\Value
  16. {
  17. /**
  18. * @var \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory
  19. */
  20. protected $_configsFactory;
  21. /**
  22. * @var \Magento\Store\Model\WebsiteFactory
  23. */
  24. protected $_websiteFactory;
  25. /**
  26. * @var \Magento\Store\Model\StoreFactory
  27. */
  28. protected $_storeFactory;
  29. /**
  30. * @var \Magento\Framework\Locale\CurrencyInterface
  31. */
  32. protected $_localeCurrency;
  33. /**
  34. * @param \Magento\Framework\Model\Context $context
  35. * @param \Magento\Framework\Registry $registry
  36. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  37. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  38. * @param \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configsFactory
  39. * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
  40. * @param \Magento\Store\Model\StoreFactory $storeFactory
  41. * @param \Magento\Framework\Locale\CurrencyInterface $localeCurrency
  42. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  43. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  44. * @param array $data
  45. *
  46. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  47. */
  48. public function __construct(
  49. \Magento\Framework\Model\Context $context,
  50. \Magento\Framework\Registry $registry,
  51. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  52. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  53. \Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configsFactory,
  54. \Magento\Store\Model\WebsiteFactory $websiteFactory,
  55. \Magento\Store\Model\StoreFactory $storeFactory,
  56. \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
  57. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  58. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  59. array $data = []
  60. ) {
  61. $this->_configsFactory = $configsFactory;
  62. $this->_websiteFactory = $websiteFactory;
  63. $this->_storeFactory = $storeFactory;
  64. $this->_localeCurrency = $localeCurrency;
  65. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  66. }
  67. /**
  68. * @return $this
  69. * @throws \Magento\Framework\Exception\LocalizedException
  70. */
  71. public function afterSave()
  72. {
  73. /** @var $collection \Magento\Config\Model\ResourceModel\Config\Data\Collection */
  74. $collection = $this->_configsFactory->create();
  75. $collection->addPathFilter('currency/options');
  76. $values = explode(',', $this->getValue());
  77. $exceptions = [];
  78. foreach ($collection as $data) {
  79. $match = false;
  80. $scopeName = __('Default scope');
  81. if (preg_match('/(base|default)$/', $data->getPath(), $match)) {
  82. if (!in_array($data->getValue(), $values)) {
  83. $currencyName = $this->_localeCurrency->getCurrency($data->getValue())->getName();
  84. if ($match[1] == 'base') {
  85. $fieldName = __('Base currency');
  86. } else {
  87. $fieldName = __('Display default currency');
  88. }
  89. switch ($data->getScope()) {
  90. case ScopeConfigInterface::SCOPE_TYPE_DEFAULT:
  91. $scopeName = __('Default scope');
  92. break;
  93. case \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE:
  94. /** @var $website \Magento\Store\Model\Website */
  95. $website = $this->_websiteFactory->create();
  96. $websiteName = $website->load($data->getScopeId())->getName();
  97. $scopeName = __('website(%1) scope', $websiteName);
  98. break;
  99. case \Magento\Store\Model\ScopeInterface::SCOPE_STORE:
  100. /** @var $store \Magento\Store\Model\Store */
  101. $store = $this->_storeFactory->create();
  102. $storeName = $store->load($data->getScopeId())->getName();
  103. $scopeName = __('store(%1) scope', $storeName);
  104. break;
  105. }
  106. $exceptions[] = __('Currency "%1" is used as %2 in %3.', $currencyName, $fieldName, $scopeName);
  107. }
  108. }
  109. }
  110. if ($exceptions) {
  111. throw new \Magento\Framework\Exception\LocalizedException(__(join("\n", $exceptions)));
  112. }
  113. return parent::afterSave();
  114. }
  115. }