Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/view/form/control/AphrontFormSelectControl.php
12262 views
1
<?php
2
3
final class AphrontFormSelectControl extends AphrontFormControl {
4
5
protected function getCustomControlClass() {
6
return 'aphront-form-control-select';
7
}
8
9
private $options;
10
private $disabledOptions = array();
11
12
public function setOptions(array $options) {
13
$this->options = $options;
14
return $this;
15
}
16
17
public function getOptions() {
18
return $this->options;
19
}
20
21
public function setDisabledOptions(array $disabled) {
22
$this->disabledOptions = $disabled;
23
return $this;
24
}
25
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(),
34
),
35
$this->disabledOptions);
36
}
37
38
public static function renderSelectTag(
39
$selected,
40
array $options,
41
array $attrs = array(),
42
array $disabled = array()) {
43
44
$option_tags = self::renderOptions($selected, $options, $disabled);
45
46
return javelin_tag(
47
'select',
48
$attrs,
49
$option_tags);
50
}
51
52
private static function renderOptions(
53
$selected,
54
array $options,
55
array $disabled = array()) {
56
$disabled = array_fuse($disabled);
57
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,
66
),
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;
79
}
80
81
$tags[] = phutil_tag(
82
'option',
83
array(
84
'selected' => $is_selected,
85
'value' => $value,
86
'disabled' => isset($disabled[$value]) ? 'disabled' : null,
87
),
88
$thing);
89
}
90
}
91
return $tags;
92
}
93
94
}
95
96