Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Directory_Block_Adminhtml_Frontend_Currency_Base
  • Mage_Directory_Block_Adminhtml_Frontend_Region_Updater
  • Mage_Directory_Block_Currency
  • Mage_Directory_Block_Data
  • Mage_Directory_CurrencyController
  • Mage_Directory_Helper_Data
  • Mage_Directory_Helper_Url
  • Mage_Directory_Model_Country
  • Mage_Directory_Model_Country_Api
  • Mage_Directory_Model_Country_Api_V2
  • Mage_Directory_Model_Country_Format
  • Mage_Directory_Model_Currency
  • Mage_Directory_Model_Currency_Filter
  • Mage_Directory_Model_Currency_Import_Abstract
  • Mage_Directory_Model_Currency_Import_Webservicex
  • Mage_Directory_Model_Mysql4_Country
  • Mage_Directory_Model_Mysql4_Country_Collection
  • Mage_Directory_Model_Mysql4_Country_Format
  • Mage_Directory_Model_Mysql4_Country_Format_Collection
  • Mage_Directory_Model_Mysql4_Currency
  • Mage_Directory_Model_Mysql4_Currency_Collection
  • Mage_Directory_Model_Mysql4_Region
  • Mage_Directory_Model_Mysql4_Region_Collection
  • Mage_Directory_Model_Observer
  • Mage_Directory_Model_Region
  • Mage_Directory_Model_Region_Api
  • Mage_Directory_Model_Region_Api_V2
  • Mage_Directory_Model_Resource_Country
  • Mage_Directory_Model_Resource_Country_Collection
  • Mage_Directory_Model_Resource_Country_Format
  • Mage_Directory_Model_Resource_Country_Format_Collection
  • Mage_Directory_Model_Resource_Currency
  • Mage_Directory_Model_Resource_Region
  • Mage_Directory_Model_Resource_Region_Collection

Exceptions

  • Mage_Directory_Exception
  • Overview
  • Package
  • Class
  • Tree
  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: /**
 29:  * Directory Currency Resource Model
 30:  *
 31:  * @category    Mage
 32:  * @package     Mage_Directory
 33:  * @author      Magento Core Team <core@magentocommerce.com>
 34:  */
 35: class Mage_Directory_Model_Resource_Currency extends Mage_Core_Model_Resource_Db_Abstract
 36: {
 37:     /**
 38:      * Currency rate table
 39:      *
 40:      * @var string
 41:      */
 42:     protected $_currencyRateTable;
 43: 
 44:     /**
 45:      * Currency rate cache array
 46:      *
 47:      * @var array
 48:      */
 49:     protected static $_rateCache;
 50: 
 51:     /**
 52:      * Define main and currency rate tables
 53:      *
 54:      */
 55:     protected function _construct()
 56:     {
 57:         $this->_init('directory/currency', 'currency_code');
 58:         $this->_currencyRateTable   = $this->getTable('directory/currency_rate');
 59:     }
 60: 
 61:     /**
 62:      * Retrieve currency rate (only base=>allowed)
 63:      *
 64:      * @param Mage_Directory_Model_Currency|string $currencyFrom
 65:      * @param Mage_Directory_Model_Currency|string $currencyTo
 66:      * @return float
 67:      */
 68:     public function getRate($currencyFrom, $currencyTo)
 69:     {
 70:         if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
 71:             $currencyFrom = $currencyFrom->getCode();
 72:         }
 73: 
 74:         if ($currencyTo instanceof Mage_Directory_Model_Currency) {
 75:             $currencyTo = $currencyTo->getCode();
 76:         }
 77: 
 78:         if ($currencyFrom == $currencyTo) {
 79:             return 1;
 80:         }
 81: 
 82:         if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
 83:             $read = $this->_getReadAdapter();
 84:             $bind = array(
 85:                 ':currency_from' => strtoupper($currencyFrom),
 86:                 ':currency_to'   => strtoupper($currencyTo)
 87:             );
 88:             $select = $read->select()
 89:                 ->from($this->_currencyRateTable, 'rate')
 90:                 ->where('currency_from = :currency_from')
 91:                 ->where('currency_to = :currency_to');
 92: 
 93:             self::$_rateCache[$currencyFrom][$currencyTo] = $read->fetchOne($select, $bind);
 94:         }
 95: 
 96:         return self::$_rateCache[$currencyFrom][$currencyTo];
 97:     }
 98: 
 99:     /**
100:      * Retrieve currency rate (base=>allowed or allowed=>base)
101:      *
102:      * @param Mage_Directory_Model_Currency|string $currencyFrom
103:      * @param Mage_Directory_Model_Currency|string $currencyTo
104:      * @return float
105:      */
106:     public function getAnyRate($currencyFrom, $currencyTo)
107:     {
108:         if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
109:             $currencyFrom = $currencyFrom->getCode();
110:         }
111: 
112:         if ($currencyTo instanceof Mage_Directory_Model_Currency) {
113:             $currencyTo = $currencyTo->getCode();
114:         }
115: 
116:         if ($currencyFrom == $currencyTo) {
117:             return 1;
118:         }
119: 
120:         if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
121:             $adapter = $this->_getReadAdapter();
122:             $bind    = array(
123:                 ':currency_from' => strtoupper($currencyFrom),
124:                 ':currency_to'   => strtoupper($currencyTo)
125:             );
126:             $select  = $adapter->select()
127:                 ->from($this->_currencyRateTable, 'rate')
128:                 ->where('currency_from = :currency_from')
129:                 ->where('currency_to = :currency_to');
130: 
131:             $rate    = $adapter->fetchOne($select, $bind);
132:             if ($rate === false) {
133:                 $select = $adapter->select()
134:                     ->from($this->_currencyRateTable, new Zend_Db_Expr('1/rate'))
135:                     ->where('currency_to = :currency_from')
136:                     ->where('currency_from = :currency_to');
137:                 $rate = $adapter->fetchOne($select, $bind);
138:             }
139:             self::$_rateCache[$currencyFrom][$currencyTo] = $rate;
140:         }
141: 
142:         return self::$_rateCache[$currencyFrom][$currencyTo];
143:     }
144: 
145:     /**
146:      * Saving currency rates
147:      *
148:      * @param array $rates
149:      */
150:     public function saveRates($rates)
151:     {
152:         if (is_array($rates) && sizeof($rates) > 0) {
153:             $adapter = $this->_getWriteAdapter();
154:             $data    = array();
155:             foreach ($rates as $currencyCode => $rate) {
156:                 foreach ($rate as $currencyTo => $value) {
157:                     $value = abs($value);
158:                     if ($value == 0) {
159:                         continue;
160:                     }
161:                     $data[] = array(
162:                         'currency_from' => $currencyCode,
163:                         'currency_to'   => $currencyTo,
164:                         'rate'          => $value,
165:                     );
166:                 }
167:             }
168:             if ($data) {
169:                 $adapter->insertOnDuplicate($this->_currencyRateTable, $data, array('rate'));
170:             }
171:         } else {
172:             Mage::throwException(Mage::helper('directory')->__('Invalid rates received'));
173:         }
174:     }
175: 
176:     /**
177:      * Retrieve config currency data by config path
178:      *
179:      * @param Mage_Directory_Model_Currency $model
180:      * @param string $path
181:      *
182:      * @return array
183:      */
184:     public function getConfigCurrencies($model, $path)
185:     {
186:         $adapter = $this->_getReadAdapter();
187:         $bind    = array(':config_path' => $path);
188:         $select  = $adapter->select()
189:                 ->from($this->getTable('core/config_data'))
190:                 ->where('path = :config_path');
191:         $result  = array();
192:         $rowSet  = $adapter->fetchAll($select, $bind);
193:         foreach ($rowSet as $row) {
194:             $result = array_merge($result, explode(',', $row['value']));
195:         }
196:         sort($result);
197: 
198:         return array_unique($result);
199:     }
200: 
201:     /**
202:      * Return currency rates
203:      *
204:      * @param string|array $currency
205:      * @param array $toCurrencies
206:      *
207:      * @return array
208:      */
209:     public function getCurrencyRates($currency, $toCurrencies = null)
210:     {
211:         $rates = array();
212:         if (is_array($currency)) {
213:             foreach ($currency as $code) {
214:                 $rates[$code] = $this->_getRatesByCode($code, $toCurrencies);
215:             }
216:         } else {
217:             $rates = $this->_getRatesByCode($currency, $toCurrencies);
218:         }
219: 
220:         return $rates;
221:     }
222: 
223:     /**
224:      * Protected method used by getCurrencyRates() method
225:      *
226:      * @param string $code
227:      * @param array $toCurrencies
228:      * @return array
229:      */
230:     protected function _getRatesByCode($code, $toCurrencies = null)
231:     {
232:         $adapter = $this->_getReadAdapter();
233:         $bind    = array(
234:             ':currency_from' => $code
235:         );
236:         $select  = $adapter->select()
237:             ->from($this->getTable('directory/currency_rate'), array('currency_to', 'rate'))
238:             ->where('currency_from = :currency_from')
239:             ->where('currency_to IN(?)', $toCurrencies);
240:         $rowSet  = $adapter->fetchAll($select, $bind);
241:         $result  = array();
242: 
243:         foreach ($rowSet as $row) {
244:             $result[$row['currency_to']] = $row['rate'];
245:         }
246: 
247:         return $result;
248:     }
249: }
250: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0