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_Multiselect 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('multiple');
00041 $this->setSize(10);
00042 }
00043
00044 public function getName()
00045 {
00046 $name = parent::getName();
00047 if (strpos($name, '[]') === false) {
00048 $name.= '[]';
00049 }
00050 return $name;
00051 }
00052
00053 public function getElementHtml()
00054 {
00055 $this->addClass('select multiselect');
00056 $html = '';
00057 if ($this->getCanBeEmpty()) {
00058 $html .= '<input type="hidden" name="' . parent::getName() . '" value="" />';
00059 }
00060 $html .= '<select id="'.$this->getHtmlId().'" name="'.$this->getName().'" '.$this->serialize($this->getHtmlAttributes()).' multiple="multiple">'."\n";
00061
00062 $value = $this->getValue();
00063 if (!is_array($value)) {
00064 $value = explode(',', $value);
00065 }
00066
00067 if ($values = $this->getValues()) {
00068 foreach ($values as $option) {
00069 if (is_array($option['value'])) {
00070 $html.='<optgroup label="'.$option['label'].'">'."\n";
00071 foreach ($option['value'] as $groupItem) {
00072 $html.= $this->_optionToHtml($groupItem, $value);
00073 }
00074 $html.='</optgroup>'."\n";
00075 }
00076 else {
00077 $html.= $this->_optionToHtml($option, $value);
00078 }
00079 }
00080 }
00081
00082 $html.= '</select>'."\n";
00083 $html.= $this->getAfterElementHtml();
00084 return $html;
00085 }
00086
00087 public function getHtmlAttributes()
00088 {
00089 return array('title', 'class', 'style', 'onclick', 'onchange', 'disabled', 'size');
00090 }
00091
00092 public function getDefaultHtml()
00093 {
00094 $result = ( $this->getNoSpan() === true ) ? '' : '<span class="field-row">'."\n";
00095 $result.= $this->getLabelHtml();
00096 $result.= $this->getElementHtml();
00097
00098
00099 if($this->getSelectAll() && $this->getDeselectAll()) {
00100 $result.= '<a href="#" onclick="return ' . $this->getJsObjectName() . '.selectAll()">' . $this->getSelectAll() . '</a> <span class="separator"> | </span>';
00101 $result.= '<a href="#" onclick="return ' . $this->getJsObjectName() . '.deselectAll()">' . $this->getDeselectAll() . '</a>';
00102 }
00103
00104 $result.= ( $this->getNoSpan() === true ) ? '' : '</span>'."\n";
00105
00106
00107 $result.= '<script type="text/javascript">' . "\n";
00108 $result.= ' var ' . $this->getJsObjectName() . ' = {' . "\n";
00109 $result.= ' selectAll: function() { ' . "\n";
00110 $result.= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
00111 $result.= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
00112 $result.= ' sel.options[i].selected = true; ' . "\n";
00113 $result.= ' } ' . "\n";
00114 $result.= ' return false; ' . "\n";
00115 $result.= ' },' . "\n";
00116 $result.= ' deselectAll: function() {' . "\n";
00117 $result.= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
00118 $result.= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
00119 $result.= ' sel.options[i].selected = false; ' . "\n";
00120 $result.= ' } ' . "\n";
00121 $result.= ' return false; ' . "\n";
00122 $result.= ' }' . "\n";
00123 $result.= ' }' . "\n";
00124 $result.= "\n</script>";
00125
00126 return $result;
00127 }
00128
00129 public function getJsObjectName() {
00130 return $this->getHtmlId() . 'ElementControl';
00131 }
00132
00133 protected function _optionToHtml($option, $selected)
00134 {
00135 $html = '<option value="'.$this->_escape($option['value']).'"';
00136 $html.= isset($option['title']) ? 'title="'.$option['title'].'"' : '';
00137 $html.= isset($option['style']) ? 'style="'.$option['style'].'"' : '';
00138 if (in_array($option['value'], $selected)) {
00139 $html.= ' selected="selected"';
00140 }
00141 $html.= '>'.$option['label']. '</option>'."\n";
00142 return $html;
00143 }
00144 }