00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Varien_Data_Form_Element_Select extends Varien_Data_Form_Element_Abstract
00035 {
00036 public function __construct($attributes=array())
00037 {
00038 parent::__construct($attributes);
00039 $this->setType('select');
00040 $this->setExtType('combobox');
00041 $this->_prepareOptions();
00042 }
00043
00044 public function getElementHtml()
00045 {
00046 $this->addClass('select');
00047 $html = '<select id="'.$this->getHtmlId().'" name="'.$this->getName().'" '.$this->serialize($this->getHtmlAttributes()).'>'."\n";
00048
00049 $value = $this->getValue();
00050 if (!is_array($value)) {
00051 $value = array($value);
00052 }
00053
00054 if ($values = $this->getValues()) {
00055 foreach ($values as $key => $option) {
00056 if (!is_array($option)) {
00057 $html.= $this->_optionToHtml(array(
00058 'value' => $key,
00059 'label' => $option),
00060 $value
00061 );
00062 }
00063 elseif (is_array($option['value'])) {
00064 $html.='<optgroup label="'.$option['label'].'">'."\n";
00065 foreach ($option['value'] as $groupItem) {
00066 $html.= $this->_optionToHtml($groupItem, $value);
00067 }
00068 $html.='</optgroup>'."\n";
00069 }
00070 else {
00071 $html.= $this->_optionToHtml($option, $value);
00072 }
00073 }
00074 }
00075
00076 $html.= '</select>'."\n";
00077 $html.= $this->getAfterElementHtml();
00078 return $html;
00079 }
00080
00081 protected function _optionToHtml($option, $selected)
00082 {
00083 if (is_array($option['value'])) {
00084 $html ='<optgroup label="'.$option['label'].'">'."\n";
00085 foreach ($option['value'] as $groupItem) {
00086 $html .= $this->_optionToHtml($groupItem, $selected);
00087 }
00088 $html .='</optgroup>'."\n";
00089 }
00090 else {
00091 $html = '<option value="'.$this->_escape($option['value']).'"';
00092 $html.= isset($option['title']) ? 'title="'.$option['title'].'"' : '';
00093 $html.= isset($option['style']) ? 'style="'.$option['style'].'"' : '';
00094 if (in_array($option['value'], $selected)) {
00095 $html.= ' selected="selected"';
00096 }
00097 $html.= '>'.$option['label']. '</option>'."\n";
00098 }
00099 return $html;
00100 }
00101
00102 protected function _prepareOptions()
00103 {
00104 $values = $this->getValues();
00105 if (empty($values)) {
00106 $options = $this->getOptions();
00107 if (is_array($options)) {
00108 $values = array();
00109 foreach ($options as $value => $label) {
00110 $values[] = array('value' => $value, 'label' => $label);
00111 }
00112 } elseif (is_string($options)) {
00113 $values = array( array('value' => $options, 'label' => $options) );
00114 }
00115 $this->setValues($values);
00116 }
00117 }
00118
00119 public function getHtmlAttributes()
00120 {
00121 return array('title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'readonly');
00122 }
00123 }