[!!!][TASK] Make TimeTracker a singleton
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / RecordsContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
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
17 use TYPO3\CMS\Core\Database\RelationHandler;
18 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Frontend\Category\Collection\CategoryCollection;
21
22 /**
23 * Contains RECORDS class object.
24 */
25 class RecordsContentObject extends AbstractContentObject
26 {
27 /**
28 * List of all items with table and uid information
29 *
30 * @var array
31 */
32 protected $itemArray = array();
33
34 /**
35 * List of all selected records with full data, arranged per table
36 *
37 * @var array
38 */
39 protected $data = array();
40
41 /**
42 * Rendering the cObject, RECORDS
43 *
44 * @param array $conf Array of TypoScript properties
45 * @return string Output
46 */
47 public function render($conf = array())
48 {
49 // Reset items and data
50 $this->itemArray = array();
51 $this->data = array();
52
53 $theValue = '';
54 $originalRec = $GLOBALS['TSFE']->currentRecord;
55 // If the currentRecord is set, we register, that this record has invoked this function.
56 // It's should not be allowed to do this again then!!
57 if ($originalRec) {
58 ++$GLOBALS['TSFE']->recordRegister[$originalRec];
59 }
60
61 $tables = isset($conf['tables.']) ? $this->cObj->stdWrap($conf['tables'], $conf['tables.']) : $conf['tables'];
62 if ($tables) {
63 $tablesArray = array_unique(GeneralUtility::trimExplode(',', $tables, true));
64 // Add tables which have a configuration (note that this may create duplicate entries)
65 if (is_array($conf['conf.'])) {
66 foreach ($conf['conf.'] as $key => $value) {
67 if (substr($key, -1) != '.' && !in_array($key, $tablesArray)) {
68 $tablesArray[] = $key;
69 }
70 }
71 }
72
73 // Get the data, depending on collection method.
74 // Property "source" is considered more precise and thus takes precedence over "categories"
75 $source = isset($conf['source.']) ? $this->cObj->stdWrap($conf['source'], $conf['source.']) : $conf['source'];
76 $categories = isset($conf['categories.']) ? $this->cObj->stdWrap($conf['categories'], $conf['categories.']) : $conf['categories'];
77 if ($source) {
78 $this->collectRecordsFromSource($source, $tablesArray);
79 } elseif ($categories) {
80 $relationField = isset($conf['categories.']['relation.']) ? $this->cObj->stdWrap($conf['categories.']['relation'], $conf['categories.']['relation.']) : $conf['categories.']['relation'];
81 $this->collectRecordsFromCategories($categories, $tablesArray, $relationField);
82 }
83 $itemArrayCount = count($this->itemArray);
84 if ($itemArrayCount > 0) {
85 /** @var ContentObjectRenderer $cObj */
86 $cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
87 $cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
88 $this->cObj->currentRecordNumber = 0;
89 $this->cObj->currentRecordTotal = $itemArrayCount;
90 foreach ($this->itemArray as $val) {
91 $row = $this->data[$val['table']][$val['id']];
92 // Perform overlays if necessary (records coming from category collections are already overlaid)
93 if ($source) {
94 // Versioning preview
95 $GLOBALS['TSFE']->sys_page->versionOL($val['table'], $row);
96 // Language overlay
97 if (is_array($row) && $GLOBALS['TSFE']->sys_language_contentOL) {
98 if ($val['table'] === 'pages') {
99 $row = $GLOBALS['TSFE']->sys_page->getPageOverlay($row);
100 } else {
101 $row = $GLOBALS['TSFE']->sys_page->getRecordOverlay($val['table'], $row, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
102 }
103 }
104 }
105 // Might be unset during the overlay process
106 if (is_array($row)) {
107 $dontCheckPid = isset($conf['dontCheckPid.']) ? $this->cObj->stdWrap($conf['dontCheckPid'], $conf['dontCheckPid.']) : $conf['dontCheckPid'];
108 if (!$dontCheckPid) {
109 $row = $this->cObj->checkPid($row['pid']) ? $row : '';
110 }
111 if ($row && !$GLOBALS['TSFE']->recordRegister[$val['table'] . ':' . $val['id']]) {
112 $renderObjName = $conf['conf.'][$val['table']] ?: '<' . $val['table'];
113 $renderObjKey = $conf['conf.'][$val['table']] ? 'conf.' . $val['table'] : '';
114 $renderObjConf = $conf['conf.'][$val['table'] . '.'];
115 $this->cObj->currentRecordNumber++;
116 $cObj->parentRecordNumber = $this->cObj->currentRecordNumber;
117 $GLOBALS['TSFE']->currentRecord = $val['table'] . ':' . $val['id'];
118 $this->cObj->lastChanged($row['tstamp']);
119 $cObj->start($row, $val['table']);
120 $tmpValue = $cObj->cObjGetSingle($renderObjName, $renderObjConf, $renderObjKey);
121 $theValue .= $tmpValue;
122 }
123 }
124 }
125 }
126 }
127 $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
128 if ($wrap) {
129 $theValue = $this->cObj->wrap($theValue, $wrap);
130 }
131 if (isset($conf['stdWrap.'])) {
132 $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
133 }
134 // Restore
135 $GLOBALS['TSFE']->currentRecord = $originalRec;
136 if ($originalRec) {
137 --$GLOBALS['TSFE']->recordRegister[$originalRec];
138 }
139 return $theValue;
140 }
141
142 /**
143 * Collects records according to the configured source
144 *
145 * @param string $source Source of records
146 * @param array $tables List of tables
147 * @return void
148 */
149 protected function collectRecordsFromSource($source, array $tables)
150 {
151 /** @var RelationHandler $loadDB*/
152 $loadDB = GeneralUtility::makeInstance(RelationHandler::class);
153 $loadDB->setFetchAllFields(true);
154 $loadDB->start($source, implode(',', $tables));
155 foreach ($loadDB->tableArray as $table => $v) {
156 if (isset($GLOBALS['TCA'][$table])) {
157 $loadDB->additionalWhere[$table] = $this->cObj->enableFields($table);
158 }
159 }
160 $this->data = $loadDB->getFromDB();
161 reset($loadDB->itemArray);
162 $this->itemArray = $loadDB->itemArray;
163 }
164
165 /**
166 * Collects records for all selected tables and categories.
167 *
168 * @param string $selectedCategories Comma-separated list of categories
169 * @param array $tables List of tables
170 * @param string $relationField Name of the field containing the categories relation
171 * @return void
172 */
173 protected function collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
174 {
175 $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, true));
176
177 // Loop on all selected tables
178 foreach ($tables as $table) {
179
180 // Get the records for each selected category
181 $tableRecords = array();
182 $categoriesPerRecord = array();
183 foreach ($selectedCategories as $aCategory) {
184 try {
185 $collection = CategoryCollection::load(
186 $aCategory,
187 true,
188 $table,
189 $relationField
190 );
191 if ($collection->count() > 0) {
192 // Add items to the collection of records for the current table
193 foreach ($collection as $item) {
194 $tableRecords[$item['uid']] = $item;
195 // Keep track of all categories a given item belongs to
196 if (!isset($categoriesPerRecord[$item['uid']])) {
197 $categoriesPerRecord[$item['uid']] = array();
198 }
199 $categoriesPerRecord[$item['uid']][] = $aCategory;
200 }
201 }
202 } catch (\Exception $e) {
203 $message = sprintf(
204 'Could not get records for category id %d. Error: %s (%d)',
205 $aCategory,
206 $e->getMessage(),
207 $e->getCode()
208 );
209 $this->getTimeTracker()->setTSlogMessage($message, 2);
210 }
211 }
212 // Store the resulting records into the itemArray and data results array
213 if (!empty($tableRecords)) {
214 $this->data[$table] = array();
215 foreach ($tableRecords as $record) {
216 $this->itemArray[] = array(
217 'id' => $record['uid'],
218 'table' => $table
219 );
220 // Add to the record the categories it belongs to
221 $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
222 $this->data[$table][$record['uid']] = $record;
223 }
224 }
225 }
226 }
227
228 /**
229 * @return TimeTracker
230 */
231 protected function getTimeTracker()
232 {
233 return GeneralUtility::makeInstance(TimeTracker::class);
234 }
235 }