‪TYPO3CMS  10.4
SelectCheckBoxElement.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
30 {
36  protected ‪$defaultFieldInformation = [
37  'tcaDescription' => [
38  'renderType' => 'tcaDescription',
39  ],
40  ];
41 
47  protected ‪$defaultFieldWizard = [
48  'localizationStateSelector' => [
49  'renderType' => 'localizationStateSelector',
50  ],
51  'otherLanguageContent' => [
52  'renderType' => 'otherLanguageContent',
53  'after' => [
54  'localizationStateSelector'
55  ],
56  ],
57  'defaultLanguageDifferences' => [
58  'renderType' => 'defaultLanguageDifferences',
59  'after' => [
60  'otherLanguageContent',
61  ],
62  ],
63  ];
64 
70  public function ‪render()
71  {
72  $resultArray = $this->‪initializeResultArray();
73 
74  $html = [];
75  // Field configuration from TCA:
76  $parameterArray = $this->data['parameterArray'];
77  $config = $parameterArray['fieldConf']['config'];
78  $disabled = !empty($config['readOnly']);
79 
80  $selItems = $config['items'];
81  if (!empty($selItems)) {
82  // Get values in an array (and make unique, which is fine because there can be no duplicates anyway)
83  // In case e.g. "l10n_display" is set to "defaultAsReadonly" only one value (as string) could be handed in
84  if (is_array($parameterArray['itemFormElValue'])) {
85  $itemArray = $parameterArray['itemFormElValue'];
86  } else {
87  $itemArray = [(string)$parameterArray['itemFormElValue']];
88  }
89  $itemArray = array_flip($itemArray);
90 
91  // Traverse the Array of selector box items:
92  $groups = [];
93  $currentGroup = 0;
94  $c = 0;
95  $sOnChange = '';
96  if (!$disabled) {
97  $sOnChange = implode('', $parameterArray['fieldChangeFunc']);
98  // Used to accumulate the JS needed to restore the original selection.
99  foreach ($selItems as $p) {
100  // Non-selectable element:
101  if ($p[1] === '--div--') {
102  $selIcon = '';
103  if (isset($p[2]) && $p[2] !== 'empty-empty') {
104  $selIcon = ‪FormEngineUtility::getIconHtml($p[2]);
105  }
106  $currentGroup++;
107  $groups[$currentGroup]['header'] = [
108  'icon' => $selIcon,
109  'title' => $p[0]
110  ];
111  } else {
112  // Check if some help text is available
113  // Help text is expected to be an associative array
114  // with two key, "title" and "description"
115  // For the sake of backwards compatibility, we test if the help text
116  // is a string and use it as a description (this could happen if items
117  // are modified with an itemProcFunc)
118  $hasHelp = false;
119  $help = '';
120  $helpArray = [];
121  if (!empty($p[4])) {
122  $hasHelp = true;
123  if (is_array($p[4])) {
124  $helpArray = $p[4];
125  } else {
126  $helpArray['description'] = $p[4];
127  }
128  }
129  if ($hasHelp) {
130  $help = ‪BackendUtility::wrapInHelp('', '', '', $helpArray);
131  }
132 
133  // Selected or not by default:
134  $checked = 0;
135  if (isset($itemArray[$p[1]])) {
136  $checked = 1;
137  unset($itemArray[$p[1]]);
138  }
139 
140  // Build item array
141  $groups[$currentGroup]['items'][] = [
142  'id' => ‪StringUtility::getUniqueId('select_checkbox_row_'),
143  'name' => $parameterArray['itemFormElName'] . '[' . $c . ']',
144  'value' => $p[1],
145  'checked' => $checked,
146  'disabled' => false,
147  'class' => '',
148  'icon' => ‪FormEngineUtility::getIconHtml(!empty($p[2]) ? $p[2] : 'empty-empty'),
149  'title' => $p[0],
150  'help' => $help
151  ];
152  $c++;
153  }
154  }
155  }
156 
157  $fieldInformationResult = $this->‪renderFieldInformation();
158  $fieldInformationHtml = $fieldInformationResult['html'];
159  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);
160 
161  $fieldWizardResult = $this->‪renderFieldWizard();
162  $fieldWizardHtml = $fieldWizardResult['html'];
163  $resultArray = $this->‪mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
164 
165  $html[] = '<div class="formengine-field-item t3js-formengine-field-item" data-formengine-validation-rules="' . htmlspecialchars($this->‪getValidationDataAsJsonString($config)) . '">';
166  $html[] = $fieldInformationHtml;
167  $html[] = '<div class="form-wizards-wrap">';
168  $html[] = '<div class="form-wizards-element">';
169 
170  // Add an empty hidden field which will send a blank value if all items are unselected.
171  $html[] = '<input type="hidden" class="select-checkbox" name="' . htmlspecialchars($parameterArray['itemFormElName']) . '" value="">';
172 
173  // Building the checkboxes
174  foreach ($groups as $groupKey => $group) {
175  $groupId = htmlspecialchars($parameterArray['itemFormElID']) . '-group-' . $groupKey;
176  $groupIdCollapsible = $groupId . '-collapse';
177  $html[] = '<div id="' . $groupId . '" class="panel panel-default">';
178  if (is_array($group['header'])) {
179  $html[] = '<div class="panel-heading">';
180  $html[] = '<a data-toggle="collapse" href="#' . $groupIdCollapsible . '" aria-expanded="false" aria-controls="' . $groupIdCollapsible . '">';
181  $html[] = $group['header']['icon'];
182  $html[] = htmlspecialchars($group['header']['title']);
183  $html[] = '</a>';
184  $html[] = '</div>';
185  }
186  if (is_array($group['items']) && !empty($group['items'])) {
187  $tableRows = [];
188 
189  // Render rows
190  foreach ($group['items'] as $item) {
191  $tableRows[] = '<tr class="' . $item['class'] . '">';
192  $tableRows[] = '<td class="col-checkbox">';
193  $tableRows[] = '<input type="checkbox" class="t3js-checkbox" '
194  . 'id="' . $item['id'] . '" '
195  . 'name="' . htmlspecialchars($item['name']) . '" '
196  . 'value="' . htmlspecialchars($item['value']) . '" '
197  . 'onclick="' . htmlspecialchars($sOnChange) . '" '
198  . ($item['checked'] ? 'checked=checked ' : '')
199  . ($item['disabled'] ? 'disabled=disabled ' : '') . '>';
200  $tableRows[] = '</td>';
201  $tableRows[] = '<td class="col-title">';
202  $tableRows[] = '<label class="label-block nowrap-disabled" for="' . $item['id'] . '">';
203  $tableRows[] = '<span class="inline-icon">' . $item['icon'] . '</span>';
204  $tableRows[] = htmlspecialchars($this->‪appendValueToLabelInDebugMode($item['title'], $item['value']), ENT_COMPAT, 'UTF-8', false);
205  $tableRows[] = '</label>';
206  $tableRows[] = '</td>';
207  $tableRows[] = '<td class="text-right">' . $item['help'] . '</td>';
208  $tableRows[] = '</tr>';
209  }
210 
211  // Build reset group button
212  $resetGroupBtn = '';
213  if (!empty($group['items'])) {
214  $title = htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.revertSelection'));
215  $resetGroupBtn = '<button type="button" '
216  . 'class="btn btn-default btn-sm t3js-revert-selection" '
217  . 'title="' . $title . '"'
218  . '>'
219  . $this->iconFactory->getIcon('actions-edit-undo', ‪Icon::SIZE_SMALL)->render() . ' '
220  . $this->‪getLanguageService()->‪sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.revertSelection') . '</button>';
221  }
222 
223  if (is_array($group['header'])) {
224  $html[] = '<div id="' . $groupIdCollapsible . '" class="panel-collapse collapse" role="tabpanel">';
225  }
226  $checkboxId = ‪StringUtility::getUniqueId($groupId);
227  $title = htmlspecialchars($this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.toggleall'));
228  $html[] = '<div class="table-responsive">';
229  $html[] = '<table class="table table-transparent table-hover">';
230  $html[] = '<thead>';
231  $html[] = '<tr>';
232  $html[] = '<th class="col-checkbox">';
233  $html[] = '<input type="checkbox" id="' . $checkboxId . '" class="t3js-toggle-checkboxes" data-trigger="hover" data-placement="right" data-title="' . $title . '" data-toggle="tooltip" />';
234  $html[] = '</th>';
235  $html[] = '<th class="col-title"><label for="' . $checkboxId . '">' . $title . '</label></th>';
236  $html[] = '<th class="text-right">' . $resetGroupBtn . '</th>';
237  $html[] = '</tr>';
238  $html[] = '</thead>';
239  $html[] = '<tbody>' . implode(LF, $tableRows) . '</tbody>';
240  $html[] = '</table>';
241  $html[] = '</div>';
242  if (is_array($group['header'])) {
243  $html[] = '</div>';
244  }
245 
246  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SelectCheckBoxElement' => '
247  function(SelectCheckBoxElement) {
248  new SelectCheckBoxElement(' . GeneralUtility::quoteJSvalue($checkboxId) . ');
249  }'
250  ];
251  }
252  $html[] = '</div>';
253  }
254 
255  $html[] = '</div>';
256  if (!$disabled && !empty($fieldWizardHtml)) {
257  $html[] = '<div class="form-wizards-items-bottom">';
258  $html[] = $fieldWizardHtml;
259  $html[] = '</div>';
260  }
261  $html[] = '</div>';
262  $html[] = '</div>';
263  }
264 
265  $resultArray['html'] = implode(LF, $html);
266  $resultArray['requireJsModules'][] = 'TYPO3/CMS/Backend/Tooltip';
267  return $resultArray;
268  }
269 }
‪TYPO3\CMS\Backend\Form\Element\SelectCheckBoxElement
Definition: SelectCheckBoxElement.php:30
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldInformation
‪array renderFieldInformation()
Definition: AbstractFormElement.php:72
‪TYPO3\CMS\Backend\Form\AbstractNode\mergeChildReturnIntoExistingResult
‪array mergeChildReturnIntoExistingResult(array $existing, array $childReturn, bool $mergeHtml=true)
Definition: AbstractNode.php:116
‪TYPO3\CMS\Backend\Form\AbstractNode\initializeResultArray
‪array initializeResultArray()
Definition: AbstractNode.php:90
‪TYPO3\CMS\Backend\Form\Element\SelectCheckBoxElement\render
‪array render()
Definition: SelectCheckBoxElement.php:68
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Backend\Form\Element\SelectCheckBoxElement\$defaultFieldWizard
‪array $defaultFieldWizard
Definition: SelectCheckBoxElement.php:45
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement
Definition: AbstractFormElement.php:32
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Backend\Form\Element
Definition: AbstractFormElement.php:16
‪TYPO3\CMS\Backend\Utility\BackendUtility\wrapInHelp
‪static string wrapInHelp($table, $field, $text='', array $overloadHelpText=[])
Definition: BackendUtility.php:2260
‪TYPO3\CMS\Backend\Form\Utility\FormEngineUtility
Definition: FormEngineUtility.php:39
‪TYPO3\CMS\Backend\Form\AbstractNode\getValidationDataAsJsonString
‪string getValidationDataAsJsonString(array $config)
Definition: AbstractNode.php:151
‪TYPO3\CMS\Backend\Form\Utility\FormEngineUtility\getIconHtml
‪static string getIconHtml($icon, $alt='', $title='')
Definition: FormEngineUtility.php:121
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\appendValueToLabelInDebugMode
‪string appendValueToLabelInDebugMode($label, $value)
Definition: AbstractFormElement.php:378
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\getLanguageService
‪LanguageService getLanguageService()
Definition: AbstractFormElement.php:390
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Backend\Form\Element\AbstractFormElement\renderFieldWizard
‪array renderFieldWizard()
Definition: AbstractFormElement.php:104
‪TYPO3\CMS\Backend\Form\Element\SelectCheckBoxElement\$defaultFieldInformation
‪array $defaultFieldInformation
Definition: SelectCheckBoxElement.php:35