1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Directory_Block_Data extends Mage_Core_Block_Template
35: {
36: public function getLoadrRegionUrl()
37: {
38: return $this->getUrl('directory/json/childRegion');
39: }
40:
41: public function getCountryCollection()
42: {
43: $collection = $this->getData('country_collection');
44: if (is_null($collection)) {
45: $collection = Mage::getModel('directory/country')->getResourceCollection()
46: ->loadByStore();
47: $this->setData('country_collection', $collection);
48: }
49:
50: return $collection;
51: }
52:
53: public function getCountryHtmlSelect($defValue=null, $name='country_id', $id='country', $title='Country')
54: {
55: Varien_Profiler::start('TEST: '.__METHOD__);
56: if (is_null($defValue)) {
57: $defValue = $this->getCountryId();
58: }
59: $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_'.Mage::app()->getStore()->getCode();
60: if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
61: $options = unserialize($cache);
62: } else {
63: $options = $this->getCountryCollection()->toOptionArray();
64: if (Mage::app()->useCache('config')) {
65: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
66: }
67: }
68: $html = $this->getLayout()->createBlock('core/html_select')
69: ->setName($name)
70: ->setId($id)
71: ->setTitle(Mage::helper('directory')->__($title))
72: ->setClass('validate-select')
73: ->setValue($defValue)
74: ->setOptions($options)
75: ->getHtml();
76:
77: Varien_Profiler::stop('TEST: '.__METHOD__);
78: return $html;
79: }
80:
81: public function getRegionCollection()
82: {
83: $collection = $this->getData('region_collection');
84: if (is_null($collection)) {
85: $collection = Mage::getModel('directory/region')->getResourceCollection()
86: ->addCountryFilter($this->getCountryId())
87: ->load();
88:
89: $this->setData('region_collection', $collection);
90: }
91: return $collection;
92: }
93:
94:
95: public function getRegionHtmlSelect()
96: {
97: Varien_Profiler::start('TEST: '.__METHOD__);
98: $cacheKey = 'DIRECTORY_REGION_SELECT_STORE'.Mage::app()->getStore()->getId();
99: if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
100: $options = unserialize($cache);
101: } else {
102: $options = $this->getRegionCollection()->toOptionArray();
103: if (Mage::app()->useCache('config')) {
104: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
105: }
106: }
107: $html = $this->getLayout()->createBlock('core/html_select')
108: ->setName('region')
109: ->setTitle(Mage::helper('directory')->__('State/Province'))
110: ->setId('state')
111: ->setClass('required-entry validate-state')
112: ->setValue(intval($this->getRegionId()))
113: ->setOptions($options)
114: ->getHtml();
115: Varien_Profiler::start('TEST: '.__METHOD__);
116: return $html;
117: }
118:
119: public function getCountryId()
120: {
121: $countryId = $this->getData('country_id');
122: if (is_null($countryId)) {
123: $countryId = Mage::helper('core')->getDefaultCountry();
124: }
125: return $countryId;
126: }
127:
128: public function getRegionsJs()
129: {
130: Varien_Profiler::start('TEST: '.__METHOD__);
131: $regionsJs = $this->getData('regions_js');
132: if (!$regionsJs) {
133: $countryIds = array();
134: foreach ($this->getCountryCollection() as $country) {
135: $countryIds[] = $country->getCountryId();
136: }
137: $collection = Mage::getModel('directory/region')->getResourceCollection()
138: ->addCountryFilter($countryIds)
139: ->load();
140: $regions = array();
141: foreach ($collection as $region) {
142: if (!$region->getRegionId()) {
143: continue;
144: }
145: $regions[$region->getCountryId()][$region->getRegionId()] = array(
146: 'code'=>$region->getCode(),
147: 'name'=>$region->getName()
148: );
149: }
150: $regionsJs = Mage::helper('core')->jsonEncode($regions);
151: }
152: Varien_Profiler::stop('TEST: '.__METHOD__);
153: return $regionsJs;
154: }
155: }
156: