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
00033
00034 class Mage_Directory_Block_Data extends Mage_Core_Block_Template
00035 {
00036 public function getLoadrRegionUrl()
00037 {
00038 return $this->getUrl('directory/json/childRegion');
00039 }
00040
00041 public function getCountryCollection()
00042 {
00043 $collection = $this->getData('country_collection');
00044 if (is_null($collection)) {
00045 $collection = Mage::getModel('directory/country')->getResourceCollection()
00046 ->loadByStore();
00047 $this->setData('country_collection', $collection);
00048 }
00049
00050 return $collection;
00051 }
00052
00053 public function getCountryHtmlSelect($defValue=null, $name='country_id', $id='country', $title='Country')
00054 {
00055 Varien_Profiler::start('TEST: '.__METHOD__);
00056 if (is_null($defValue)) {
00057 $defValue = $this->getCountryId();
00058 }
00059 $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_'.Mage::app()->getStore()->getCode();
00060 if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
00061 $options = unserialize($cache);
00062 } else {
00063 $options = $this->getCountryCollection()->toOptionArray();
00064 if (Mage::app()->useCache('config')) {
00065 Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
00066 }
00067 }
00068 $html = $this->getLayout()->createBlock('core/html_select')
00069 ->setName($name)
00070 ->setId($id)
00071 ->setTitle(Mage::helper('directory')->__($title))
00072 ->setClass('validate-select')
00073 ->setValue($defValue)
00074 ->setOptions($options)
00075 ->getHtml();
00076
00077 Varien_Profiler::stop('TEST: '.__METHOD__);
00078 return $html;
00079 }
00080
00081 public function getRegionCollection()
00082 {
00083 $collection = $this->getData('region_collection');
00084 if (is_null($collection)) {
00085 $collection = Mage::getModel('directory/region')->getResourceCollection()
00086 ->addCountryFilter($this->getCountryId())
00087 ->load();
00088
00089 $this->setData('region_collection', $collection);
00090 }
00091 return $collection;
00092 }
00093
00094
00095 public function getRegionHtmlSelect()
00096 {
00097 Varien_Profiler::start('TEST: '.__METHOD__);
00098 $cacheKey = 'DIRECTORY_REGION_SELECT_STORE'.Mage::app()->getStore()->getId();
00099 if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
00100 $options = unserialize($cache);
00101 } else {
00102 $options = $this->getRegionCollection()->toOptionArray();
00103 if (Mage::app()->useCache('config')) {
00104 Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
00105 }
00106 }
00107 $html = $this->getLayout()->createBlock('core/html_select')
00108 ->setName('region')
00109 ->setTitle(Mage::helper('directory')->__('State/Province'))
00110 ->setId('state')
00111 ->setClass('required-entry validate-state')
00112 ->setValue($this->getRegionId())
00113 ->setOptions($options)
00114 ->getHtml();
00115 Varien_Profiler::start('TEST: '.__METHOD__);
00116 return $html;
00117 }
00118
00119 public function getCountryId()
00120 {
00121 $countryId = $this->getData('country_id');
00122 if (is_null($countryId)) {
00123 $countryId = Mage::getStoreConfig('general/country/default');
00124 }
00125 return $countryId;
00126 }
00127
00128 public function getRegionsJs()
00129 {
00130 Varien_Profiler::start('TEST: '.__METHOD__);
00131 $regionsJs = $this->getData('regions_js');
00132 if (!$regionsJs) {
00133 $countryIds = array();
00134 foreach ($this->getCountryCollection() as $country) {
00135 $countryIds[] = $country->getCountryId();
00136 }
00137 $collection = Mage::getModel('directory/region')->getResourceCollection()
00138 ->addCountryFilter($countryIds)
00139 ->load();
00140 $regions = array();
00141 foreach ($collection as $region) {
00142 if (!$region->getRegionId()) {
00143 continue;
00144 }
00145 $regions[$region->getCountryId()][$region->getRegionId()] = array(
00146 'code'=>$region->getCode(),
00147 'name'=>$region->getName()
00148 );
00149 }
00150 $regionsJs = Zend_Json::encode($regions);
00151 }
00152 Varien_Profiler::stop('TEST: '.__METHOD__);
00153 return $regionsJs;
00154 }
00155 }