_escapeLabel = (bool) $flag; return $this; } public function getEscapeLabel() { return $this->_escapeLabel; } public function formSelect($name, $value = null, $attribs = null, $options = null, $listsep = "
\n") { $info = $this->_getInfo($name, $value, $attribs, $options, $listsep); $name = $id = $value = $attribs = $options = $listsep = $disable = null; extract($info); // name, id, value, attribs, options, listsep, disable // force $value to array so we can compare multiple values to multiple // options; also ensure it's a string for comparison purposes. $value = array_map('strval', (array) $value); // check if element may have multiple values $multiple = ''; if (substr($name, -2) == '[]') { // multiple implied by the name $multiple = ' multiple="multiple"'; } if (isset($attribs['escapeLabel'])) { $this->setEscapeLabel($attribs['escapeLabel']); unset($attribs['escapeLabel']); } if (isset($attribs['multiple'])) { // Attribute set if ($attribs['multiple']) { // True attribute; set multiple attribute $multiple = ' multiple="multiple"'; // Make sure name indicates multiple values are allowed if (!empty($multiple) && (substr($name, -2) != '[]')) { $name .= '[]'; } } else { // False attribute; ensure attribute not set $multiple = ''; } unset($attribs['multiple']); } // now start building the XHTML. $disabled = ''; if (true === $disable) { $disabled = ' disabled="disabled"'; } // Build the surrounding select element first. $xhtml = '_htmlAttribs($attribs) . ">\n "; // build the list of options $list = array(); foreach ((array) $options as $opt_value => $opt_label) { if (is_array($opt_label)) { $opt_disable = ''; if (is_array($disable) && in_array($opt_value, $disable)) { $opt_disable = ' disabled="disabled"'; } $list[] = ''; foreach ($opt_label as $val => $lab) { $list[] = $this->_build($val, $lab, $value, $disable); } $list[] = ''; } else { $list[] = $this->_build($opt_value, $opt_label, $value, $disable); } } // add the options to the xhtml and close the select $xhtml .= implode("\n ", $list) . "\n"; return $xhtml; } protected function _build($value, $label, $selected, $disable, $optionClasses = array()) { $attribs = []; if ($label instanceof SelectOption) { $attribs = $label->getAttribs(); $label = $label->getLabel(); } if (is_bool($disable)) { $disable = array(); } $class = null; if (array_key_exists($value, $optionClasses)) { $class = $optionClasses[$value]; } $attribs['value'] = $value; if ($class) { $attribs['class'] = $class; } if (in_array((string) $value, $selected)) { $attribs['selected'] = 'selected'; } if (in_array($value, $disable)) { $attribs['disabled'] = 'disabled'; } return Html::renderContainer('option', $this->escapeLabel($label), $attribs); } protected function escapeLabel($label) { if ($this->getEscapeLabel()) { return $this->view->escape($label); } return $label; } }