TYPO3 CMS  TYPO3_8-7
FilesContentObject.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 {
33  public function render($conf = [])
34  {
35  if (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) {
36  return '';
37  }
38  // Store the original "currentFile" within a variable so it can be re-applied later-on
39  $originalFileInContentObject = $this->cObj->getCurrentFile();
40 
41  $fileCollector = $this->findAndSortFiles($conf);
42  $fileObjects = $fileCollector->getFiles();
43  $availableFileObjectCount = count($fileObjects);
44 
45  // optionSplit applied to conf to allow different settings per file
46  $splitConf = GeneralUtility::makeInstance(TypoScriptService::class)
47  ->explodeConfigurationForOptionSplit($conf, $availableFileObjectCount);
48 
49  $start = 0;
50  if (!empty($conf['begin'])) {
51  $start = (int)$conf['begin'];
52  }
53  if (!empty($conf['begin.'])) {
54  $start = (int)$this->cObj->stdWrap($start, $conf['begin.']);
55  }
56  $start = MathUtility::forceIntegerInRange($start, 0, $availableFileObjectCount);
57 
58  $limit = $availableFileObjectCount;
59  if (!empty($conf['maxItems'])) {
60  $limit = (int)$conf['maxItems'];
61  }
62  if (!empty($conf['maxItems.'])) {
63  $limit = (int)$this->cObj->stdWrap($limit, $conf['maxItems.']);
64  }
65 
66  $end = MathUtility::forceIntegerInRange($start + $limit, $start, $availableFileObjectCount);
67 
68  if (isset($GLOBALS['TSFE'])) {
69  $GLOBALS['TSFE']->register['FILES_COUNT'] = min($limit, $availableFileObjectCount);
70  }
71  $fileObjectCounter = 0;
72  $keys = array_keys($fileObjects);
73 
74  $content = '';
75  for ($i = $start; $i < $end; $i++) {
76  $key = $keys[$i];
77  $fileObject = $fileObjects[$key];
78 
79  if (isset($GLOBALS['TSFE'])) {
80  $GLOBALS['TSFE']->register['FILE_NUM_CURRENT'] = $fileObjectCounter;
81  }
82  $this->cObj->setCurrentFile($fileObject);
83  $content .= $this->cObj->cObjGetSingle($splitConf[$key]['renderObj'], $splitConf[$key]['renderObj.']);
84  $fileObjectCounter++;
85  }
86 
87  // Reset current file within cObj to the original file after rendering output of FILES
88  // so e.g. stdWrap is not working on the last current file applied, thus avoiding side-effects
89  $this->cObj->setCurrentFile($originalFileInContentObject);
90 
91  return $this->cObj->stdWrap($content, $conf['stdWrap.']);
92  }
93 
101  protected function findAndSortFiles(array $conf)
102  {
103  $fileCollector = $this->getFileCollector();
104 
105  // Getting the files
106  if ($conf['references'] || $conf['references.']) {
107  /*
108  The TypoScript could look like this:
109  # all items related to the page.media field:
110  references {
111  table = pages
112  uid.data = page:uid
113  fieldName = media
114  }
115  # or: sys_file_references with uid 27:
116  references = 27
117  */
118  $referencesUidList = $this->cObj->stdWrapValue('references', $conf);
119  $referencesUids = GeneralUtility::intExplode(',', $referencesUidList, true);
120  $fileCollector->addFileReferences($referencesUids);
121 
122  if (!empty($conf['references.'])) {
123  $this->addFileReferences($conf, (array)$this->cObj->data, $fileCollector);
124  }
125  }
126 
127  if ($conf['files'] || $conf['files.']) {
128  /*
129  The TypoScript could look like this:
130  # with sys_file UIDs:
131  files = 12,14,15# using stdWrap:
132  files.field = some_field
133  */
134  $fileUids = GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('files', $conf), true);
135  $fileCollector->addFiles($fileUids);
136  }
137 
138  if ($conf['collections'] || $conf['collections.']) {
139  $collectionUids = GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('collections', $conf), true);
140  $fileCollector->addFilesFromFileCollections($collectionUids);
141  }
142 
143  if ($conf['folders'] || $conf['folders.']) {
144  $folderIdentifiers = GeneralUtility::trimExplode(',', $this->cObj->stdWrapValue('folders', $conf));
145  $fileCollector->addFilesFromFolders($folderIdentifiers, !empty($conf['folders.']['recursive']));
146  }
147 
148  // Enable sorting for multiple fileObjects
149  $sortingProperty = '';
150  if ($conf['sorting'] || $conf['sorting.']) {
151  $sortingProperty = $this->cObj->stdWrapValue('sorting', $conf);
152  }
153  if ($sortingProperty !== '') {
154  $sortingDirection = isset($conf['sorting.']['direction']) ? $conf['sorting.']['direction'] : '';
155  if (isset($conf['sorting.']['direction.'])) {
156  $sortingDirection = $this->cObj->stdWrap($sortingDirection, $conf['sorting.']['direction.']);
157  }
158  $fileCollector->sort($sortingProperty, $sortingDirection);
159  }
160 
161  return $fileCollector;
162  }
163 
172  protected function addFileReferences(array $configuration, array $element, FileCollector $fileCollector)
173  {
174 
175  // It's important that this always stays "fieldName" and not be renamed to "field" as it would otherwise collide with the stdWrap key of that name
176  $referencesFieldName = $this->cObj->stdWrapValue('fieldName', $configuration['references.']);
177 
178  // If no reference fieldName is set, there's nothing to do
179  if (empty($referencesFieldName)) {
180  return;
181  }
182 
183  $currentId = !empty($element['uid']) ? $element['uid'] : 0;
184  $tableName = $this->cObj->getCurrentTable();
185 
186  // Fetch the references of the default element
187  $referencesForeignTable = $this->cObj->stdWrapValue('table', $configuration['references.'], $tableName);
188  $referencesForeignUid = $this->cObj->stdWrapValue('uid', $configuration['references.'], $currentId);
189 
190  $pageRepository = $this->getPageRepository();
191  // Fetch element if definition has been modified via TypoScript
192  if ($referencesForeignTable !== $tableName || $referencesForeignUid !== $currentId) {
193  $element = $pageRepository->getRawRecord(
194  $referencesForeignTable,
195  $referencesForeignUid,
196  '*',
197  false
198  );
199 
200  $pageRepository->versionOL($referencesForeignTable, $element, true);
201  if ($referencesForeignTable === 'pages') {
202  $element = $pageRepository->getPageOverlay($element);
203  } else {
204  $element = $pageRepository->getRecordOverlay(
205  $referencesForeignTable,
206  $element,
207  $GLOBALS['TSFE']->sys_language_content,
208  $GLOBALS['TSFE']->sys_language_contentOL
209  );
210  }
211  }
212 
213  if (is_array($element)) {
214  $fileCollector->addFilesFromRelation($referencesForeignTable, $referencesFieldName, $element);
215  }
216  }
217 
221  protected function getPageRepository()
222  {
223  return $GLOBALS['TSFE']->sys_page;
224  }
225 
229  protected function getFileCollector()
230  {
231  return GeneralUtility::makeInstance(FileCollector::class);
232  }
233 }
addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
static makeInstance($className,... $constructorArguments)
addFileReferences(array $configuration, array $element, FileCollector $fileCollector)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']