Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / view / form / control / AphrontFormCheckboxControl.php
blobe030b7d17e5859b4986b3037387113207c9cc594
1 <?php
3 final class AphrontFormCheckboxControl extends AphrontFormControl {
5 private $boxes = array();
6 private $checkboxKey;
8 public function setCheckboxKey($checkbox_key) {
9 $this->checkboxKey = $checkbox_key;
10 return $this;
13 public function getCheckboxKey() {
14 return $this->checkboxKey;
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,
30 return $this;
33 protected function getCustomControlClass() {
34 return 'aphront-form-control-checkbox';
37 public function setOptions(array $options) {
38 $boxes = array();
39 foreach ($options as $key => $value) {
40 $boxes[] = array(
41 'value' => $key,
42 'label' => $value,
46 $this->boxes = $boxes;
48 return $this;
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();
59 $name = idx($box, 'name');
60 if ($name === null) {
61 $name = $this->getName().'[]';
64 $value = $box['value'];
66 if (array_key_exists('checked', $box)) {
67 $checked = $box['checked'];
68 } else {
69 $checked = in_array($value, $this->getValue());
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,
87 $box['label']);
88 $rows[] = phutil_tag('tr', array(), array(
89 phutil_tag('td', array(), $checkbox),
90 phutil_tag('th', array(), $label),
91 ));
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.
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,
110 return phutil_tag(
111 'table',
112 array('class' => 'aphront-form-control-checkbox-layout'),
113 $rows);