00001 <?php
00002
00017 import('currency.Currency');
00018
00019 class CurrencyDAO extends DAO {
00020
00024 function CurrencyDAO() {
00025 parent::DAO();
00026 }
00027
00028 function &_getCache() {
00029 $locale = Locale::getLocale();
00030 static $cache;
00031 if (!isset($cache)) {
00032 import('cache.CacheManager');
00033 $cacheManager = CacheManager::getManager();
00034 $cache =& $cacheManager->getFileCache(
00035 'currencies', $locale,
00036 array(&$this, '_cacheMiss')
00037 );
00038 $cacheTime = $cache->getCacheTime();
00039 if ($cacheTime !== null && $cacheTime < filemtime($this->getCurrencyFilename($locale))) {
00040 $cache->flush();
00041 }
00042 }
00043
00044 return $cache;
00045 }
00046
00047 function _cacheMiss(&$cache, $id) {
00048 static $allCurrencies;
00049 if (!isset($allCurrencies)) {
00050
00051 $notes =& Registry::get('system.debug.notes');
00052 $filename = $this->getCurrencyFilename(Locale::getLocale());
00053 $notes[] = array('debug.notes.currencyListLoad', array('filename' => $filename));
00054
00055
00056 $xmlDao = &new XMLDAO();
00057 $data = $xmlDao->parseStruct($filename, array('currency'));
00058
00059
00060 if (isset($data['currency'])) {
00061 foreach ($data['currency'] as $currencyData) {
00062 $allCurrencies[$currencyData['attributes']['code_alpha']] = array(
00063 $currencyData['attributes']['name'],
00064 $currencyData['attributes']['code_numeric']
00065 );
00066 }
00067 }
00068 asort($allCurrencies);
00069 $cache->setEntireCache($allCurrencies);
00070 }
00071 return null;
00072 }
00073
00074 function getCurrencyFilename($locale) {
00075 return "locale/$locale/currencies.xml";
00076 }
00077
00083 function &getCurrencyByAlphaCode($codeAlpha) {
00084 $cache =& $this->_getCache();
00085 $returner =& $this->_returnCurrencyFromRow($codeAlpha, $cache->get($codeAlpha));
00086 return $returner;
00087 }
00088
00093 function &getCurrencies() {
00094 $cache =& $this->_getCache();
00095 $returner = array();
00096 foreach ($cache->getContents() as $codeAlpha => $entry) {
00097 $returner[] =& $this->_returnCurrencyFromRow($codeAlpha, $entry);
00098 }
00099 return $returner;
00100 }
00101
00107 function &_returnCurrencyFromRow($codeAlpha, &$entry) {
00108 $currency = &new Currency();
00109 $currency->setCodeAlpha($codeAlpha);
00110 $currency->setName($entry[0]);
00111 $currency->setCodeNumeric($entry[1]);
00112
00113 HookRegistry::call('CurrencyDAO::_returnCurrencyFromRow', array(&$currency, &$codeAlpha, &$entry));
00114
00115 return $currency;
00116 }
00117 }
00118
00119 ?>