TYPO3 CMS  TYPO3_7-6
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 
25 
34 class FileCollector implements \Countable
35 {
41  protected $files = [];
42 
48  protected $fileRepository;
49 
56 
62  protected $resourceFactory;
63 
69  public function addFiles(array $fileUids = [])
70  {
71  if (!empty($fileUids)) {
72  foreach ($fileUids as $fileUid) {
73  try {
74  $this->addFileObject($this->getResourceFactory()->getFileObject($fileUid));
75  } catch (Exception $e) {
76  $this->getLogger()->warning(
77  'The file with uid "' . $fileUid
78  . '" could not be found and won\'t be included in frontend output',
79  ['exception' => $e]
80  );
81  }
82  }
83  }
84  }
85 
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 
113  public function addFileReferences(array $fileReferenceUids = [])
114  {
115  foreach ($fileReferenceUids as $fileReferenceUid) {
116  $fileObject = $this->getFileRepository()->findFileReferenceByUid($fileReferenceUid);
117  $this->addFileObject($fileObject);
118  }
119  }
120 
127  public function addFilesFromFileCollections(array $fileCollectionUids = [])
128  {
129  foreach ($fileCollectionUids as $fileCollectionUid) {
130  $this->addFilesFromFileCollection($fileCollectionUid);
131  }
132  }
133 
140  public function addFilesFromFileCollection($fileCollectionUid = null)
141  {
142  if (!empty($fileCollectionUid)) {
143  try {
144  $fileCollection = $this->getFileCollectionRepository()->findByUid($fileCollectionUid);
145 
146  if ($fileCollection instanceof \TYPO3\CMS\Core\Resource\Collection\AbstractFileCollection) {
147  $fileCollection->loadContents();
148  $files = $fileCollection->getItems();
149 
150  $this->addFileObjects($files);
151  }
152  } catch (Exception $e) {
153  $this->getLogger()->warning(
154  'The file-collection with uid "' . $fileCollectionUid
155  . '" could not be found or contents could not be loaded and won\'t be included in frontend output.',
156  ['exception' => $e]
157  );
158  }
159  }
160  }
161 
169  public function addFilesFromFolders(array $folderIdentifiers = [], $recursive = false)
170  {
171  foreach ($folderIdentifiers as $folderIdentifier) {
172  $this->addFilesFromFolder($folderIdentifier, $recursive);
173  }
174  }
175 
182  public function addFilesFromFolder($folderIdentifier, $recursive = false)
183  {
184  if ($folderIdentifier) {
185  try {
186  $folder = $this->getResourceFactory()->getFolderObjectFromCombinedIdentifier($folderIdentifier);
187  if ($folder instanceof Folder) {
188  $files = $folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive);
189  $this->addFileObjects(array_values($files));
190  }
191  } catch (Exception $e) {
192  $this->getLogger()->warning(
193  'The folder with identifier "' . $folderIdentifier
194  . '" could not be found and won\'t be included in frontend output',
195  ['exception' => $e]
196  );
197  }
198  }
199  }
200 
208  public function sort($sortingProperty = '', $sortingOrder = 'ascending')
209  {
210  if ($sortingProperty !== '' && count($this->files) > 1) {
211  @usort(
212  $this->files,
213  function (
215  FileInterface $b
216  ) use ($sortingProperty) {
217  if ($a->hasProperty($sortingProperty) && $b->hasProperty($sortingProperty)) {
218  return strnatcasecmp($a->getProperty($sortingProperty), $b->getProperty($sortingProperty));
219  } else {
220  return 0;
221  }
222  }
223  );
224 
225  switch (strtolower($sortingOrder)) {
226  case 'descending':
227  case 'desc':
228  $this->files = array_reverse($this->files);
229  break;
230  case 'random':
231  case 'rand':
232  shuffle($this->files);
233  break;
234  }
235  }
236  }
237 
244  public function addFileObject(FileInterface $file)
245  {
246  $this->files[] = $file;
247  }
248 
255  public function addFileObjects($files)
256  {
257  $this->files = array_merge($this->files, $files);
258  }
259 
265  public function getFiles()
266  {
267  return $this->files;
268  }
269 
273  public function count()
274  {
275  return count($this->files);
276  }
277 
281  protected function getLogger()
282  {
283  return GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
284  }
285 
289  protected function getResourceFactory()
290  {
291  if ($this->resourceFactory === null) {
292  $this->resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
293  }
294  return $this->resourceFactory;
295  }
296 
300  protected function getFileCollectionRepository()
301  {
302  if ($this->fileCollectionRepository === null) {
303  $this->fileCollectionRepository = GeneralUtility::makeInstance(FileCollectionRepository::class);
304  }
306  }
307 
311  protected function getFileRepository()
312  {
313  if ($this->fileRepository === null) {
314  $this->fileRepository = GeneralUtility::makeInstance(FileRepository::class);
315  }
316  return $this->fileRepository;
317  }
318 }
addFilesFromFolder($folderIdentifier, $recursive=false)
sort($sortingProperty='', $sortingOrder='ascending')
addFilesFromRelation($relationTable, $relationField, array $referenceRecord)
addFilesFromFolders(array $folderIdentifiers=[], $recursive=false)
addFilesFromFileCollections(array $fileCollectionUids=[])
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=[])