TYPO3 CMS  TYPO3_7-6
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 = 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  // 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 
139  public function setName($name)
140  {
141  $this->name = $name;
142  }
143 
149  public function getStorage()
150  {
151  return $this->storage;
152  }
153 
160  public function getIdentifier()
161  {
162  return $this->identifier;
163  }
164 
170  public function getHashedIdentifier()
171  {
172  return $this->storage->hashFileIdentifier($this->identifier);
173  }
174 
181  public function getCombinedIdentifier()
182  {
183  return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
184  }
185 
195  public function getPublicUrl($relativeToCurrentScript = false)
196  {
197  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
198  }
199 
218  public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false, $sort = '', $sortRev = false)
219  {
220  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
221  if ($filterMode === false) {
222  $useFilters = false;
223  $backedUpFilters = [];
224  } else {
225  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
226  }
227 
228  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
229 
230  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
231 
232  return $fileObjects;
233  }
234 
244  public function getFileCount(array $filterMethods = [], $recursive = false)
245  {
246  return $this->storage->countFilesInFolder($this, true, $recursive);
247  }
248 
256  public function getSubfolder($name)
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 
273  public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = false)
274  {
275  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
276  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
277  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
278  return $folderObjects;
279  }
280 
290  public function addFile($localFilePath, $fileName = null, $conflictMode = DuplicationBehavior::CANCEL)
291  {
292  $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
293  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
294  }
295 
303  public function addUploadedFile(array $uploadedFileData, $conflictMode = DuplicationBehavior::CANCEL)
304  {
305  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
306  }
307 
314  public function rename($newName)
315  {
316  return $this->storage->renameFolder($this, $newName);
317  }
318 
325  public function delete($deleteRecursively = true)
326  {
327  return $this->storage->deleteFolder($this, $deleteRecursively);
328  }
329 
336  public function createFile($fileName)
337  {
338  return $this->storage->createFile($fileName, $this);
339  }
340 
347  public function createFolder($folderName)
348  {
349  return $this->storage->createFolder($folderName, $this);
350  }
351 
360  public function copyTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
361  {
362  return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
363  }
364 
373  public function moveTo(Folder $targetFolder, $targetFolderName = null, $conflictMode = DuplicationBehavior::RENAME)
374  {
375  return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
376  }
377 
384  public function hasFile($name)
385  {
386  return $this->storage->hasFileInFolder($name, $this);
387  }
388 
395  public function hasFolder($name)
396  {
397  return $this->storage->hasFolderInFolder($name, $this);
398  }
399 
406  public function checkActionPermission($action)
407  {
408  try {
409  return $this->getStorage()->checkFolderActionPermission($action, $this);
410  } catch (Exception\ResourcePermissionsUnavailableException $e) {
411  return false;
412  }
413  }
414 
424  public function updateProperties(array $properties)
425  {
426  // Setting identifier and name to update values
427  if (isset($properties['identifier'])) {
428  $this->identifier = $properties['identifier'];
429  }
430  if (isset($properties['name'])) {
431  $this->name = $properties['name'];
432  }
433  }
434 
441  protected function prepareFiltersInStorage($filterMode)
442  {
443  $backedUpFilters = null;
444  $useFilters = true;
445 
446  switch ($filterMode) {
447  case self::FILTER_MODE_USE_OWN_FILTERS:
448  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
449  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
450 
451  break;
452 
453  case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
454  if (!empty($this->fileAndFolderNameFilters)) {
455  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
456  foreach ($this->fileAndFolderNameFilters as $filter) {
457  $this->storage->addFileAndFolderNameFilter($filter);
458  }
459  }
460 
461  break;
462 
463  case self::FILTER_MODE_USE_STORAGE_FILTERS:
464  // nothing to do here
465 
466  break;
467 
468  case self::FILTER_MODE_NO_FILTERS:
469  $useFilters = false;
470 
471  break;
472  }
473  return [$backedUpFilters, $useFilters];
474  }
475 
483  protected function restoreBackedUpFiltersInStorage($backedUpFilters)
484  {
485  if ($backedUpFilters !== null) {
486  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
487  }
488  }
489 
496  public function setFileAndFolderNameFilters(array $filters)
497  {
498  $this->fileAndFolderNameFilters = $filters;
499  }
500 
506  public function getRole()
507  {
508  return $this->storage->getRole($this);
509  }
510 
520  public function getParentFolder()
521  {
522  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
523  }
524 }
getPublicUrl($relativeToCurrentScript=false)
Definition: Folder.php:195
copyTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:360
setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:496
moveTo(Folder $targetFolder, $targetFolderName=null, $conflictMode=DuplicationBehavior::RENAME)
Definition: Folder.php:373
getReadablePath($rootId=null)
Definition: Folder.php:105
__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:81
prepareFiltersInStorage($filterMode)
Definition: Folder.php:441
updateProperties(array $properties)
Definition: Folder.php:424
addFile($localFilePath, $fileName=null, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:290
getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=false, $sort='', $sortRev=false)
Definition: Folder.php:218
addUploadedFile(array $uploadedFileData, $conflictMode=DuplicationBehavior::CANCEL)
Definition: Folder.php:303
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:273
getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:244
restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:483