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:  * Currency model
 29:  *
 30:  * @category   Mage
 31:  * @package    Mage_Directory
 32:  * @author      Magento Core Team <core@magentocommerce.com>
 33:  */
 34: class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
 35: {
 36:     /**
 37:      * CONFIG path constants
 38:     */
 39:     const XML_PATH_CURRENCY_ALLOW   = 'currency/options/allow';
 40:     const XML_PATH_CURRENCY_DEFAULT = 'currency/options/default';
 41:     const XML_PATH_CURRENCY_BASE    = 'currency/options/base';
 42: 
 43:     protected $_filter;
 44: 
 45:     /**
 46:      * Currency Rates
 47:      *
 48:      * @var array
 49:      */
 50:     protected $_rates;
 51: 
 52: 
 53:     protected function _construct()
 54:     {
 55:         $this->_init('directory/currency');
 56:     }
 57: 
 58:     /**
 59:      * Get currency code
 60:      *
 61:      * @return string
 62:      */
 63:     public function getCode()
 64:     {
 65:         return $this->_getData('currency_code');
 66:     }
 67: 
 68:     public function getCurrencyCode()
 69:     {
 70:         return $this->_getData('currency_code');
 71:     }
 72: 
 73:     /**
 74:      * Currency Rates getter
 75:      *
 76:      * @return array
 77:      */
 78:     public function getRates()
 79:     {
 80:         return $this->_rates;
 81:     }
 82: 
 83:     /**
 84:      * Currency Rates setter
 85:      *
 86:      * @param array Currency Rates
 87:      * @return Mage_Directory_Model_Currency
 88:      */
 89:     public function setRates(array $rates)
 90:     {
 91:         $this->_rates = $rates;
 92:         return $this;
 93:     }
 94: 
 95:     /**
 96:      * Loading currency data
 97:      *
 98:      * @param   string $id
 99:      * @param   string $field
100:      * @return  Mage_Directory_Model_Currency
101:      */
102:     public function load($id, $field=null)
103:     {
104:         $this->unsRate();
105:         $this->setData('currency_code', $id);
106:         return $this;
107:     }
108: 
109:     /**
110:      * Get currency rate (only base=>allowed)
111:      *
112:      * @param   string $toCurrency
113:      * @return  double
114:      */
115:     public function getRate($toCurrency)
116:     {
117:         if (is_string($toCurrency)) {
118:             $code = $toCurrency;
119:         } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
120:             $code = $toCurrency->getCurrencyCode();
121:         } else {
122:             throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
123:         }
124:         $rates = $this->getRates();
125:         if (!isset($rates[$code])) {
126:             $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency);
127:             $this->setRates($rates);
128:         }
129:         return $rates[$code];
130:     }
131: 
132:     /**
133:      * Get currency rate (base=>allowed or allowed=>base)
134:      *
135:      * @param   string $toCurrency
136:      * @return  double
137:      */
138:     public function getAnyRate($toCurrency)
139:     {
140:         if (is_string($toCurrency)) {
141:             $code = $toCurrency;
142:         } elseif ($toCurrency instanceof Mage_Directory_Model_Currency) {
143:             $code = $toCurrency->getCurrencyCode();
144:         } else {
145:             throw Mage::exception('Mage_Directory', Mage::helper('directory')->__('Invalid target currency.'));
146:         }
147:         $rates = $this->getRates();
148:         if (!isset($rates[$code])) {
149:             $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency);
150:             $this->setRates($rates);
151:         }
152:         return $rates[$code];
153:     }
154: 
155:     /**
156:      * Convert price to currency format
157:      *
158:      * @param   double $price
159:      * @param   string $toCurrency
160:      * @return  double
161:      */
162:     public function convert($price, $toCurrency=null)
163:     {
164:         if (is_null($toCurrency)) {
165:             return $price;
166:         }
167:         elseif ($rate = $this->getRate($toCurrency)) {
168:             return $price*$rate;
169:         }
170: 
171:         throw new Exception(Mage::helper('directory')->__('Undefined rate from "%s-%s".', $this->getCode(), $toCurrency->getCode()));
172:     }
173: 
174:     /**
175:      * Get currency filter
176:      *
177:      * @return Mage_Directory_Model_Currency_Filter
178:      */
179:     public function getFilter()
180:     {
181:         if (!$this->_filter) {
182:             $this->_filter = new Mage_Directory_Model_Currency_Filter($this->getCode());
183:         }
184: 
185:         return $this->_filter;
186:     }
187: 
188:     /**
189:      * Format price to currency format
190:      *
191:      * @param   double $price
192:      * @param   bool $includeContainer
193:      * @return  string
194:      */
195:     public function format($price, $options=array(), $includeContainer = true, $addBrackets = false)
196:     {
197:         return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets);
198:     }
199: 
200:     /**
201:      * Apply currency format to number with specific rounding precision
202:      *
203:      * @param   float $price
204:      * @param   int $precision
205:      * @param   array $options
206:      * @param   bool $includeContainer
207:      * @param   bool $addBrackets
208:      * @return  string
209:      */
210:     public function formatPrecision($price, $precision, $options=array(), $includeContainer = true, $addBrackets = false)
211:     {
212:         if (!isset($options['precision'])) {
213:             $options['precision'] = $precision;
214:         }
215:         if ($includeContainer) {
216:             return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt($price, $options) . ($addBrackets ? ']' : '') . '</span>';
217:         }
218:         return $this->formatTxt($price, $options);
219:     }
220: 
221:     public function formatTxt($price, $options=array())
222:     {
223:         if (!is_numeric($price)) {
224:             $price = Mage::app()->getLocale()->getNumber($price);
225:         }
226:         /**
227:          * Fix problem with 12 000 000, 1 200 000
228:          *
229:          * %f - the argument is treated as a float, and presented as a floating-point number (locale aware).
230:          * %F - the argument is treated as a float, and presented as a floating-point number (non-locale aware).
231:          */
232:         $price = sprintf("%F", $price);
233:         return Mage::app()->getLocale()->currency($this->getCode())->toCurrency($price, $options);
234:     }
235: 
236:     public function getOutputFormat()
237:     {
238:         $formated = $this->formatTxt(0);
239:         $number = $this->formatTxt(0, array('display'=>Zend_Currency::NO_SYMBOL));
240:         return str_replace($number, '%s', $formated);
241:     }
242: 
243:     /**
244:      * Retrieve allowed currencies according to config
245:      *
246:      */
247:     public function getConfigAllowCurrencies()
248:     {
249:         $allowedCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_ALLOW);
250:         $appBaseCurrencyCode = Mage::app()->getBaseCurrencyCode();
251:         if (!in_array($appBaseCurrencyCode, $allowedCurrencies)) {
252:             $allowedCurrencies[] = $appBaseCurrencyCode;
253:         }
254:         foreach (Mage::app()->getStores() as $store) {
255:             $code = $store->getBaseCurrencyCode();
256:             if (!in_array($code, $allowedCurrencies)) {
257:                 $allowedCurrencies[] = $code;
258:             }
259:         }
260: 
261:         return $allowedCurrencies;
262:     }
263: 
264:     /**
265:      * Retrieve default currencies according to config
266:      *
267:      */
268:     public function getConfigDefaultCurrencies()
269:     {
270:         $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_DEFAULT);
271:         return $defaultCurrencies;
272:     }
273: 
274: 
275:     public function getConfigBaseCurrencies()
276:     {
277:         $defaultCurrencies = $this->_getResource()->getConfigCurrencies($this, self::XML_PATH_CURRENCY_BASE);
278:         return $defaultCurrencies;
279:     }
280: 
281:     /**
282:      * Retrieve currency rates to other currencies
283:      *
284:      * @param string $currency
285:      * @param array $toCurrencies
286:      * @return array
287:      */
288:     public function getCurrencyRates($currency, $toCurrencies=null)
289:     {
290:         if ($currency instanceof Mage_Directory_Model_Currency) {
291:             $currency = $currency->getCode();
292:         }
293:         $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies);
294:         return $data;
295:     }
296: 
297:     /**
298:      * Save currency rates
299:      *
300:      * @param array $rates
301:      * @return object
302:      */
303:     public function saveRates($rates)
304:     {
305:         $this->_getResource()->saveRates($rates);
306:         return $this;
307:     }
308: }
309: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0