Matrix.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Manage currency block
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\CurrencySymbol\Block\Adminhtml\System\Currency\Rate;
  12. class Matrix extends \Magento\Backend\Block\Template
  13. {
  14. /**
  15. * @var string
  16. */
  17. protected $_template = 'Magento_CurrencySymbol::system/currency/rate/matrix.phtml';
  18. /**
  19. * @var \Magento\Directory\Model\CurrencyFactory
  20. */
  21. protected $_dirCurrencyFactory;
  22. /**
  23. * @param \Magento\Backend\Block\Template\Context $context
  24. * @param \Magento\Directory\Model\CurrencyFactory $dirCurrencyFactory
  25. * @param array $data
  26. */
  27. public function __construct(
  28. \Magento\Backend\Block\Template\Context $context,
  29. \Magento\Directory\Model\CurrencyFactory $dirCurrencyFactory,
  30. array $data = []
  31. ) {
  32. $this->_dirCurrencyFactory = $dirCurrencyFactory;
  33. parent::__construct($context, $data);
  34. }
  35. /**
  36. * Prepare layout
  37. *
  38. * @return \Magento\Framework\View\Element\AbstractBlock
  39. */
  40. protected function _prepareLayout()
  41. {
  42. $newRates = $this->_backendSession->getRates();
  43. $this->_backendSession->unsetData('rates');
  44. $currencyModel = $this->_dirCurrencyFactory->create();
  45. $currencies = $currencyModel->getConfigAllowCurrencies();
  46. $defaultCurrencies = $currencyModel->getConfigBaseCurrencies();
  47. $oldCurrencies = $this->_prepareRates($currencyModel->getCurrencyRates($defaultCurrencies, $currencies));
  48. foreach ($currencies as $currency) {
  49. foreach ($oldCurrencies as $key => $value) {
  50. if (!array_key_exists($currency, $oldCurrencies[$key])) {
  51. $oldCurrencies[$key][$currency] = '';
  52. }
  53. }
  54. }
  55. foreach ($oldCurrencies as $key => $value) {
  56. ksort($oldCurrencies[$key]);
  57. }
  58. sort($currencies);
  59. $this->setAllowedCurrencies(
  60. $currencies
  61. )->setDefaultCurrencies(
  62. $defaultCurrencies
  63. )->setOldRates(
  64. $oldCurrencies
  65. )->setNewRates(
  66. $this->_prepareRates($newRates)
  67. );
  68. return parent::_prepareLayout();
  69. }
  70. /**
  71. * Get rates form action
  72. *
  73. * @return string
  74. * @codeCoverageIgnore
  75. */
  76. public function getRatesFormAction()
  77. {
  78. return $this->getUrl('adminhtml/*/saveRates');
  79. }
  80. /**
  81. * Prepare rates
  82. *
  83. * @param array $array
  84. * @return array
  85. */
  86. protected function _prepareRates($array)
  87. {
  88. if (!is_array($array)) {
  89. return $array;
  90. }
  91. foreach ($array as $key => $rate) {
  92. foreach ($rate as $code => $value) {
  93. $parts = explode('.', $value);
  94. if (sizeof($parts) == 2) {
  95. $parts[1] = str_pad(rtrim($parts[1], 0), 4, '0', STR_PAD_RIGHT);
  96. $array[$key][$code] = join('.', $parts);
  97. } elseif ($value > 0) {
  98. $array[$key][$code] = number_format($value, 4);
  99. } else {
  100. $array[$key][$code] = null;
  101. }
  102. }
  103. }
  104. return $array;
  105. }
  106. }