Mage_Directory_Model_Mysql4_Currency Class Reference

Inheritance diagram for Mage_Directory_Model_Mysql4_Currency:

Mage_Core_Model_Mysql4_Abstract Mage_Core_Model_Resource_Abstract

List of all members.

Public Member Functions

 __construct ()
 getRate ($currencyFrom, $currencyTo)
 saveRates ($rates)
 getConfigCurrencies ($model, $path)
 getCurrencyRates ($currency, $toCurrencies=null)

Protected Member Functions

 _construct ()
 _getRatesByCode ($code, $toCurrencies=null)

Protected Attributes

 $_currencyRateTable

Static Protected Attributes

static $_rateCache


Detailed Description

Definition at line 34 of file Currency.php.


Constructor & Destructor Documentation

__construct (  ) 

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 55 of file Currency.php.

00056     {
00057         $resource = Mage::getSingleton('core/resource');
00058         $this->_currencyRateTable   = $resource->getTableName('directory/currency_rate');
00059 
00060         parent::__construct();
00061     }


Member Function Documentation

_construct (  )  [protected]

Resource initialization

Reimplemented from Mage_Core_Model_Resource_Abstract.

Definition at line 50 of file Currency.php.

00051     {
00052         $this->_init('directory/currency', 'currency_code');
00053     }

_getRatesByCode ( code,
toCurrencies = null 
) [protected]

Protected method used by getCurrencyRates() method

Parameters:
string $code
array $toCurrencies
Returns:
array

Definition at line 183 of file Currency.php.

00184     {
00185         $read = $this->_getReadAdapter();
00186         $select = $read->select()
00187             ->from($this->getTable('directory/currency_rate'), array('currency_to', 'rate'))
00188             ->where($read->quoteInto('currency_from = ?', $code))
00189             ->where($read->quoteInto('currency_to IN(?)', $toCurrencies));
00190 
00191         $data = $read->fetchAll($select);
00192 
00193         $tmp_array = array();
00194         foreach( $data as $currencyFrom => $rate ) {
00195             $tmp_array[$rate['currency_to']] = $rate['rate'];
00196         }
00197         $data = $tmp_array;
00198 
00199         return $data;
00200     }

getConfigCurrencies ( model,
path 
)

Retrieve config currency data by config path

Parameters:
object $model
string $path
Returns:
array

Definition at line 136 of file Currency.php.

00137     {
00138         $read = $this->_getReadAdapter();
00139         $select = $read->select()
00140                 ->from($this->getTable('core/config_data'))
00141                 ->where($read->quoteInto(' path = ? ', $path))
00142                 //->where('inherit = 0')
00143                 ->order(' value ASC ');
00144 
00145         $data = $read->fetchAll($select);
00146         $tmp_array = array();
00147         foreach( $data as $configRecord ) {
00148             $tmp_array = array_merge($tmp_array, explode(',', $configRecord['value']));
00149         }
00150 
00151         $data = array_unique($tmp_array);
00152         return $data;
00153     }

getCurrencyRates ( currency,
toCurrencies = null 
)

Retieve currency rates

Parameters:
string|array $currency
array $toCurrencies
Returns:
array

Definition at line 162 of file Currency.php.

00163     {
00164         $rates = array();
00165         if( is_array($currency) ) {
00166             foreach( $currency as $code ) {
00167                 $rates[$code] = $this->_getRatesByCode($code, $toCurrencies);
00168             }
00169         } else {
00170             $rates = $this->_getRatesByCode($currency, $toCurrencies);
00171         }
00172 
00173         return $rates;
00174     }

getRate ( currencyFrom,
currencyTo 
)

Retrieve currency rate

Parameters:
string $currencyFrom
string $currencyTo
Returns:
float

Definition at line 70 of file Currency.php.

00071     {
00072         if ($currencyFrom instanceof Mage_Directory_Model_Currency) {
00073             $currencyFrom = $currencyFrom->getCode();
00074         }
00075 
00076         if ($currencyTo instanceof Mage_Directory_Model_Currency) {
00077             $currencyTo = $currencyTo->getCode();
00078         }
00079 
00080         if ($currencyFrom == $currencyTo) {
00081             return 1;
00082         }
00083 
00084         if (!isset(self::$_rateCache[$currencyFrom][$currencyTo])) {
00085             $read = $this->_getReadAdapter();
00086             $select = $read->select()
00087                 ->from($this->_currencyRateTable, 'rate')
00088                 ->where('currency_from=?', strtoupper($currencyFrom))
00089                 ->where('currency_to=?', strtoupper($currencyTo));
00090 
00091             self::$_rateCache[$currencyFrom][$currencyTo] = $read->fetchOne($select);
00092         }
00093 
00094         return self::$_rateCache[$currencyFrom][$currencyTo];
00095     }

saveRates ( rates  ) 

Saving currency rates

Parameters:
array $rates

Definition at line 102 of file Currency.php.

00103     {
00104         if( is_array($rates) && sizeof($rates) > 0 ) {
00105             $write = $this->_getWriteAdapter();
00106             $table  = $write->quoteIdentifier($this->_currencyRateTable);
00107             $colFrom= $write->quoteIdentifier('currency_from');
00108             $colTo  = $write->quoteIdentifier('currency_to');
00109             $colRate= $write->quoteIdentifier('rate');
00110 
00111             $sql = 'REPLACE INTO ' . $table . ' (' . $colFrom . ', ' . $colTo . ', ' . $colRate . ') VALUES ';
00112             $values = array();
00113             foreach ($rates as $currencyCode => $rate) {
00114                 foreach( $rate as $currencyTo => $value ) {
00115                     $value = abs($value);
00116                     if( $value == 0 ) {
00117                         continue;
00118                     }
00119                     $values[] = $write->quoteInto('(?)', array($currencyCode, $currencyTo, $value));
00120                 }
00121             }
00122             $sql.= implode(',', $values);
00123             $write->query($sql);
00124         } else {
00125             Mage::throwException(Mage::helper('directory')->__('Invalid rates received'));
00126         }
00127     }


Member Data Documentation

$_currencyRateTable [protected]

Definition at line 41 of file Currency.php.

$_rateCache [static, protected]

Definition at line 48 of file Currency.php.


The documentation for this class was generated from the following file:

Generated on Sat Jul 4 17:24:07 2009 for Magento by  doxygen 1.5.8