Path: blob/master/src/view/form/control/AphrontFormCheckboxControl.php
12256 views
<?php12final class AphrontFormCheckboxControl extends AphrontFormControl {34private $boxes = array();5private $checkboxKey;67public function setCheckboxKey($checkbox_key) {8$this->checkboxKey = $checkbox_key;9return $this;10}1112public function getCheckboxKey() {13return $this->checkboxKey;14}1516public function addCheckbox(17$name,18$value,19$label,20$checked = false,21$id = null) {22$this->boxes[] = array(23'name' => $name,24'value' => $value,25'label' => $label,26'checked' => $checked,27'id' => $id,28);29return $this;30}3132protected function getCustomControlClass() {33return 'aphront-form-control-checkbox';34}3536public function setOptions(array $options) {37$boxes = array();38foreach ($options as $key => $value) {39$boxes[] = array(40'value' => $key,41'label' => $value,42);43}4445$this->boxes = $boxes;4647return $this;48}4950protected function renderInput() {51$rows = array();52foreach ($this->boxes as $box) {53$id = idx($box, 'id');54if ($id === null) {55$id = celerity_generate_unique_node_id();56}5758$name = idx($box, 'name');59if ($name === null) {60$name = $this->getName().'[]';61}6263$value = $box['value'];6465if (array_key_exists('checked', $box)) {66$checked = $box['checked'];67} else {68$checked = in_array($value, $this->getValue());69}7071$checkbox = phutil_tag(72'input',73array(74'id' => $id,75'type' => 'checkbox',76'name' => $name,77'value' => $box['value'],78'checked' => $checked ? 'checked' : null,79'disabled' => $this->getDisabled() ? 'disabled' : null,80));81$label = phutil_tag(82'label',83array(84'for' => $id,85),86$box['label']);87$rows[] = phutil_tag('tr', array(), array(88phutil_tag('td', array(), $checkbox),89phutil_tag('th', array(), $label),90));91}9293// When a user submits a form with a checkbox unchecked, the browser94// doesn't submit anything to the server. This hidden key lets the server95// know that the checkboxes were present on the client, the user just did96// not select any of them.9798$checkbox_key = $this->getCheckboxKey();99if ($checkbox_key) {100$rows[] = phutil_tag(101'input',102array(103'type' => 'hidden',104'name' => $checkbox_key,105'value' => 1,106));107}108109return phutil_tag(110'table',111array('class' => 'aphront-form-control-checkbox-layout'),112$rows);113}114115}116117118