TYPO3  7.6
SelectSingleElement.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 
30 {
36  public function render()
37  {
38  $table = $this->data['tableName'];
39  $field = $this->data['fieldName'];
40  $row = $this->data['databaseRow'];
41  $parameterArray = $this->data['parameterArray'];
42  $config = $parameterArray['fieldConf']['config'];
43 
44  $selectItems = $parameterArray['fieldConf']['config']['items'];
45 
46  // Check against inline uniqueness
48  $inlineStackProcessor = GeneralUtility::makeInstance(InlineStackProcessor::class);
49  $inlineStackProcessor->initializeByGivenStructure($this->data['inlineStructure']);
50  $uniqueIds = null;
51  if ($this->data['isInlineChild'] && $this->data['inlineParentUid']) {
52  $inlineObjectName = $inlineStackProcessor->getCurrentStructureDomObjectIdPrefix($this->data['inlineFirstPid']);
53  $inlineFormName = $inlineStackProcessor->getCurrentStructureFormPrefix();
54  if ($this->data['inlineParentConfig']['foreign_table'] === $table
55  && $this->data['inlineParentConfig']['foreign_unique'] === $field
56  ) {
57  $uniqueIds = $this->data['inlineData']['unique'][$inlineObjectName . '-' . $table]['used'];
58  $parameterArray['fieldChangeFunc']['inlineUnique'] = 'inline.updateUnique(this,'
59  . GeneralUtility::quoteJSvalue($inlineObjectName . '-' . $table) . ','
60  . GeneralUtility::quoteJSvalue($inlineFormName) . ','
61  . GeneralUtility::quoteJSvalue($row['uid']) . ');';
62  }
63  // hide uid of parent record for symmetric relations
64  if ($this->data['inlineParentConfig']['foreign_table'] === $table
65  && (
66  $this->data['inlineParentConfig']['foreign_field'] === $field
67  || $this->data['inlineParentConfig']['symmetric_field'] === $field
68  )
69  ) {
70  $uniqueIds[] = $this->data['inlineParentUid'];
71  }
72  }
73 
74  // Initialization:
75  $selectId = StringUtility::getUniqueId('tceforms-select-');
76  $selectedIcon = '';
77  $size = (int)$config['size'];
78 
79  // Style set on <select/>
80  $options = '';
81  $disabled = false;
82  if (!empty($config['readOnly'])) {
83  $disabled = true;
84  }
85 
86  // Prepare groups
87  $selectItemCounter = 0;
88  $selectItemGroupCount = 0;
89  $selectItemGroups = array();
90  $selectIcons = array();
91  $selectedValue = '';
92  $hasIcons = false;
93 
94  if (!empty($parameterArray['itemFormElValue'])) {
95  $selectedValue = (string)$parameterArray['itemFormElValue'][0];
96  }
97 
98  foreach ($selectItems as $item) {
99  if ($item[1] === '--div--') {
100  // IS OPTGROUP
101  if ($selectItemCounter !== 0) {
102  $selectItemGroupCount++;
103  }
104  $selectItemGroups[$selectItemGroupCount]['header'] = array(
105  'title' => $item[0],
106  );
107  } else {
108  // IS ITEM
109  $title = htmlspecialchars($item['0'], ENT_COMPAT, 'UTF-8', false);
110  $icon = !empty($item[2]) ? FormEngineUtility::getIconHtml($item[2], $title, $title) : '';
111  $selected = $selectedValue === (string)$item[1];
112 
113  if ($selected) {
114  $selectedIcon = $icon;
115  }
116 
117  $selectItemGroups[$selectItemGroupCount]['items'][] = array(
118  'title' => $title,
119  'value' => $item[1],
120  'icon' => $icon,
121  'selected' => $selected,
122  'index' => $selectItemCounter
123  );
124 
125  // ICON
126  if ($icon) {
127  $selectIcons[] = array(
128  'title' => $title,
129  'icon' => $icon,
130  'index' => $selectItemCounter,
131  );
132  }
133 
134  $selectItemCounter++;
135  }
136  }
137 
138  // Fallback icon
139  // @todo: assign a special icon for non matching values?
140  if (!$selectedIcon && $selectItemGroups[0]['items'][0]['icon']) {
141  $selectedIcon = $selectItemGroups[0]['items'][0]['icon'];
142  }
143 
144  // Process groups
145  foreach ($selectItemGroups as $selectItemGroup) {
146  // suppress groups without items
147  if (empty($selectItemGroup['items'])) {
148  continue;
149  }
150 
151  $optionGroup = is_array($selectItemGroup['header']);
152  $options .= ($optionGroup ? '<optgroup label="' . htmlspecialchars($selectItemGroup['header']['title'], ENT_COMPAT, 'UTF-8', false) . '">' : '');
153 
154  if (is_array($selectItemGroup['items'])) {
155  foreach ($selectItemGroup['items'] as $item) {
156  $options .= '<option value="' . htmlspecialchars($item['value']) . '" data-icon="' .
157  htmlspecialchars($item['icon']) . '"'
158  . ($item['selected'] ? ' selected="selected"' : '') . '>' . $item['title'] . '</option>';
159  }
160  $hasIcons = !empty($item['icon']);
161  }
162 
163  $options .= ($optionGroup ? '</optgroup>' : '');
164  }
165 
166  // Build the element
167  $html = ['<div class="form-control-wrap">'];
168 
169  if ($hasIcons) {
170  $html[] = '<div class="input-group">';
171  $html[] = '<span class="input-group-addon input-group-icon">';
172  $html[] = $selectedIcon;
173  $html[] = '</span>';
174  }
175 
176  $html[] = '<select'
177  . ' id="' . $selectId . '"'
178  . ' name="' . htmlspecialchars($parameterArray['itemFormElName']) . '"'
179  . $this->getValidationDataAsDataAttribute($config)
180  . ' class="form-control form-control-adapt"'
181  . ($size ? ' size="' . $size . '"' : '')
182  . ($disabled ? ' disabled="disabled"' : '')
183  . '>';
184  $html[] = $options;
185  $html[] = '</select>';
186 
187  if ($hasIcons) {
188  $html[] = '</div>';
189  }
190 
191  $html[] = '</div>';
192 
193  // Create icon table:
194  if (!empty($selectIcons) && !empty($config['showIconTable'])) {
195  $selectIconColumns = (int)$config['selicon_cols'];
196 
197  if (!$selectIconColumns) {
198  $selectIconColumns = count($selectIcons);
199  }
200 
201  $selectIconColumns = ($selectIconColumns > 12 ? 12 : $selectIconColumns);
202  $selectIconRows = ceil(count($selectIcons) / $selectIconColumns);
203  $selectIcons = array_pad($selectIcons, $selectIconRows * $selectIconColumns, '');
204 
205  $html[] = '<div class="t3js-forms-select-single-icons table-icons table-fit table-fit-inline-block">';
206  $html[] = '<table class="table table-condensed table-white table-center">';
207  $html[] = '<tbody>';
208  $html[] = '<tr>';
209 
210  foreach ($selectIcons as $i => $selectIcon) {
211  if ($i % $selectIconColumns === 0 && $i !== 0) {
212  $html[] = '</tr>';
213  $html[] = '<tr>';
214  }
215 
216  $html[] = '<td>';
217 
218  if (is_array($selectIcon)) {
219  $html[] = '<a href="#" title="' . $selectIcon['title'] . '" data-select-index="' . $selectIcon['index'] . '">';
220  $html[] = $selectIcon['icon'];
221  $html[] = '</a>';
222  }
223 
224  $html[] = '</td>';
225  }
226 
227  $html[] = '</tr>';
228  $html[] = '</tbody>';
229  $html[] = '</table>';
230  $html[] = '</div>';
231  }
232 
233  $html = implode(LF, $html);
234 
235  // Wizards:
236  if (!$disabled) {
237  $html = $this->renderWizards(
238  array($html),
239  $config['wizards'],
240  $table,
241  $row,
242  $field,
243  $parameterArray,
244  $parameterArray['itemFormElName'],
245  BackendUtility::getSpecConfParts($parameterArray['fieldConf']['defaultExtras'])
246  );
247  }
248 
249  $resultArray = $this->initializeResultArray();
250  $resultArray['html'] = $html;
251  $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/SelectSingleElement' => implode(LF, [
252  'function(SelectSingleElement) {',
253  'SelectSingleElement.initialize(',
254  GeneralUtility::quoteJSvalue('#' . $selectId) . ',',
255  '{',
256  'onChange: function() {',
257  implode('', $parameterArray['fieldChangeFunc']),
258  '},',
259  'onFocus: function() {',
260  $parameterArray['onFocus'],
261  '},',
262  '}',
263  ');',
264  '}',
265  ])];
266 
267  return $resultArray;
268  }
269 }