TYPO3  7.6
core/Classes/Resource/Folder.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
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 = array();
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 = rtrim($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  $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
109  }
110  $readablePath = '/';
111  if ($this->identifier !== $rootId) {
112  try {
113  $readablePath = $this->getParentFolder()->getReadablePath($rootId);
114  } catch (Exception\InsufficientFolderAccessPermissionsException $e) {
115  // May no access to parent folder (e.g. because of mount point)
116  $readablePath = '/';
117  }
118  }
119  return $readablePath . ($this->name ? $this->name . '/' : '');
120  }
121 
130  public function setName($name)
131  {
132  $this->name = $name;
133  }
134 
140  public function getStorage()
141  {
142  return $this->storage;
143  }
144 
151  public function getIdentifier()
152  {
153  return $this->identifier;
154  }
155 
161  public function getHashedIdentifier()
162  {
163  return $this->storage->hashFileIdentifier($this->identifier);
164  }
165 
172  public function getCombinedIdentifier()
173  {
174  return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
175  }
176 
186  public function getPublicUrl($relativeToCurrentScript = false)
187  {
188  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
189  }
190 
209  public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
210  {
211  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
212  if ($filterMode === false) {
213  $useFilters = false;
214  $backedUpFilters = array();
215  } else {
216  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
217  }
218 
219  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
220 
221  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
222 
223  return $fileObjects;
224  }
225 
235  public function getFileCount(array $filterMethods = array(), $recursive = false)
236  {
237  return $this->storage->countFilesInFolder($this, true, $recursive);
238  }
239 
247  public function getSubfolder($name)
248  {
249  if (!$this->storage->hasFolderInFolder($name, $this)) {
250  throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
251  }
252  return $this->storage->getFolderInFolder($name, $this);
253  }
254 
264  public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
265  {
266  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
267  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
268  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
269  return $folderObjects;
270  }
271 
281  public function addFile($localFilePath, $fileName = null, $conflictMode = DuplicationBehavior::CANCEL)
282  {
283  $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
284  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
285  }
286 
294  public function addUploadedFile(array $uploadedFileData, $conflictMode = DuplicationBehavior::CANCEL)
295  {
296  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
297  }
298 
305  public function rename($newName)
306  {
307  return $this->storage->renameFolder($this, $newName);
308  }
309 
316  public function delete($deleteRecursively = true)
317  {
318  return $this->storage->deleteFolder($this, $deleteRecursively);
319  }
320 
327  public function createFile($fileName)
328  {
329  return $this->storage->createFile($fileName, $this);
330  }
331 
338  public function createFolder($folderName)
339  {
340  return $this->storage->createFolder($folderName, $this);
341  }
342 
351  public function copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
352  {
353  return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
354  }
355 
364  public function moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
365  {
366  return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
367  }
368 
375  public function hasFile($name)
376  {
377  return $this->storage->hasFileInFolder($name, $this);
378  }
379 
386  public function hasFolder($name)
387  {
388  return $this->storage->hasFolderInFolder($name, $this);
389  }
390 
397  public function checkActionPermission($action)
398  {
399  try {
400  return $this->getStorage()->checkFolderActionPermission($action, $this);
401  } catch (Exception\ResourcePermissionsUnavailableException $e) {
402  return false;
403  }
404  }
405 
415  public function updateProperties(array $properties)
416  {
417  // Setting identifier and name to update values
418  if (isset($properties['identifier'])) {
419  $this->identifier = $properties['identifier'];
420  }
421  if (isset($properties['name'])) {
422  $this->name = $properties['name'];
423  }
424  }
425 
432  protected function prepareFiltersInStorage($filterMode)
433  {
434  $backedUpFilters = null;
435  $useFilters = true;
436 
437  switch ($filterMode) {
438  case self::FILTER_MODE_USE_OWN_FILTERS:
439  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
440  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
441 
442  break;
443 
444  case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
445  if (!empty($this->fileAndFolderNameFilters)) {
446  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
447  foreach ($this->fileAndFolderNameFilters as $filter) {
448  $this->storage->addFileAndFolderNameFilter($filter);
449  }
450  }
451 
452  break;
453 
454  case self::FILTER_MODE_USE_STORAGE_FILTERS:
455  // nothing to do here
456 
457  break;
458 
459  case self::FILTER_MODE_NO_FILTERS:
460  $useFilters = false;
461 
462  break;
463  }
464  return array($backedUpFilters, $useFilters);
465  }
466 
474  protected function restoreBackedUpFiltersInStorage($backedUpFilters)
475  {
476  if ($backedUpFilters !== null) {
477  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
478  }
479  }
480 
487  public function setFileAndFolderNameFilters(array $filters)
488  {
489  $this->fileAndFolderNameFilters = $filters;
490  }
491 
497  public function getRole()
498  {
499  return $this->storage->getRole($this);
500  }
501 
511  public function getParentFolder()
512  {
513  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
514  }
515 }