TYPO3 CMS  TYPO3_8-7
RecordsContentObject.php
Go to the documentation of this file.
1 <?php
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 
21 
26 {
32  protected $itemArray = [];
33 
39  protected $data = [];
40 
47  public function render($conf = [])
48  {
49  // Reset items and data
50  $this->itemArray = [];
51  $this->data = [];
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) {
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 
148  protected function collectRecordsFromSource($source, array $tables)
149  {
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 
171  protected function collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
172  {
173  $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, true));
174 
175  // Loop on all selected tables
176  foreach ($tables as $table) {
177 
178  // Get the records for each selected category
179  $tableRecords = [];
180  $categoriesPerRecord = [];
181  foreach ($selectedCategories as $aCategory) {
182  try {
183  $collection = CategoryCollection::load(
184  $aCategory,
185  true,
186  $table,
187  $relationField
188  );
189  if ($collection->count() > 0) {
190  // Add items to the collection of records for the current table
191  foreach ($collection as $item) {
192  $tableRecords[$item['uid']] = $item;
193  // Keep track of all categories a given item belongs to
194  if (!isset($categoriesPerRecord[$item['uid']])) {
195  $categoriesPerRecord[$item['uid']] = [];
196  }
197  $categoriesPerRecord[$item['uid']][] = $aCategory;
198  }
199  }
200  } catch (\Exception $e) {
201  $message = sprintf(
202  'Could not get records for category id %d. Error: %s (%d)',
203  $aCategory,
204  $e->getMessage(),
205  $e->getCode()
206  );
207  $this->getTimeTracker()->setTSlogMessage($message, 2);
208  }
209  }
210  // Store the resulting records into the itemArray and data results array
211  if (!empty($tableRecords)) {
212  $this->data[$table] = [];
213  foreach ($tableRecords as $record) {
214  $this->itemArray[] = [
215  'id' => $record['uid'],
216  'table' => $table
217  ];
218  // Add to the record the categories it belongs to
219  $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
220  $this->data[$table][$record['uid']] = $record;
221  }
222  }
223  }
224  }
225 
229  protected function getTimeTracker()
230  {
231  return GeneralUtility::makeInstance(TimeTracker::class);
232  }
233 }
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
static load($id, $fillItems=false, $tableName='', $fieldName='')
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']