TYPO3 CMS  TYPO3_7-6
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 
20 
25 {
31  protected $itemArray = [];
32 
38  protected $data = [];
39 
46  public function render($conf = [])
47  {
48  // Reset items and data
49  $this->itemArray = [];
50  $this->data = [];
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) {
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 
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 
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 = [];
181  $categoriesPerRecord = [];
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']] = [];
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] = [];
214  foreach ($tableRecords as $record) {
215  $this->itemArray[] = [
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 
232  protected function getTimeTracker()
233  {
234  return $GLOBALS['TT'];
235  }
236 }
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)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']