00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 class Mage_Directory_Helper_Data extends Mage_Core_Helper_Abstract
00033 {
00034 protected $_countryCollection;
00035 protected $_regionCollection;
00036 protected $_regionJson;
00037 protected $_currencyCache = array();
00038
00039 public function getRegionCollection()
00040 {
00041 if (!$this->_regionCollection) {
00042 $this->_regionCollection = Mage::getModel('directory/region')->getResourceCollection()
00043 ->addCountryFilter($this->getAddress()->getCountryId())
00044 ->load();
00045 }
00046 return $this->_regionCollection;
00047 }
00048
00049 public function getCountryCollection()
00050 {
00051 if (!$this->_countryCollection) {
00052 $this->_countryCollection = Mage::getModel('directory/country')->getResourceCollection()
00053 ->loadByStore();
00054 }
00055 return $this->_countryCollection;
00056 }
00057
00058
00059
00060
00061
00062
00063 public function getRegionJson()
00064 {
00065
00066 Varien_Profiler::start('TEST: '.__METHOD__);
00067 if (!$this->_regionJson) {
00068 $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE'.Mage::app()->getStore()->getId();
00069 if (Mage::app()->useCache('config')) {
00070 $json = Mage::app()->loadCache($cacheKey);
00071 }
00072 if (empty($json)) {
00073 $countryIds = array();
00074 foreach ($this->getCountryCollection() as $country) {
00075 $countryIds[] = $country->getCountryId();
00076 }
00077 $collection = Mage::getModel('directory/region')->getResourceCollection()
00078 ->addCountryFilter($countryIds)
00079 ->load();
00080 $regions = array();
00081 foreach ($collection as $region) {
00082 if (!$region->getRegionId()) {
00083 continue;
00084 }
00085 $regions[$region->getCountryId()][$region->getRegionId()] = array(
00086 'code'=>$region->getCode(),
00087 'name'=>$region->getName()
00088 );
00089 }
00090 $json = Zend_Json::encode($regions);
00091
00092 if (Mage::app()->useCache('config')) {
00093 Mage::app()->saveCache($json, $cacheKey, array('config'));
00094 }
00095 }
00096 $this->_regionJson = $json;
00097 }
00098
00099 Varien_Profiler::stop('TEST: '.__METHOD__);
00100 return $this->_regionJson;
00101 }
00102
00103 public function currencyConvert($amount, $from, $to=null)
00104 {
00105 if (empty($this->_currencyCache[$from])) {
00106 $this->_currencyCache[$from] = Mage::getModel('directory/currency')->load($from);
00107 }
00108 if (is_null($to)) {
00109 $to = Mage::app()->getStore()->getCurrentCurrencyCode();
00110 }
00111 $converted = $this->_currencyCache[$from]->convert($amount, $to);
00112 return $converted;
00113 }
00114 }