/app/code/core/Mage/Adminhtml/Block/System/Currency/Rate/Matrix.php

https://bitbucket.org/jokusafet/magento2 · PHP · 97 lines · 54 code · 12 blank · 31 comment · 5 complexity · ea59124543f79ffa56de391919acaaf7 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Manage currency block
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_System_Currency_Rate_Matrix extends Mage_Adminhtml_Block_Template
  34. {
  35. protected $_template = 'system/currency/rate/matrix.phtml';
  36. protected function _prepareLayout()
  37. {
  38. $newRates = Mage::getSingleton('Mage_Adminhtml_Model_Session')->getRates();
  39. Mage::getSingleton('Mage_Adminhtml_Model_Session')->unsetData('rates');
  40. $currencyModel = Mage::getModel('Mage_Directory_Model_Currency');
  41. $currencies = $currencyModel->getConfigAllowCurrencies();
  42. $defaultCurrencies = $currencyModel->getConfigBaseCurrencies();
  43. $oldCurrencies = $this->_prepareRates($currencyModel->getCurrencyRates($defaultCurrencies, $currencies));
  44. foreach( $currencies as $currency ) {
  45. foreach( $oldCurrencies as $key => $value ) {
  46. if( !array_key_exists($currency, $oldCurrencies[$key]) ) {
  47. $oldCurrencies[$key][$currency] = '';
  48. }
  49. }
  50. }
  51. foreach( $oldCurrencies as $key => $value ) {
  52. ksort($oldCurrencies[$key]);
  53. }
  54. sort($currencies);
  55. $this->setAllowedCurrencies($currencies)
  56. ->setDefaultCurrencies($defaultCurrencies)
  57. ->setOldRates($oldCurrencies)
  58. ->setNewRates($this->_prepareRates($newRates));
  59. return parent::_prepareLayout();
  60. }
  61. protected function getRatesFormAction()
  62. {
  63. return $this->getUrl('*/*/saveRates');
  64. }
  65. protected function _prepareRates($array)
  66. {
  67. if( !is_array($array) ) {
  68. return $array;
  69. }
  70. foreach ($array as $key => $rate) {
  71. foreach ($rate as $code => $value) {
  72. $parts = explode('.', $value);
  73. if( sizeof($parts) == 2 ) {
  74. $parts[1] = str_pad(rtrim($parts[1], 0), 4, '0', STR_PAD_RIGHT);
  75. $array[$key][$code] = join('.', $parts);
  76. } elseif( $value > 0 ) {
  77. $array[$key][$code] = number_format($value, 4);
  78. } else {
  79. $array[$key][$code] = null;
  80. }
  81. }
  82. }
  83. return $array;
  84. }
  85. }