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:
35: class Mage_Core_Block_Html_Select extends Mage_Core_Block_Abstract
36: {
37:
38: protected $_options = array();
39:
40: 41: 42: 43: 44:
45: public function getOptions()
46: {
47: return $this->_options;
48: }
49:
50: 51: 52: 53: 54: 55:
56: public function setOptions($options)
57: {
58: $this->_options = $options;
59: return $this;
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69:
70: public function addOption($value, $label, $params=array())
71: {
72: $this->_options[] = array('value' => $value, 'label' => $label, 'params' => $params);
73: return $this;
74: }
75:
76: 77: 78: 79: 80: 81:
82: public function setId($id)
83: {
84: $this->setData('id', $id);
85: return $this;
86: }
87:
88: 89: 90: 91: 92: 93:
94: public function setClass($class)
95: {
96: $this->setData('class', $class);
97: return $this;
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function setTitle($title)
107: {
108: $this->setData('title', $title);
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: public function getId()
118: {
119: return $this->getData('id');
120: }
121:
122: 123: 124: 125: 126:
127: public function getClass()
128: {
129: return $this->getData('class');
130: }
131:
132: 133: 134: 135: 136:
137: public function getTitle()
138: {
139: return $this->getData('title');
140: }
141:
142: 143: 144: 145: 146:
147: protected function _toHtml()
148: {
149: if (!$this->_beforeToHtml()) {
150: return '';
151: }
152:
153: $html = '<select name="' . $this->getName() . '" id="' . $this->getId() . '" class="'
154: . $this->getClass() . '" title="' . $this->getTitle() . '" ' . $this->getExtraParams() . '>';
155: $values = $this->getValue();
156:
157: if (!is_array($values)){
158: if (!is_null($values)) {
159: $values = array($values);
160: } else {
161: $values = array();
162: }
163: }
164:
165: $isArrayOption = true;
166: foreach ($this->getOptions() as $key => $option) {
167: if ($isArrayOption && is_array($option)) {
168: $value = $option['value'];
169: $label = (string)$option['label'];
170: $params = (!empty($option['params'])) ? $option['params'] : array();
171: } else {
172: $value = (string)$key;
173: $label = (string)$option;
174: $isArrayOption = false;
175: $params = array();
176: }
177:
178: if (is_array($value)) {
179: $html .= '<optgroup label="' . $label . '">';
180: foreach ($value as $keyGroup => $optionGroup) {
181: if (!is_array($optionGroup)) {
182: $optionGroup = array(
183: 'value' => $keyGroup,
184: 'label' => $optionGroup
185: );
186: }
187: $html .= $this->_optionToHtml(
188: $optionGroup,
189: in_array($optionGroup['value'], $values)
190: );
191: }
192: $html .= '</optgroup>';
193: } else {
194: $html .= $this->_optionToHtml(
195: array(
196: 'value' => $value,
197: 'label' => $label,
198: 'params' => $params
199: ),
200: in_array($value, $values)
201: );
202: }
203: }
204: $html .= '</select>';
205: return $html;
206: }
207:
208: 209: 210: 211: 212: 213: 214:
215: protected function _optionToHtml($option, $selected = false)
216: {
217: $selectedHtml = $selected ? ' selected="selected"' : '';
218: if ($this->getIsRenderToJsTemplate() === true) {
219: $selectedHtml .= ' #{option_extra_attr_' . self::calcOptionHash($option['value']) . '}';
220: }
221:
222: $params = '';
223: if (!empty($option['params']) && is_array($option['params'])) {
224: foreach ($option['params'] as $key => $value) {
225: if (is_array($value)) {
226: foreach ($value as $keyMulti => $valueMulti) {
227: $params .= sprintf(' %s="%s" ', $keyMulti, $valueMulti);
228: }
229: } else {
230: $params .= sprintf(' %s="%s" ', $key, $value);
231: }
232: }
233: }
234:
235: return sprintf('<option value="%s"%s %s>%s</option>',
236: $this->escapeHtml($option['value']),
237: $selectedHtml,
238: $params,
239: $this->escapeHtml($option['label']));
240: }
241:
242: 243: 244: 245: 246:
247: public function getHtml()
248: {
249: return $this->toHtml();
250: }
251:
252: 253: 254: 255: 256: 257:
258: public function calcOptionHash($optionValue)
259: {
260: return sprintf('%u', crc32($this->getName() . $this->getId() . $optionValue));
261: }
262: }
263: