TcaGroup.php 7.04 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\FormDataProvider;

18
use TYPO3\CMS\Backend\Clipboard\Clipboard;
19
use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Core\Database\RelationHandler;
22
use TYPO3\CMS\Core\Resource\Exception;
23
24
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\ResourceFactory;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Core\Utility\MathUtility;
27
28
29
30

/**
 * Resolve databaseRow field content to the real connected rows for type=group
 */
31
32
33
34
35
36
37
38
class TcaGroup implements FormDataProviderInterface
{
    /**
     * Initialize new row with default values from various sources
     *
     * @param array $result
     * @return array
     * @throws \UnexpectedValueException
39
     * @throws \RuntimeException
40
41
42
     */
    public function addData(array $result)
    {
43
        foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) {
44
45
46
47
48
49
            if (empty($fieldConfig['config']['type'])
                || $fieldConfig['config']['type'] !== 'group'
                || empty($fieldConfig['config']['internal_type'])
            ) {
                continue;
            }
50

51
52
            // Sanitize max items, set to 99999 if not defined
            $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = MathUtility::forceIntegerInRange(
53
                $fieldConfig['config']['maxitems'] ?? 0,
54
55
                0,
                99999
56
57
58
59
60
            );
            if ($result['processedTca']['columns'][$fieldName]['config']['maxitems'] === 0) {
                $result['processedTca']['columns'][$fieldName]['config']['maxitems'] = 99999;
            }

61
62
63
64
            $databaseRowFieldContent = '';
            if (!empty($result['databaseRow'][$fieldName])) {
                $databaseRowFieldContent = (string)$result['databaseRow'][$fieldName];
            }
65

66
67
            $items = [];
            $sanitizedClipboardElements = [];
68
            $internalType = $fieldConfig['config']['internal_type'];
69
            if ($internalType === 'db') {
70
71
72
73
74
75
76
                if (empty($fieldConfig['config']['allowed'])) {
                    throw new \RuntimeException(
                        'Mandatory TCA config setting "allowed" missing in field "' . $fieldName . '" of table "' . $result['tableName'] . '"',
                        1482250512
                    );
                }

77
78
79
                // In case of vanilla uid, 0 is used to query relations by splitting $databaseRowFieldContent (possible defVals)
                $MMuid = MathUtility::canBeInterpretedAsInteger($result['databaseRow']['uid']) ? $result['databaseRow']['uid'] : 0;

80
81
82
                $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
                $relationHandler->start(
                    $databaseRowFieldContent,
83
84
                    $fieldConfig['config']['allowed'] ?? '',
                    $fieldConfig['config']['MM'] ?? '',
85
                    $MMuid,
86
87
                    $result['tableName'] ?? '',
                    $fieldConfig['config'] ?? []
88
89
                );
                $relationHandler->getFromDB();
90
91
92
                $relations = $relationHandler->getResolvedItemArray();
                foreach ($relations as $relation) {
                    $tableName = $relation['table'];
93
94
                    $record = $relation['record'];
                    BackendUtility::workspaceOL($tableName, $record);
95
96
97
                    $title = BackendUtility::getRecordTitle($tableName, $record, false, false);
                    $items[] = [
                        'table' => $tableName,
98
                        'uid' => $record['uid'] ?? null,
99
100
101
102
103
104
105
106
107
108
109
110
                        'title' => $title,
                        'row' => $record,
                    ];
                }

                // Register elements from clipboard
                $allowed = GeneralUtility::trimExplode(',', $fieldConfig['config']['allowed'], true);
                $clipboard = GeneralUtility::makeInstance(Clipboard::class);
                $clipboard->initializeClipboard();
                if ($allowed[0] !== '*') {
                    // Only some tables, filter them:
                    foreach ($allowed as $tablename) {
111
112
                        foreach ($clipboard->elFromTable($tablename) as $recordUid) {
                            $record = BackendUtility::getRecordWSOL($tablename, $recordUid);
113
                            $sanitizedClipboardElements[] = [
114
115
                                'title' => BackendUtility::getRecordTitle($tablename, $record),
                                'value' => $tablename . '_' . $recordUid,
116
117
118
119
120
121
122
                            ];
                        }
                    }
                } else {
                    // All tables allowed for relation:
                    $clipboardElements = array_keys($clipboard->elFromTable(''));
                    foreach ($clipboardElements as $elementValue) {
123
                        [$elementTable, $elementUid] = explode('|', $elementValue);
124
                        $record = BackendUtility::getRecordWSOL($elementTable, (int)$elementUid);
125
126
127
128
129
130
                        $sanitizedClipboardElements[] = [
                            'title' => BackendUtility::getRecordTitle($elementTable, $record),
                            'value' => $elementTable . '_' . $elementUid,
                        ];
                    }
                }
131
132
133
134
            } elseif ($internalType === 'folder') {
                // Simple list of folders
                $folderList = GeneralUtility::trimExplode(',', $databaseRowFieldContent, true);
                foreach ($folderList as $folder) {
135
136
137
138
                    if (empty($folder)) {
                        continue;
                    }
                    try {
139
                        $folderObject = GeneralUtility::makeInstance(ResourceFactory::class)->retrieveFileOrFolderObject($folder);
140
                        if ($folderObject instanceof Folder) {
141
142
143
                            $items[] = [
                                'folder' => $folder,
                            ];
144
                        }
145
146
                    } catch (Exception $exception) {
                        continue;
147
148
                    }
                }
149
150
151
            } else {
                throw new \UnexpectedValueException(
                    'TCA internal_type of field "' . $fieldName . '" in table ' . $result['tableName']
152
                    . ' must be set to "db" or "folder".',
153
154
155
                    1438780511
                );
            }
156
157
158

            $result['databaseRow'][$fieldName] = $items;
            $result['processedTca']['columns'][$fieldName]['config']['clipboardElements'] = $sanitizedClipboardElements;
159
        }
160

161
162
        return $result;
    }
163
}