Changeset View
Changeset View
Standalone View
Standalone View
src/view/form/control/AphrontFormSelectControl.php
<?php | <?php | ||||
final class AphrontFormSelectControl extends AphrontFormControl { | final class AphrontFormSelectControl extends AphrontFormControl { | ||||
protected function getCustomControlClass() { | protected function getCustomControlClass() { | ||||
return 'aphront-form-control-select'; | return 'aphront-form-control-select'; | ||||
} | } | ||||
private $options; | private $options; | ||||
private $disabledOptions = array(); | |||||
public function setOptions(array $options) { | public function setOptions(array $options) { | ||||
$this->options = $options; | $this->options = $options; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getOptions() { | public function getOptions() { | ||||
return $this->options; | return $this->options; | ||||
} | } | ||||
public function setDisabledOptions(array $disabled) { | |||||
$this->disabledOptions = $disabled; | |||||
return $this; | |||||
} | |||||
protected function renderInput() { | protected function renderInput() { | ||||
return self::renderSelectTag( | return self::renderSelectTag( | ||||
$this->getValue(), | $this->getValue(), | ||||
$this->getOptions(), | $this->getOptions(), | ||||
array( | array( | ||||
'name' => $this->getName(), | 'name' => $this->getName(), | ||||
'disabled' => $this->getDisabled() ? 'disabled' : null, | 'disabled' => $this->getDisabled() ? 'disabled' : null, | ||||
'id' => $this->getID(), | 'id' => $this->getID(), | ||||
)); | ), | ||||
$this->disabledOptions); | |||||
} | } | ||||
public static function renderSelectTag( | public static function renderSelectTag( | ||||
$selected, | $selected, | ||||
array $options, | array $options, | ||||
array $attrs = array()) { | array $attrs = array(), | ||||
array $disabled = array()) { | |||||
$option_tags = self::renderOptions($selected, $options); | $option_tags = self::renderOptions($selected, $options, $disabled); | ||||
return javelin_tag( | return javelin_tag( | ||||
'select', | 'select', | ||||
$attrs, | $attrs, | ||||
$option_tags); | $option_tags); | ||||
} | } | ||||
private static function renderOptions($selected, array $options) { | private static function renderOptions( | ||||
$selected, | |||||
array $options, | |||||
array $disabled = array()) { | |||||
$disabled = array_fuse($disabled); | |||||
$tags = array(); | $tags = array(); | ||||
foreach ($options as $value => $thing) { | foreach ($options as $value => $thing) { | ||||
if (is_array($thing)) { | if (is_array($thing)) { | ||||
$tags[] = phutil_tag( | $tags[] = phutil_tag( | ||||
'optgroup', | 'optgroup', | ||||
array( | array( | ||||
'label' => $value, | 'label' => $value, | ||||
), | ), | ||||
self::renderOptions($selected, $thing)); | self::renderOptions($selected, $thing)); | ||||
} else { | } else { | ||||
$tags[] = phutil_tag( | $tags[] = phutil_tag( | ||||
'option', | 'option', | ||||
array( | array( | ||||
'selected' => ($value == $selected) ? 'selected' : null, | 'selected' => ($value == $selected) ? 'selected' : null, | ||||
'value' => $value, | 'value' => $value, | ||||
'disabled' => isset($disabled[$value]) ? 'disabled' : null, | |||||
), | ), | ||||
$thing); | $thing); | ||||
} | } | ||||
} | } | ||||
return $tags; | return $tags; | ||||
} | } | ||||
} | } |