Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / view / form / control / AphrontFormSelectControl.php
blob8799fcfd913302d0a2a726f1b8bd1beb3a70a1ad
1 <?php
3 final class AphrontFormSelectControl extends AphrontFormControl {
5 protected function getCustomControlClass() {
6 return 'aphront-form-control-select';
9 private $options;
10 private $disabledOptions = array();
12 public function setOptions(array $options) {
13 $this->options = $options;
14 return $this;
17 public function getOptions() {
18 return $this->options;
21 public function setDisabledOptions(array $disabled) {
22 $this->disabledOptions = $disabled;
23 return $this;
26 protected function renderInput() {
27 return self::renderSelectTag(
28 $this->getValue(),
29 $this->getOptions(),
30 array(
31 'name' => $this->getName(),
32 'disabled' => $this->getDisabled() ? 'disabled' : null,
33 'id' => $this->getID(),
35 $this->disabledOptions);
38 public static function renderSelectTag(
39 $selected,
40 array $options,
41 array $attrs = array(),
42 array $disabled = array()) {
44 $option_tags = self::renderOptions($selected, $options, $disabled);
46 return javelin_tag(
47 'select',
48 $attrs,
49 $option_tags);
52 private static function renderOptions(
53 $selected,
54 array $options,
55 array $disabled = array()) {
56 $disabled = array_fuse($disabled);
58 $tags = array();
59 $already_selected = false;
60 foreach ($options as $value => $thing) {
61 if (is_array($thing)) {
62 $tags[] = phutil_tag(
63 'optgroup',
64 array(
65 'label' => $value,
67 self::renderOptions($selected, $thing));
68 } else {
69 // When there are a list of options including similar values like
70 // "0" and "" (the empty string), only select the first matching
71 // value. Ideally this should be more precise about matching, but we
72 // have 2,000 of these controls at this point so hold that for a
73 // broader rewrite.
74 if (!$already_selected && ($value == $selected)) {
75 $is_selected = 'selected';
76 $already_selected = true;
77 } else {
78 $is_selected = null;
81 $tags[] = phutil_tag(
82 'option',
83 array(
84 'selected' => $is_selected,
85 'value' => $value,
86 'disabled' => isset($disabled[$value]) ? 'disabled' : null,
88 $thing);
91 return $tags;