‪TYPO3CMS  10.4
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 
72  // Merge local filters into storage's filters
74  // Only use the filters provided by the storage
76  // Only use the filters provided by the current class
78 
87  {
88  $this->storage = ‪$storage;
89  $this->identifier = ‪$identifier;
90  $this->name = ‪$name;
91  }
92 
98  public function ‪getName()
99  {
100  return ‪$this->name;
101  }
102 
110  public function ‪getReadablePath($rootId = null)
111  {
112  if ($rootId === null) {
113  // Find first matching filemount and use that as root
114  foreach ($this->storage->getFileMounts() as $fileMount) {
115  if ($this->storage->isWithinFolder($fileMount['folder'], $this)) {
116  $rootId = $fileMount['folder']->getIdentifier();
117  break;
118  }
119  }
120  if ($rootId === null) {
121  $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
122  }
123  }
124  $readablePath = '/';
125  if ($this->identifier !== $rootId) {
126  try {
127  $readablePath = $this->‪getParentFolder()->getReadablePath($rootId);
128  } catch (InsufficientFolderAccessPermissionsException $e) {
129  // May no access to parent folder (e.g. because of mount point)
130  $readablePath = '/';
131  }
132  }
133  return $readablePath . ($this->name ? $this->name . '/' : '');
134  }
135 
143  public function ‪setName(‪$name)
144  {
145  $this->name = ‪$name;
146  }
147 
153  public function ‪getStorage()
154  {
155  return ‪$this->storage;
156  }
157 
164  public function ‪getIdentifier()
165  {
166  return ‪$this->identifier;
167  }
168 
174  public function ‪getHashedIdentifier()
175  {
176  return $this->storage->hashFileIdentifier($this->identifier);
177  }
178 
185  public function ‪getCombinedIdentifier()
186  {
187  return $this->‪getStorage()->‪getUid() . ':' . $this->‪getIdentifier();
188  }
189 
199  public function ‪getPublicUrl($relativeToCurrentScript = false)
200  {
201  return $this->‪getStorage()->‪getPublicUrl($this, $relativeToCurrentScript);
202  }
203 
222  public function ‪getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
223  {
224  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
225  if ($filterMode === false) {
226  $useFilters = false;
227  $backedUpFilters = [];
228  } else {
229  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
230  }
231 
232  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
233 
234  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
235 
236  return $fileObjects;
237  }
238 
247  public function ‪searchFiles(FileSearchDemand $searchDemand, int $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS): FileSearchResultInterface
248  {
249  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
250  $searchResult = $this->storage->searchFiles($searchDemand, $this, $useFilters);
251  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
252 
253  return $searchResult;
254  }
255 
265  public function ‪getFileCount(array $filterMethods = [], $recursive = false)
266  {
267  return $this->storage->countFilesInFolder($this, true, $recursive);
268  }
269 
277  public function ‪getSubfolder(‪$name)
278  {
279  if (!$this->storage->hasFolderInFolder(‪$name, $this)) {
280  throw new \InvalidArgumentException('Folder "' . ‪$name . '" does not exist in "' . $this->identifier . '"', 1329836110);
281  }
282  return $this->storage->getFolderInFolder(‪$name, $this);
283  }
284 
294  public function ‪getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
295  {
296  [$backedUpFilters, $useFilters] = $this->‪prepareFiltersInStorage($filterMode);
297  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
298  $this->‪restoreBackedUpFiltersInStorage($backedUpFilters);
299  return $folderObjects;
300  }
301 
311  public function ‪addFile($localFilePath, $fileName = null, $conflictMode = ‪DuplicationBehavior::CANCEL)
312  {
313  $fileName = $fileName ?: ‪PathUtility::basename($localFilePath);
314  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
315  }
316 
324  public function ‪addUploadedFile(array $uploadedFileData, $conflictMode = ‪DuplicationBehavior::CANCEL)
325  {
326  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
327  }
328 
335  public function ‪rename($newName)
336  {
337  return $this->storage->renameFolder($this, $newName);
338  }
339 
346  public function delete($deleteRecursively = true)
347  {
348  return $this->storage->deleteFolder($this, $deleteRecursively);
349  }
350 
357  public function ‪createFile($fileName)
358  {
359  return $this->storage->createFile($fileName, $this);
360  }
361 
368  public function ‪createFolder($folderName)
369  {
370  return $this->storage->‪createFolder($folderName, $this);
371  }
372 
381  public function ‪copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
382  {
383  return $targetFolder->‪getStorage()->‪copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
384  }
385 
394  public function ‪moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = ‪DuplicationBehavior::RENAME)
395  {
396  return $targetFolder->‪getStorage()->‪moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
397  }
398 
405  public function ‪hasFile(‪$name)
406  {
407  return $this->storage->hasFileInFolder(‪$name, $this);
408  }
409 
416  public function ‪hasFolder(‪$name)
417  {
418  return $this->storage->hasFolderInFolder(‪$name, $this);
419  }
420 
427  public function ‪checkActionPermission($action)
428  {
429  try {
430  return $this->‪getStorage()->‪checkFolderActionPermission($action, $this);
431  } catch (ResourcePermissionsUnavailableException $e) {
432  return false;
433  }
434  }
435 
444  public function ‪updateProperties(array $properties)
445  {
446  // Setting identifier and name to update values
447  if (isset($properties['identifier'])) {
448  $this->identifier = $properties['identifier'];
449  }
450  if (isset($properties['name'])) {
451  $this->name = $properties['name'];
452  }
453  }
454 
461  protected function ‪prepareFiltersInStorage($filterMode)
462  {
463  $backedUpFilters = null;
464  $useFilters = true;
465 
466  switch ($filterMode) {
468  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
469  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
470 
471  break;
472 
474  if (!empty($this->fileAndFolderNameFilters)) {
475  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
476  foreach ($this->fileAndFolderNameFilters as $filter) {
477  $this->storage->addFileAndFolderNameFilter($filter);
478  }
479  }
480 
481  break;
482 
484  // nothing to do here
485 
486  break;
487 
489  $useFilters = false;
490 
491  break;
492  }
493  return [$backedUpFilters, $useFilters];
494  }
495 
503  protected function ‪restoreBackedUpFiltersInStorage($backedUpFilters)
504  {
505  if ($backedUpFilters !== null) {
506  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
507  }
508  }
509 
516  public function ‪setFileAndFolderNameFilters(array $filters)
517  {
518  $this->fileAndFolderNameFilters = $filters;
519  }
520 
526  public function ‪getRole()
527  {
528  return $this->storage->getRole($this);
529  }
530 
540  public function ‪getParentFolder()
541  {
542  return $this->‪getStorage()->‪getFolder($this->‪getStorage()->getFolderIdentifierFromFileIdentifier($this->‪getIdentifier()));
543  }
544 
550  public function ‪getModificationTime()
551  {
552  return $this->storage->getFolderInfo($this)['mtime'];
553  }
554 
560  public function ‪getCreationTime()
561  {
562  return $this->storage->getFolderInfo($this)['ctime'];
563  }
564 }
‪TYPO3\CMS\Core\Resource\FolderInterface\getSubfolders
‪Folder[] getSubfolders()
‪TYPO3\CMS\Core\Resource\Folder\$identifier
‪string $identifier
Definition: Folder.php:50
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Core\Resource\DuplicationBehavior\CANCEL
‪const CANCEL
Definition: DuplicationBehavior.php:46
‪TYPO3\CMS\Core\Resource\ResourceStorage\getPublicUrl
‪string null getPublicUrl(ResourceInterface $resourceObject, $relativeToCurrentScript=false)
Definition: ResourceStorage.php:1369
‪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:2182
‪TYPO3\CMS\Core\Resource\Folder\getParentFolder
‪FolderInterface getParentFolder()
Definition: Folder.php:536
‪TYPO3\CMS\Core\Resource\Folder\__construct
‪__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:82
‪TYPO3\CMS\Core\Resource\Folder\hasFile
‪bool hasFile($name)
Definition: Folder.php:401
‪TYPO3\CMS\Core\Resource\Folder\updateProperties
‪updateProperties(array $properties)
Definition: Folder.php:440
‪TYPO3\CMS\Core\Resource\Folder\copyTo
‪Folder copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:377
‪TYPO3\CMS\Core\Resource\Folder\$fileAndFolderNameFilters
‪callable[] $fileAndFolderNameFilters
Definition: Folder.php:62
‪TYPO3\CMS\Core\Resource\Folder\createFile
‪File createFile($fileName)
Definition: Folder.php:353
‪TYPO3\CMS\Core\Resource\Folder\addUploadedFile
‪File addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:320
‪TYPO3\CMS\Core\Resource\ResourceStorage\getFolder
‪Folder InaccessibleFolder getFolder($identifier, $returnInaccessibleFolderObject=false)
Definition: ResourceStorage.php:2541
‪TYPO3\CMS\Core\Resource\Folder\prepareFiltersInStorage
‪array prepareFiltersInStorage($filterMode)
Definition: Folder.php:457
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:261
‪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\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: Folder.php:195
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:165
‪TYPO3\CMS\Core\Resource\Folder\getName
‪string getName()
Definition: Folder.php:94
‪TYPO3\CMS\Core\Resource\Folder\moveTo
‪Folder moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:390
‪TYPO3\CMS\Core\Resource\Folder\$name
‪string $name
Definition: Folder.php:56
‪TYPO3\CMS\Core\Resource\Folder\restoreBackedUpFiltersInStorage
‪restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:499
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:149
‪TYPO3\CMS\Core\Resource\Folder\$storage
‪ResourceStorage $storage
Definition: Folder.php:42
‪TYPO3\CMS\Core\Resource\Folder\rename
‪Folder rename($newName)
Definition: Folder.php:331
‪TYPO3\CMS\Core\Resource\ResourceStorage\checkFolderActionPermission
‪bool checkFolderActionPermission($action, Folder $folder=null)
Definition: ResourceStorage.php:760
‪TYPO3\CMS\Core\Resource\Folder\getModificationTime
‪int getModificationTime()
Definition: Folder.php:546
‪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\getSubfolders
‪Folder[] getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:290
‪TYPO3\CMS\Core\Resource\Folder\createFolder
‪Folder createFolder($folderName)
Definition: Folder.php:364
‪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:423
‪TYPO3\CMS\Core\Resource\Folder\getRole
‪string getRole()
Definition: Folder.php:522
‪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:218
‪TYPO3\CMS\Core\Resource\Search\Result\FileSearchResultInterface
Definition: FileSearchResultInterface.php:25
‪TYPO3\CMS\Core\Resource\Folder\getSubfolder
‪Folder getSubfolder($name)
Definition: Folder.php:273
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪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:122
‪TYPO3\CMS\Core\Resource\Folder\getReadablePath
‪string getReadablePath($rootId=null)
Definition: Folder.php:106
‪TYPO3\CMS\Core\Resource\Folder\setFileAndFolderNameFilters
‪setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:512
‪TYPO3\CMS\Core\Resource\FolderInterface
Definition: FolderInterface.php:22
‪TYPO3\CMS\Core\Resource\Folder\setName
‪setName($name)
Definition: Folder.php:139
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪string getIdentifier()
Definition: Folder.php:160
‪TYPO3\CMS\Core\Resource\Folder\getCombinedIdentifier
‪string getCombinedIdentifier()
Definition: Folder.php:181
‪TYPO3\CMS\Core\Resource\Folder\addFile
‪File addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:307
‪TYPO3\CMS\Core\Resource\Folder\searchFiles
‪FileSearchResultInterface searchFiles(FileSearchDemand $searchDemand, int $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:243
‪TYPO3\CMS\Core\Resource\Folder\FILTER_MODE_NO_FILTERS
‪const FILTER_MODE_NO_FILTERS
Definition: Folder.php:67
‪TYPO3\CMS\Core\Resource\Folder\getHashedIdentifier
‪string getHashedIdentifier()
Definition: Folder.php:170
‪TYPO3\CMS\Core\Resource\Folder\hasFolder
‪bool hasFolder($name)
Definition: Folder.php:412
‪TYPO3\CMS\Core\Resource\ResourceStorage\copyFolder
‪Folder copyFolder(FolderInterface $folderToCopy, FolderInterface $targetParentFolder, $newFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: ResourceStorage.php:2248
‪TYPO3\CMS\Core\Resource\Folder\getCreationTime
‪int getCreationTime()
Definition: Folder.php:556