[BUGFIX] Allow to render the same TS object twice
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / ContentObject / RecordsContentObject.php
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Xavier Perseguers <typo3@perseguers.ch>
8 * (c) 2010-2013 Steffen Kamper <steffen@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the text file GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33 /**
34 * Contains RECORDS class object.
35 *
36 * @author Xavier Perseguers <typo3@perseguers.ch>
37 * @author Steffen Kamper <steffen@typo3.org>
38 */
39 class RecordsContentObject extends AbstractContentObject {
40
41 /**
42 * List of all items with table and uid information
43 *
44 * @var array
45 */
46 protected $itemArray = array();
47
48 /**
49 * List of all selected records with full data, arranged per table
50 *
51 * @var array
52 */
53 protected $data = array();
54
55 /**
56 * Rendering the cObject, RECORDS
57 *
58 * @param array $conf Array of TypoScript properties
59 * @return string Output
60 */
61 public function render($conf = array()) {
62 // Reset items and data
63 $this->itemArray = array();
64 $this->data = array();
65
66 $theValue = '';
67 $originalRec = $GLOBALS['TSFE']->currentRecord;
68 // If the currentRecord is set, we register, that this record has invoked this function.
69 // It's should not be allowed to do this again then!!
70 if ($originalRec) {
71 ++$GLOBALS['TSFE']->recordRegister[$originalRec];
72 }
73 $tables = isset($conf['tables.']) ? $this->cObj->stdWrap($conf['tables'], $conf['tables.']) : $conf['tables'];
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
77 $tablesArray = array_unique(GeneralUtility::trimExplode(',', $tables, TRUE));
78 if ($tables) {
79 // Add tables which have a configuration (note that this may create duplicate entries)
80 if (is_array($conf['conf.'])) {
81 foreach ($conf['conf.'] as $key => $value) {
82 if (substr($key, -1) != '.' && !in_array($key, $tablesArray)) {
83 $tablesArray[] = $key;
84 }
85 }
86 }
87
88 // Get the data, depending on collection method.
89 // Property "source" is considered more precise and thus takes precedence over "categories"
90 if ($source) {
91 $this->collectRecordsFromSource($source, $tablesArray);
92 } elseif ($categories) {
93 $relationField = isset($conf['categories.']['relation.']) ? $this->cObj->stdWrap($conf['categories.']['relation'], $conf['categories.']['relation.']) : $conf['categories.']['relation'];
94 $this->collectRecordsFromCategories($categories, $tablesArray, $relationField);
95 }
96
97 if (count($this->itemArray) > 0) {
98 /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
99 $cObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
100 $cObj->setParent($this->cObj->data, $this->cObj->currentRecord);
101 $this->cObj->currentRecordNumber = 0;
102 $this->cObj->currentRecordTotal = count($this->itemArray);
103 foreach ($this->itemArray as $val) {
104 $row = $this->data[$val['table']][$val['id']];
105 // Perform overlays if necessary (records coming from category collections are already overlaid)
106 if ($source) {
107 // Versioning preview
108 $GLOBALS['TSFE']->sys_page->versionOL($val['table'], $row);
109 // Language overlay
110 if (is_array($row) && $GLOBALS['TSFE']->sys_language_contentOL) {
111 if ($val['table'] === 'pages') {
112 $row = $GLOBALS['TSFE']->sys_page->getPageOverlay($row);
113 } else {
114 $row = $GLOBALS['TSFE']->sys_page->getRecordOverlay($val['table'], $row, $GLOBALS['TSFE']->sys_language_content, $GLOBALS['TSFE']->sys_language_contentOL);
115 }
116 }
117 }
118 // Might be unset during the overlay process
119 if (is_array($row)) {
120 $dontCheckPid = isset($conf['dontCheckPid.']) ? $this->cObj->stdWrap($conf['dontCheckPid'], $conf['dontCheckPid.']) : $conf['dontCheckPid'];
121 if (!$dontCheckPid) {
122 $row = $this->cObj->checkPid($row['pid']) ? $row : '';
123 }
124 if ($row && !$GLOBALS['TSFE']->recordRegister[($val['table'] . ':' . $val['id'])]) {
125 $renderObjName = $conf['conf.'][$val['table']] ?: '<' . $val['table'];
126 $renderObjKey = $conf['conf.'][$val['table']] ? 'conf.' . $val['table'] : '';
127 $renderObjConf = $conf['conf.'][$val['table'] . '.'];
128 $this->cObj->currentRecordNumber++;
129 $cObj->parentRecordNumber = $this->cObj->currentRecordNumber;
130 $GLOBALS['TSFE']->currentRecord = $val['table'] . ':' . $val['id'];
131 $this->cObj->lastChanged($row['tstamp']);
132 $cObj->start($row, $val['table']);
133 $tmpValue = $cObj->cObjGetSingle($renderObjName, $renderObjConf, $renderObjKey);
134 $theValue .= $tmpValue;
135 }
136 }
137 }
138 }
139 }
140 $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
141 if ($wrap) {
142 $theValue = $this->cObj->wrap($theValue, $wrap);
143 }
144 if (isset($conf['stdWrap.'])) {
145 $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
146 }
147 // Restore
148 $GLOBALS['TSFE']->currentRecord = $originalRec;
149 if ($originalRec) {
150 --$GLOBALS['TSFE']->recordRegister[$originalRec];
151 }
152 return $theValue;
153 }
154
155 /**
156 * Collects records according to the configured source
157 *
158 * @param string $source Source of records
159 * @param array $tables List of tables
160 * @return void
161 */
162 protected function collectRecordsFromSource($source, array $tables) {
163 /** @var \TYPO3\CMS\Core\Database\RelationHandler $loadDB*/
164 $loadDB = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
165 $loadDB->setFetchAllFields(TRUE);
166 $loadDB->start($source, implode(',', $tables));
167 foreach ($loadDB->tableArray as $table => $v) {
168 if (isset($GLOBALS['TCA'][$table])) {
169 $loadDB->additionalWhere[$table] = $this->cObj->enableFields($table);
170 }
171 }
172 $this->data = $loadDB->getFromDB();
173 reset($loadDB->itemArray);
174 $this->itemArray = $loadDB->itemArray;
175 }
176
177 /**
178 * Collects records for all selected tables and categories.
179 *
180 * @param string $selectedCategories Comma-separated list of categories
181 * @param array $tables List of tables
182 * @param string $relationField Name of the field containing the categories relation
183 * @return void
184 */
185 protected function collectRecordsFromCategories($selectedCategories, array $tables, $relationField) {
186 $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, TRUE));
187
188 // Loop on all selected tables
189 foreach ($tables as $table) {
190
191 // Get the records for each selected category
192 $tableRecords = array();
193 $categoriesPerRecord = array();
194 foreach ($selectedCategories as $aCategory) {
195 try {
196 $collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
197 $aCategory,
198 TRUE,
199 $table,
200 $relationField
201 );
202 if ($collection->count() > 0) {
203 // Add items to the collection of records for the current table
204 foreach ($collection as $item) {
205 $tableRecords[$item['uid']] = $item;
206 // Keep track of all categories a given item belongs to
207 if (!isset($categoriesPerRecord[$item['uid']])) {
208 $categoriesPerRecord[$item['uid']] = array();
209 }
210 $categoriesPerRecord[$item['uid']][] = $aCategory;
211 }
212 }
213 } catch (\Exception $e) {
214 $message = sprintf(
215 'Could not get records for category id %d. Error: %s (%d)',
216 $aCategory,
217 $e->getMessage(),
218 $e->getCode()
219 );
220 $this->getTimeTracker()->setTSlogMessage($message, 2);
221 }
222 }
223 // Store the resulting records into the itemArray and data results array
224 if (count($tableRecords) > 0) {
225 $this->data[$table] = array();
226 foreach ($tableRecords as $record) {
227 $this->itemArray[] = array(
228 'id' => $record['uid'],
229 'table' => $table
230 );
231 // Add to the record the categories it belongs to
232 $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
233 $this->data[$table][$record['uid']] = $record;
234 }
235 }
236 }
237 }
238
239 /**
240 * Wrapper around the $GLOBALS['TT'] variable
241 *
242 * @return \TYPO3\CMS\Core\TimeTracker\TimeTracker
243 */
244 protected function getTimeTracker() {
245 return $GLOBALS['TT'];
246 }
247 }