Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/view/form/control/AphrontFormCheckboxControl.php
12256 views
1
<?php
2
3
final class AphrontFormCheckboxControl extends AphrontFormControl {
4
5
private $boxes = array();
6
private $checkboxKey;
7
8
public function setCheckboxKey($checkbox_key) {
9
$this->checkboxKey = $checkbox_key;
10
return $this;
11
}
12
13
public function getCheckboxKey() {
14
return $this->checkboxKey;
15
}
16
17
public function addCheckbox(
18
$name,
19
$value,
20
$label,
21
$checked = false,
22
$id = null) {
23
$this->boxes[] = array(
24
'name' => $name,
25
'value' => $value,
26
'label' => $label,
27
'checked' => $checked,
28
'id' => $id,
29
);
30
return $this;
31
}
32
33
protected function getCustomControlClass() {
34
return 'aphront-form-control-checkbox';
35
}
36
37
public function setOptions(array $options) {
38
$boxes = array();
39
foreach ($options as $key => $value) {
40
$boxes[] = array(
41
'value' => $key,
42
'label' => $value,
43
);
44
}
45
46
$this->boxes = $boxes;
47
48
return $this;
49
}
50
51
protected function renderInput() {
52
$rows = array();
53
foreach ($this->boxes as $box) {
54
$id = idx($box, 'id');
55
if ($id === null) {
56
$id = celerity_generate_unique_node_id();
57
}
58
59
$name = idx($box, 'name');
60
if ($name === null) {
61
$name = $this->getName().'[]';
62
}
63
64
$value = $box['value'];
65
66
if (array_key_exists('checked', $box)) {
67
$checked = $box['checked'];
68
} else {
69
$checked = in_array($value, $this->getValue());
70
}
71
72
$checkbox = phutil_tag(
73
'input',
74
array(
75
'id' => $id,
76
'type' => 'checkbox',
77
'name' => $name,
78
'value' => $box['value'],
79
'checked' => $checked ? 'checked' : null,
80
'disabled' => $this->getDisabled() ? 'disabled' : null,
81
));
82
$label = phutil_tag(
83
'label',
84
array(
85
'for' => $id,
86
),
87
$box['label']);
88
$rows[] = phutil_tag('tr', array(), array(
89
phutil_tag('td', array(), $checkbox),
90
phutil_tag('th', array(), $label),
91
));
92
}
93
94
// When a user submits a form with a checkbox unchecked, the browser
95
// doesn't submit anything to the server. This hidden key lets the server
96
// know that the checkboxes were present on the client, the user just did
97
// not select any of them.
98
99
$checkbox_key = $this->getCheckboxKey();
100
if ($checkbox_key) {
101
$rows[] = phutil_tag(
102
'input',
103
array(
104
'type' => 'hidden',
105
'name' => $checkbox_key,
106
'value' => 1,
107
));
108
}
109
110
return phutil_tag(
111
'table',
112
array('class' => 'aphront-form-control-checkbox-layout'),
113
$rows);
114
}
115
116
}
117
118