‪TYPO3CMS  ‪main
Folder.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 
24 
37 class ‪Folder implements ‪FolderInterface
38 {
44  protected ‪$storage;
45 
53  protected ‪$identifier;
54 
60  protected ‪$name;
61 
67  protected ‪$fileAndFolderNameFilters = [];
68 
72  public const ‪FILTER_MODE_NO_FILTERS = 0;
73  // Merge local filters into storage's filters
75  // Only use the filters provided by the storage
76  public const ‪FILTER_MODE_USE_STORAGE_FILTERS = 2;
77  // Only use the filters provided by the current class
78  public const ‪FILTER_MODE_USE_OWN_FILTERS = 3;
79 
87  {
88  $this->storage = ‪$storage;
89  $this->identifier = ‪$identifier;
90  $this->name = ‪$name;
91  }
92 
93  public function ‪getName(): string
94  {
95  return ‪$this->name;
96  }
97 
105  public function ‪getReadablePath($rootId = null)
106  {
107  if ($rootId === null) {
108  // Find first matching filemount and use that as root
109  foreach ($this->storage->getFileMounts() as $fileMount) {
110  if ($this->storage->isWithinFolder($fileMount['folder'], $this)) {
111  $rootId = $fileMount['folder']->getIdentifier();
112  break;
113  }
114  }
115  if ($rootId === null) {
116  $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
117  }
118  }
119  $readablePath = '/';
120  if ($this->identifier !== $rootId) {
121  try {
122  $readablePath = $this->‪getParentFolder()->getReadablePath($rootId);
123  } catch (InsufficientFolderAccessPermissionsException $e) {
124  // May no access to parent folder (e.g. because of mount point)
125  $readablePath = '/';
126  }
127  }
128  return $readablePath . ($this->name ? $this->name . '/' : '');
129  }
130 
138  public function ‪setName(‪$name)
139  {
140  $this->name = ‪$name;
141  }
142 
143  public function ‪getStorage(): ‪ResourceStorage
144  {
145  return ‪$this->storage;
146  }
147 
154  public function ‪getIdentifier(): string
155  {
156  return ‪$this->identifier;
157  }
158 
159  public function ‪getHashedIdentifier(): string
160  {
161  return $this->storage->hashFileIdentifier($this->identifier);
162  }
163 
170  public function ‪getCombinedIdentifier()
171  {
172  return $this->‪getStorage()->getUid() . ':' . $this->‪getIdentifier();
173  }
174 
183  public function ‪getPublicUrl()
184  {
185  return $this->‪getStorage()->getPublicUrl($this);
186  }
187 
206  public function ‪getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
207  {
208  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
209  if ($filterMode === false) {
210  $useFilters = false;
211  $backedUpFilters = [];
212  } else {
213  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
214  }
215 
216  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
217 
218  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
219 
220  return $fileObjects;
221  }
222 
229  public function ‪searchFiles(FileSearchDemand $searchDemand, int $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS): FileSearchResultInterface
230  {
231  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
232  $searchResult = $this->storage->searchFiles($searchDemand, $this, $useFilters);
233  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
234 
235  return $searchResult;
236  }
237 
246  public function ‪getFileCount(array $filterMethods = [], $recursive = false)
247  {
248  return $this->storage->countFilesInFolder($this, true, $recursive);
249  }
250 
256  public function ‪getSubfolder(string ‪$name): Folder
257  {
258  if (!$this->storage->hasFolderInFolder(‪$name, $this)) {
259  throw new \InvalidArgumentException('Folder "' . ‪$name . '" does not exist in "' . $this->identifier . '"', 1329836110);
260  }
261  return $this->storage->getFolderInFolder(‪$name, $this);
262  }
263 
271  public function ‪getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false): array
272  {
273  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
274  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
275  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
276  return $folderObjects;
277  }
278 
289  public function ‪addFile($localFilePath, $fileName = null, $conflictMode = ‪DuplicationBehavior::CANCEL)
290  {
291  $fileName = $fileName ?: ‪PathUtility::basename($localFilePath);
292 
293  if (!$conflictMode instanceof ‪DuplicationBehavior) {
294  trigger_error(
295  'Using the non-native enumeration TYPO3\CMS\Core\Resource\DuplicationBehavior in Folder->addFile()'
296  . ' will stop working in TYPO3 v14.0. Use native TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior instead.',
297  E_USER_DEPRECATED
298  );
300  }
301 
302  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
303  }
304 
313  public function ‪addUploadedFile(array $uploadedFileData, $conflictMode = ‪DuplicationBehavior::CANCEL)
314  {
315  if (!$conflictMode instanceof ‪DuplicationBehavior) {
316  trigger_error(
317  'Using the non-native enumeration TYPO3\CMS\Core\Resource\DuplicationBehavior in Folder->addUploadedFile()'
318  . ' will stop working in TYPO3 v14.0. Use native TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior instead.',
319  E_USER_DEPRECATED
320  );
322  }
323 
324  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
325  }
326 
330  public function ‪rename(string $newName): self
331  {
332  return $this->storage->renameFolder($this, $newName);
333  }
334 
340  public function delete($deleteRecursively = true): bool
341  {
342  return $this->storage->deleteFolder($this, $deleteRecursively);
343  }
344 
351  public function ‪createFile($fileName)
352  {
353  return $this->storage->createFile($fileName, $this);
354  }
355 
362  public function ‪createFolder($folderName)
363  {
364  return $this->storage->‪createFolder($folderName, $this);
365  }
366 
376  public function ‪copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
377  {
378  if (!$conflictMode instanceof ‪DuplicationBehavior) {
379  trigger_error(
380  'Using the non-native enumeration TYPO3\CMS\Core\Resource\DuplicationBehavior in Folder->copyTo()'
381  . ' will stop working in TYPO3 v14.0. Use native TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior instead.',
382  E_USER_DEPRECATED
383  );
385  }
386 
387  return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
388  }
389 
399  public function ‪moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
400  {
401  if (!$conflictMode instanceof ‪DuplicationBehavior) {
402  trigger_error(
403  'Using the non-native enumeration TYPO3\CMS\Core\Resource\DuplicationBehavior in Folder->moveTo()'
404  . ' will stop working in TYPO3 v14.0. Use native TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior instead.',
405  E_USER_DEPRECATED
406  );
408  }
409 
410  return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
411  }
412 
416  public function ‪hasFile(string ‪$name): bool
417  {
418  return $this->storage->hasFileInFolder(‪$name, $this);
419  }
420 
424  public function ‪getFile(string $fileName): ?‪FileInterface
425  {
426  if ($this->storage->hasFileInFolder($fileName, $this)) {
427  return $this->storage->getFileInFolder($fileName, $this);
428  }
429  return null;
430  }
431 
435  public function ‪hasFolder(string ‪$name): bool
436  {
437  return $this->storage->hasFolderInFolder(‪$name, $this);
438  }
439 
446  public function ‪checkActionPermission($action)
447  {
448  try {
449  return $this->‪getStorage()->checkFolderActionPermission($action, $this);
450  } catch (ResourcePermissionsUnavailableException $e) {
451  return false;
452  }
453  }
454 
463  public function ‪updateProperties(array $properties)
464  {
465  // Setting identifier and name to update values
466  if (isset($properties['identifier'])) {
467  $this->identifier = $properties['identifier'];
468  }
469  if (isset($properties['name'])) {
470  $this->name = $properties['name'];
471  }
472  }
473 
480  protected function ‪prepareFiltersInStorage($filterMode)
481  {
482  $backedUpFilters = null;
483  $useFilters = true;
484 
485  switch ($filterMode) {
487  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
488  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
489 
490  break;
491 
493  if (!empty($this->fileAndFolderNameFilters)) {
494  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
495  foreach ($this->fileAndFolderNameFilters as $filter) {
496  $this->storage->addFileAndFolderNameFilter($filter);
497  }
498  }
499 
500  break;
501 
503  // nothing to do here
504 
505  break;
506 
508  $useFilters = false;
509 
510  break;
511  }
512  return [$backedUpFilters, $useFilters];
513  }
514 
522  protected function ‪restoreBackedUpFiltersInStorage($backedUpFilters)
523  {
524  if ($backedUpFilters !== null) {
525  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
526  }
527  }
528 
533  public function ‪setFileAndFolderNameFilters(array $filters)
534  {
535  $this->fileAndFolderNameFilters = $filters;
536  }
537 
543  public function ‪getRole()
544  {
545  return $this->storage->getRole($this);
546  }
547 
557  public function ‪getParentFolder(): FolderInterface
558  {
559  return $this->‪getStorage()->getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
560  }
561 
565  public function ‪getModificationTime(): int
566  {
567  return (int)$this->storage->getFolderInfo($this)['mtime'];
568  }
569 
573  public function ‪getCreationTime(): int
574  {
575  return (int)$this->storage->getFolderInfo($this)['ctime'];
576  }
577 }
‪TYPO3\CMS\Core\Resource\Folder\getModificationTime
‪getModificationTime()
Definition: Folder.php:561
‪TYPO3\CMS\Core\Resource\Folder\$identifier
‪string $identifier
Definition: Folder.php:51
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Resource\DuplicationBehavior
Definition: DuplicationBehavior.php:25
‪TYPO3\CMS\Core\Resource\Folder\getCreationTime
‪getCreationTime()
Definition: Folder.php:569
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\CANCEL
‪const CANCEL
Definition: DuplicationBehavior.php:47
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:225
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:23
‪TYPO3\CMS\Core\Resource\Folder\__construct
‪__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:82
‪TYPO3\CMS\Core\Resource\Folder\updateProperties
‪updateProperties(array $properties)
Definition: Folder.php:459
‪TYPO3\CMS\Core\Resource\Folder\copyTo
‪Folder copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:372
‪TYPO3\CMS\Core\Resource\Folder\$fileAndFolderNameFilters
‪callable[] $fileAndFolderNameFilters
Definition: Folder.php:63
‪TYPO3\CMS\Core\Resource\Folder\createFile
‪File createFile($fileName)
Definition: Folder.php:347
‪TYPO3\CMS\Core\Resource\Folder\addUploadedFile
‪File addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:309
‪TYPO3\CMS\Core\Resource\Folder\getSubfolders
‪getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:267
‪TYPO3\CMS\Core\Resource\Folder\prepareFiltersInStorage
‪array prepareFiltersInStorage($filterMode)
Definition: Folder.php:476
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:242
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
‪const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:70
‪TYPO3\CMS\Core\Resource\Folder\rename
‪rename(string $newName)
Definition: Folder.php:326
‪TYPO3\CMS\Core\Resource\FolderInterface\getSubfolders
‪getSubfolders()
‪TYPO3\CMS\Core\Resource\Exception\ResourcePermissionsUnavailableException
Definition: ResourcePermissionsUnavailableException.php:25
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static basename(string $path)
Definition: PathUtility.php:220
‪TYPO3\CMS\Frontend\Content\tryFrom
‪@ tryFrom
Definition: ContentSlideMode.php:27
‪TYPO3\CMS\Core\Resource\Folder\getFile
‪getFile(string $fileName)
Definition: Folder.php:420
‪TYPO3\CMS\Core\Resource\Folder\moveTo
‪Folder moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:395
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪getHashedIdentifier()
Definition: Folder.php:155
‪TYPO3\CMS\Core\Resource\Folder\$name
‪string $name
Definition: Folder.php:57
‪TYPO3\CMS\Core\Resource\Folder\restoreBackedUpFiltersInStorage
‪restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:518
‪TYPO3\CMS\Core\Resource\Folder\hasFile
‪hasFile(string $name)
Definition: Folder.php:412
‪TYPO3\CMS\Core\Resource\Folder\getParentFolder
‪getParentFolder()
Definition: Folder.php:553
‪TYPO3\CMS\Core\Resource\Folder\getSubfolder
‪getSubfolder(string $name)
Definition: Folder.php:252
‪TYPO3\CMS\Core\Resource\Folder\$storage
‪ResourceStorage $storage
Definition: Folder.php:43
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_FILTERS
‪const FILTER_MODE_USE_OWN_FILTERS
Definition: Folder.php:74
‪TYPO3\CMS\Core\Resource\Enum\getDefaultDuplicationBehaviour
‪@ getDefaultDuplicationBehaviour
Definition: DuplicationBehavior.php:53
‪TYPO3\CMS\Core\Resource\Folder\createFolder
‪Folder createFolder($folderName)
Definition: Folder.php:358
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:26
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:33
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪getStorage()
Definition: Folder.php:139
‪TYPO3\CMS\Core\Resource\Folder\checkActionPermission
‪bool checkActionPermission($action)
Definition: Folder.php:442
‪TYPO3\CMS\Core\Resource\Enum\DuplicationBehavior
‪DuplicationBehavior
Definition: DuplicationBehavior.php:28
‪TYPO3\CMS\Core\Resource\Folder\getName
‪getName()
Definition: Folder.php:89
‪TYPO3\CMS\Core\Resource\Folder\getRole
‪string getRole()
Definition: Folder.php:539
‪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:202
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:24
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:54
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_STORAGE_FILTERS
‪const FILTER_MODE_USE_STORAGE_FILTERS
Definition: Folder.php:72
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪TYPO3\CMS\Core\Resource\Folder\getReadablePath
‪string getReadablePath($rootId=null)
Definition: Folder.php:101
‪TYPO3\CMS\Core\Resource\Folder\setFileAndFolderNameFilters
‪setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:529
‪TYPO3\CMS\Core\Resource\Folder\hasFolder
‪hasFolder(string $name)
Definition: Folder.php:431
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:24
‪TYPO3\CMS\Core\Resource\Folder\setName
‪setName($name)
Definition: Folder.php:134
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪non empty string getIdentifier()
Definition: Folder.php:150
‪TYPO3\CMS\Core\Resource\Folder\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: Folder.php:166
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:285
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_NO_FILTERS
‪const FILTER_MODE_NO_FILTERS
Definition: Folder.php:68
‪TYPO3\CMS\Core\Resource\Folder\getPublicUrl
‪string null getPublicUrl()
Definition: Folder.php:179