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