-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Expand file tree
/
Copy pathMatrix.php
More file actions
121 lines (107 loc) · 3.26 KB
/
Matrix.php
File metadata and controls
121 lines (107 loc) · 3.26 KB
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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Manage currency block
*/
namespace Magento\CurrencySymbol\Block\Adminhtml\System\Currency\Rate;
/**
* Manage currency block
*/
class Matrix extends \Magento\Backend\Block\Template
{
/**
* @var string
*/
protected $_template = 'Magento_CurrencySymbol::system/currency/rate/matrix.phtml';
/**
* @var \Magento\Directory\Model\CurrencyFactory
*/
protected $_dirCurrencyFactory;
/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Directory\Model\CurrencyFactory $dirCurrencyFactory
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Directory\Model\CurrencyFactory $dirCurrencyFactory,
array $data = []
) {
$this->_dirCurrencyFactory = $dirCurrencyFactory;
parent::__construct($context, $data);
}
/**
* Prepare layout
*
* @return \Magento\Framework\View\Element\AbstractBlock
*/
protected function _prepareLayout()
{
$newRates = $this->_backendSession->getRates();
$this->_backendSession->unsetData('rates');
$currencyModel = $this->_dirCurrencyFactory->create();
$currencies = $currencyModel->getConfigAllowCurrencies();
$defaultCurrencies = $currencyModel->getConfigBaseCurrencies();
$oldCurrencies = $this->_prepareRates($currencyModel->getCurrencyRates($defaultCurrencies, $currencies));
foreach ($currencies as $currency) {
foreach ($oldCurrencies as $key => $value) {
if (!array_key_exists($currency, $oldCurrencies[$key])) {
$oldCurrencies[$key][$currency] = '';
}
}
}
foreach ($oldCurrencies as $key => $value) {
ksort($oldCurrencies[$key]);
}
sort($currencies);
$this->setAllowedCurrencies(
$currencies
)->setDefaultCurrencies(
$defaultCurrencies
)->setOldRates(
$oldCurrencies
)->setNewRates(
$this->_prepareRates($newRates)
);
return parent::_prepareLayout();
}
/**
* Get rates form action
*
* @return string
* @codeCoverageIgnore
*/
public function getRatesFormAction()
{
return $this->getUrl('adminhtml/*/saveRates');
}
/**
* Prepare rates
*
* @param array $array
* @return array
*/
protected function _prepareRates($array)
{
if (!is_array($array)) {
return $array;
}
foreach ($array as $key => $rate) {
foreach ($rate as $code => $value) {
$parts = $value !== null ? explode('.', $value) : [];
if (count($parts) == 2) {
$parts[1] = str_pad(rtrim($parts[1], 0), 4, '0', STR_PAD_RIGHT);
$array[$key][$code] = join('.', $parts);
} elseif ($value > 0) {
$array[$key][$code] = number_format($value, 4);
} else {
$array[$key][$code] = null;
}
}
}
return $array;
}
}