Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Currencysymbol.php
Go to the documentation of this file.
1 <?php
7 
11 
21 {
27  protected $_symbolsData = [];
28 
34  protected $_storeId;
35 
41  protected $_websiteId;
42 
48  protected $_cacheTypes = [
53  ];
54 
58  const XML_PATH_CUSTOM_CURRENCY_SYMBOL = 'currency/options/customsymbol';
59 
61 
62  /*
63  * Separator used in config in allowed currencies list
64  */
66 
70  const CONFIG_SECTION = 'currency';
71 
77  protected $_eventManager;
78 
82  protected $_cacheTypeList;
83 
87  protected $_configFactory;
88 
92  protected $_systemStore;
93 
97  protected $_storeManager;
98 
102  protected $localeResolver;
103 
107  protected $_coreConfig;
108 
114  protected $_scopeConfig;
115 
119  private $serializer;
120 
132  public function __construct(
133  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
134  \Magento\Framework\App\Config\ReinitableConfigInterface $coreConfig,
135  \Magento\Config\Model\Config\Factory $configFactory,
136  \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
137  \Magento\Store\Model\StoreManagerInterface $storeManager,
138  \Magento\Framework\Locale\ResolverInterface $localeResolver,
139  \Magento\Store\Model\System\Store $systemStore,
140  \Magento\Framework\Event\ManagerInterface $eventManager,
141  Json $serializer = null
142  ) {
143  $this->_coreConfig = $coreConfig;
144  $this->_configFactory = $configFactory;
145  $this->_cacheTypeList = $cacheTypeList;
146  $this->_storeManager = $storeManager;
147  $this->localeResolver = $localeResolver;
148  $this->_systemStore = $systemStore;
149  $this->_eventManager = $eventManager;
150  $this->_scopeConfig = $scopeConfig;
151  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
152  }
153 
159  public function getCurrencySymbolsData()
160  {
161  if ($this->_symbolsData) {
162  return $this->_symbolsData;
163  }
164 
165  $this->_symbolsData = [];
166 
167  $currentSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
168 
169  foreach ($this->getAllowedCurrencies() as $code) {
170  $currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'];
171  $symbol = $currencies[$code][0] ?: $code;
172  $name = $currencies[$code][1] ?: $code;
173  $this->_symbolsData[$code] = ['parentSymbol' => $symbol, 'displayName' => $name];
174 
175  if (isset($currentSymbols[$code]) && !empty($currentSymbols[$code])) {
176  $this->_symbolsData[$code]['displaySymbol'] = $currentSymbols[$code];
177  } else {
178  $this->_symbolsData[$code]['displaySymbol'] = $this->_symbolsData[$code]['parentSymbol'];
179  }
180  $this->_symbolsData[$code]['inherited'] =
181  ($this->_symbolsData[$code]['parentSymbol'] == $this->_symbolsData[$code]['displaySymbol']);
182  }
183 
184  return $this->_symbolsData;
185  }
186 
193  public function setCurrencySymbolsData($symbols = [])
194  {
195  foreach ($this->getCurrencySymbolsData() as $code => $values) {
196  if (isset($symbols[$code]) && ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code]))) {
197  unset($symbols[$code]);
198  }
199  }
200  $value = [];
201  if ($symbols) {
202  $value['options']['fields']['customsymbol']['value'] = $this->serializer->serialize($symbols);
203  } else {
204  $value['options']['fields']['customsymbol']['inherit'] = 1;
205  }
206 
207  $this->_configFactory->create()
208  ->setSection(self::CONFIG_SECTION)
209  ->setWebsite(null)
210  ->setStore(null)
211  ->setGroups($value)
212  ->save();
213 
214  $this->_eventManager->dispatch(
215  'admin_system_config_changed_section_currency_before_reinit',
216  ['website' => $this->_websiteId, 'store' => $this->_storeId]
217  );
218 
219  // reinit configuration
220  $this->_coreConfig->reinit();
221 
222  $this->clearCache();
223  //Reset symbols cache since new data is added
224  $this->_symbolsData = [];
225 
226  $this->_eventManager->dispatch(
227  'admin_system_config_changed_section_currency',
228  ['website' => $this->_websiteId, 'store' => $this->_storeId]
229  );
230 
231  return $this;
232  }
233 
240  public function getCurrencySymbol($code)
241  {
242  $customSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
243  if (array_key_exists($code, $customSymbols)) {
244  return $customSymbols[$code];
245  }
246 
247  return false;
248  }
249 
255  protected function clearCache()
256  {
257  // clear cache for frontend
258  foreach ($this->_cacheTypes as $cacheType) {
259  $this->_cacheTypeList->invalidate($cacheType);
260  }
261  return $this;
262  }
263 
271  protected function _unserializeStoreConfig($configPath, $storeId = null)
272  {
273  $result = [];
274  $configData = (string)$this->_scopeConfig->getValue(
275  $configPath,
276  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
277  $storeId
278  );
279  if ($configData) {
280  $result = $this->serializer->unserialize($configData);
281  }
282 
283  return is_array($result) ? $result : [];
284  }
285 
291  protected function getAllowedCurrencies()
292  {
293  $allowedCurrencies = explode(
294  self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
295  $this->_scopeConfig->getValue(
296  self::XML_PATH_ALLOWED_CURRENCIES,
297  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
298  null
299  )
300  );
301 
302  $storeModel = $this->_systemStore;
304  foreach ($storeModel->getWebsiteCollection() as $website) {
305  $websiteShow = false;
307  foreach ($storeModel->getGroupCollection() as $group) {
308  if ($group->getWebsiteId() != $website->getId()) {
309  continue;
310  }
312  foreach ($storeModel->getStoreCollection() as $store) {
313  if ($store->getGroupId() != $group->getId()) {
314  continue;
315  }
316  if (!$websiteShow) {
317  $websiteShow = true;
318  $websiteSymbols = $website->getConfig(self::XML_PATH_ALLOWED_CURRENCIES);
319  $allowedCurrencies = array_merge(
320  $allowedCurrencies,
321  explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $websiteSymbols)
322  );
323  }
324  $storeSymbols = $this->_scopeConfig->getValue(
325  self::XML_PATH_ALLOWED_CURRENCIES,
326  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
327  $store
328  );
329  $allowedCurrencies = array_merge(
330  $allowedCurrencies,
331  explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $storeSymbols)
332  );
333  }
334  }
335  }
336  return array_unique($allowedCurrencies);
337  }
338 }
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Config\ReinitableConfigInterface $coreConfig, \Magento\Config\Model\Config\Factory $configFactory, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Magento\Store\Model\System\Store $systemStore, \Magento\Framework\Event\ManagerInterface $eventManager, Json $serializer=null)
$group
Definition: sections.phtml:16
$storeManager
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16
$configFactory
Definition: config_data.php:43
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14