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_XmlConnect_Model_Simplexml_Form_Element_CountryListSelect
35: extends Mage_XmlConnect_Model_Simplexml_Form_Element_Select
36: {
37: 38: 39: 40: 41:
42: protected $_countryListValues = array('country_id', 'region_id', 'region');
43:
44: 45: 46: 47: 48:
49: public function __construct($attributes = array())
50: {
51: parent::__construct($attributes);
52: }
53:
54: 55: 56: 57: 58:
59: protected function _setValues()
60: {
61: $value = $this->getValue();
62:
63: foreach ($this->_countryListValues as $param) {
64: if (isset($value[$param])) {
65: $this->setData($param, $value[$param]);
66: }
67: }
68:
69: return $this;
70: }
71:
72: 73: 74: 75: 76: 77: 78:
79: protected function _addOldStandardValue(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
80: {
81: $countries = $this->_getCountryOptions();
82:
83: if (is_array($countries)) {
84: $valuesXmlObj = $xmlObj->addCustomChild('values');
85: foreach ($countries as $data) {
86: $regions = array();
87:
88: if ($data['value']) {
89: $regions = $this->_getRegionOptions($data['value']);
90: }
91:
92: $relationType = is_array($regions) && !empty($regions) ? 'region_id' : 'region';
93:
94: $selectedCountry = array();
95: if ($this->getCountryId() == $data['value']) {
96: $selectedCountry = array('selected' => 1);
97: }
98:
99: $item = $valuesXmlObj->addCustomChild('item', null,
100: array('relation' => $relationType) + $selectedCountry
101: );
102:
103: $item->addCustomChild('label', (string)$data['label']);
104: $item->addCustomChild('value', $data['value']);
105:
106: if ($relationType == 'region_id') {
107: $regionsXmlObj = $item->addCustomChild('regions');
108: foreach ($regions as $regionData) {
109: $selectedRegion = array();
110:
111: if (!empty($selectedCountry) && $this->getRegionId() == $regionData['value']) {
112: $selectedRegion = array('selected' => 1);
113: }
114:
115: $regionItem = $regionsXmlObj->addCustomChild('region_item', null, $selectedRegion);
116: $regionItem->addCustomChild('label', (string)$regionData['label']);
117: $regionItem->addCustomChild('value', (string)$regionData['value']);
118: }
119: }
120: }
121: }
122:
123: return $this;
124: }
125:
126: 127: 128: 129: 130: 131:
132: protected function _addValue(Mage_XmlConnect_Model_Simplexml_Element $xmlObj)
133: {
134: $this->_setValues();
135:
136: if ($this->getOldFormat()) {
137: $this->_addOldStandardValue($xmlObj);
138: return $this;
139: }
140:
141: if ($this->getCountryId()) {
142: $xmlObj->addAttribute('value', $xmlObj->xmlAttribute($this->getCountryId()));
143: }
144: $countries = $this->_getCountryOptions();
145:
146: if (is_array($countries)) {
147: $values = $xmlObj->addCustomChild('values');
148: foreach ($countries as $data) {
149: $regions = array();
150:
151: if ($data['value']) {
152: $regions = $this->_getRegionOptions($data['value']);
153: }
154:
155: if (is_array($regions) && !empty($regions)) {
156: $relationType = 'region_id';
157: } else {
158: $relationType = 'region';
159: }
160:
161: $item = $values->addCustomChild('item', null, array(
162: 'relation' => $relationType, 'label' => (string)$data['label'], 'value' => $data['value']
163: ));
164:
165: if ($relationType !== 'region') {
166:
167: $selectedRegion = array();
168: if ($this->getCountryId() == $data['value']) {
169: $selectedRegion = array('value' => $this->getRegionId());
170: }
171:
172: $regionsXmlObj = $item->addCustomChild('field', null, array(
173: 'id' => 'region_list_' . $data['value'], 'name' => 'region_id',
174: 'label' => Mage::helper('xmlconnect')->__('State/Province'), 'type' => 'select',
175: 'required' => 1
176: ) + $selectedRegion);
177:
178: $regionValues = $regionsXmlObj->addCustomChild('values');
179:
180: foreach ($regions as $regionData) {
181: $regionValues->addCustomChild('item', null, array(
182: 'label' => (string)$regionData['label'], 'value' => (string)$regionData['value']
183: ));
184: }
185: } elseif ($this->getCountryId() == $data['value']) {
186: $item->addCustomChild('field', null, array(
187: 'id' => 'region_' . $data['value'], 'name' => 'region',
188: 'label' => Mage::helper('xmlconnect')->__('State/Province'), 'type' => 'text',
189: 'value' => $this->getRegion(), 'required' => 1
190: ));
191: }
192: }
193: }
194: return $this;
195: }
196:
197: 198: 199: 200: 201: 202:
203: protected function _getRegionOptions($countryId)
204: {
205: $cacheKey = 'DIRECTORY_REGION_SELECT_STORE' . Mage::app()->getStore()->getId() . $countryId;
206: $cache = Mage::app()->loadCache($cacheKey);
207: if (Mage::app()->useCache('config') && $cache) {
208: $options = unserialize($cache);
209: } else {
210: $collection = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter($countryId)
211: ->load();
212: $options = $collection->toOptionArray();
213: if (Mage::app()->useCache('config')) {
214: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
215: }
216: }
217: return $options;
218: }
219:
220: 221: 222: 223: 224:
225: protected function _getCountryOptions()
226: {
227: $cacheKey = 'DIRECTORY_COUNTRY_SELECT_STORE_' . Mage::app()->getStore()->getCode();
228: $cache = Mage::app()->loadCache($cacheKey);
229: if (Mage::app()->useCache('config') && $cache) {
230: $options = unserialize($cache);
231: } else {
232: $collection = Mage::getModel('directory/country')->getResourceCollection()->loadByStore();
233: $options = $collection->toOptionArray(false);
234: if (Mage::app()->useCache('config')) {
235: Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
236: }
237: }
238: return $options;
239: }
240: }
241: