[TASK] Use strict comparison for strings
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Form / Element / SelectCheckBoxElement.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\Utility\FormEngineUtility;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\Imaging\Icon;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\StringUtility;
22
23 /**
24 * Creates a widget with check box elements.
25 *
26 * This is rendered for config type=select, renderType=selectCheckBox
27 */
28 class SelectCheckBoxElement extends AbstractFormElement
29 {
30 /**
31 * Default field wizards enabled for this element.
32 *
33 * @var array
34 */
35 protected $defaultFieldWizard = [
36 'otherLanguageContent' => [
37 'renderType' => 'otherLanguageContent',
38 ],
39 'defaultLanguageDifferences' => [
40 'renderType' => 'defaultLanguageDifferences',
41 'after' => [
42 'otherLanguageContent',
43 ],
44 ],
45 ];
46
47 /**
48 * Render check boxes
49 *
50 * @return array As defined in initializeResultArray() of AbstractNode
51 */
52 public function render()
53 {
54 $resultArray = $this->initializeResultArray();
55
56 $html = [];
57 // Field configuration from TCA:
58 $parameterArray = $this->data['parameterArray'];
59 $config = $parameterArray['fieldConf']['config'];
60 $disabled = !empty($config['readOnly']);
61
62 $selItems = $config['items'];
63 if (!empty($selItems)) {
64 // Get values in an array (and make unique, which is fine because there can be no duplicates anyway):
65 $itemArray = array_flip($parameterArray['itemFormElValue']);
66
67 // Traverse the Array of selector box items:
68 $groups = [];
69 $currentGroup = 0;
70 $c = 0;
71 $sOnChange = '';
72 if (!$disabled) {
73 $sOnChange = implode('', $parameterArray['fieldChangeFunc']);
74 // Used to accumulate the JS needed to restore the original selection.
75 foreach ($selItems as $p) {
76 // Non-selectable element:
77 if ($p[1] === '--div--') {
78 $selIcon = '';
79 if (isset($p[2]) && $p[2] !== 'empty-empty') {
80 $selIcon = FormEngineUtility::getIconHtml($p[2]);
81 }
82 $currentGroup++;
83 $groups[$currentGroup]['header'] = [
84 'icon' => $selIcon,
85 'title' => $p[0]
86 ];
87 } else {
88 // Check if some help text is available
89 // Since TYPO3 4.5 help text is expected to be an associative array
90 // with two key, "title" and "description"
91 // For the sake of backwards compatibility, we test if the help text
92 // is a string and use it as a description (this could happen if items
93 // are modified with an itemProcFunc)
94 $hasHelp = false;
95 $help = '';
96 $helpArray = [];
97 if (!empty($p[3])) {
98 $hasHelp = true;
99 if (is_array($p[3])) {
100 $helpArray = $p[3];
101 } else {
102 $helpArray['description'] = $p[3];
103 }
104 }
105 if ($hasHelp) {
106 $help = BackendUtility::wrapInHelp('', '', '', $helpArray);
107 }
108
109 // Selected or not by default:
110 $checked = 0;
111 if (isset($itemArray[$p[1]])) {
112 $checked = 1;
113 unset($itemArray[$p[1]]);
114 }
115
116 // Build item array
117 $groups[$currentGroup]['items'][] = [
118 'id' => StringUtility::getUniqueId('select_checkbox_row_'),
119 'name' => $parameterArray['itemFormElName'] . '[' . $c . ']',
120 'value' => $p[1],
121 'checked' => $checked,
122 'disabled' => false,
123 'class' => '',
124 'icon' => FormEngineUtility::getIconHtml(!empty($p[2]) ? $p[2] : 'empty-empty'),
125 'title' => $p[0],
126 'help' => $help
127 ];
128 $c++;
129 }
130 }
131 }
132
133 $legacyWizards = $this->renderWizards();
134 $legacyFieldWizardHtml = implode(LF, $legacyWizards['fieldWizard']);
135
136 $fieldInformationResult = $this->renderFieldInformation();
137 $fieldInformationHtml = $fieldInformationResult['html'];
138 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
139
140 $fieldWizardResult = $this->renderFieldWizard();
141 $fieldWizardHtml = $legacyFieldWizardHtml . $fieldWizardResult['html'];
142 $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
143
144 $html[] = '<div class="t3js-formengine-field-item">';
145 if (!$disabled) {
146 $html[] = $fieldInformationHtml;
147 }
148 $html[] = '<div class="form-wizards-wrap">';
149 $html[] = '<div class="form-wizards-element">';
150
151 // Add an empty hidden field which will send a blank value if all items are unselected.
152 $html[] = '<input type="hidden" class="select-checkbox" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
153
154 // Building the checkboxes
155 foreach ($groups as $groupKey => $group) {
156 $groupId = htmlspecialchars($parameterArray['itemFormElID']) . '-group-' . $groupKey;
157 $html[] = '<div class="panel panel-default">';
158 if (is_array($group['header'])) {
159 $html[] = '<div class="panel-heading">';
160 $html[] = '<a data-toggle="collapse" href="#' . $groupId . '" aria-expanded="false" aria-controls="' . $groupId . '">';
161 $html[] = $group['header']['icon'];
162 $html[] = htmlspecialchars($group['header']['title']);
163 $html[] = '</a>';
164 $html[] = '</div>';
165 }
166 if (is_array($group['items']) && !empty($group['items'])) {
167 $tableRows = [];
168 $resetGroup = [];
169
170 // Render rows
171 foreach ($group['items'] as $item) {
172 $tableRows[] = '<tr class="' . $item['class'] . '">';
173 $tableRows[] = '<td class="col-checkbox">';
174 $tableRows[] = '<input type="checkbox" class="t3js-checkbox" '
175 . 'id="' . $item['id'] . '" '
176 . 'name="' . htmlspecialchars($item['name']) . '" '
177 . 'value="' . htmlspecialchars($item['value']) . '" '
178 . 'onclick="' . htmlspecialchars($sOnChange) . '" '
179 . ($item['checked'] ? 'checked=checked ' : '')
180 . ($item['disabled'] ? 'disabled=disabled ' : '') . '>';
181 $tableRows[] = '</td>';
182 $tableRows[] = '<td class="col-icon">';
183 $tableRows[] = '<label class="label-block" for="' . $item['id'] . '">' . $item['icon'] . '</label>';
184 $tableRows[] = '</td>';
185 $tableRows[] = '<td class="col-title">';
186 $tableRows[] = '<label class="label-block" for="' . $item['id'] . '">' . htmlspecialchars($item['title'], ENT_COMPAT, 'UTF-8', false) . '</label>';
187 $tableRows[] = '</td>';
188 $tableRows[] = '<td class="text-right">' . $item['help'] . '</td>';
189 $tableRows[] = '</tr>';
190 $resetGroup[] = 'document.editform[' . GeneralUtility::quoteJSvalue($item['name']) . '].checked=' . $item['checked'] . ';';
191 }
192
193 // Build reset group button
194 $resetGroupBtn = '';
195 if (!empty($resetGroup)) {
196 $resetGroup[] = 'TYPO3.FormEngine.updateCheckboxState(this);';
197 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.revertSelection'));
198 $resetGroupBtn = '<a href="#" '
199 . 'class="btn btn-default btn-sm" '
200 . 'onclick="' . implode('', $resetGroup) . ' return false;" '
201 . 'title="' . $title . '">'
202 . $this->iconFactory->getIcon('actions-edit-undo', Icon::SIZE_SMALL)->render() . ' '
203 . $this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.revertSelection') . '</a>';
204 }
205
206 if (is_array($group['header'])) {
207 $html[] = '<div id="' . $groupId . '" class="panel-collapse collapse" role="tabpanel">';
208 }
209 $checkboxId = uniqid($groupId);
210 $title = htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:labels.toggleall'));
211 $html[] = '<div class="table-responsive">';
212 $html[] = '<table class="table table-transparent table-hover">';
213 $html[] = '<thead>';
214 $html[] = '<tr>';
215 $html[] = '<th class="col-checkbox">';
216 $html[] = '<input type="checkbox" id="' . $checkboxId . '" class="t3js-toggle-checkboxes" data-trigger="hover" data-placement="right" data-title="' . $title . '" data-toggle="tooltip" />';
217 $html[] = '</th>';
218 $html[] = '<th class="col-title" colspan="2"><label for="' . $checkboxId . '">' . $title . '</label></th>';
219 $html[] = '<th class="text-right">' . $resetGroupBtn . '</th>';
220 $html[] = '</tr>';
221 $html[] = '</thead>';
222 $html[] = '<tbody>' . implode(LF, $tableRows) . '</tbody>';
223 $html[] = '</table>';
224 $html[] = '</div>';
225 if (is_array($group['header'])) {
226 $html[] = '</div>';
227 }
228 }
229 $html[] = '</div>';
230 }
231
232 $html[] = '</div>';
233 if (!$disabled) {
234 $html[] = '<div class="form-wizards-items-bottom">';
235 $html[] = $fieldWizardHtml;
236 $html[] = '</div>';
237 }
238 $html[] = '</div>';
239 $html[] = '</div>';
240 }
241
242 $resultArray['html'] = implode(LF, $html);
243 $resultArray['requireJsModules'][] = 'TYPO3/CMS/Backend/Tooltip';
244 return $resultArray;
245 }
246 }