TYPO3 CMS  TYPO3_8-7
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 
18 
31 class Folder implements FolderInterface
32 {
38  protected $storage;
39 
47  protected $identifier;
48 
54  protected $name;
55 
61  protected $fileAndFolderNameFilters = [];
62 
67  // Merge local filters into storage's filters
69  // Only use the filters provided by the storage
71  // Only use the filters provided by the current class
73 
82  {
83  $this->storage = $storage;
84  $this->identifier = $identifier;
85  $this->name = $name;
86  }
87 
93  public function getName()
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 (Exception\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 
148  public function getStorage()
149  {
150  return $this->storage;
151  }
152 
159  public function getIdentifier()
160  {
161  return $this->identifier;
162  }
163 
169  public function getHashedIdentifier()
170  {
171  return $this->storage->hashFileIdentifier($this->identifier);
172  }
173 
180  public function getCombinedIdentifier()
181  {
182  return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
183  }
184 
194  public function getPublicUrl($relativeToCurrentScript = false)
195  {
196  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
197  }
198 
217  public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
218  {
219  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
220  if ($filterMode === false) {
221  $useFilters = false;
222  $backedUpFilters = [];
223  } else {
224  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
225  }
226 
227  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
228 
229  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
230 
231  return $fileObjects;
232  }
233 
243  public function getFileCount(array $filterMethods = [], $recursive = false)
244  {
245  return $this->storage->countFilesInFolder($this, true, $recursive);
246  }
247 
255  public function getSubfolder($name)
256  {
257  if (!$this->storage->hasFolderInFolder($name, $this)) {
258  throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
259  }
260  return $this->storage->getFolderInFolder($name, $this);
261  }
262 
272  public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
273  {
274  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
275  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
276  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
277  return $folderObjects;
278  }
279 
289  public function addFile($localFilePath, $fileName = null, $conflictMode = DuplicationBehavior::CANCEL)
290  {
291  $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
292  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
293  }
294 
302  public function addUploadedFile(array $uploadedFileData, $conflictMode = DuplicationBehavior::CANCEL)
303  {
304  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
305  }
306 
313  public function rename($newName)
314  {
315  return $this->storage->renameFolder($this, $newName);
316  }
317 
324  public function delete($deleteRecursively = true)
325  {
326  return $this->storage->deleteFolder($this, $deleteRecursively);
327  }
328 
335  public function createFile($fileName)
336  {
337  return $this->storage->createFile($fileName, $this);
338  }
339 
346  public function createFolder($folderName)
347  {
348  return $this->storage->createFolder($folderName, $this);
349  }
350 
359  public function copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
360  {
361  return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
362  }
363 
372  public function moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
373  {
374  return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
375  }
376 
383  public function hasFile($name)
384  {
385  return $this->storage->hasFileInFolder($name, $this);
386  }
387 
394  public function hasFolder($name)
395  {
396  return $this->storage->hasFolderInFolder($name, $this);
397  }
398 
405  public function checkActionPermission($action)
406  {
407  try {
408  return $this->getStorage()->checkFolderActionPermission($action, $this);
409  } catch (Exception\ResourcePermissionsUnavailableException $e) {
410  return false;
411  }
412  }
413 
422  public function updateProperties(array $properties)
423  {
424  // Setting identifier and name to update values
425  if (isset($properties['identifier'])) {
426  $this->identifier = $properties['identifier'];
427  }
428  if (isset($properties['name'])) {
429  $this->name = $properties['name'];
430  }
431  }
432 
439  protected function prepareFiltersInStorage($filterMode)
440  {
441  $backedUpFilters = null;
442  $useFilters = true;
443 
444  switch ($filterMode) {
445  case self::FILTER_MODE_USE_OWN_FILTERS:
446  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
447  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
448 
449  break;
450 
451  case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
452  if (!empty($this->fileAndFolderNameFilters)) {
453  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
454  foreach ($this->fileAndFolderNameFilters as $filter) {
455  $this->storage->addFileAndFolderNameFilter($filter);
456  }
457  }
458 
459  break;
460 
461  case self::FILTER_MODE_USE_STORAGE_FILTERS:
462  // nothing to do here
463 
464  break;
465 
466  case self::FILTER_MODE_NO_FILTERS:
467  $useFilters = false;
468 
469  break;
470  }
471  return [$backedUpFilters, $useFilters];
472  }
473 
481  protected function restoreBackedUpFiltersInStorage($backedUpFilters)
482  {
483  if ($backedUpFilters !== null) {
484  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
485  }
486  }
487 
494  public function setFileAndFolderNameFilters(array $filters)
495  {
496  $this->fileAndFolderNameFilters = $filters;
497  }
498 
504  public function getRole()
505  {
506  return $this->storage->getRole($this);
507  }
508 
518  public function getParentFolder()
519  {
520  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
521  }
522 
528  public function getModificationTime()
529  {
530  return $this->storage->getFolderInfo($this)['mtime'];
531  }
532 
538  public function getCreationTime()
539  {
540  return $this->storage->getFolderInfo($this)['ctime'];
541  }
542 }
getPublicUrl($relativeToCurrentScript=false)
Definition: Folder.php:194
copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:359
setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:494
moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:372
getReadablePath($rootId=null)
Definition: Folder.php:105
__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:81
prepareFiltersInStorage($filterMode)
Definition: Folder.php:439
updateProperties(array $properties)
Definition: Folder.php:422
addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:289
getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort='', $sortRev=false)
Definition: Folder.php:217
addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:302
const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:68
getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false)
Definition: Folder.php:272
getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:243
restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:481