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_Adminhtml_Model_Customer_Renderer_Region implements Varien_Data_Form_Element_Renderer_Interface
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44:
45: static protected $_regionCollections;
46:
47: public function render(Varien_Data_Form_Element_Abstract $element)
48: {
49: $html = '<tr>'."\n";
50:
51: $countryId = false;
52: if ($country = $element->getForm()->getElement('country_id')) {
53: $countryId = $country->getValue();
54: }
55:
56: $regionCollection = false;
57: if ($countryId) {
58: if (!isset(self::$_regionCollections[$countryId])) {
59: self::$_regionCollections[$countryId] = Mage::getModel('directory/country')
60: ->setId($countryId)
61: ->getLoadedRegionCollection()
62: ->toOptionArray();
63: }
64: $regionCollection = self::$_regionCollections[$countryId];
65: }
66:
67: $regionId = intval($element->getForm()->getElement('region_id')->getValue());
68:
69: $htmlAttributes = $element->getHtmlAttributes();
70: foreach ($htmlAttributes as $key => $attribute) {
71: if ('type' === $attribute) {
72: unset($htmlAttributes[$key]);
73: break;
74: }
75: }
76:
77:
78:
79:
80:
81: $regionHtmlName = $element->getName();
82: $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName);
83: $regionHtmlId = $element->getHtmlId();
84: $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId);
85:
86: if ($regionCollection && count($regionCollection) > 0) {
87: $elementClass = $element->getClass();
88: $html.= '<td class="label">'.$element->getLabelHtml().'</td>';
89: $html.= '<td class="value">';
90:
91: $html .= '<select id="' . $regionIdHtmlId . '" name="' . $regionIdHtmlName . '" '
92: . $element->serialize($htmlAttributes) .'>' . "\n";
93: foreach ($regionCollection as $region) {
94: $selected = ($regionId==$region['value']) ? ' selected="selected"' : '';
95: $html.= '<option value="'.(int)$region['value'].'"'.$selected.'>'.$region['label'].'</option>';
96: }
97: $html.= '</select>' . "\n";
98:
99: $html .= '<input type="hidden" name="' . $regionHtmlName . '" id="' . $regionHtmlId . '" value=""/>';
100:
101: $html.= '</td>';
102: $element->setClass($elementClass);
103: } else {
104: $element->setClass('input-text');
105: $html.= '<td class="label"><label for="'.$element->getHtmlId().'">'
106: . $element->getLabel()
107: . ' <span class="required" style="display:none">*</span></label></td>';
108:
109: $element->setRequired(false);
110: $html.= '<td class="value">';
111: $html .= '<input id="' . $regionHtmlId . '" name="' . $regionHtmlName
112: . '" value="' . $element->getEscapedValue() . '" ' . $element->serialize($htmlAttributes) . "/>" . "\n";
113: $html .= '<input type="hidden" name="' . $regionIdHtmlName . '" id="' . $regionIdHtmlId . '" value=""/>';
114: $html .= '</td>'."\n";
115: }
116: $html.= '</tr>'."\n";
117: return $html;
118: }
119: }
120: