Path: blob/master/src/view/form/control/AphrontFormSelectControl.php
12262 views
<?php12final class AphrontFormSelectControl extends AphrontFormControl {34protected function getCustomControlClass() {5return 'aphront-form-control-select';6}78private $options;9private $disabledOptions = array();1011public function setOptions(array $options) {12$this->options = $options;13return $this;14}1516public function getOptions() {17return $this->options;18}1920public function setDisabledOptions(array $disabled) {21$this->disabledOptions = $disabled;22return $this;23}2425protected function renderInput() {26return self::renderSelectTag(27$this->getValue(),28$this->getOptions(),29array(30'name' => $this->getName(),31'disabled' => $this->getDisabled() ? 'disabled' : null,32'id' => $this->getID(),33),34$this->disabledOptions);35}3637public static function renderSelectTag(38$selected,39array $options,40array $attrs = array(),41array $disabled = array()) {4243$option_tags = self::renderOptions($selected, $options, $disabled);4445return javelin_tag(46'select',47$attrs,48$option_tags);49}5051private static function renderOptions(52$selected,53array $options,54array $disabled = array()) {55$disabled = array_fuse($disabled);5657$tags = array();58$already_selected = false;59foreach ($options as $value => $thing) {60if (is_array($thing)) {61$tags[] = phutil_tag(62'optgroup',63array(64'label' => $value,65),66self::renderOptions($selected, $thing));67} else {68// When there are a list of options including similar values like69// "0" and "" (the empty string), only select the first matching70// value. Ideally this should be more precise about matching, but we71// have 2,000 of these controls at this point so hold that for a72// broader rewrite.73if (!$already_selected && ($value == $selected)) {74$is_selected = 'selected';75$already_selected = true;76} else {77$is_selected = null;78}7980$tags[] = phutil_tag(81'option',82array(83'selected' => $is_selected,84'value' => $value,85'disabled' => isset($disabled[$value]) ? 'disabled' : null,86),87$thing);88}89}90return $tags;91}9293}949596