GroupElement.php 18.3 KB
Newer Older
1
<?php
2

3
/*
4
5
6
7
8
9
10
11
12
13
14
15
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

16
17
namespace TYPO3\CMS\Backend\Form\Element;

18
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
use TYPO3\CMS\Core\Imaging\Icon;
20
use TYPO3\CMS\Core\Localization\LanguageService;
21
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MathUtility;
23
use TYPO3\CMS\Core\Utility\StringUtility;
24
25

/**
26
 * Generation of elements of the type "group"
27
 */
28
29
class GroupElement extends AbstractFormElement
{
30
31
32
33
34
35
36
37
38
39
40
    /**
     * Default field information enabled for this element.
     *
     * @var array
     */
    protected $defaultFieldInformation = [
        'tcaDescription' => [
            'renderType' => 'tcaDescription',
        ],
    ];

41
    /**
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
     * Default field controls for this element.
     *
     * @var array
     */
    protected $defaultFieldControl = [
        'elementBrowser' => [
            'renderType' => 'elementBrowser',
        ],
        'insertClipboard' => [
            'renderType' => 'insertClipboard',
            'after' => [ 'elementBrowser' ],
        ],
        'editPopup' => [
            'renderType' => 'editPopup',
            'disabled' => true,
            'after' => [ 'insertClipboard' ],
        ],
        'addRecord' => [
            'renderType' => 'addRecord',
            'disabled' => true,
            'after' => [ 'editPopup' ],
        ],
        'listModule' => [
            'renderType' => 'listModule',
            'disabled' => true,
            'after' => [ 'addRecord' ],
        ],
    ];

    /**
     * Default field wizards for this element
     *
     * @var array
75
     */
76
77
78
79
80
81
    protected $defaultFieldWizard = [
        'tableList' => [
            'renderType' => 'tableList',
        ],
        'recordsOverview' => [
            'renderType' => 'recordsOverview',
82
            'after' => [ 'tableList' ],
83
        ],
84
85
        'localizationStateSelector' => [
            'renderType' => 'localizationStateSelector',
86
            'after' => [ 'recordsOverview' ],
87
        ],
88
89
        'otherLanguageContent' => [
            'renderType' => 'otherLanguageContent',
90
            'after' => [ 'localizationStateSelector' ],
91
92
93
94
95
96
        ],
        'defaultLanguageDifferences' => [
            'renderType' => 'defaultLanguageDifferences',
            'after' => [ 'otherLanguageContent' ],
        ],
    ];
97

98
99
100
101
102
    /**
     * This will render a selector box into which elements from either
     * the file system or database can be inserted. Relations.
     *
     * @return array As defined in initializeResultArray() of AbstractNode
103
     * @throws \RuntimeException
104
105
106
     */
    public function render()
    {
107
108
        $languageService = $this->getLanguageService();
        $backendUser = $this->getBackendUserAuthentication();
109
        $resultArray = $this->initializeResultArray();
110

111
112
113
114
115
        $table = $this->data['tableName'];
        $fieldName = $this->data['fieldName'];
        $row = $this->data['databaseRow'];
        $parameterArray = $this->data['parameterArray'];
        $config = $parameterArray['fieldConf']['config'];
116
117
118
119
120
121
122
123
124
125
126
127
128
        $elementName = $parameterArray['itemFormElName'];

        $selectedItems = $parameterArray['itemFormElValue'];
        $selectedItemsCount = count($selectedItems);

        $maxItems = $config['maxitems'];
        $autoSizeMax = MathUtility::forceIntegerInRange($config['autoSizeMax'], 0);
        $size = 5;
        if (isset($config['size'])) {
            $size = (int)$config['size'];
        }
        if ($autoSizeMax >= 1) {
            $size = MathUtility::forceIntegerInRange($selectedItemsCount + 1, MathUtility::forceIntegerInRange($size, 1), $autoSizeMax);
129
        }
Christian Kuhn's avatar
Christian Kuhn committed
130

131
132
        $internalType = (string)$config['internal_type'];
        $maxTitleLength = $backendUser->uc['titleLen'];
Benjamin Kott's avatar
Benjamin Kott committed
133

134
135
        $listOfSelectedValues = [];
        $selectorOptionsHtml = [];
136
        if ($internalType === 'folder') {
137
138
139
140
141
142
143
144
            foreach ($selectedItems as $selectedItem) {
                $folder = $selectedItem['folder'];
                $listOfSelectedValues[] = $folder;
                $selectorOptionsHtml[] =
                    '<option value="' . htmlspecialchars($folder) . '" title="' . htmlspecialchars($folder) . '">'
                        . htmlspecialchars($folder)
                    . '</option>';
            }
145
        } elseif ($internalType === 'db') {
146
147
148
149
150
            foreach ($selectedItems as $selectedItem) {
                $tableWithUid = $selectedItem['table'] . '_' . $selectedItem['uid'];
                $listOfSelectedValues[] = $tableWithUid;
                $title = $selectedItem['title'];
                if (empty($title)) {
151
                    $title = '[' . $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.no_title') . ']';
152
153
154
155
                }
                $shortenedTitle = GeneralUtility::fixed_lgd_cs($title, $maxTitleLength);
                $selectorOptionsHtml[] =
                    '<option value="' . htmlspecialchars($tableWithUid) . '" title="' . htmlspecialchars($title) . '">'
156
                        . htmlspecialchars($this->appendValueToLabelInDebugMode($shortenedTitle, $tableWithUid))
157
158
                    . '</option>';
            }
159
160
161
162
163
164
165
        } else {
            throw new \RuntimeException(
                'internal_type missing on type="group" field',
                1485007097
            );
        }

166
167
168
169
        $fieldInformationResult = $this->renderFieldInformation();
        $fieldInformationHtml = $fieldInformationResult['html'];
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldInformationResult, false);

170
171
172
        if (isset($config['readOnly']) && $config['readOnly']) {
            // Return early if element is read only
            $html = [];
173
            $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
174
            $html[] =   $fieldInformationHtml;
175
176
177
178
179
            $html[] =   '<div class="form-wizards-wrap">';
            $html[] =       '<div class="form-wizards-element">';
            $html[] =           '<select';
            $html[] =               ' size="' . $size . '"';
            $html[] =               ' disabled="disabled"';
180
            $html[] =               ' class="form-select"';
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
            $html[] =               ($maxItems !== 1 && $size !== 1) ? ' multiple="multiple"' : '';
            $html[] =           '>';
            $html[] =               implode(LF, $selectorOptionsHtml);
            $html[] =           '</select>';
            $html[] =       '</div>';
            $html[] =       '<div class="form-wizards-items-aside">';
            $html[] =       '</div>';
            $html[] =   '</div>';
            $html[] = '</div>';
            $resultArray['html'] = implode(LF, $html);
            return $resultArray;
        }

        // Need some information if in flex form scope for the suggest element
        $dataStructureIdentifier = '';
        $flexFormSheetName = '';
        $flexFormFieldName = '';
        $flexFormContainerName = '';
        $flexFormContainerFieldName = '';
        if ($this->data['processedTca']['columns'][$fieldName]['config']['type'] === 'flex') {
            $flexFormConfig = $this->data['processedTca']['columns'][$fieldName];
            $dataStructureIdentifier = $flexFormConfig['config']['dataStructureIdentifier'];
            if (!isset($flexFormConfig['config']['dataStructureIdentifier'])) {
                throw new \RuntimeException(
                    'A data structure identifier must be set in [\'config\'] part of a flex form.'
                    . ' This is usually added by TcaFlexPrepare data processor',
                    1485206970
                );
            }
210
211
            if (isset($this->data['flexFormSheetName'])) {
                $flexFormSheetName = $this->data['flexFormSheetName'];
212
            }
213
214
            if (isset($this->data['flexFormFieldName'])) {
                $flexFormFieldName = $this->data['flexFormFieldName'];
215
            }
216
217
            if (isset($this->data['flexFormContainerName'])) {
                $flexFormContainerName = $this->data['flexFormContainerName'];
218
            }
219
220
            if (isset($this->data['flexFormContainerFieldName'])) {
                $flexFormContainerFieldName = $this->data['flexFormContainerFieldName'];
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
            }
        }
        // Get minimum characters for suggest from TCA and override by TsConfig
        $suggestMinimumCharacters = 0;
        if (isset($config['suggestOptions']['default']['minimumCharacters'])) {
            $suggestMinimumCharacters = (int)$config['suggestOptions']['default']['minimumCharacters'];
        }
        if (isset($parameterArray['fieldTSConfig']['suggest.']['default.']['minimumCharacters'])) {
            $suggestMinimumCharacters = (int)$parameterArray['fieldTSConfig']['suggest.']['default.']['minimumCharacters'];
        }
        $suggestMinimumCharacters = $suggestMinimumCharacters > 0 ? $suggestMinimumCharacters : 2;

        $itemCanBeSelectedMoreThanOnce = !empty($config['multiple']);

        $showMoveIcons = true;
        if (isset($config['hideMoveIcons']) && $config['hideMoveIcons']) {
            $showMoveIcons = false;
        }
        $showDeleteControl = true;
        if (isset($config['hideDeleteIcon']) && $config['hideDeleteIcon']) {
            $showDeleteControl = false;
        }

Andreas Fernandez's avatar
Andreas Fernandez committed
244
245
        $fieldId = StringUtility::getUniqueId('tceforms-multiselect-');

246
        $selectorAttributes = [
Andreas Fernandez's avatar
Andreas Fernandez committed
247
            'id' => $fieldId,
248
249
            'data-formengine-input-name' => htmlspecialchars($elementName),
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
250
251
            'data-maxitems' => (string)$maxItems,
            'size' => (string)$size,
252
        ];
253
        $selectorAttributes['class'] = 'form-select';
254
255
        if ($maxItems !== 1 && $size !== 1) {
            $selectorAttributes['multiple'] = 'multiple';
256
257
        }

258
        $fieldControlResult = $this->renderFieldControl();
259
        $fieldControlHtml = $fieldControlResult['html'];
260
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldControlResult, false);
261

262
        $fieldWizardResult = $this->renderFieldWizard();
263
        $fieldWizardHtml = $fieldWizardResult['html'];
264
        $resultArray = $this->mergeChildReturnIntoExistingResult($resultArray, $fieldWizardResult, false);
265
266

        $html = [];
267
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
268
269
270
271
272
273
274
275
276
277
        $html[] =   $fieldInformationHtml;
        $html[] =   '<div class="form-wizards-wrap">';
        if ($internalType === 'db' && (!isset($config['hideSuggest']) || (bool)$config['hideSuggest'] !== true)) {
            $html[] =   '<div class="form-wizards-items-top">';
            $html[] =       '<div class="autocomplete t3-form-suggest-container">';
            $html[] =           '<div class="input-group">';
            $html[] =               '<span class="input-group-addon">';
            $html[] =                   $this->iconFactory->getIcon('actions-search', Icon::SIZE_SMALL)->render();
            $html[] =               '</span>';
            $html[] =               '<input type="search" class="t3-form-suggest form-control"';
278
            $html[] =                   ' placeholder="' . $languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.findRecord') . '"';
279
280
281
282
            $html[] =                   ' data-fieldname="' . htmlspecialchars($fieldName) . '"';
            $html[] =                   ' data-tablename="' . htmlspecialchars($table) . '"';
            $html[] =                   ' data-field="' . htmlspecialchars($elementName) . '"';
            $html[] =                   ' data-uid="' . htmlspecialchars($this->data['databaseRow']['uid']) . '"';
283
            $html[] =                   ' data-pid="' . htmlspecialchars($this->data['parentPageRow']['uid'] ?? 0) . '"';
284
            $html[] =                   ' data-fieldtype="' . htmlspecialchars($config['type']) . '"';
285
            $html[] =                   ' data-minchars="' . htmlspecialchars((string)$suggestMinimumCharacters) . '"';
286
287
288
289
290
291
292
            $html[] =                   ' data-datastructureidentifier="' . htmlspecialchars($dataStructureIdentifier) . '"';
            $html[] =                   ' data-flexformsheetname="' . htmlspecialchars($flexFormSheetName) . '"';
            $html[] =                   ' data-flexformfieldname="' . htmlspecialchars($flexFormFieldName) . '"';
            $html[] =                   ' data-flexformcontainername="' . htmlspecialchars($flexFormContainerName) . '"';
            $html[] =                   ' data-flexformcontainerfieldname="' . htmlspecialchars($flexFormContainerFieldName) . '"';
            $html[] =               '/>';
            $html[] =           '</div>';
293
            $html[] =       '</div>';
294
            $html[] =   '</div>';
295
        }
296
297
298
299
300
        $html[] =       '<div class="form-wizards-element">';
        $html[] =           '<input type="hidden" class="t3js-group-hidden-field" data-formengine-input-name="' . htmlspecialchars($elementName) . '" value="' . $itemCanBeSelectedMoreThanOnce . '" />';
        $html[] =           '<select ' . GeneralUtility::implodeAttributes($selectorAttributes, true) . '>';
        $html[] =               implode(LF, $selectorOptionsHtml);
        $html[] =           '</select>';
301
        $html[] =       '</div>';
302
303
304
305
        $html[] =       '<div class="form-wizards-items-aside">';
        $html[] =           '<div class="btn-group-vertical">';
        if ($maxItems > 1 && $size >=5 && $showMoveIcons) {
            $html[] =           '<a href="#"';
Andreas Fernandez's avatar
Andreas Fernandez committed
306
            $html[] =               ' class="btn btn-default t3js-btn-option t3js-btn-moveoption-top"';
307
            $html[] =               ' data-fieldname="' . htmlspecialchars($elementName) . '"';
308
            $html[] =               ' title="' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.move_to_top')) . '"';
309
310
311
312
313
314
            $html[] =           '>';
            $html[] =               $this->iconFactory->getIcon('actions-move-to-top', Icon::SIZE_SMALL)->render();
            $html[] =           '</a>';
        }
        if ($maxItems > 1 && $size > 1 && $showMoveIcons) {
            $html[] =           '<a href="#"';
Andreas Fernandez's avatar
Andreas Fernandez committed
315
            $html[] =               ' class="btn btn-default t3js-btn-option t3js-btn-moveoption-up"';
316
            $html[] =               ' data-fieldname="' . htmlspecialchars($elementName) . '"';
317
            $html[] =               ' title="' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.move_up')) . '"';
318
319
320
321
            $html[] =           '>';
            $html[] =               $this->iconFactory->getIcon('actions-move-up', Icon::SIZE_SMALL)->render();
            $html[] =           '</a>';
            $html[] =           '<a href="#"';
Andreas Fernandez's avatar
Andreas Fernandez committed
322
            $html[] =               ' class="btn btn-default t3js-btn-option t3js-btn-moveoption-down"';
323
            $html[] =               ' data-fieldname="' . htmlspecialchars($elementName) . '"';
324
            $html[] =               ' title="' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.move_down')) . '"';
325
326
327
328
329
330
            $html[] =           '>';
            $html[] =               $this->iconFactory->getIcon('actions-move-down', Icon::SIZE_SMALL)->render();
            $html[] =           '</a>';
        }
        if ($maxItems > 1 && $size >= 5 && $showMoveIcons) {
            $html[] =           '<a href="#"';
Andreas Fernandez's avatar
Andreas Fernandez committed
331
            $html[] =               ' class="btn btn-default t3js-btn-option t3js-btn-moveoption-bottom"';
332
            $html[] =               ' data-fieldname="' . htmlspecialchars($elementName) . '"';
333
            $html[] =               ' title="' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.move_to_bottom')) . '"';
334
335
336
337
338
339
            $html[] =           '>';
            $html[] =               $this->iconFactory->getIcon('actions-move-to-bottom', Icon::SIZE_SMALL)->render();
            $html[] =           '</a>';
        }
        if ($showDeleteControl) {
            $html[] =           '<a href="#"';
340
            $html[] =               ' class="btn btn-default t3js-btn-option t3js-btn-removeoption t3js-revert-unique"';
341
            $html[] =               ' data-fieldname="' . htmlspecialchars($elementName) . '"';
342
            $html[] =               ' data-uid="' . htmlspecialchars($row['uid']) . '"';
343
            $html[] =               ' title="' . htmlspecialchars($languageService->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.remove_selected')) . '"';
344
345
346
347
348
349
350
351
352
353
354
            $html[] =           '>';
            $html[] =               $this->iconFactory->getIcon('actions-selection-delete', Icon::SIZE_SMALL)->render();
            $html[] =           '</a>';
        }
        $html[] =           '</div>';
        $html[] =       '</div>';
        $html[] =       '<div class="form-wizards-items-aside">';
        $html[] =           '<div class="btn-group-vertical">';
        $html[] =               $fieldControlHtml;
        $html[] =           '</div>';
        $html[] =       '</div>';
355
356
357
358
359
        if (!empty($fieldWizardHtml)) {
            $html[] = '<div class="form-wizards-items-bottom">';
            $html[] = $fieldWizardHtml;
            $html[] = '</div>';
        }
360
        $html[] =   '</div>';
361
        $html[] =   '<input type="hidden" name="' . htmlspecialchars($elementName) . '" value="' . htmlspecialchars(implode(',', $listOfSelectedValues)) . '" />';
362
363
        $html[] = '</div>';

Andreas Fernandez's avatar
Andreas Fernandez committed
364
365
366
367
368
369
        $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/GroupElement' => '
            function(GroupElement) {
                new GroupElement(' . GeneralUtility::quoteJSvalue($fieldId) . ');
            }'
        ];

370
        $resultArray['html'] = implode(LF, $html);
371
372
        return $resultArray;
    }
373

374
375
376
377
378
379
380
    /**
     * @return BackendUserAuthentication
     */
    protected function getBackendUserAuthentication()
    {
        return $GLOBALS['BE_USER'];
    }
381
382
383
384
385
386
387
388

    /**
     * @return LanguageService
     */
    protected function getLanguageService()
    {
        return $GLOBALS['LANG'];
    }
389
}