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