‪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 
23 
36 class ‪Folder implements ‪FolderInterface
37 {
43  protected ‪$storage;
44 
52  protected ‪$identifier;
53 
59  protected ‪$name;
60 
66  protected ‪$fileAndFolderNameFilters = [];
67 
71  public const ‪FILTER_MODE_NO_FILTERS = 0;
72  // Merge local filters into storage's filters
74  // Only use the filters provided by the storage
75  public const ‪FILTER_MODE_USE_STORAGE_FILTERS = 2;
76  // Only use the filters provided by the current class
77  public const ‪FILTER_MODE_USE_OWN_FILTERS = 3;
78 
86  {
87  $this->storage = ‪$storage;
88  $this->identifier = ‪$identifier;
89  $this->name = ‪$name;
90  }
91 
97  public function ‪getName()
98  {
99  return ‪$this->name;
100  }
101 
109  public function ‪getReadablePath($rootId = null)
110  {
111  if ($rootId === null) {
112  // Find first matching filemount and use that as root
113  foreach ($this->storage->getFileMounts() as $fileMount) {
114  if ($this->storage->isWithinFolder($fileMount['folder'], $this)) {
115  $rootId = $fileMount['folder']->getIdentifier();
116  break;
117  }
118  }
119  if ($rootId === null) {
120  $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
121  }
122  }
123  $readablePath = '/';
124  if ($this->identifier !== $rootId) {
125  try {
126  $readablePath = $this->‪getParentFolder()->getReadablePath($rootId);
127  } catch (InsufficientFolderAccessPermissionsException $e) {
128  // May no access to parent folder (e.g. because of mount point)
129  $readablePath = '/';
130  }
131  }
132  return $readablePath . ($this->name ? $this->name . '/' : '');
133  }
134 
142  public function ‪setName(‪$name)
143  {
144  $this->name = ‪$name;
145  }
146 
152  public function ‪getStorage()
153  {
154  return ‪$this->storage;
155  }
156 
163  public function ‪getIdentifier()
164  {
165  return ‪$this->identifier;
166  }
167 
173  public function ‪getHashedIdentifier()
174  {
175  return $this->storage->hashFileIdentifier($this->identifier);
176  }
177 
184  public function ‪getCombinedIdentifier()
185  {
186  return $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
187  }
188 
197  public function ‪getPublicUrl()
198  {
199  return $this->‪getStorage()->‪getPublicUrl($this);
200  }
201 
220  public function ‪getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
221  {
222  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
223  if ($filterMode === false) {
224  $useFilters = false;
225  $backedUpFilters = [];
226  } else {
227  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
228  }
229 
230  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
231 
232  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
233 
234  return $fileObjects;
235  }
236 
243  public function ‪searchFiles(FileSearchDemand $searchDemand, int $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS): FileSearchResultInterface
244  {
245  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
246  $searchResult = $this->storage->searchFiles($searchDemand, $this, $useFilters);
247  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
248 
249  return $searchResult;
250  }
251 
260  public function ‪getFileCount(array $filterMethods = [], $recursive = false)
261  {
262  return $this->storage->countFilesInFolder($this, true, $recursive);
263  }
264 
272  public function ‪getSubfolder(‪$name)
273  {
274  if (!$this->storage->hasFolderInFolder(‪$name, $this)) {
275  throw new \InvalidArgumentException('Folder "' . ‪$name . '" does not exist in "' . $this->identifier . '"', 1329836110);
276  }
277  return $this->storage->getFolderInFolder(‪$name, $this);
278  }
279 
287  public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
288  {
289  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
290  ‪$folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
291  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
292  return ‪$folderObjects;
293  }
294 
304  public function ‪addFile($localFilePath, $fileName = null, $conflictMode = ‪DuplicationBehavior::CANCEL)
305  {
306  $fileName = $fileName ?: ‪PathUtility::basename($localFilePath);
307  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
308  }
309 
317  public function ‪addUploadedFile(array $uploadedFileData, $conflictMode = ‪DuplicationBehavior::CANCEL)
318  {
319  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
320  }
321 
328  public function ‪rename($newName)
329  {
330  return $this->storage->renameFolder($this, $newName);
331  }
332 
339  public function delete($deleteRecursively = true)
340  {
341  return $this->storage->deleteFolder($this, $deleteRecursively);
342  }
343 
350  public function ‪createFile($fileName)
351  {
352  return $this->storage->createFile($fileName, $this);
353  }
354 
361  public function ‪createFolder($folderName)
362  {
363  return $this->storage->‪createFolder($folderName, $this);
364  }
365 
374  public function ‪copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
375  {
376  return $targetFolder->‪getStorage()->‪copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
377  }
378 
387  public function ‪moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
388  {
389  return $targetFolder->‪getStorage()->‪moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
390  }
391 
398  public function ‪hasFile(‪$name)
399  {
400  return $this->storage->hasFileInFolder(‪$name, $this);
401  }
402 
408  public function ‪getFile(string $fileName)
409  {
410  if ($this->storage->hasFileInFolder($fileName, $this)) {
411  return $this->storage->getFileInFolder($fileName, $this);
412  }
413  return null;
414  }
415 
422  public function ‪hasFolder(‪$name)
423  {
424  return $this->storage->hasFolderInFolder(‪$name, $this);
425  }
426 
433  public function ‪checkActionPermission($action)
434  {
435  try {
436  return $this->‪getStorage()->‪checkFolderActionPermission($action, $this);
437  } catch (ResourcePermissionsUnavailableException $e) {
438  return false;
439  }
440  }
441 
450  public function ‪updateProperties(array $properties)
451  {
452  // Setting identifier and name to update values
453  if (isset($properties['identifier'])) {
454  $this->identifier = $properties['identifier'];
455  }
456  if (isset($properties['name'])) {
457  $this->name = $properties['name'];
458  }
459  }
460 
467  protected function ‪prepareFiltersInStorage($filterMode)
468  {
469  $backedUpFilters = null;
470  $useFilters = true;
471 
472  switch ($filterMode) {
474  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
475  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
476 
477  break;
478 
480  if (!empty($this->fileAndFolderNameFilters)) {
481  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
482  foreach ($this->fileAndFolderNameFilters as $filter) {
483  $this->storage->addFileAndFolderNameFilter($filter);
484  }
485  }
486 
487  break;
488 
490  // nothing to do here
491 
492  break;
493 
495  $useFilters = false;
496 
497  break;
498  }
499  return [$backedUpFilters, $useFilters];
500  }
501 
509  protected function ‪restoreBackedUpFiltersInStorage($backedUpFilters)
510  {
511  if ($backedUpFilters !== null) {
512  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
513  }
514  }
515 
520  public function ‪setFileAndFolderNameFilters(array $filters)
521  {
522  $this->fileAndFolderNameFilters = $filters;
523  }
524 
530  public function ‪getRole()
531  {
532  return $this->storage->getRole($this);
533  }
534 
545  public function ‪getParentFolder()
546  {
547  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
548  }
549 
555  public function ‪getModificationTime()
556  {
557  return $this->storage->getFolderInfo($this)['mtime'];
558  }
559 
565  public function ‪getCreationTime()
566  {
567  return $this->storage->getFolderInfo($this)['ctime'];
568  }
569 }
‪TYPO3\CMS\Core\Resource\Folder\$identifier
‪string $identifier
Definition: Folder.php:50
‪TYPO3\CMS\Core\Resource\Folder\$folderObjects
‪return $folderObjects
Definition: Folder.php:288
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:328
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\CANCEL
‪const CANCEL
Definition: DuplicationBehavior.php:46
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:239
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderAccessPermissionsException
Definition: InsufficientFolderAccessPermissionsException.php:24
‪TYPO3\CMS\Core\Resource\ResourceStorage\moveFolder
‪Folder moveFolder(Folder $folderToMove, Folder $targetParentFolder, $newFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2147
‪TYPO3\CMS\Core\Resource\Folder\getParentFolder
‪FolderInterface getParentFolder()
Definition: Folder.php:541
‪TYPO3\CMS\Core\Resource\Folder\__construct
‪__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:81
‪TYPO3\CMS\Core\Resource\Folder\prepareFiltersInStorage
‪array< string|int, getSubfolders( $start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false) {[ $backedUpFilters, $useFilters]=$this-> prepareFiltersInStorage($filterMode)
‪TYPO3\CMS\Core\Resource\Folder\hasFile
‪bool hasFile($name)
Definition: Folder.php:394
‪TYPO3\CMS\Core\Resource\Folder\updateProperties
‪updateProperties(array $properties)
Definition: Folder.php:446
‪TYPO3\CMS\Core\Resource\Folder\copyTo
‪Folder copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:370
‪TYPO3\CMS\Core\Resource\Folder\$fileAndFolderNameFilters
‪callable[] $fileAndFolderNameFilters
Definition: Folder.php:62
‪TYPO3\CMS\Core\Resource\Folder\createFile
‪File createFile($fileName)
Definition: Folder.php:346
‪TYPO3\CMS\Core\Resource\Folder\addUploadedFile
‪File addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:313
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2494
‪TYPO3\CMS\Core\Resource\Folder\prepareFiltersInStorage
‪array prepareFiltersInStorage($filterMode)
Definition: Folder.php:463
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:256
‪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\ResourcePermissionsUnavailableException
Definition: ResourcePermissionsUnavailableException.php:26
‪TYPO3\CMS\Core\Resource\Folder\getFile
‪File ProcessedFile null getFile(string $fileName)
Definition: Folder.php:404
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static basename(string $path)
Definition: PathUtility.php:219
‪TYPO3\CMS\Core\Resource\Folder\getName
‪string getName()
Definition: Folder.php:93
‪TYPO3\CMS\Core\Resource\Folder\$folderObjects
‪$folderObjects
Definition: Folder.php:286
‪TYPO3\CMS\Core\Resource\Folder\moveTo
‪Folder moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:383
‪TYPO3\CMS\Core\Resource\Folder\$name
‪string $name
Definition: Folder.php:56
‪TYPO3\CMS\Core\Resource\Folder\restoreBackedUpFiltersInStorage
‪restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:505
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:148
‪TYPO3\CMS\Core\Resource\Folder\$storage
‪ResourceStorage $storage
Definition: Folder.php:42
‪TYPO3\CMS\Core\Resource\Folder\rename
‪Folder rename($newName)
Definition: Folder.php:324
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFolderActionPermission
‪bool checkFolderActionPermission($action, Folder $folder=null)
Definition: ResourceStorage.php:763
‪TYPO3\CMS\Core\Resource\Folder\getModificationTime
‪int getModificationTime()
Definition: Folder.php:551
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_OWN_FILTERS
‪const FILTER_MODE_USE_OWN_FILTERS
Definition: Folder.php:73
‪TYPO3\CMS\Core\Resource\Folder\createFolder
‪Folder createFolder($folderName)
Definition: Folder.php:357
‪TYPO3\CMS\Core\Resource\Search\FileSearchDemand
Definition: FileSearchDemand.php:26
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\RENAME
‪const RENAME
Definition: DuplicationBehavior.php:32
‪TYPO3\CMS\Core\Resource\Folder\checkActionPermission
‪bool checkActionPermission($action)
Definition: Folder.php:429
‪TYPO3\CMS\Core\Resource\Folder\getRole
‪string getRole()
Definition: Folder.php:526
‪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:216
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:25
‪TYPO3\CMS\Core\Resource\Folder\getSubfolder
‪Folder getSubfolder($name)
Definition: Folder.php:268
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_USE_STORAGE_FILTERS
‪const FILTER_MODE_USE_STORAGE_FILTERS
Definition: Folder.php:71
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:127
‪TYPO3\CMS\Core\Resource\Folder\getReadablePath
‪string getReadablePath($rootId=null)
Definition: Folder.php:105
‪TYPO3\CMS\Core\Resource\Folder\setFileAndFolderNameFilters
‪setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:516
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Core\Resource\Folder\setName
‪setName($name)
Definition: Folder.php:138
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪string getIdentifier()
Definition: Folder.php:159
‪TYPO3\CMS\Core\Resource\Folder\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: Folder.php:180
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:300
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_NO_FILTERS
‪const FILTER_MODE_NO_FILTERS
Definition: Folder.php:67
‪TYPO3\CMS\Core\Resource\Folder\getPublicUrl
‪string null getPublicUrl()
Definition: Folder.php:193
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪string getHashedIdentifier()
Definition: Folder.php:169
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject)
Definition: ResourceStorage.php:1327
‪TYPO3\CMS\Core\Resource\Folder\hasFolder
‪bool hasFolder($name)
Definition: Folder.php:418
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFolder
‪Folder copyFolder(FolderInterface $folderToCopy, FolderInterface $targetParentFolder, $newFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2209
‪TYPO3\CMS\Core\Resource\Folder\getCreationTime
‪int getCreationTime()
Definition: Folder.php:561