‪TYPO3CMS  10.4
RecordsContentObject.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
28 {
34  protected ‪$itemArray = [];
35 
41  protected ‪$data = [];
42 
49  public function ‪render($conf = [])
50  {
51  // Reset items and data
52  $this->itemArray = [];
53  $this->data = [];
54 
55  $theValue = '';
56  $originalRec = ‪$GLOBALS['TSFE']->currentRecord;
57  // If the currentRecord is set, we register, that this record has invoked this function.
58  // It's should not be allowed to do this again then!!
59  if ($originalRec) {
60  ++‪$GLOBALS['TSFE']->recordRegister[$originalRec];
61  }
62 
63  $tables = isset($conf['tables.']) ? $this->cObj->stdWrap($conf['tables'], $conf['tables.']) : $conf['tables'];
64  if ($tables) {
65  $tablesArray = array_unique(‪GeneralUtility::trimExplode(',', $tables, true));
66  // Add tables which have a configuration (note that this may create duplicate entries)
67  if (is_array($conf['conf.'])) {
68  foreach ($conf['conf.'] as $key => $value) {
69  if (substr($key, -1) !== '.' && !in_array($key, $tablesArray)) {
70  $tablesArray[] = $key;
71  }
72  }
73  }
74 
75  // Get the data, depending on collection method.
76  // Property "source" is considered more precise and thus takes precedence over "categories"
77  $source = isset($conf['source.']) ? $this->cObj->stdWrap($conf['source'], $conf['source.']) : $conf['source'];
78  $categories = isset($conf['categories.']) ? $this->cObj->stdWrap($conf['categories'], $conf['categories.']) : $conf['categories'];
79  if ($source) {
80  $this->collectRecordsFromSource($source, $tablesArray);
81  } elseif ($categories) {
82  $relationField = isset($conf['categories.']['relation.']) ? $this->cObj->stdWrap($conf['categories.']['relation'], $conf['categories.']['relation.']) : $conf['categories.']['relation'];
83  $this->‪collectRecordsFromCategories($categories, $tablesArray, $relationField);
84  }
85  $itemArrayCount = count($this->itemArray);
86  if ($itemArrayCount > 0) {
88  ‪$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
89  ‪$cObj->‪setParent($this->cObj->data, $this->cObj->currentRecord);
90  $this->cObj->currentRecordNumber = 0;
91  $this->cObj->currentRecordTotal = $itemArrayCount;
92  foreach ($this->itemArray as $val) {
93  $row = $this->data[$val['table']][$val['id']];
94  // Perform overlays if necessary (records coming from category collections are already overlaid)
95  if ($source) {
96  // Versioning preview
97  $this->‪getPageRepository()->‪versionOL($val['table'], $row);
98  // Language overlay
99  if (is_array($row)) {
100  $row = $this->‪getPageRepository()->‪getLanguageOverlay($val['table'], $row);
101  }
102  }
103  // Might be unset during the overlay process
104  if (is_array($row)) {
105  $dontCheckPid = isset($conf['dontCheckPid.']) ? $this->cObj->stdWrap($conf['dontCheckPid'], $conf['dontCheckPid.']) : $conf['dontCheckPid'];
106  if (!$dontCheckPid) {
107  $validPageId = $this->‪getPageRepository()->‪filterAccessiblePageIds([$row['pid']]);
108  $row = !empty($validPageId) ? $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 
147  protected function collectRecordsFromSource($source, array $tables)
148  {
150  $loadDB = GeneralUtility::makeInstance(RelationHandler::class);
151  $loadDB->setFetchAllFields(true);
152  $loadDB->start($source, implode(',', $tables));
153  foreach ($loadDB->tableArray as $table => $v) {
154  if (isset(‪$GLOBALS['TCA'][$table])) {
155  $loadDB->additionalWhere[$table] = $this->‪getPageRepository()->‪enableFields($table);
156  }
157  }
158  $this->data = $loadDB->getFromDB();
159  reset($loadDB->itemArray);
160  $this->itemArray = $loadDB->itemArray;
161  }
162 
170  protected function ‪collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
171  {
172  $selectedCategories = array_unique(‪GeneralUtility::intExplode(',', $selectedCategories, true));
173 
174  // Loop on all selected tables
175  foreach ($tables as $table) {
176 
177  // Get the records for each selected category
178  $tableRecords = [];
179  $categoriesPerRecord = [];
180  foreach ($selectedCategories as $aCategory) {
181  try {
182  $collection = ‪CategoryCollection::load(
183  $aCategory,
184  true,
185  $table,
186  $relationField
187  );
188  if ($collection->count() > 0) {
189  // Add items to the collection of records for the current table
190  foreach ($collection as $item) {
191  $tableRecords[$item['uid']] = $item;
192  // Keep track of all categories a given item belongs to
193  if (!isset($categoriesPerRecord[$item['uid']])) {
194  $categoriesPerRecord[$item['uid']] = [];
195  }
196  $categoriesPerRecord[$item['uid']][] = $aCategory;
197  }
198  }
199  } catch (\‪Exception $e) {
200  $message = sprintf(
201  'Could not get records for category id %d. Error: %s (%d)',
202  $aCategory,
203  $e->getMessage(),
204  $e->getCode()
205  );
206  $this->‪getTimeTracker()->‪setTSlogMessage($message, 2);
207  }
208  }
209  // Store the resulting records into the itemArray and data results array
210  if (!empty($tableRecords)) {
211  $this->data[$table] = [];
212  foreach ($tableRecords as $record) {
213  $this->itemArray[] = [
214  'id' => $record['uid'],
215  'table' => $table
216  ];
217  // Add to the record the categories it belongs to
218  $record['_categories'] = implode(',', $categoriesPerRecord[$record['uid']]);
219  $this->data[$table][$record['uid']] = $record;
220  }
221  }
222  }
223  }
224 
228  protected function ‪getTimeTracker()
229  {
230  return GeneralUtility::makeInstance(TimeTracker::class);
231  }
232 
236  protected function ‪getPageRepository(): ‪PageRepository
237  {
238  return ‪$GLOBALS['TSFE']->sys_page ?: GeneralUtility::makeInstance(PageRepository::class);
239  }
240 }
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\getLanguageOverlay
‪array null getLanguageOverlay(string $table, array $row)
Definition: PageRepository.php:356
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\versionOL
‪versionOL($table, &$row, $unsetMovePointers=false, $bypassEnableFieldsCheck=false)
Definition: PageRepository.php:1565
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\start
‪start($data, $table='')
Definition: ContentObjectRenderer.php:533
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\collectRecordsFromCategories
‪collectRecordsFromCategories($selectedCategories, array $tables, $relationField)
Definition: RecordsContentObject.php:168
‪TYPO3\CMS\Core\Database\RelationHandler
Definition: RelationHandler.php:35
‪TYPO3\CMS\Frontend\ContentObject
Definition: AbstractContentObject.php:16
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\setParent
‪setParent($data, $currentRecord)
Definition: ContentObjectRenderer.php:601
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\filterAccessiblePageIds
‪int[] filterAccessiblePageIds(array $pageIds, QueryRestrictionContainerInterface $restrictionContainer=null)
Definition: PageRepository.php:1177
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\getTimeTracker
‪TimeTracker getTimeTracker()
Definition: RecordsContentObject.php:226
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\$itemArray
‪array $itemArray
Definition: RecordsContentObject.php:33
‪TYPO3\CMS\Core\TimeTracker\TimeTracker\setTSlogMessage
‪setTSlogMessage($content, $num=0)
Definition: TimeTracker.php:215
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\render
‪string render($conf=[])
Definition: RecordsContentObject.php:47
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject
Definition: AbstractContentObject.php:25
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection\load
‪static TYPO3 CMS Core Collection CollectionInterface load($id, $fillItems=false, $tableName='', $fieldName='')
Definition: CategoryCollection.php:72
‪TYPO3\CMS\Frontend\ContentObject\AbstractContentObject\$cObj
‪ContentObjectRenderer $cObj
Definition: AbstractContentObject.php:28
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\$data
‪array $data
Definition: RecordsContentObject.php:39
‪TYPO3\CMS\Core\Domain\Repository\PageRepository\enableFields
‪string enableFields($table, $show_hidden=-1, $ignore_array=[])
Definition: PageRepository.php:1314
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject\getPageRepository
‪PageRepository getPageRepository()
Definition: RecordsContentObject.php:234
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:988
‪TYPO3\CMS\Core\Domain\Repository\PageRepository
Definition: PageRepository.php:52
‪TYPO3\CMS\Frontend\ContentObject\RecordsContentObject
Definition: RecordsContentObject.php:28
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\cObjGetSingle
‪string cObjGetSingle($name, $conf, $TSkey='__')
Definition: ContentObjectRenderer.php:673
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\TimeTracker\TimeTracker
Definition: TimeTracker.php:30
‪TYPO3\CMS\Frontend\Category\Collection\CategoryCollection
Definition: CategoryCollection.php:32