b0468a048ba800f6d459ec27fe7c36eefdbdce0f
[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 use TYPO3\CMS\Backend\Form\DataPreprocessor;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Backend\Form\Utility\FormEngineUtility;
20
21 /**
22 * Generation of TCEform elements of the type "check"
23 */
24 class CheckboxElement extends AbstractFormElement {
25
26 /**
27 * This will render a checkbox or an array of checkboxes
28 *
29 * @return array As defined in initializeResultArray() of AbstractNode
30 */
31 public function render() {
32 $config = $this->globalOptions['parameterArray']['fieldConf']['config'];
33 $html = '';
34 $disabled = FALSE;
35 if ($this->isGlobalReadonly() || $config['readOnly']) {
36 $disabled = TRUE;
37 }
38 // Traversing the array of items
39 $items = FormEngineUtility::initItemArray($this->globalOptions['parameterArray']['fieldConf']);
40 if ($config['itemsProcFunc']) {
41 $dataPreprocessor = GeneralUtility::makeInstance(DataPreprocessor::class);
42 $items = $dataPreprocessor->procItems(
43 $items,
44 $this->globalOptions['parameterArray']['fieldTSConfig']['itemsProcFunc.'],
45 $config,
46 $this->globalOptions['table'],
47 $this->globalOptions['databaseRow'],
48 $this->globalOptions['fieldName']
49 );
50 }
51
52 $numberOfItems = count($items);
53 if ($numberOfItems === 0) {
54 $items[] = array('', '');
55 $numberOfItems = 1;
56 }
57 $formElementValue = (int)$this->globalOptions['parameterArray']['itemFormElValue'];
58 $cols = (int)$config['cols'];
59 if ($cols > 1) {
60 $colWidth = (int)floor(12 / $cols);
61 $colClass = "col-md-12";
62 $colClear = array();
63 if ($colWidth == 6){
64 $colClass = "col-sm-6";
65 $colClear = array(
66 2 => 'visible-sm-block visible-md-block visible-lg-block',
67 );
68 } elseif ($colWidth === 4) {
69 $colClass = "col-sm-4";
70 $colClear = array(
71 3 => 'visible-sm-block visible-md-block visible-lg-block',
72 );
73 } elseif ($colWidth === 3) {
74 $colClass = "col-sm-6 col-md-3";
75 $colClear = array(
76 2 => 'visible-sm-block',
77 4 => 'visible-md-block visible-lg-block',
78 );
79 } elseif ($colWidth <= 2) {
80 $colClass = "checkbox-column col-sm-6 col-md-3 col-lg-2";
81 $colClear = array(
82 2 => 'visible-sm-block',
83 4 => 'visible-md-block',
84 6 => 'visible-lg-block'
85 );
86 }
87 $html .= '<div class="checkbox-row row">';
88 for ($counter = 0; $counter < $numberOfItems; $counter++) {
89 // use "default" for typical single checkboxes
90 $tsConfigKey = ($numberOfItems === 1 ? 'default' : $items[$counter][1]);
91 // useful for e.g. pages.l18n_cfg, where there is no value set
92 if ($tsConfigKey === '') {
93 $tsConfigKey = $counter;
94 }
95 if (isset($this->globalOptions['parameterArray']['fieldTSConfig']['altLabels.'][$tsConfigKey])) {
96 $label = $this->getLanguageService()->sL($this->globalOptions['parameterArray']['fieldTSConfig']['altLabels.'][$tsConfigKey]);
97 } else {
98 $label = $items[$counter][0];
99 }
100 $html .=
101 '<div class="checkbox-column ' . $colClass . '">'
102 . $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->globalOptions['parameterArray'], $disabled) .
103 '</div>';
104 if ($counter + 1 < $numberOfItems && !empty($colClear)) {
105 foreach ($colClear as $rowBreakAfter => $clearClass) {
106 if (($counter + 1) % $rowBreakAfter === 0) {
107 $html .= '<div class="clearfix '. $clearClass . '"></div>';
108 }
109 }
110 }
111 }
112 $html .= '</div>';
113 } else {
114 for ($counter = 0; $counter < $numberOfItems; $counter++) {
115 // use "default" for typical single checkboxes
116 $tsConfigKey = ($numberOfItems === 1 ? 'default' : $items[$counter][1]);
117 // useful for e.g. pages.l18n_cfg, where there is no value set
118 if ($tsConfigKey === '') {
119 $tsConfigKey = $counter;
120 }
121 if (isset($this->globalOptions['parameterArray']['fieldTSConfig']['altLabels.'][$tsConfigKey])) {
122 $label = $this->getLanguageService()->sL($this->globalOptions['parameterArray']['fieldTSConfig']['altLabels.'][$tsConfigKey]);
123 } else {
124 $label = $items[$counter][0];
125 }
126 $html .= $this->renderSingleCheckboxElement($label, $counter, $formElementValue, $numberOfItems, $this->globalOptions['parameterArray'], $disabled);
127 }
128 }
129 if (!$disabled) {
130 $html .= '<input type="hidden" name="' . $this->globalOptions['parameterArray']['itemFormElName'] . '" value="' . htmlspecialchars($formElementValue) . '" />';
131 }
132 $resultArray = $this->initializeResultArray();
133 $resultArray['html'] = $html;
134 return $resultArray;
135 }
136
137 /**
138 * This functions builds the HTML output for the checkbox
139 *
140 * @param string $label Label of this item
141 * @param int $itemCounter Number of this element in the list of all elements
142 * @param int $formElementValue Value of this element
143 * @param int $numberOfItems Full number of items
144 * @param array $additionalInformation Information with additional configuration options.
145 * @param bool $disabled TRUE if form element is disabled
146 * @return string Single element HTML
147 */
148 protected function renderSingleCheckboxElement($label, $itemCounter, $formElementValue, $numberOfItems, $additionalInformation, $disabled) {
149 $config = $additionalInformation['fieldConf']['config'];
150 $inline = !empty($config['cols']) && $config['cols'] === "inline";
151 $checkboxParameters = $this->checkBoxParams(
152 $additionalInformation['itemFormElName'],
153 $formElementValue,
154 $itemCounter,
155 $numberOfItems,
156 implode('', $additionalInformation['fieldChangeFunc'])
157 );
158 $checkboxName = $additionalInformation['itemFormElName'] . '_' . $itemCounter;
159 $checkboxId = $additionalInformation['itemFormElID'] . '_' . $itemCounter;
160 return '
161 <div class="checkbox' . ($inline ? ' checkbox-inline' : '') . (!$disabled ? '' : ' disabled') . '">
162 <label>
163 <input type="checkbox"
164 value="1"
165 name="' . $checkboxName . '"
166 ' . $checkboxParameters . '
167 ' . $additionalInformation['onFocus'] . '
168 ' . (!$disabled ?: ' disabled="disabled"') . '
169 id="' . $checkboxId . '" />
170 ' . ($label ? htmlspecialchars($label) : '&nbsp;') . '
171 </label>
172 </div>';
173 }
174
175 /**
176 * Creates checkbox parameters
177 *
178 * @param string $itemName Form element name
179 * @param int $formElementValue The value of the checkbox (representing checkboxes with the bits)
180 * @param int $checkbox Checkbox # (0-9?)
181 * @param int $checkboxesCount Total number of checkboxes in the array.
182 * @param string $additionalJavaScript Additional JavaScript for the onclick handler.
183 * @return string The onclick attribute + possibly the checked-option set.
184 */
185 protected function checkBoxParams($itemName, $formElementValue, $checkbox, $checkboxesCount, $additionalJavaScript = '') {
186 $elementName = 'document.editform[' . Generalutility::quoteJSvalue($itemName) . ']';
187 $checkboxPow = pow(2, $checkbox);
188 $onClick = $elementName . '.value=this.checked?(' . $elementName . '.value|' . $checkboxPow . '):('
189 . $elementName . '.value&' . (pow(2, $checkboxesCount) - 1 - $checkboxPow) . ');' . $additionalJavaScript;
190 return ' onclick="' . htmlspecialchars($onClick) . '"' . ($formElementValue & $checkboxPow ? ' checked="checked"' : '');
191 }
192
193 }