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
00035 class Mage_Core_Block_Html_Select extends Mage_Core_Block_Abstract
00036 {
00037
00038 protected $_options = array();
00039
00040 public function getOptions()
00041 {
00042 return $this->_options;
00043 }
00044
00045 public function setOptions($options)
00046 {
00047 $this->_options = $options;
00048 return $this;
00049 }
00050
00051 public function addOption($value, $label, $params=array())
00052 {
00053 $this->_options[] = array('value'=>$value, 'label'=>$label);
00054 return $this;
00055 }
00056
00057 public function setId($id)
00058 {
00059 $this->setData('id', $id);
00060 return $this;
00061 }
00062
00063 public function setClass($class)
00064 {
00065 $this->setData('class', $class);
00066 return $this;
00067 }
00068
00069 public function setTitle($title)
00070 {
00071 $this->setData('title', $title);
00072 return $this;
00073 }
00074
00075 public function getId()
00076 {
00077 return $this->getData('id');
00078 }
00079
00080 public function getClass()
00081 {
00082 return $this->getData('class');
00083 }
00084
00085 public function getTitle()
00086 {
00087 return $this->getData('title');
00088 }
00089
00090 protected function _toHtml()
00091 {
00092 if (!$this->_beforeToHtml()) {
00093 return '';
00094 }
00095
00096 $html = '<select name="'.$this->getName().'" id="'.$this->getId().'" class="'
00097 .$this->getClass().'" title="'.$this->getTitle().'" '.$this->getExtraParams().'>';
00098 $values = $this->getValue();
00099
00100 if (!is_array($values)){
00101 if (!is_null($values)) {
00102 $values = array($values);
00103 } else {
00104 $values = array();
00105 }
00106 }
00107
00108 $isArrayOption = true;
00109 foreach ($this->getOptions() as $key => $option) {
00110 if ($isArrayOption && is_array($option)) {
00111 $value = $option['value'];
00112 $label = $option['label'];
00113 }
00114 else {
00115 $value = $key;
00116 $label = $option;
00117 $isArrayOption = false;
00118 }
00119
00120 if (is_array($value)) {
00121 $html.= '<optgroup label="'.$label.'">';
00122 foreach ($value as $keyGroup => $optionGroup) {
00123 if (!is_array($optionGroup)) {
00124 $optionGroup = array(
00125 'value' => $keyGroup,
00126 'label' => $optionGroup
00127 );
00128 }
00129 $html.= $this->_optionToHtml(
00130 $optionGroup,
00131 in_array($optionGroup['value'], $values)
00132 );
00133 }
00134 $html.= '</optgroup>';
00135 } else {
00136 $html.= $this->_optionToHtml(array(
00137 'value' => $value,
00138 'label' => $label
00139 ),
00140 in_array($value, $values)
00141 );
00142 }
00143 }
00144 $html.= '</select>';
00145 return $html;
00146 }
00147
00148 protected function _optionToHtml($option, $selected=false)
00149 {
00150 $selectedHtml = $selected ? ' selected="selected"' : '';
00151 $html = '<option value="'.$option['value'].'"'.$selectedHtml.'>'.$option['label'].'</option>';
00152
00153 return $html;
00154 }
00155
00156 public function getHtml()
00157 {
00158 return $this->toHtml();
00159 }
00160
00161 }