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_Directory
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Abstract model for import currency
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: abstract class Mage_Directory_Model_Currency_Import_Abstract
33: {
34: /**
35: * Retrieve currency codes
36: *
37: * @return array
38: */
39: protected function _getCurrencyCodes()
40: {
41: return Mage::getModel('directory/currency')->getConfigAllowCurrencies();
42: }
43:
44: /**
45: * Retrieve default currency codes
46: *
47: * @return array
48: */
49: protected function _getDefaultCurrencyCodes()
50: {
51: return Mage::getModel('directory/currency')->getConfigBaseCurrencies();
52: }
53:
54: /**
55: * Retrieve rate
56: *
57: * @param string $currencyFrom
58: * @param string $currencyTo
59: * @return float
60: */
61: abstract protected function _convert($currencyFrom, $currencyTo);
62:
63: /**
64: * Saving currency rates
65: *
66: * @param array $rates
67: * @return Mage_Directory_Model_Currency_Import_Abstract
68: */
69: protected function _saveRates($rates)
70: {
71: foreach ($rates as $currencyCode => $currencyRates) {
72: Mage::getModel('directory/currency')
73: ->setId($currencyCode)
74: ->setRates($currencyRates)
75: ->save();
76: }
77: return $this;
78: }
79:
80: /**
81: * Import rates
82: *
83: * @return Mage_Directory_Model_Currency_Import_Abstract
84: */
85: public function importRates()
86: {
87: $data = $this->fetchRates();
88: $this->_saveRates($data);
89: return $this;
90: }
91:
92: public function fetchRates()
93: {
94: $data = array();
95: $currencies = $this->_getCurrencyCodes();
96: $defaultCurrencies = $this->_getDefaultCurrencyCodes();
97: @set_time_limit(0);
98: foreach ($defaultCurrencies as $currencyFrom) {
99: if (!isset($data[$currencyFrom])) {
100: $data[$currencyFrom] = array();
101: }
102:
103: foreach ($currencies as $currencyTo) {
104: if ($currencyFrom == $currencyTo) {
105: $data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
106: }
107: else {
108: $data[$currencyFrom][$currencyTo] = $this->_numberFormat($this->_convert($currencyFrom, $currencyTo));
109: }
110: }
111: ksort($data[$currencyFrom]);
112: }
113:
114: return $data;
115: }
116:
117: protected function _numberFormat($number)
118: {
119: return $number;
120: }
121:
122: public function getMessages()
123: {
124: return $this->_messages;
125: }
126: }
127: