/
Currencysymbol.php
349 lines (309 loc) · 10.7 KB
/
Currencysymbol.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CurrencySymbol\Model\System;
use Magento\Framework\Locale\Bundle\CurrencyBundle;
use Magento\Framework\Unserialize\SecureUnserializer;
use Magento\Framework\App\ObjectManager;
use Psr\Log\LoggerInterface;
/**
* Custom currency symbol model
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Currencysymbol
{
/**
* Custom currency symbol properties
*
* @var array
*/
protected $_symbolsData = [];
/**
* Store id
*
* @var string|null
*/
protected $_storeId;
/**
* Website id
*
* @var string|null
*/
protected $_websiteId;
/**
* Cache types which should be invalidated
*
* @var array
*/
protected $_cacheTypes = [
\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER,
\Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
\Magento\Framework\App\Cache\Type\Layout::TYPE_IDENTIFIER,
\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER,
];
/**
* Config path to custom currency symbol value
*/
const XML_PATH_CUSTOM_CURRENCY_SYMBOL = 'currency/options/customsymbol';
const XML_PATH_ALLOWED_CURRENCIES = \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_ALLOW;
/*
* Separator used in config in allowed currencies list
*/
const ALLOWED_CURRENCIES_CONFIG_SEPARATOR = ',';
/**
* Config currency section
*/
const CONFIG_SECTION = 'currency';
/**
* Core event manager proxy
*
* @var \Magento\Framework\Event\ManagerInterface
*/
protected $_eventManager;
/**
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $_cacheTypeList;
/**
* @var \Magento\Config\Model\Config\Factory
*/
protected $_configFactory;
/**
* @var \Magento\Store\Model\System\Store
*/
protected $_systemStore;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Framework\Locale\ResolverInterface
*/
protected $localeResolver;
/**
* @var \Magento\Framework\App\Config\ReinitableConfigInterface
*/
protected $_coreConfig;
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @var SecureUnserializer
*/
private $unserializer;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\App\Config\ReinitableConfigInterface $coreConfig
* @param \Magento\Config\Model\Config\Factory $configFactory
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param \Magento\Store\Model\System\Store $systemStore
* @param \Magento\Framework\Event\ManagerInterface $eventManager
* @param SecureUnserializer|null $unserializer
* @param LoggerInterface|null $logger
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __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,
SecureUnserializer $unserializer = null,
LoggerInterface $logger = null
) {
$this->_coreConfig = $coreConfig;
$this->_configFactory = $configFactory;
$this->_cacheTypeList = $cacheTypeList;
$this->_storeManager = $storeManager;
$this->localeResolver = $localeResolver;
$this->_systemStore = $systemStore;
$this->_eventManager = $eventManager;
$this->_scopeConfig = $scopeConfig;
$this->unserializer = $unserializer ?: ObjectManager::getInstance()->get(SecureUnserializer::class);
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
}
/**
* Return currency symbol properties array based on config values
*
* @return array
*/
public function getCurrencySymbolsData()
{
if ($this->_symbolsData) {
return $this->_symbolsData;
}
$this->_symbolsData = [];
$currentSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
foreach ($this->getAllowedCurrencies() as $code) {
$currencies = (new CurrencyBundle())->get($this->localeResolver->getLocale())['Currencies'];
$symbol = $currencies[$code][0] ?: $code;
$name = $currencies[$code][1] ?: $code;
$this->_symbolsData[$code] = ['parentSymbol' => $symbol, 'displayName' => $name];
if (isset($currentSymbols[$code]) && !empty($currentSymbols[$code])) {
$this->_symbolsData[$code]['displaySymbol'] = $currentSymbols[$code];
} else {
$this->_symbolsData[$code]['displaySymbol'] = $this->_symbolsData[$code]['parentSymbol'];
}
$this->_symbolsData[$code]['inherited'] =
($this->_symbolsData[$code]['parentSymbol'] == $this->_symbolsData[$code]['displaySymbol']);
}
return $this->_symbolsData;
}
/**
* Save currency symbol to config
*
* @param $symbols array
* @return $this
*/
public function setCurrencySymbolsData($symbols = [])
{
foreach ($this->getCurrencySymbolsData() as $code => $values) {
if (isset($symbols[$code]) && ($symbols[$code] == $values['parentSymbol'] || empty($symbols[$code]))) {
unset($symbols[$code]);
}
}
$value = [];
if ($symbols) {
$value['options']['fields']['customsymbol']['value'] = serialize($symbols);
} else {
$value['options']['fields']['customsymbol']['inherit'] = 1;
}
$this->_configFactory->create()
->setSection(self::CONFIG_SECTION)
->setWebsite(null)
->setStore(null)
->setGroups($value)
->save();
$this->_eventManager->dispatch(
'admin_system_config_changed_section_currency_before_reinit',
['website' => $this->_websiteId, 'store' => $this->_storeId]
);
// reinit configuration
$this->_coreConfig->reinit();
$this->clearCache();
//Reset symbols cache since new data is added
$this->_symbolsData = [];
$this->_eventManager->dispatch(
'admin_system_config_changed_section_currency',
['website' => $this->_websiteId, 'store' => $this->_storeId]
);
return $this;
}
/**
* Return custom currency symbol by currency code
*
* @param string $code
* @return string|false
*/
public function getCurrencySymbol($code)
{
$customSymbols = $this->_unserializeStoreConfig(self::XML_PATH_CUSTOM_CURRENCY_SYMBOL);
if (array_key_exists($code, $customSymbols)) {
return $customSymbols[$code];
}
return false;
}
/**
* Clear translate cache
*
* @return $this
*/
protected function clearCache()
{
// clear cache for frontend
foreach ($this->_cacheTypes as $cacheType) {
$this->_cacheTypeList->invalidate($cacheType);
}
return $this;
}
/**
* Unserialize data from Store Config.
*
* @param string $configPath
* @param int $storeId
* @return array
*/
protected function _unserializeStoreConfig($configPath, $storeId = null)
{
$result = [];
$configData = (string)$this->_scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
if ($configData) {
try {
$result = $this->unserializer->unserialize($configData);
} catch (\InvalidArgumentException $e) {
$this->logger->critical($e);
}
}
return is_array($result) ? $result : [];
}
/**
* Return allowed currencies
*
* @return array
*/
protected function getAllowedCurrencies()
{
$allowedCurrencies = explode(
self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR,
$this->_scopeConfig->getValue(
self::XML_PATH_ALLOWED_CURRENCIES,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
null
)
);
$storeModel = $this->_systemStore;
/** @var \Magento\Store\Model\Website $website */
foreach ($storeModel->getWebsiteCollection() as $website) {
$websiteShow = false;
/** @var \Magento\Store\Model\Group $group */
foreach ($storeModel->getGroupCollection() as $group) {
if ($group->getWebsiteId() != $website->getId()) {
continue;
}
/** @var \Magento\Store\Model\Store $store */
foreach ($storeModel->getStoreCollection() as $store) {
if ($store->getGroupId() != $group->getId()) {
continue;
}
if (!$websiteShow) {
$websiteShow = true;
$websiteSymbols = $website->getConfig(self::XML_PATH_ALLOWED_CURRENCIES);
$allowedCurrencies = array_merge(
$allowedCurrencies,
explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $websiteSymbols)
);
}
$storeSymbols = $this->_scopeConfig->getValue(
self::XML_PATH_ALLOWED_CURRENCIES,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
$allowedCurrencies = array_merge(
$allowedCurrencies,
explode(self::ALLOWED_CURRENCIES_CONFIG_SEPARATOR, $storeSymbols)
);
}
}
}
return array_unique($allowedCurrencies);
}
}