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: class Mage_Directory_Helper_Data extends Mage_Core_Helper_Abstract
33: {
34: 35: 36:
37: const OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH = 'general/country/optional_zip_countries';
38:
39: 40: 41:
42: const XML_PATH_STATES_REQUIRED = 'general/region/state_required';
43:
44: 45: 46:
47: const XML_PATH_DISPLAY_ALL_STATES = 'general/region/display_all';
48:
49: 50: 51: 52: 53:
54: protected $_countryCollection;
55:
56: 57: 58: 59: 60:
61: protected $_regionCollection;
62:
63: 64: 65: 66: 67:
68: protected $_regionJson;
69:
70: 71: 72: 73: 74:
75: protected $_currencyCache = array();
76:
77: 78: 79: 80: 81:
82: protected $_optionalZipCountries = null;
83:
84: 85: 86: 87: 88:
89: public function getRegionCollection()
90: {
91: if (!$this->_regionCollection) {
92: $this->_regionCollection = Mage::getModel('directory/region')->getResourceCollection()
93: ->addCountryFilter($this->getAddress()->getCountryId())
94: ->load();
95: }
96: return $this->_regionCollection;
97: }
98:
99: 100: 101: 102: 103:
104: public function getCountryCollection()
105: {
106: if (!$this->_countryCollection) {
107: $this->_countryCollection = Mage::getModel('directory/country')->getResourceCollection()
108: ->loadByStore();
109: }
110: return $this->_countryCollection;
111: }
112:
113: 114: 115: 116: 117:
118: public function getRegionJson()
119: {
120:
121: Varien_Profiler::start('TEST: '.__METHOD__);
122: if (!$this->_regionJson) {
123: $cacheKey = 'DIRECTORY_REGIONS_JSON_STORE'.Mage::app()->getStore()->getId();
124: if (Mage::app()->useCache('config')) {
125: $json = Mage::app()->loadCache($cacheKey);
126: }
127: if (empty($json)) {
128: $countryIds = array();
129: foreach ($this->getCountryCollection() as $country) {
130: $countryIds[] = $country->getCountryId();
131: }
132: $collection = Mage::getModel('directory/region')->getResourceCollection()
133: ->addCountryFilter($countryIds)
134: ->load();
135: $regions = array(
136: 'config' => array(
137: 'show_all_regions' => $this->getShowNonRequiredState(),
138: 'regions_required' => $this->getCountriesWithStatesRequired()
139: )
140: );
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' => $this->__($region->getName())
148: );
149: }
150: $json = Mage::helper('core')->jsonEncode($regions);
151:
152: if (Mage::app()->useCache('config')) {
153: Mage::app()->saveCache($json, $cacheKey, array('config'));
154: }
155: }
156: $this->_regionJson = $json;
157: }
158:
159: Varien_Profiler::stop('TEST: '.__METHOD__);
160: return $this->_regionJson;
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170:
171: public function currencyConvert($amount, $from, $to = null)
172: {
173: if (empty($this->_currencyCache[$from])) {
174: $this->_currencyCache[$from] = Mage::getModel('directory/currency')->load($from);
175: }
176: if (is_null($to)) {
177: $to = Mage::app()->getStore()->getCurrentCurrencyCode();
178: }
179: $converted = $this->_currencyCache[$from]->convert($amount, $to);
180: return $converted;
181: }
182:
183: 184: 185: 186: 187: 188:
189: public function getCountriesWithOptionalZip($asJson = false)
190: {
191: if (null === $this->_optionalZipCountries) {
192: $this->_optionalZipCountries = preg_split('/\,/',
193: Mage::getStoreConfig(self::OPTIONAL_ZIP_COUNTRIES_CONFIG_PATH), 0, PREG_SPLIT_NO_EMPTY);
194: }
195: if ($asJson) {
196: return Mage::helper('core')->jsonEncode($this->_optionalZipCountries);
197: }
198: return $this->_optionalZipCountries;
199: }
200:
201: 202: 203: 204: 205: 206:
207: public function isZipCodeOptional($countryCode)
208: {
209: $this->getCountriesWithOptionalZip();
210: return in_array($countryCode, $this->_optionalZipCountries);
211: }
212:
213: 214: 215: 216: 217: 218:
219: public function getCountriesWithStatesRequired($asJson = false)
220: {
221: $countryList = explode(',', Mage::getStoreConfig(self::XML_PATH_STATES_REQUIRED));
222: if ($asJson) {
223: return Mage::helper('core')->jsonEncode($countryList);
224: }
225: return $countryList;
226: }
227:
228: 229: 230: 231: 232:
233: public function getShowNonRequiredState()
234: {
235: return (boolean)Mage::getStoreConfig(self::XML_PATH_DISPLAY_ALL_STATES);
236: }
237:
238: 239: 240: 241: 242: 243:
244: public function isRegionRequired($countryId)
245: {
246: $countyList = $this->getCountriesWithStatesRequired();
247: if(!is_array($countyList)) {
248: return false;
249: }
250: return in_array($countryId, $countyList);
251: }
252: }
253: