‪TYPO3CMS  ‪main
FileCollector.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Log\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
32 
46 class ‪FileCollector implements \Countable, LoggerAwareInterface
47 {
48  use LoggerAwareTrait;
49 
55  protected array ‪$files = [];
56 
57  public function ‪__construct(
58  protected readonly ‪ResourceFactory $resourceFactory,
59  protected readonly ‪FileCollectionRepository $fileCollectionRepository,
60  protected readonly ‪FileRepository $fileRepository,
61  ) {
62  }
63 
67  public function ‪addFiles(array $fileUids = []): void
68  {
69  foreach ($fileUids as $fileUid) {
70  try {
71  $this->‪addFileObject($this->resourceFactory->getFileObject($fileUid));
72  } catch (‪Exception $e) {
73  $this->logger->warning(
74  'The file with uid "' . $fileUid
75  . '" could not be found and won\'t be included in frontend output',
76  ['exception' => $e]
77  );
78  }
79  }
80  }
81 
89  public function ‪addFilesFromRelation(string $relationTable, string $relationField, array $referenceRecord): void
90  {
91  $fileReferences = $this->‪getFileReferences($relationTable, $relationField, $referenceRecord);
92  if (!empty($fileReferences)) {
93  $this->‪addFileObjects($fileReferences);
94  }
95  }
96 
100  public function ‪addFileReferences(array $fileReferenceUids = []): void
101  {
102  foreach ($fileReferenceUids as $fileReferenceUid) {
103  $fileObject = $this->resourceFactory->getFileReferenceObject((int)$fileReferenceUid);
104  if (!$fileObject instanceof ‪FileInterface) {
105  continue;
106  }
107  $this->‪addFileObject($fileObject);
108  }
109  }
110 
114  public function ‪addFilesFromFileCollections(array $fileCollectionUids = []): void
115  {
116  foreach ($fileCollectionUids as $fileCollectionUid) {
117  $this->‪addFilesFromFileCollection((int)$fileCollectionUid);
118  }
119  }
120 
124  public function ‪addFilesFromFileCollection(int $fileCollectionUid): void
125  {
126  if ($fileCollectionUid <= 0) {
127  return;
128  }
129  try {
130  $fileCollection = $this->fileCollectionRepository->findByUid($fileCollectionUid);
131  if ($fileCollection instanceof ‪AbstractFileCollection) {
132  $fileCollection->loadContents();
133  ‪$files = $fileCollection->getItems();
134  $this->‪addFileObjects($files);
135  }
136  } catch (‪Exception $e) {
137  $this->logger->warning(
138  'The file-collection with uid "' . $fileCollectionUid
139  . '" could not be found or contents could not be loaded and won\'t be included in frontend output.',
140  ['exception' => $e]
141  );
142  }
143  }
144 
151  public function ‪addFilesFromFolders(array $folderIdentifiers, bool $recursive = false): void
152  {
153  foreach ($folderIdentifiers as $folderIdentifier) {
154  $this->‪addFilesFromFolder($folderIdentifier, $recursive);
155  }
156  }
157 
164  public function ‪addFilesFromFolder(string $folderIdentifier, bool $recursive = false): void
165  {
166  if ($folderIdentifier) {
167  try {
168  if (str_starts_with($folderIdentifier, 't3://folder')) {
169  // a t3://folder link to a folder in FAL
170  $linkService = GeneralUtility::makeInstance(LinkService::class);
171  $data = $linkService->resolveByStringRepresentation($folderIdentifier);
172  $folder = $data['folder'];
173  } else {
174  $folder = $this->resourceFactory->getFolderObjectFromCombinedIdentifier($folderIdentifier);
175  }
176  if ($folder instanceof ‪Folder) {
177  ‪$files = $folder->getFiles(0, 0, ‪Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive);
178  $this->‪addFileObjects(array_values(‪$files));
179  }
180  } catch (‪Exception $e) {
181  $this->logger->warning('The folder with identifier "{folder}" could not be found and won\'t be included in frontend output', [
182  'folder' => $folderIdentifier,
183  'exception' => $e,
184  ]);
185  }
186  }
187  }
188 
195  public function ‪sort(string $sortingProperty = '', string $sortingOrder = 'ascending'): void
196  {
197  if ($sortingProperty !== '' && ‪count($this->files) > 1) {
198  @usort(
199  $this->files,
200  static function (
201  ‪FileInterface $a,
203  ) use ($sortingProperty) {
204  if ($a->‪hasProperty($sortingProperty) && $b->‪hasProperty($sortingProperty)) {
205  return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
206  }
207  return 0;
208  }
209  );
210 
211  switch (strtolower($sortingOrder)) {
212  case 'descending':
213  case 'desc':
214  $this->files = array_reverse($this->files);
215  break;
216  case 'random':
217  case 'rand':
218  shuffle($this->files);
219  break;
220  }
221  }
222  }
223 
227  public function ‪addFileObject(‪FileInterface $file): void
228  {
229  $this->files[] = $file;
230  }
231 
237  public function ‪addFileObjects(array ‪$files): void
238  {
239  $this->files = array_merge($this->files, ‪$files);
240  }
241 
245  public function ‪getFiles(): array
246  {
247  return ‪$this->files;
248  }
249 
250  public function ‪count(): int
251  {
252  return ‪count($this->files);
253  }
254 
263  protected function ‪getFileReferences(string $tableName, string $fieldName, array $element): array
264  {
265  $currentId = !empty($element['uid']) ? (int)$element['uid'] : 0;
266 
267  // Fetch the references of the default element
268  try {
269  $references = $this->fileRepository->findByRelation($tableName, $fieldName, $currentId);
270  } catch (‪FileDoesNotExistException $e) {
275  return [];
276  } catch (\InvalidArgumentException $e) {
281  $this->logger->error('{exception_message}: table: {table}, field: {field}, currentId: {current_id}', [
282  'table' => $tableName,
283  'field' => $fieldName,
284  'currentId' => $currentId,
285  'exception' => $e,
286  ]);
287  return [];
288  }
289 
290  $localizedId = null;
291  if (isset($element['_LOCALIZED_UID'])) {
292  $localizedId = $element['_LOCALIZED_UID'];
293  } elseif (isset($element['_PAGES_OVERLAY_UID'])) {
294  $localizedId = $element['_PAGES_OVERLAY_UID'];
295  }
296 
297  $isTableLocalizable = (
298  !empty(‪$GLOBALS['TCA'][$tableName]['ctrl']['languageField'])
299  && !empty(‪$GLOBALS['TCA'][$tableName]['ctrl']['transOrigPointerField'])
300  );
301  if ($isTableLocalizable && $localizedId !== null) {
302  $localizedReferences = $this->fileRepository->findByRelation($tableName, $fieldName, (int)$localizedId);
303  $references = $localizedReferences;
304  }
305 
306  return $references;
307  }
308 }
‪TYPO3\CMS\Frontend\Resource\FileCollector\__construct
‪__construct(protected readonly ResourceFactory $resourceFactory, protected readonly FileCollectionRepository $fileCollectionRepository, protected readonly FileRepository $fileRepository,)
Definition: FileCollector.php:57
‪TYPO3\CMS\Core\Resource\FileCollectionRepository
Definition: FileCollectionRepository.php:30
‪TYPO3\CMS\Frontend\Resource\FileCollector\sort
‪sort(string $sortingProperty='', string $sortingOrder='ascending')
Definition: FileCollector.php:195
‪TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection
Definition: AbstractFileCollection.php:28
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:24
‪TYPO3\CMS\Frontend\Resource
Definition: FileCollector.php:18
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFolders
‪addFilesFromFolders(array $folderIdentifiers, bool $recursive=false)
Definition: FileCollector.php:151
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFiles
‪addFiles(array $fileUids=[])
Definition: FileCollector.php:67
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFileCollection
‪addFilesFromFileCollection(int $fileCollectionUid)
Definition: FileCollector.php:124
‪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\Core\Resource\FileInterface\hasProperty
‪hasProperty(string $key)
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFileReferences
‪getFileReferences(string $tableName, string $fieldName, array $element)
Definition: FileCollector.php:263
‪TYPO3\CMS\Frontend\Resource\FileCollector
Definition: FileCollector.php:47
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileReferences
‪addFileReferences(array $fileReferenceUids=[])
Definition: FileCollector.php:100
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFolder
‪addFilesFromFolder(string $folderIdentifier, bool $recursive=false)
Definition: FileCollector.php:164
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:38
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileObject
‪addFileObject(FileInterface $file)
Definition: FileCollector.php:227
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromRelation
‪addFilesFromRelation(string $relationTable, string $relationField, array $referenceRecord)
Definition: FileCollector.php:89
‪TYPO3\CMS\Frontend\Resource\FileCollector\$files
‪array $files
Definition: FileCollector.php:55
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFilesFromFileCollections
‪addFilesFromFileCollections(array $fileCollectionUids=[])
Definition: FileCollector.php:114
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Resource\FileCollector\getFiles
‪getFiles()
Definition: FileCollector.php:245
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Frontend\Resource\FileCollector\count
‪count()
Definition: FileCollector.php:250
‪TYPO3\CMS\Frontend\Resource\FileCollector\addFileObjects
‪addFileObjects(array $files)
Definition: FileCollector.php:237
‪TYPO3\CMS\Core\Resource\Exception
Definition: AbstractFileOperationException.php:16