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