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