‪TYPO3CMS  9.5
Folder.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 
20 
33 class ‪Folder implements ‪FolderInterface
34 {
40  protected ‪$storage;
41 
49  protected ‪$identifier;
50 
56  protected ‪$name;
57 
63  protected ‪$fileAndFolderNameFilters = [];
64 
69  // Merge local filters into storage's filters
71  // Only use the filters provided by the storage
73  // Only use the filters provided by the current class
75 
84  {
85  $this->storage = ‪$storage;
86  $this->identifier = ‪$identifier;
87  $this->name = ‪$name;
88  }
89 
95  public function ‪getName()
96  {
97  return ‪$this->name;
98  }
99 
107  public function ‪getReadablePath($rootId = null)
108  {
109  if ($rootId === null) {
110  // Find first matching filemount and use that as root
111  foreach ($this->storage->getFileMounts() as $fileMount) {
112  if ($this->storage->isWithinFolder($fileMount['folder'], $this)) {
113  $rootId = $fileMount['folder']->getIdentifier();
114  break;
115  }
116  }
117  if ($rootId === null) {
118  $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
119  }
120  }
121  $readablePath = '/';
122  if ($this->identifier !== $rootId) {
123  try {
124  $readablePath = $this->‪getParentFolder()->‪getReadablePath($rootId);
125  } catch (Exception\InsufficientFolderAccessPermissionsException $e) {
126  // May no access to parent folder (e.g. because of mount point)
127  $readablePath = '/';
128  }
129  }
130  return $readablePath . ($this->name ? $this->name . '/' : '');
131  }
132 
140  public function ‪setName(‪$name)
141  {
142  $this->name = ‪$name;
143  }
144 
150  public function ‪getStorage()
151  {
152  return ‪$this->storage;
153  }
154 
161  public function ‪getIdentifier()
162  {
163  return ‪$this->identifier;
164  }
165 
171  public function ‪getHashedIdentifier()
172  {
173  return $this->storage->hashFileIdentifier($this->identifier);
174  }
175 
182  public function ‪getCombinedIdentifier()
183  {
184  return $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
185  }
186 
196  public function ‪getPublicUrl($relativeToCurrentScript = false)
197  {
198  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
199  }
200 
219  public function ‪getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
220  {
221  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
222  if ($filterMode === false) {
223  $useFilters = false;
224  $backedUpFilters = [];
225  } else {
226  list($backedUpFilters, $useFilters) = $this->‪prepareFiltersInStorage($filterMode);
227  }
228 
229  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
230 
231  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
232 
233  return $fileObjects;
234  }
235 
244  public function ‪searchFiles(FileSearchDemand $searchDemand, int $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS): FileSearchResultInterface
245  {
246  list($backedUpFilters, $useFilters) = $this->‪prepareFiltersInStorage($filterMode);
247  $searchResult = $this->storage->searchFiles($searchDemand, $this, $useFilters);
248  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
249 
250  return $searchResult;
251  }
252 
262  public function ‪getFileCount(array $filterMethods = [], $recursive = false)
263  {
264  return $this->storage->countFilesInFolder($this, true, $recursive);
265  }
266 
274  public function ‪getSubfolder(‪$name)
275  {
276  if (!$this->storage->hasFolderInFolder(‪$name, $this)) {
277  throw new \InvalidArgumentException('Folder "' . ‪$name . '" does not exist in "' . $this->identifier . '"', 1329836110);
278  }
279  return $this->storage->getFolderInFolder(‪$name, $this);
280  }
281 
291  public function ‪getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
292  {
293  list($backedUpFilters, $useFilters) = $this->‪prepareFiltersInStorage($filterMode);
294  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
295  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
296  return $folderObjects;
297  }
298 
308  public function ‪addFile($localFilePath, $fileName = null, $conflictMode = ‪DuplicationBehavior::CANCEL)
309  {
310  $fileName = $fileName ? $fileName : ‪PathUtility::basename($localFilePath);
311  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
312  }
313 
321  public function ‪addUploadedFile(array $uploadedFileData, $conflictMode = ‪DuplicationBehavior::CANCEL)
322  {
323  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
324  }
325 
332  public function ‪rename($newName)
333  {
334  return $this->storage->renameFolder($this, $newName);
335  }
336 
343  public function delete($deleteRecursively = true)
344  {
345  return $this->storage->deleteFolder($this, $deleteRecursively);
346  }
347 
354  public function ‪createFile($fileName)
355  {
356  return $this->storage->createFile($fileName, $this);
357  }
358 
365  public function ‪createFolder($folderName)
366  {
367  return $this->storage->‪createFolder($folderName, $this);
368  }
369 
378  public function ‪copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
379  {
380  return $targetFolder->‪getStorage()->‪copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
381  }
382 
391  public function ‪moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
392  {
393  return $targetFolder->‪getStorage()->‪moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
394  }
395 
402  public function ‪hasFile(‪$name)
403  {
404  return $this->storage->hasFileInFolder(‪$name, $this);
405  }
406 
413  public function ‪hasFolder(‪$name)
414  {
415  return $this->storage->hasFolderInFolder(‪$name, $this);
416  }
417 
424  public function ‪checkActionPermission($action)
425  {
426  try {
427  return $this->‪getStorage()->‪checkFolderActionPermission($action, $this);
428  } catch (Exception\ResourcePermissionsUnavailableException $e) {
429  return false;
430  }
431  }
432 
441  public function ‪updateProperties(array $properties)
442  {
443  // Setting identifier and name to update values
444  if (isset($properties['identifier'])) {
445  $this->identifier = $properties['identifier'];
446  }
447  if (isset($properties['name'])) {
448  $this->name = $properties['name'];
449  }
450  }
451 
458  protected function ‪prepareFiltersInStorage($filterMode)
459  {
460  $backedUpFilters = null;
461  $useFilters = true;
462 
463  switch ($filterMode) {
465  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
466  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
467 
468  break;
469 
471  if (!empty($this->fileAndFolderNameFilters)) {
472  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
473  foreach ($this->fileAndFolderNameFilters as $filter) {
474  $this->storage->addFileAndFolderNameFilter($filter);
475  }
476  }
477 
478  break;
479 
481  // nothing to do here
482 
483  break;
484 
486  $useFilters = false;
487 
488  break;
489  }
490  return [$backedUpFilters, $useFilters];
491  }
492 
500  protected function ‪restoreBackedUpFiltersInStorage($backedUpFilters)
501  {
502  if ($backedUpFilters !== null) {
503  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
504  }
505  }
506 
513  public function ‪setFileAndFolderNameFilters(array $filters)
514  {
515  $this->fileAndFolderNameFilters = $filters;
516  }
517 
523  public function ‪getRole()
524  {
525  return $this->storage->getRole($this);
526  }
527 
537  public function ‪getParentFolder()
538  {
539  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
540  }
541 
547  public function ‪getModificationTime()
548  {
549  return $this->storage->getFolderInfo($this)['mtime'];
550  }
551 
557  public function ‪getCreationTime()
558  {
559  return $this->storage->getFolderInfo($this)['ctime'];
560  }
561 }
‪TYPO3\CMS\Core\Resource\FolderInterface\getSubfolders
‪Folder[] getSubfolders()
‪TYPO3\CMS\Core\Resource\Folder\$identifier
‪string $identifier
Definition: Folder.php:47
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:271
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\CANCEL
‪const CANCEL
Definition: DuplicationBehavior.php:45
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1317
‪TYPO3\CMS\Core\Resource\ResourceStorage\moveFolder
‪Folder moveFolder(Folder $folderToMove, Folder $targetParentFolder, $newFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2127
‪TYPO3\CMS\Core\Resource\Folder\__construct
‪__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:79
‪TYPO3\CMS\Core\Resource\Folder\hasFile
‪bool hasFile($name)
Definition: Folder.php:398
‪TYPO3\CMS\Core\Resource\Folder\updateProperties
‪updateProperties(array $properties)
Definition: Folder.php:437
‪TYPO3\CMS\Core\Resource\Folder\copyTo
‪Folder copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:374
‪TYPO3\CMS\Core\Resource\Folder\$fileAndFolderNameFilters
‪callable[] $fileAndFolderNameFilters
Definition: Folder.php:59
‪TYPO3\CMS\Core\Resource\Folder\createFile
‪File createFile($fileName)
Definition: Folder.php:350
‪TYPO3\CMS\Core\Resource\Folder\addUploadedFile
‪File addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:317
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2462
‪TYPO3\CMS\Core\Resource\Folder\prepareFiltersInStorage
‪array prepareFiltersInStorage($filterMode)
Definition: Folder.php:454
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:258
‪TYPO3\CMS\Core\Resource\Folder\getRole
‪int getRole()
Definition: Folder.php:519
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:66
‪TYPO3\CMS\Core\Resource\Folder\getParentFolder
‪Folder getParentFolder()
Definition: Folder.php:533
‪TYPO3\CMS\Core\Resource\Folder\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: Folder.php:192
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:164
‪TYPO3\CMS\Core\Resource\Folder\getName
‪string getName()
Definition: Folder.php:91
‪TYPO3\CMS\Core\Resource\Folder\moveTo
‪Folder moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:387
‪TYPO3\CMS\Core\Resource\Folder\$name
‪string $name
Definition: Folder.php:53
‪TYPO3\CMS\Core\Resource\Folder\restoreBackedUpFiltersInStorage
‪restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:496
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:146
‪TYPO3\CMS\Core\Resource\Folder\$storage
‪ResourceStorage $storage
Definition: Folder.php:39
‪TYPO3\CMS\Core\Resource\Folder\rename
‪Folder rename($newName)
Definition: Folder.php:328
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFolderActionPermission
‪bool checkFolderActionPermission($action, Folder $folder=null)
Definition: ResourceStorage.php:707
‪TYPO3\CMS\Core\Resource\Folder\getModificationTime
‪int getModificationTime()
Definition: Folder.php:543
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_FILTERS
‪const FILTER_MODE_USE_OWN_FILTERS
Definition: Folder.php:70
‪TYPO3\CMS\Core\Resource\Folder\getSubfolders
‪Folder[] getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:287
‪TYPO3\CMS\Core\Resource\Folder\createFolder
‪Folder createFolder($folderName)
Definition: Folder.php:361
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:24
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:31
‪TYPO3\CMS\Core\Resource\Folder\checkActionPermission
‪bool checkActionPermission($action)
Definition: Folder.php:420
‪TYPO3\CMS\Core\Resource\Folder\getFiles
‪TYPO3 CMS Core Resource File[] getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort='', $sortRev=false)
Definition: Folder.php:215
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:23
‪TYPO3\CMS\Core\Resource\Folder\getSubfolder
‪Folder getSubfolder($name)
Definition: Folder.php:270
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:37
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_STORAGE_FILTERS
‪const FILTER_MODE_USE_STORAGE_FILTERS
Definition: Folder.php:68
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Resource\Folder\getReadablePath
‪string getReadablePath($rootId=null)
Definition: Folder.php:103
‪TYPO3\CMS\Core\Resource\Folder\setFileAndFolderNameFilters
‪setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:509
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:21
‪TYPO3\CMS\Core\Resource\Folder\setName
‪setName($name)
Definition: Folder.php:136
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪string getIdentifier()
Definition: Folder.php:157
‪TYPO3\CMS\Core\Resource\Folder\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: Folder.php:178
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:304
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪FileSearchResultInterface searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:240
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_NO_FILTERS
‪const FILTER_MODE_NO_FILTERS
Definition: Folder.php:64
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪string getHashedIdentifier()
Definition: Folder.php:167
‪TYPO3\CMS\Core\Resource\Folder\hasFolder
‪bool hasFolder($name)
Definition: Folder.php:409
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFolder
‪Folder copyFolder(FolderInterface $folderToCopy, FolderInterface $targetParentFolder, $newFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2188
‪TYPO3\CMS\Core\Resource\Folder\getCreationTime
‪int getCreationTime()
Definition: Folder.php:553