‪TYPO3CMS  9.5
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 
22 
27 {
33  protected ‪$itemArray = [];
34 
40  protected ‪$data = [];
41 
48  public function ‪render($conf = [])
49  {
50  // Reset items and data
51  $this->itemArray = [];
52  $this->data = [];
53 
54  $theValue = '';
55  $originalRec = ‪$GLOBALS['TSFE']->currentRecord;
56  // If the currentRecord is set, we register, that this record has invoked this function.
57  // It's should not be allowed to do this again then!!
58  if ($originalRec) {
59  ++‪$GLOBALS['TSFE']->recordRegister[$originalRec];
60  }
61 
62  $tables = isset($conf['tables.']) ? $this->cObj->stdWrap($conf['tables'], $conf['tables.']) : $conf['tables'];
63  if ($tables) {
64  $tablesArray = array_unique(GeneralUtility::trimExplode(',', $tables, true));
65  // Add tables which have a configuration (note that this may create duplicate entries)
66  if (is_array($conf['conf.'])) {
67  foreach ($conf['conf.'] as $key => $value) {
68  if (substr($key, -1) !== '.' && !in_array($key, $tablesArray)) {
69  $tablesArray[] = $key;
70  }
71  }
72  }
73 
74  // Get the data, depending on collection method.
75  // Property "source" is considered more precise and thus takes precedence over "categories"
76  $source = isset($conf['source.']) ? $this->cObj->stdWrap($conf['source'], $conf['source.']) : $conf['source'];
77  $categories = isset($conf['categories.']) ? $this->cObj->stdWrap($conf['categories'], $conf['categories.']) : $conf['categories'];
78  if ($source) {
79  $this->collectRecordsFromSource($source, $tablesArray);
80  } elseif ($categories) {
81  $relationField = isset($conf['categories.']['relation.']) ? $this->cObj->stdWrap($conf['categories.']['relation'], $conf['categories.']['relation.']) : $conf['categories.']['relation'];
82  $this->‪collectRecordsFromCategories($categories, $tablesArray, $relationField);
83  }
84  $itemArrayCount = count($this->itemArray);
85  if ($itemArrayCount > 0) {
87  ‪$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
88  ‪$cObj->‪setParent($this->cObj->data, $this->cObj->currentRecord);
89  $this->cObj->currentRecordNumber = 0;
90  $this->cObj->currentRecordTotal = $itemArrayCount;
91  foreach ($this->itemArray as $val) {
92  $row = $this->data[$val['table']][$val['id']];
93  // Perform overlays if necessary (records coming from category collections are already overlaid)
94  if ($source) {
95  // Versioning preview
96  $this->‪getPageRepository()->‪versionOL($val['table'], $row);
97  // Language overlay
98  if (is_array($row)) {
99  $row = $this->‪getPageRepository()->‪getLanguageOverlay($val['table'], $row);
100  }
101  }
102  // Might be unset during the overlay process
103  if (is_array($row)) {
104  $dontCheckPid = isset($conf['dontCheckPid.']) ? $this->cObj->stdWrap($conf['dontCheckPid'], $conf['dontCheckPid.']) : $conf['dontCheckPid'];
105  if (!$dontCheckPid) {
106  $row = $this->cObj->checkPid($row['pid']) ? $row : '';
107  }
108  if ($row && !‪$GLOBALS['TSFE']->recordRegister[$val['table'] . ':' . $val['id']]) {
109  $renderObjName = $conf['conf.'][$val['table']] ?: '<' . $val['table'];
110  $renderObjKey = $conf['conf.'][$val['table']] ? 'conf.' . $val['table'] : '';
111  $renderObjConf = $conf['conf.'][$val['table'] . '.'];
112  $this->cObj->currentRecordNumber++;
113  ‪$cObj->parentRecordNumber = $this->cObj->currentRecordNumber;
114  ‪$GLOBALS['TSFE']->currentRecord = $val['table'] . ':' . $val['id'];
115  $this->cObj->lastChanged($row['tstamp']);
116  ‪$cObj->‪start($row, $val['table']);
117  $tmpValue = ‪$cObj->‪cObjGetSingle($renderObjName, $renderObjConf, $renderObjKey);
118  $theValue .= $tmpValue;
119  }
120  }
121  }
122  }
123  }
124  $wrap = isset($conf['wrap.']) ? $this->cObj->stdWrap($conf['wrap'], $conf['wrap.']) : $conf['wrap'];
125  if ($wrap) {
126  $theValue = $this->cObj->wrap($theValue, $wrap);
127  }
128  if (isset($conf['stdWrap.'])) {
129  $theValue = $this->cObj->stdWrap($theValue, $conf['stdWrap.']);
130  }
131  // Restore
132  ‪$GLOBALS['TSFE']->currentRecord = $originalRec;
133  if ($originalRec) {
134  --‪$GLOBALS['TSFE']->recordRegister[$originalRec];
135  }
136  return $theValue;
137  }
138 
145  protected function collectRecordsFromSource($source, array $tables)
146  {
148  $loadDB = GeneralUtility::makeInstance(RelationHandler::class);
149  $loadDB->setFetchAllFields(true);
150  $loadDB->start($source, implode(',', $tables));
151  foreach ($loadDB->tableArray as $table => $v) {
152  if (isset(‪$GLOBALS['TCA'][$table])) {
153  $loadDB->additionalWhere[$table] = $this->‪getPageRepository()->‪enableFields($table);
154  }
155  }
156  $this->data = $loadDB->getFromDB();
157  reset($loadDB->itemArray);
158  $this->itemArray = $loadDB->itemArray;
159  }
160 
168  protected function ‪collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
169  {
170  $selectedCategories = array_unique(GeneralUtility::intExplode(',', $selectedCategories, true));
171 
172  // Loop on all selected tables
173  foreach ($tables as $table) {
174 
175  // Get the records for each selected category
176  $tableRecords = [];
177  $categoriesPerRecord = [];
178  foreach ($selectedCategories as $aCategory) {
179  try {
180  $collection = ‪CategoryCollection::load(
181  $aCategory,
182  true,
183  $table,
184  $relationField
185  );
186  if ($collection->count() > 0) {
187  // Add items to the collection of records for the current table
188  foreach ($collection as $item) {
189  $tableRecords[$item['uid']] = $item;
190  // Keep track of all categories a given item belongs to
191  if (!isset($categoriesPerRecord[$item['uid']])) {
192  $categoriesPerRecord[$item['uid']] = [];
193  }
194  $categoriesPerRecord[$item['uid']][] = $aCategory;
195  }
196  }
197  } catch (\‪Exception $e) {
198  $message = sprintf(
199  'Could not get records for category id %d. Error: %s (%d)',
200  $aCategory,
201  $e->getMessage(),
202  $e->getCode()
203  );
204  $this->‪getTimeTracker()->‪setTSlogMessage($message, 2);
205  }
206  }
207  // Store the resulting records into the itemArray and data results array
208  if (!empty($tableRecords)) {
209  $this->data[$table] = [];
210  foreach ($tableRecords as $record) {
211  $this->itemArray[] = [
212  'id' => $record['uid'],
213  'table' => $table
214  ];
215  // Add to the record the categories it belongs to
216  $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
217  $this->data[$table][$record['uid']] = $record;
218  }
219  }
220  }
221  }
222 
226  protected function ‪getTimeTracker()
227  {
228  return GeneralUtility::makeInstance(TimeTracker::class);
229  }
230 
234  protected function ‪getPageRepository(): ‪PageRepository
235  {
236  return ‪$GLOBALS['TSFE']->sys_page ?: GeneralUtility::makeInstance(PageRepository::class);
237  }
238 }
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\start
‪start($data, $table='')
Definition: ContentObjectRenderer.php:532
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\collectRecordsFromCategories
‪collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
Definition: RecordsContentObject.php:166
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:32
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:2
‪TYPO3\CMS\Frontend\Page\PageRepository\enableFields
‪string enableFields($table, $show_hidden=-1, $ignore_array=[], $noVersionPreview=false)
Definition: PageRepository.php:1591
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\setParent
‪setParent($data, $currentRecord)
Definition: ContentObjectRenderer.php:599
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\getTimeTracker
‪TimeTracker getTimeTracker()
Definition: RecordsContentObject.php:224
‪TYPO3\CMS\Frontend\Page\PageRepository\getLanguageOverlay
‪array null getLanguageOverlay(string $table, array $row)
Definition: PageRepository.php:495
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\$itemArray
‪array $itemArray
Definition: RecordsContentObject.php:32
‪TYPO3\CMS\Core\TimeTracker\TimeTracker\setTSlogMessage
‪setTSlogMessage($content, $num=0)
Definition: TimeTracker.php:193
‪TYPO3\CMS\Frontend\Page\PageRepository\versionOL
‪versionOL($table, &$row, $unsetMovePointers=false, $bypassEnableFieldsCheck=false)
Definition: PageRepository.php:1832
‪TYPO3\CMS\Frontend\Page\PageRepository
Definition: PageRepository.php:53
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\render
‪string render($conf=[])
Definition: RecordsContentObject.php:46
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:24
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false, $tableName='', $fieldName='')
Definition: CategoryCollection.php:71
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractContentObject.php:27
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\$data
‪array $data
Definition: RecordsContentObject.php:38
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\getPageRepository
‪PageRepository getPageRepository()
Definition: RecordsContentObject.php:232
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject
Definition: RecordsContentObject.php:27
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\cObjGetSingle
‪string cObjGetSingle($name, $conf, $TSkey='__')
Definition: ContentObjectRenderer.php:671
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:27
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:31