TYPO3  7.6
FilesContentObject.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Frontend\ContentObject;
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 = array())
33  {
34  if (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) {
35  return '';
36  }
37 
38  $fileCollector = $this->findAndSortFiles($conf);
39  $fileObjects = $fileCollector->getFiles();
40  $availableFileObjectCount = count($fileObjects);
41 
42  // optionSplit applied to conf to allow different settings per file
43  $splitConf = $GLOBALS['TSFE']->tmpl->splitConfArray($conf, $availableFileObjectCount);
44 
45  $start = 0;
46  if (!empty($conf['begin'])) {
47  $start = (int)$conf['begin'];
48  }
49  if (!empty($conf['begin.'])) {
50  $start = (int)$this->cObj->stdWrap($start, $conf['begin.']);
51  }
52  $start = MathUtility::forceIntegerInRange($start, 0, $availableFileObjectCount);
53 
54  $limit = $availableFileObjectCount;
55  if (!empty($conf['maxItems'])) {
56  $limit = (int)$conf['maxItems'];
57  }
58  if (!empty($conf['maxItems.'])) {
59  $limit = (int)$this->cObj->stdWrap($limit, $conf['maxItems.']);
60  }
61 
62  $end = MathUtility::forceIntegerInRange($start + $limit, $start, $availableFileObjectCount);
63 
64  $GLOBALS['TSFE']->register['FILES_COUNT'] = min($limit, $availableFileObjectCount);
65  $fileObjectCounter = 0;
66  $keys = array_keys($fileObjects);
67 
68  $content = '';
69  for ($i = $start; $i < $end; $i++) {
70  $key = $keys[$i];
71  $fileObject = $fileObjects[$key];
72 
73  $GLOBALS['TSFE']->register['FILE_NUM_CURRENT'] = $fileObjectCounter;
74  $this->cObj->setCurrentFile($fileObject);
75  $content .= $this->cObj->cObjGetSingle($splitConf[$key]['renderObj'], $splitConf[$key]['renderObj.']);
76  $fileObjectCounter++;
77  }
78 
79  return $this->cObj->stdWrap($content, $conf['stdWrap.']);
80  }
81 
89  protected function findAndSortFiles(array $conf)
90  {
91  $fileCollector = $this->getFileCollector();
92 
93  // Getting the files
94  if ($conf['references'] || $conf['references.']) {
95  /*
96  The TypoScript could look like this:
97  # all items related to the page.media field:
98  references {
99  table = pages
100  uid.data = page:uid
101  fieldName = media
102  }
103  # or: sys_file_references with uid 27:
104  references = 27
105  */
106  $referencesUidList = $this->cObj->stdWrapValue('references', $conf);
107  $referencesUids = GeneralUtility::intExplode(',', $referencesUidList, true);
108  $fileCollector->addFileReferences($referencesUids);
109 
110  if (!empty($conf['references.'])) {
111  $this->addFileReferences($conf, (array)$this->cObj->data, $fileCollector);
112  }
113  }
114 
115  if ($conf['files'] || $conf['files.']) {
116  /*
117  The TypoScript could look like this:
118  # with sys_file UIDs:
119  files = 12,14,15# using stdWrap:
120  files.field = some_field
121  */
122  $fileUids = GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('files', $conf), true);
123  $fileCollector->addFiles($fileUids);
124  }
125 
126  if ($conf['collections'] || $conf['collections.']) {
127  $collectionUids = GeneralUtility::intExplode(',', $this->cObj->stdWrapValue('collections', $conf), true);
128  $fileCollector->addFilesFromFileCollections($collectionUids);
129  }
130 
131  if ($conf['folders'] || $conf['folders.']) {
132  $folderIdentifiers = GeneralUtility::trimExplode(',', $this->cObj->stdWrapValue('folders', $conf));
133  $fileCollector->addFilesFromFolders($folderIdentifiers, !empty($conf['folders.']['recursive']));
134  }
135 
136  // Enable sorting for multiple fileObjects
137  $sortingProperty = '';
138  if ($conf['sorting'] || $conf['sorting.']) {
139  $sortingProperty = $this->cObj->stdWrapValue('sorting', $conf);
140  }
141  if ($sortingProperty !== '') {
142  $sortingDirection = isset($conf['sorting.']['direction']) ? $conf['sorting.']['direction'] : '';
143  if (isset($conf['sorting.']['direction.'])) {
144  $sortingDirection = $this->cObj->stdWrap($sortingDirection, $conf['sorting.']['direction.']);
145  }
146  $fileCollector->sort($sortingProperty, $sortingDirection);
147  }
148 
149  return $fileCollector;
150  }
151 
160  protected function addFileReferences(array $configuration, array $element, FileCollector $fileCollector)
161  {
162 
163  // 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
164  $referencesFieldName = $this->cObj->stdWrapValue('fieldName', $configuration['references.']);
165 
166  // If no reference fieldName is set, there's nothing to do
167  if (empty($referencesFieldName)) {
168  return;
169  }
170 
171  $currentId = !empty($element['uid']) ? $element['uid'] : 0;
172  $tableName = $this->cObj->getCurrentTable();
173 
174  // Fetch the references of the default element
175  $referencesForeignTable = $this->cObj->stdWrapValue('table', $configuration['references.'], $tableName);
176  $referencesForeignUid = $this->cObj->stdWrapValue('uid', $configuration['references.'], $currentId);
177 
178  $pageRepository = $this->getPageRepository();
179  // Fetch element if definition has been modified via TypoScript
180  if ($referencesForeignTable !== $tableName || $referencesForeignUid !== $currentId) {
181  $element = $pageRepository->getRawRecord(
182  $referencesForeignTable,
183  $referencesForeignUid,
184  '*',
185  false
186  );
187 
188  $pageRepository->versionOL($referencesForeignTable, $element, true);
189  if ($referencesForeignTable === 'pages') {
190  $element = $pageRepository->getPageOverlay($element);
191  } else {
192  $element = $pageRepository->getRecordOverlay(
193  $referencesForeignTable,
194  $element,
195  $GLOBALS['TSFE']->sys_language_content,
196  $GLOBALS['TSFE']->sys_language_contentOL
197  );
198  }
199  }
200 
201  if (is_array($element)) {
202  $fileCollector->addFilesFromRelation($referencesForeignTable, $referencesFieldName, $element);
203  }
204  }
205 
209  protected function getPageRepository()
210  {
211  return $GLOBALS['TSFE']->sys_page;
212  }
213 
217  protected function getFileCollector()
218  {
219  return GeneralUtility::makeInstance(FileCollector::class);
220  }
221 }