TYPO3 CMS  TYPO3_8-7
FileCollector.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 
26 
35 class FileCollector implements \Countable
36 {
42  protected $files = [];
43 
49  protected $fileRepository;
50 
57 
63  protected $resourceFactory;
64 
70  public function addFiles(array $fileUids = [])
71  {
72  if (!empty($fileUids)) {
73  foreach ($fileUids as $fileUid) {
74  try {
75  $this->addFileObject($this->getResourceFactory()->getFileObject($fileUid));
76  } catch (Exception $e) {
77  $this->getLogger()->warning(
78  'The file with uid "' . $fileUid
79  . '" could not be found and won\'t be included in frontend output',
80  ['exception' => $e]
81  );
82  }
83  }
84  }
85  }
86 
94  public function addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
95  {
96  if (is_object($GLOBALS['TSFE']) && is_object($GLOBALS['TSFE']->sys_page)) {
97  $fileReferences = $GLOBALS['TSFE']->sys_page->getFileReferences($relationTable, $relationField, $referenceRecord);
98  } else {
99  $fileReferences = $this->getFileRepository()->findByRelation($relationTable, $relationField, $referenceRecord['uid']);
100  }
101 
102  if (!empty($fileReferences)) {
103  $this->addFileObjects($fileReferences);
104  }
105  }
106 
112  public function addFileReferences(array $fileReferenceUids = [])
113  {
114  foreach ($fileReferenceUids as $fileReferenceUid) {
115  $fileObject = $this->getFileRepository()->findFileReferenceByUid($fileReferenceUid);
116  $this->addFileObject($fileObject);
117  }
118  }
119 
125  public function addFilesFromFileCollections(array $fileCollectionUids = [])
126  {
127  foreach ($fileCollectionUids as $fileCollectionUid) {
128  $this->addFilesFromFileCollection($fileCollectionUid);
129  }
130  }
131 
137  public function addFilesFromFileCollection($fileCollectionUid = null)
138  {
139  if (!empty($fileCollectionUid)) {
140  try {
141  $fileCollection = $this->getFileCollectionRepository()->findByUid($fileCollectionUid);
142 
143  if ($fileCollection instanceof \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection) {
144  $fileCollection->loadContents();
145  $files = $fileCollection->getItems();
146 
147  $this->addFileObjects($files);
148  }
149  } catch (Exception $e) {
150  $this->getLogger()->warning(
151  'The file-collection with uid "' . $fileCollectionUid
152  . '" could not be found or contents could not be loaded and won\'t be included in frontend output.',
153  ['exception' => $e]
154  );
155  }
156  }
157  }
158 
165  public function addFilesFromFolders(array $folderIdentifiers = [], $recursive = false)
166  {
167  foreach ($folderIdentifiers as $folderIdentifier) {
168  $this->addFilesFromFolder($folderIdentifier, $recursive);
169  }
170  }
171 
178  public function addFilesFromFolder($folderIdentifier, $recursive = false)
179  {
180  if ($folderIdentifier) {
181  try {
182  if (strpos($folderIdentifier, 't3://folder') === 0) {
183  // a t3://folder link to a folder in FAL
184  $linkService = GeneralUtility::makeInstance(LinkService::class);
185  $data = $linkService->resolveByStringRepresentation($folderIdentifier);
186  $folder = $data['folder'];
187  } else {
188  $folder = $this->getResourceFactory()->getFolderObjectFromCombinedIdentifier($folderIdentifier);
189  }
190  if ($folder instanceof Folder) {
191  $files = $folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive);
192  $this->addFileObjects(array_values($files));
193  }
194  } catch (Exception $e) {
195  $this->getLogger()->warning(
196  'The folder with identifier "' . $folderIdentifier
197  . '" could not be found and won\'t be included in frontend output',
198  ['exception' => $e]
199  );
200  }
201  }
202  }
203 
210  public function sort($sortingProperty = '', $sortingOrder = 'ascending')
211  {
212  if ($sortingProperty !== '' && count($this->files) > 1) {
213  @usort(
214  $this->files,
215  function (
216  FileInterface $a,
217  FileInterface $b
218  ) use ($sortingProperty) {
219  if ($a->hasProperty($sortingProperty) && $b->hasProperty($sortingProperty)) {
220  return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
221  }
222  return 0;
223  }
224  );
225 
226  switch (strtolower($sortingOrder)) {
227  case 'descending':
228  case 'desc':
229  $this->files = array_reverse($this->files);
230  break;
231  case 'random':
232  case 'rand':
233  shuffle($this->files);
234  break;
235  }
236  }
237  }
238 
244  public function addFileObject(FileInterface $file)
245  {
246  $this->files[] = $file;
247  }
248 
254  public function addFileObjects($files)
255  {
256  $this->files = array_merge($this->files, $files);
257  }
258 
264  public function getFiles()
265  {
266  return $this->files;
267  }
268 
272  public function count()
273  {
274  return count($this->files);
275  }
276 
280  protected function getLogger()
281  {
282  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
283  }
284 
288  protected function getResourceFactory()
289  {
290  if ($this->resourceFactory === null) {
291  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
292  }
293  return $this->resourceFactory;
294  }
295 
299  protected function getFileCollectionRepository()
300  {
301  if ($this->fileCollectionRepository === null) {
302  $this->fileCollectionRepository = GeneralUtility::makeInstance(FileCollectionRepository::class);
303  }
305  }
306 
310  protected function getFileRepository()
311  {
312  if ($this->fileRepository === null) {
313  $this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
314  }
315  return $this->fileRepository;
316  }
317 }
addFilesFromFolder($folderIdentifier, $recursive=false)
sort($sortingProperty='', $sortingOrder='ascending')
addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
addFilesFromFolders(array $folderIdentifiers=[], $recursive=false)
addFilesFromFileCollections(array $fileCollectionUids=[])
static makeInstance($className,... $constructorArguments)
const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:68
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
addFilesFromFileCollection($fileCollectionUid=null)
addFileReferences(array $fileReferenceUids=[])