‪TYPO3CMS  10.4
FileCollector.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
30 
40 class ‪FileCollector implements \Countable, LoggerAwareInterface
41 {
42  use LoggerAwareTrait;
43 
49  protected ‪$files = [];
50 
56  protected ‪$fileRepository;
57 
64 
70  protected ‪$resourceFactory;
71 
77  public function ‪addFiles(array $fileUids = [])
78  {
79  if (!empty($fileUids)) {
80  foreach ($fileUids as $fileUid) {
81  try {
82  $this->‪addFileObject($this->‪getResourceFactory()->getFileObject($fileUid));
83  } catch (‪Exception $e) {
84  $this->logger->warning(
85  'The file with uid "' . $fileUid
86  . '" could not be found and won\'t be included in frontend output',
87  ['exception' => $e]
88  );
89  }
90  }
91  }
92  }
93 
101  public function ‪addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
102  {
103  $fileReferences = $this->‪getFileReferences($relationTable, $relationField, $referenceRecord);
104  if (!empty($fileReferences)) {
105  $this->‪addFileObjects($fileReferences);
106  }
107  }
108 
114  public function ‪addFileReferences(array $fileReferenceUids = [])
115  {
116  foreach ($fileReferenceUids as $fileReferenceUid) {
117  $fileObject = $this->‪getFileRepository()->‪findFileReferenceByUid($fileReferenceUid);
118  if (!$fileObject instanceof ‪FileInterface) {
119  continue;
120  }
121  $this->‪addFileObject($fileObject);
122  }
123  }
124 
130  public function ‪addFilesFromFileCollections(array $fileCollectionUids = [])
131  {
132  foreach ($fileCollectionUids as $fileCollectionUid) {
133  $this->‪addFilesFromFileCollection($fileCollectionUid);
134  }
135  }
136 
142  public function ‪addFilesFromFileCollection($fileCollectionUid = null)
143  {
144  if (!empty($fileCollectionUid)) {
145  try {
146  $fileCollection = $this->‪getFileCollectionRepository()->‪findByUid($fileCollectionUid);
147 
148  if ($fileCollection instanceof ‪AbstractFileCollection) {
149  $fileCollection->loadContents();
150  ‪$files = $fileCollection->getItems();
151 
153  }
154  } catch (‪Exception $e) {
155  $this->logger->warning(
156  'The file-collection with uid "' . $fileCollectionUid
157  . '" could not be found or contents could not be loaded and won\'t be included in frontend output.',
158  ['exception' => $e]
159  );
160  }
161  }
162  }
163 
170  public function ‪addFilesFromFolders(array $folderIdentifiers = [], $recursive = false)
171  {
172  foreach ($folderIdentifiers as $folderIdentifier) {
173  $this->‪addFilesFromFolder($folderIdentifier, $recursive);
174  }
175  }
176 
183  public function ‪addFilesFromFolder($folderIdentifier, $recursive = false)
184  {
185  if ($folderIdentifier) {
186  try {
187  if (strpos($folderIdentifier, 't3://folder') === 0) {
188  // a t3://folder link to a folder in FAL
189  $linkService = GeneralUtility::makeInstance(LinkService::class);
190  $data = $linkService->resolveByStringRepresentation($folderIdentifier);
191  $folder = $data['folder'];
192  } else {
193  $folder = $this->‪getResourceFactory()->‪getFolderObjectFromCombinedIdentifier($folderIdentifier);
194  }
195  if ($folder instanceof ‪Folder) {
196  ‪$files = $folder->getFiles(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive);
197  $this->‪addFileObjects(array_values(‪$files));
198  }
199  } catch (‪Exception $e) {
200  $this->logger->warning(
201  'The folder with identifier "' . $folderIdentifier
202  . '" could not be found and won\'t be included in frontend output',
203  ['exception' => $e]
204  );
205  }
206  }
207  }
208 
215  public function ‪sort($sortingProperty = '', $sortingOrder = 'ascending')
216  {
217  if ($sortingProperty !== '' && ‪count($this->files) > 1) {
218  @usort(
219  $this->files,
220  function (
221  ‪FileInterface $a,
223  ) use ($sortingProperty) {
224  if ($a->‪hasProperty($sortingProperty) && $b->‪hasProperty($sortingProperty)) {
225  return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
226  }
227  return 0;
228  }
229  );
230 
231  switch (strtolower($sortingOrder)) {
232  case 'descending':
233  case 'desc':
234  $this->files = array_reverse($this->files);
235  break;
236  case 'random':
237  case 'rand':
238  shuffle($this->files);
239  break;
240  }
241  }
242  }
243 
249  public function ‪addFileObject(‪FileInterface $file)
250  {
251  $this->files[] = $file;
252  }
253 
259  public function ‪addFileObjects(‪$files)
260  {
261  $this->files = array_merge($this->files, ‪$files);
262  }
263 
269  public function ‪getFiles()
270  {
271  return ‪$this->files;
272  }
273 
277  public function ‪count()
278  {
279  return ‪count($this->files);
280  }
281 
291  protected function ‪getFileReferences($tableName, $fieldName, array $element): array
292  {
294  ‪$fileRepository = GeneralUtility::makeInstance(FileRepository::class);
295  $currentId = !empty($element['uid']) ? $element['uid'] : 0;
296 
297  // Fetch the references of the default element
298  try {
299  $references = ‪$fileRepository->‪findByRelation($tableName, $fieldName, $currentId);
300  } catch (FileDoesNotExistException $e) {
305  return [];
306  } catch (\InvalidArgumentException $e) {
311  $logMessage = $e->getMessage() . ' (table: "' . $tableName . '", fieldName: "' . $fieldName . '", currentId: ' . $currentId . ')';
312  $this->logger->error($logMessage, ['exception' => $e]);
313  return [];
314  }
315 
316  $localizedId = null;
317  if (isset($element['_LOCALIZED_UID'])) {
318  $localizedId = $element['_LOCALIZED_UID'];
319  } elseif (isset($element['_PAGES_OVERLAY_UID'])) {
320  $localizedId = $element['_PAGES_OVERLAY_UID'];
321  }
322 
323  $isTableLocalizable = (
324  !empty(‪$GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
325  && !empty(‪$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
326  );
327  if ($isTableLocalizable && $localizedId !== null) {
328  $localizedReferences = ‪$fileRepository->‪findByRelation($tableName, $fieldName, $localizedId);
329  $references = $localizedReferences;
330  }
331 
332  return $references;
333  }
334 
338  protected function ‪getResourceFactory()
339  {
340  if ($this->resourceFactory === null) {
341  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
342  }
344  }
345 
349  protected function ‪getFileCollectionRepository()
350  {
351  if ($this->fileCollectionRepository === null) {
352  $this->fileCollectionRepository = GeneralUtility::makeInstance(FileCollectionRepository::class);
353  }
355  }
356 
360  protected function ‪getFileRepository()
361  {
362  if ($this->fileRepository === null) {
363  $this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
364  }
366  }
367 }
‪TYPO3\CMS\Core\Resource\FileCollectionRepository
Definition: FileCollectionRepository.php:26
‪TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection
Definition: AbstractFileCollection.php:27
‪TYPO3\CMS\Frontend\Resource\FileCollector\$resourceFactory
‪TYPO3 CMS Core Resource ResourceFactory $resourceFactory
Definition: FileCollector.php:66
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\FileInterface\hasProperty
‪bool hasProperty($key)
‪TYPO3\CMS\Frontend\Resource
Definition: FileCollector.php:16
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFiles
‪addFiles(array $fileUids=[])
Definition: FileCollector.php:73
‪TYPO3\CMS\Core\Resource\ResourceFactory\getFolderObjectFromCombinedIdentifier
‪Folder getFolderObjectFromCombinedIdentifier($identifier)
Definition: ResourceFactory.php:530
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFileCollectionRepository
‪FileCollectionRepository getFileCollectionRepository()
Definition: FileCollector.php:345
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:69
‪TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
Definition: FileDoesNotExistException.php:22
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFiles
‪array getFiles()
Definition: FileCollector.php:265
‪TYPO3\CMS\Frontend\Resource\FileCollector\$fileCollectionRepository
‪TYPO3 CMS Core Resource FileCollectionRepository $fileCollectionRepository
Definition: FileCollector.php:60
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileObjects
‪addFileObjects($files)
Definition: FileCollector.php:255
‪TYPO3\CMS\Core\Resource\FileRepository\findByRelation
‪array findByRelation($tableName, $fieldName, $uid)
Definition: FileRepository.php:69
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:41
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileReferences
‪addFileReferences(array $fileReferenceUids=[])
Definition: FileCollector.php:110
‪TYPO3\CMS\Frontend\Resource\FileCollector\$fileRepository
‪TYPO3 CMS Core Resource FileRepository $fileRepository
Definition: FileCollector.php:54
‪TYPO3\CMS\Frontend\Resource\FileCollector\count
‪int count()
Definition: FileCollector.php:273
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFileCollection
‪addFilesFromFileCollection($fileCollectionUid=null)
Definition: FileCollector.php:138
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFolder
‪addFilesFromFolder($folderIdentifier, $recursive=false)
Definition: FileCollector.php:179
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:33
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFileRepository
‪FileRepository getFileRepository()
Definition: FileCollector.php:356
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileObject
‪addFileObject(FileInterface $file)
Definition: FileCollector.php:245
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Frontend\Resource\FileCollector\$files
‪array $files
Definition: FileCollector.php:48
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromRelation
‪addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
Definition: FileCollector.php:97
‪TYPO3\CMS\Frontend\Resource\FileCollector\sort
‪sort($sortingProperty='', $sortingOrder='ascending')
Definition: FileCollector.php:211
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Frontend\Resource\FileCollector\getResourceFactory
‪ResourceFactory getResourceFactory()
Definition: FileCollector.php:334
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFolders
‪addFilesFromFolders(array $folderIdentifiers=[], $recursive=false)
Definition: FileCollector.php:166
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFileCollections
‪addFilesFromFileCollections(array $fileCollectionUids=[])
Definition: FileCollector.php:126
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Resource\FileRepository\findFileReferenceByUid
‪FileReference bool findFileReferenceByUid($uid)
Definition: FileRepository.php:145
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFileReferences
‪array getFileReferences($tableName, $fieldName, array $element)
Definition: FileCollector.php:287
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\FileCollectionRepository\findByUid
‪Collection AbstractFileCollection null findByUid($uid)
Definition: FileCollectionRepository.php:42
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:16