Completed
Push — master ( 8e078a...edce3c )
by
unknown
15:28
created

RecordsContentObject   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 103
dl 0
loc 212
rs 9.36
c 1
b 0
f 0
wmc 38

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageRepository() 0 3 2
B collectRecordsFromCategories() 0 50 9
A collectRecordsFromSource() 0 14 3
A getTimeTracker() 0 3 1
F render() 0 90 23
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace TYPO3\CMS\Frontend\ContentObject;
17
18
use TYPO3\CMS\Core\Database\RelationHandler;
19
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
20
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
21
use TYPO3\CMS\Core\Utility\GeneralUtility;
22
use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
23
24
/**
25
 * Contains RECORDS class object.
26
 */
27
class RecordsContentObject extends AbstractContentObject
28
{
29
    /**
30
     * List of all items with table and uid information
31
     *
32
     * @var array
33
     */
34
    protected $itemArray = [];
35
36
    /**
37
     * List of all selected records with full data, arranged per table
38
     *
39
     * @var array
40
     */
41
    protected $data = [];
42
43
    /**
44
     * Rendering the cObject, RECORDS
45
     *
46
     * @param array $conf Array of TypoScript properties
47
     * @return string Output
48
     */
49
    public function render($conf = [])
50
    {
51
        // Reset items and data
52
        $this->itemArray = [];
53
        $this->data = [];
54
55
        $theValue = '';
56
        $originalRec = $GLOBALS['TSFE']->currentRecord;
57
        // If the currentRecord is set, we register, that this record has invoked this function.
58
        // It's should not be allowed to do this again then!!
59
        if ($originalRec) {
60
            ++$GLOBALS['TSFE']->recordRegister[$originalRec];
61
        }
62
63
        $tables = $this->cObj->stdWrapValue('tables', $conf);
64
        if ($tables) {
65
            $tablesArray = array_unique(GeneralUtility::trimExplode(',', $tables, true));
66
            // Add tables which have a configuration (note that this may create duplicate entries)
67
            if (is_array($conf['conf.'])) {
68
                foreach ($conf['conf.'] as $key => $value) {
69
                    if (substr($key, -1) !== '.' && !in_array($key, $tablesArray)) {
70
                        $tablesArray[] = $key;
71
                    }
72
                }
73
            }
74
75
            // Get the data, depending on collection method.
76
            // Property "source" is considered more precise and thus takes precedence over "categories"
77
            $source = $this->cObj->stdWrapValue('source', $conf);
78
            $categories = $this->cObj->stdWrapValue('categories', $conf);
79
            if ($source) {
80
                $this->collectRecordsFromSource($source, $tablesArray);
81
            } elseif ($categories) {
82
                $relationField = $this->cObj->stdWrapValue('relation', $conf['categories.'] ?? []);
83
                $this->collectRecordsFromCategories($categories, $tablesArray, $relationField);
84
            }
85
            $itemArrayCount = count($this->itemArray);
86
            if ($itemArrayCount > 0) {
87
                /** @var ContentObjectRenderer $cObj */
88
                $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
89
                $cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
90
                $this->cObj->currentRecordNumber = 0;
91
                $this->cObj->currentRecordTotal = $itemArrayCount;
92
                foreach ($this->itemArray as $val) {
93
                    $row = $this->data[$val['table']][$val['id']];
94
                    // Perform overlays if necessary (records coming from category collections are already overlaid)
95
                    if ($source) {
96
                        // Versioning preview
97
                        $this->getPageRepository()->versionOL($val['table'], $row);
98
                        // Language overlay
99
                        if (is_array($row)) {
100
                            $row = $this->getPageRepository()->getLanguageOverlay($val['table'], $row);
101
                        }
102
                    }
103
                    // Might be unset during the overlay process
104
                    if (is_array($row)) {
105
                        $dontCheckPid = $this->cObj->stdWrapValue('dontCheckPid', $conf);
106
                        if (!$dontCheckPid) {
107
                            $validPageId = $this->getPageRepository()->filterAccessiblePageIds([$row['pid']]);
108
                            $row = !empty($validPageId) ? $row : '';
109
                        }
110
                        if ($row && !$GLOBALS['TSFE']->recordRegister[$val['table'] . ':' . $val['id']]) {
111
                            $renderObjName = $conf['conf.'][$val['table']] ?: '<' . $val['table'];
112
                            $renderObjKey = $conf['conf.'][$val['table']] ? 'conf.' . $val['table'] : '';
113
                            $renderObjConf = $conf['conf.'][$val['table'] . '.'];
114
                            $this->cObj->currentRecordNumber++;
115
                            $cObj->parentRecordNumber = $this->cObj->currentRecordNumber;
116
                            $GLOBALS['TSFE']->currentRecord = $val['table'] . ':' . $val['id'];
117
                            $this->cObj->lastChanged($row['tstamp']);
118
                            $cObj->start($row, $val['table']);
0 ignored issues
show
Bug introduced by
It seems like $row can also be of type string; however, parameter $data of TYPO3\CMS\Frontend\Conte...ObjectRenderer::start() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
                            $cObj->start(/** @scrutinizer ignore-type */ $row, $val['table']);
Loading history...
119
                            $tmpValue = $cObj->cObjGetSingle($renderObjName, $renderObjConf, $renderObjKey);
120
                            $theValue .= $tmpValue;
121
                        }
122
                    }
123
                }
124
            }
125
        }
126
        $wrap = $this->cObj->stdWrapValue('wrap', $conf);
127
        if ($wrap) {
128
            $theValue = $this->cObj->wrap($theValue, $wrap);
129
        }
130
        if (isset($conf['stdWrap.'])) {
131
            $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
132
        }
133
        // Restore
134
        $GLOBALS['TSFE']->currentRecord = $originalRec;
135
        if ($originalRec) {
136
            --$GLOBALS['TSFE']->recordRegister[$originalRec];
137
        }
138
        return $theValue;
139
    }
140
141
    /**
142
     * Collects records according to the configured source
143
     *
144
     * @param string $source Source of records
145
     * @param array $tables List of tables
146
     */
147
    protected function collectRecordsFromSource($source, array $tables)
148
    {
149
        /** @var RelationHandler $loadDB*/
150
        $loadDB = GeneralUtility::makeInstance(RelationHandler::class);
151
        $loadDB->setFetchAllFields(true);
152
        $loadDB->start($source, implode(',', $tables));
153
        foreach ($loadDB->tableArray as $table => $v) {
154
            if (isset($GLOBALS['TCA'][$table])) {
155
                $loadDB->additionalWhere[$table] = $this->getPageRepository()->enableFields($table);
156
            }
157
        }
158
        $this->data = $loadDB->getFromDB();
159
        reset($loadDB->itemArray);
160
        $this->itemArray = $loadDB->itemArray;
161
    }
162
163
    /**
164
     * Collects records for all selected tables and categories.
165
     *
166
     * @param string $selectedCategories Comma-separated list of categories
167
     * @param array $tables List of tables
168
     * @param string $relationField Name of the field containing the categories relation
169
     */
170
    protected function collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
171
    {
172
        $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, true));
173
174
        // Loop on all selected tables
175
        foreach ($tables as $table) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
176
177
            // Get the records for each selected category
178
            $tableRecords = [];
179
            $categoriesPerRecord = [];
180
            foreach ($selectedCategories as $aCategory) {
181
                try {
182
                    $collection = CategoryCollection::load(
183
                        $aCategory,
184
                        true,
185
                        $table,
186
                        $relationField
187
                    );
188
                    if ($collection->count() > 0) {
189
                        // Add items to the collection of records for the current table
190
                        foreach ($collection as $item) {
191
                            $tableRecords[$item['uid']] = $item;
192
                            // Keep track of all categories a given item belongs to
193
                            if (!isset($categoriesPerRecord[$item['uid']])) {
194
                                $categoriesPerRecord[$item['uid']] = [];
195
                            }
196
                            $categoriesPerRecord[$item['uid']][] = $aCategory;
197
                        }
198
                    }
199
                } catch (\Exception $e) {
200
                    $message = sprintf(
201
                        'Could not get records for category id %d. Error: %s (%d)',
202
                        $aCategory,
203
                        $e->getMessage(),
204
                        $e->getCode()
205
                    );
206
                    $this->getTimeTracker()->setTSlogMessage($message, 2);
207
                }
208
            }
209
            // Store the resulting records into the itemArray and data results array
210
            if (!empty($tableRecords)) {
211
                $this->data[$table] = [];
212
                foreach ($tableRecords as $record) {
213
                    $this->itemArray[] = [
214
                        'id' => $record['uid'],
215
                        'table' => $table
216
                    ];
217
                    // Add to the record the categories it belongs to
218
                    $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
219
                    $this->data[$table][$record['uid']] = $record;
220
                }
221
            }
222
        }
223
    }
224
225
    /**
226
     * @return TimeTracker
227
     */
228
    protected function getTimeTracker()
229
    {
230
        return GeneralUtility::makeInstance(TimeTracker::class);
231
    }
232
233
    /**
234
     * @return PageRepository
235
     */
236
    protected function getPageRepository(): PageRepository
237
    {
238
        return $GLOBALS['TSFE']->sys_page ?: GeneralUtility::makeInstance(PageRepository::class);
239
    }
240
}
241