[TASK] Followup: Migrate FormEngine Checkboxes HTML to twbs
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / CheckboxElement.php
1 <?php
2 namespace TYPO3\CMS\Backend\Form\Element;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Generation of TCEform elements of the type "check"
19 */
20 class CheckboxElement extends AbstractFormElement {
21
22 /**
23 * This will render a checkbox or an array of checkboxes
24 *
25 * @param string $table The table name of the record
26 * @param string $field The field name which this element is supposed to edit
27 * @param array $row The record data array where the value(s) for the field can be found
28 * @param array $additionalInformation An array with additional configuration options.
29 * @return string The HTML code for the TCEform field
30 */
31 public function render($table, $field, $row, &$additionalInformation) {
32 $config = $additionalInformation['fieldConf']['config'];
33 $item = '';
34 $disabled = FALSE;
35 if ($this->formEngine->renderReadonly || $config['readOnly']) {
36 $disabled = TRUE;
37 }
38 // Traversing the array of items
39 $items = $this->formEngine->initItemArray($additionalInformation['fieldConf']);
40 if ($config['itemsProcFunc']) {
41 $items = $this->formEngine->procItems(
42 $items,
43 $additionalInformation['fieldTSConfig']['itemsProcFunc.'],
44 $config,
45 $table,
46 $row,
47 $field
48 );
49 }
50
51 $numberOfItems = count($items);
52 if ($numberOfItems === 0) {
53 $items[] = array('', '');
54 $numberOfItems = 1;
55 }
56 $formElementValue = (int)$additionalInformation['itemFormElValue'];
57 $cols = (int)$config['cols'];
58 if ($cols > 1) {
59 $colWidth = floor(12 / $cols);
60 $colLeftover = 12 - $colWidth * $cols;
61 for ($counter = 0; $counter < $numberOfItems; $counter++) {
62 if (!($counter % $cols)) {
63 $item .= '<div class="row">';
64 }
65 $item .= '<div class="col-md-' . $colWidth . '">'
66 . $this->renderSingleCheckboxElement($items[$counter][0], $counter, $formElementValue, $numberOfItems, $additionalInformation, $disabled)
67 . '</div>';
68 if ($counter % $cols + 1 == $cols) {
69 $item .= ($colLeftover > 0 ? '<div class="col-md-' . $colLeftover . '"></div>' : '') . '</div>';
70 }
71 }
72 if ($counter % $cols) {
73 $rest = $cols - $counter % $cols;
74 for ($counter = 0; $counter < $rest; $counter++) {
75 $item .= '<div class="col-md-' . $colWidth . '></div>';
76 }
77 if ($counter > 0) {
78 $item .= ($colLeftover > 0 ? '<div class="col-md-' . $colLeftover . '"></div>' : '') . '</div>';
79 }
80 }
81 } else {
82 for ($counter = 0; $counter < $numberOfItems; $counter++) {
83 $item .= $this->renderSingleCheckboxElement($items[$counter][0], $counter, $formElementValue, $numberOfItems, $additionalInformation, $disabled);
84 }
85 }
86 if (!$disabled) {
87 $item .= '<input type="hidden" name="' . $additionalInformation['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
88 }
89 return $item;
90 }
91
92 /**
93 * This functions builds the HTML output for the checkbox
94 *
95 * @param string $label Label of this item
96 * @param integer $itemCounter Number of this element in the list of all elements
97 * @param integer $formElementValue Value of this element
98 * @param integer $numberOfItems Full number of items
99 * @param array $additionalInformation Information with additional configuration options.
100 * @param boolean $disabled TRUE if form element is disabled
101 * @return string Single element HTML
102 */
103 protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled) {
104 $checkboxParameters = $this->checkBoxParams(
105 $additionalInformation['itemFormElName'],
106 $formElementValue,
107 $itemCounter,
108 $numberOfItems,
109 implode('', $additionalInformation['fieldChangeFunc'])
110 );
111 $checkboxName = $additionalInformation['itemFormElName'] . '_' . $itemCounter;
112 $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter;
113 return '<div class="checkbox' . (!$disabled ?: ' disabled') . '">'
114 . '<label for="' . $checkboxId . '">'
115 . '<input '
116 . 'type="checkbox" '
117 . 'value="1" '
118 . 'class="' . $this->cssClassTypeElementPrefix . 'check" '
119 . 'name="' . $checkboxName . '" '
120 . $checkboxParameters . ' '
121 . $additionalInformation['onFocus'] . ' '
122 . (!$disabled ?: ' disabled="disabled"')
123 . ' id="' . $checkboxId . '" '
124 . ' />'
125 . htmlspecialchars($label)
126 . '</label>'
127 . '</div>';
128 }
129
130 /**
131 * Creates checkbox parameters
132 *
133 * @param string $itemName Form element name
134 * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits)
135 * @param int $checkbox Checkbox # (0-9?)
136 * @param int $checkboxesCount Total number of checkboxes in the array.
137 * @param string $additionalJavaScript Additional JavaScript for the onclick handler.
138 * @return string The onclick attribute + possibly the checked-option set.
139 */
140 protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '') {
141 $elementName = $this->formEngine->elName($itemName);
142 $checkboxPow = pow(2, $checkbox);
143 $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
144 . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
145 return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
146 }
147 }