00001 <?php
00002
00015
00016
00017
00018 class CountryDAO extends DAO {
00019 var $cache;
00020
00024 function CountryDAO() {
00025 }
00026
00031 function getFilename($locale = null) {
00032 if ($locale === null) $locale = Locale::getLocale();
00033 return Config::getVar('general', 'registry_dir') . "/locale/$locale/countries.xml";
00034 }
00035
00036 function &_getCountryCache($locale = null) {
00037 static $caches;
00038
00039 if (!isset($locale)) $locale = Locale::getLocale();
00040
00041 if (!isset($caches)) {
00042 $caches = array();
00043 }
00044
00045 if (!isset($caches[$locale])) {
00046 import('cache.CacheManager');
00047 $cacheManager =& CacheManager::getManager();
00048 $caches[$locale] = $cacheManager->getFileCache(
00049 'country', $locale,
00050 array(&$this, '_countryCacheMiss')
00051 );
00052
00053
00054 $cacheTime = $caches[$locale]->getCacheTime();
00055 if ($cacheTime !== null && $cacheTime < filemtime($this->getFilename())) {
00056 $caches[$locale]->flush();
00057 }
00058 }
00059 return $caches[$locale];
00060 }
00061
00062 function _countryCacheMiss(&$cache, $id) {
00063 static $countries;
00064 if (!isset($countries)) {
00065 $countries = array();
00066 }
00067
00068 if (!isset($countries[$id])) {
00069
00070 $xmlDao = &new XMLDAO();
00071 $data = $xmlDao->parseStruct($this->getFilename(), array('countries', 'country'));
00072
00073 if (isset($data['countries'])) {
00074 foreach ($data['country'] as $countryData) {
00075 $countries[$id][$countryData['attributes']['code']] = $countryData['attributes']['name'];
00076 }
00077 }
00078 asort($countries[$id]);
00079 $cache->setEntireCache($countries[$id]);
00080 }
00081 return null;
00082 }
00083
00089 function &getCountries($locale = null) {
00090 $cache =& $this->_getCountryCache($locale);
00091 return $cache->getContents();
00092 }
00093
00099 function getCountry($code, $locale = null) {
00100 $cache =& $this->_getCountryCache($locale);
00101 return $cache->get($code);
00102 }
00103 }
00104
00105 ?>