Completed
Push — master ( 7c1fce...baf93a )
by
unknown
18:15
created

ContentFetcher::getContentRecordsPerColumn()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 20
nc 4
nop 2
dl 0
loc 35
rs 8.9777
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\View\BackendLayout;
19
20
use Doctrine\DBAL\Driver\Statement;
21
use TYPO3\CMS\Backend\Utility\BackendUtility;
22
use TYPO3\CMS\Backend\View\PageLayoutContext;
23
use TYPO3\CMS\Backend\View\PageLayoutView;
24
use TYPO3\CMS\Core\Cache\CacheManager;
25
use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
26
use TYPO3\CMS\Core\Database\ConnectionPool;
27
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
28
use TYPO3\CMS\Core\Database\Query\QueryHelper;
29
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
30
use TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction;
31
use TYPO3\CMS\Core\Localization\LanguageService;
32
use TYPO3\CMS\Core\Messaging\FlashMessage;
33
use TYPO3\CMS\Core\Messaging\FlashMessageService;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
36
/**
37
 * Class responsible for fetching the content data related to a BackendLayout
38
 *
39
 * - Reads content records
40
 * - Performs workspace overlay on records
41
 * - Capable of returning all records in active language as flat array
42
 * - Capable of returning records for a given column in a given (optional) language
43
 * - Capable of returning translation data (brief info about translation consistency)
44
 *
45
 * @internal this is experimental and subject to change in TYPO3 v10 / v11
46
 */
47
class ContentFetcher
48
{
49
    /**
50
     * @var PageLayoutContext
51
     */
52
    protected $context;
53
54
    /**
55
     * @var array
56
     */
57
    protected $fetchedContentRecords = [];
58
59
    public function __construct(PageLayoutContext $pageLayoutContext)
60
    {
61
        $this->context = $pageLayoutContext;
62
        $this->fetchedContentRecords = $this->getRuntimeCache()->get('ContentFetcher_fetchedContentRecords') ?: [];
63
    }
64
65
    /**
66
     * Gets content records per column.
67
     * This is required for correct workspace overlays.
68
     *
69
     * @param int|null $columnNumber
70
     * @param int|null $languageId
71
     * @return array Associative array for each column (colPos) or for all columns if $columnNumber is null
72
     */
73
    public function getContentRecordsPerColumn(?int $columnNumber = null, ?int $languageId = null): array
74
    {
75
        $languageId = $languageId ?? $this->context->getSiteLanguage()->getLanguageId();
76
77
        if (empty($this->fetchedContentRecords)) {
78
            $isLanguageMode = $this->context->getDrawingConfiguration()->getLanguageMode();
79
            $queryBuilder = $this->getQueryBuilder();
80
            /** @var Statement $result */
81
            $result = $queryBuilder->execute();
82
            $records = $this->getResult($result);
83
            foreach ($records as $record) {
84
                $recordLanguage = (int)$record['sys_language_uid'];
85
                $recordColumnNumber = (int)$record['colPos'];
86
                if ($recordLanguage === -1) {
87
                    // Record is set to "all languages", place it according to view mode.
88
                    if ($isLanguageMode) {
89
                        // Force the record to only be shown in default language in "Languages" view mode.
90
                        $recordLanguage = 0;
91
                    } else {
92
                        // Force the record to be shown in the currently active language in "Columns" view mode.
93
                        $recordLanguage = $languageId;
94
                    }
95
                }
96
                $this->fetchedContentRecords[$recordLanguage][$recordColumnNumber][] = $record;
97
            }
98
            $this->getRuntimeCache()->set('ContentFetcher_fetchedContentRecords', $this->fetchedContentRecords);
99
        }
100
101
        $contentByLanguage = &$this->fetchedContentRecords[$languageId];
102
103
        if ($columnNumber === null) {
104
            return $contentByLanguage ?? [];
105
        }
106
107
        return $contentByLanguage[$columnNumber] ?? [];
108
    }
109
110
    public function getFlatContentRecords(int $languageId): iterable
111
    {
112
        $contentRecords = $this->getContentRecordsPerColumn(null, $languageId);
113
        return empty($contentRecords) ? [] : array_merge(...$contentRecords);
114
    }
115
116
    /**
117
     * A hook allows to decide whether a custom type has children which were rendered or should not be rendered.
118
     *
119
     * @return iterable
120
     */
121
    public function getUnusedRecords(): iterable
122
    {
123
        $unrendered = [];
124
        $hookArray = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['cms/layout/class.tx_cms_layout.php']['record_is_used'] ?? [];
125
        $pageLayoutView = PageLayoutView::createFromPageLayoutContext($this->context);
126
127
        $knownColumnPositionNumbers = $this->context->getBackendLayout()->getColumnPositionNumbers();
128
        $rememberer = GeneralUtility::makeInstance(RecordRememberer::class);
129
        $languageId = $this->context->getDrawingConfiguration()->getSelectedLanguageId();
130
        foreach ($this->getContentRecordsPerColumn(null, $languageId) as $contentRecordsInColumn) {
131
            foreach ($contentRecordsInColumn as $contentRecord) {
132
                $used = $rememberer->isRemembered((int)$contentRecord['uid']);
133
                // A hook mentioned that this record is used somewhere, so this is in fact "rendered" already
134
                foreach ($hookArray as $hookFunction) {
135
                    $_params = ['columns' => $knownColumnPositionNumbers, 'record' => $contentRecord, 'used' => $used];
136
                    $used = GeneralUtility::callUserFunction($hookFunction, $_params, $pageLayoutView);
137
                }
138
                if (!$used) {
139
                    $unrendered[] = $contentRecord;
140
                }
141
            }
142
        }
143
        return $unrendered;
144
    }
145
146
    public function getTranslationData(iterable $contentElements, int $language): array
147
    {
148
        if ($language === 0) {
149
            return [];
150
        }
151
152
        $languageTranslationInfo = $this->getRuntimeCache()->get('ContentFetcher_TranslationInfo_' . $language) ?: [];
153
        if (empty($languageTranslationInfo)) {
154
            $contentRecordsInDefaultLanguage = $this->getContentRecordsPerColumn(null, 0);
155
            if (!empty($contentRecordsInDefaultLanguage)) {
156
                $contentRecordsInDefaultLanguage = array_merge(...$contentRecordsInDefaultLanguage);
157
            }
158
            $untranslatedRecordUids = array_flip(array_column($contentRecordsInDefaultLanguage, 'uid'));
159
160
            foreach ($contentElements as $contentElement) {
161
                if ((int)$contentElement['sys_language_uid'] === -1) {
162
                    continue;
163
                }
164
                if ((int)$contentElement['l18n_parent'] === 0) {
165
                    $languageTranslationInfo['hasStandAloneContent'] = true;
166
                    $languageTranslationInfo['mode'] = 'free';
167
                }
168
                if ((int)$contentElement['l18n_parent'] > 0) {
169
                    $languageTranslationInfo['hasTranslations'] = true;
170
                    $languageTranslationInfo['mode'] = 'connected';
171
                }
172
                if ((int)$contentElement['l10n_source'] > 0) {
173
                    unset($untranslatedRecordUids[(int)$contentElement['l10n_source']]);
174
                }
175
            }
176
            if (!isset($languageTranslationInfo['hasTranslations'])) {
177
                $languageTranslationInfo['hasTranslations'] = false;
178
            }
179
            $languageTranslationInfo['untranslatedRecordUids'] = array_keys($untranslatedRecordUids);
180
181
            // Check for inconsistent translations, force "mixed" mode and dispatch a FlashMessage to user if such a case is encountered.
182
            if (isset($languageTranslationInfo['hasStandAloneContent'])
183
                && $languageTranslationInfo['hasTranslations']
184
            ) {
185
                $languageTranslationInfo['mode'] = 'mixed';
186
                $siteLanguage = $this->context->getSiteLanguage($language);
187
188
                $message = GeneralUtility::makeInstance(
189
                    FlashMessage::class,
190
                    $this->getLanguageService()->getLL('staleTranslationWarning'),
191
                    sprintf($this->getLanguageService()->getLL('staleTranslationWarningTitle'), $siteLanguage->getTitle()),
192
                    FlashMessage::WARNING
193
                );
194
                $service = GeneralUtility::makeInstance(FlashMessageService::class);
195
                $queue = $service->getMessageQueueByIdentifier();
196
                $queue->addMessage($message);
197
            }
198
199
            $this->getRuntimeCache()->set('ContentFetcher_TranslationInfo_' . $language, $languageTranslationInfo);
200
        }
201
        return $languageTranslationInfo;
202
    }
203
204
    protected function getQueryBuilder(): QueryBuilder
205
    {
206
        $fields = ['*'];
207
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
208
            ->getQueryBuilderForTable('tt_content');
209
        $queryBuilder->getRestrictions()
210
            ->removeAll()
211
            ->add(GeneralUtility::makeInstance(DeletedRestriction::class))
212
            ->add(GeneralUtility::makeInstance(WorkspaceRestriction::class, (int)$GLOBALS['BE_USER']->workspace));
213
        $queryBuilder
214
            ->select(...$fields)
215
            ->from('tt_content');
216
217
        $queryBuilder->andWhere(
218
            $queryBuilder->expr()->eq(
219
                'tt_content.pid',
220
                $queryBuilder->createNamedParameter($this->context->getPageId(), \PDO::PARAM_INT)
221
            )
222
        );
223
224
        $additionalConstraints = [];
225
        $parameters = [
226
            'table' => 'tt_content',
227
            'fields' => $fields,
228
            'groupBy' => null,
229
            'orderBy' => null
230
        ];
231
232
        $sortBy = (string)($GLOBALS['TCA']['tt_content']['ctrl']['sortby'] ?: $GLOBALS['TCA']['tt_content']['ctrl']['default_sortby']);
233
        foreach (QueryHelper::parseOrderBy($sortBy) as $orderBy) {
234
            $queryBuilder->addOrderBy($orderBy[0], $orderBy[1]);
235
        }
236
237
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][PageLayoutView::class]['modifyQuery'] ?? [] as $className) {
238
            $hookObject = GeneralUtility::makeInstance($className);
239
            if (method_exists($hookObject, 'modifyQuery')) {
240
                $hookObject->modifyQuery(
241
                    $parameters,
242
                    'tt_content',
243
                    $this->context->getPageId(),
244
                    $additionalConstraints,
245
                    $fields,
246
                    $queryBuilder
247
                );
248
            }
249
        }
250
251
        return $queryBuilder;
252
    }
253
254
    protected function getResult(Statement $result): array
255
    {
256
        $output = [];
257
        while ($row = $result->fetch()) {
258
            BackendUtility::workspaceOL('tt_content', $row, -99, true);
259
            if ($row) {
260
                $output[] = $row;
261
            }
262
        }
263
        return $output;
264
    }
265
266
    protected function getLanguageService(): LanguageService
267
    {
268
        return $GLOBALS['LANG'];
269
    }
270
271
    protected function getRuntimeCache(): VariableFrontend
272
    {
273
        return GeneralUtility::makeInstance(CacheManager::class)->getCache('runtime');
274
    }
275
}
276