TYPO3 CMS  TYPO3_6-2
Folder.php
Go to the documentation of this file.
1 <?php
3 
18 
34 class Folder implements FolderInterface {
35 
41  protected $storage;
42 
50  protected $identifier;
51 
57  protected $name;
58 
64  protected $fileAndFolderNameFilters = array();
65 
70  // Merge local filters into storage's filters
72  // Only use the filters provided by the storage
74  // Only use the filters provided by the current class
76 
85  $this->storage = $storage;
86  $this->identifier = rtrim($identifier, '/') . '/';
87  $this->name = $name;
88  }
89 
95  public function getName() {
96  return $this->name;
97  }
98 
106  public function getReadablePath($rootId = NULL) {
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 . '/';
129  }
130 
139  public function setName($name) {
140  $this->name = $name;
141  }
142 
148  public function getStorage() {
149  return $this->storage;
150  }
151 
158  public function getIdentifier() {
159  return $this->identifier;
160  }
161 
167  public function getHashedIdentifier() {
168  return $this->storage->hashFileIdentifier($this->identifier);
169  }
170 
177  public function getCombinedIdentifier() {
178  return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
179  }
180 
190  public function getPublicUrl($relativeToCurrentScript = FALSE) {
191  return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
192  }
193 
206  public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = FALSE) {
207  // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
208  if ($filterMode === FALSE) {
209  $useFilters = FALSE;
210  $backedUpFilters = array();
211  } else {
212  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
213  }
214 
215  $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive);
216 
217  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
218 
219  return $fileObjects;
220  }
221 
231  public function getFileCount(array $filterMethods = array(), $recursive = FALSE) {
232  return count($this->storage->getFileIdentifiersInFolder($this->identifier, TRUE, $recursive));
233  }
234 
243  public function getSubfolder($name) {
244  if (!$this->storage->hasFolderInFolder($name, $this)) {
245  throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
246  }
248  $factory = ResourceFactory::getInstance();
249  $folderObject = $factory->createFolderObject($this->storage, $this->identifier . $name . '/', $name);
250  return $folderObject;
251  }
252 
261  public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS) {
262  list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
263  $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters);
264  $this->restoreBackedUpFiltersInStorage($backedUpFilters);
265  return $folderObjects;
266  }
267 
277  public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
278  $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
279  return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
280  }
281 
289  public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
290  return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
291  }
292 
299  public function rename($newName) {
300  return $this->storage->renameFolder($this, $newName);
301  }
302 
309  public function delete($deleteRecursively = TRUE) {
310  return $this->storage->deleteFolder($this, $deleteRecursively);
311  }
312 
319  public function createFile($fileName) {
320  return $this->storage->createFile($fileName, $this);
321  }
322 
329  public function createFolder($folderName) {
330  return $this->storage->createFolder($folderName, $this);
331  }
332 
341  public function copyTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
342  return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
343  }
344 
353  public function moveTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
354  return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
355  }
356 
363  public function hasFile($name) {
364  return $this->storage->hasFileInFolder($name, $this);
365  }
366 
373  public function hasFolder($name) {
374  return $this->storage->hasFolderInFolder($name, $this);
375  }
376 
383  public function checkActionPermission($action) {
384  try {
385  return $this->getStorage()->checkFolderActionPermission($action, $this);
386  } catch (Exception\ResourcePermissionsUnavailableException $e) {
387  return FALSE;
388  }
389  }
390 
400  public function updateProperties(array $properties) {
401  // Setting identifier and name to update values
402  if (isset($properties['identifier'])) {
403  $this->identifier = $properties['identifier'];
404  }
405  if (isset($properties['name'])) {
406  $this->name = $properties['name'];
407  }
408  }
409 
416  protected function prepareFiltersInStorage($filterMode) {
417  $backedUpFilters = NULL;
418  $useFilters = TRUE;
419 
420  switch ($filterMode) {
421  case self::FILTER_MODE_USE_OWN_FILTERS:
422  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
423  $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
424 
425  break;
426 
427  case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
428  if (count($this->fileAndFolderNameFilters) > 0) {
429  $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
430  foreach ($this->fileAndFolderNameFilters as $filter) {
431  $this->storage->addFileAndFolderNameFilter($filter);
432  }
433  }
434 
435  break;
436 
437  case self::FILTER_MODE_USE_STORAGE_FILTERS:
438  // nothing to do here
439 
440  break;
441 
442  case self::FILTER_MODE_NO_FILTERS:
443  $useFilters = FALSE;
444 
445  break;
446  }
447  return array($backedUpFilters, $useFilters);
448  }
449 
457  protected function restoreBackedUpFiltersInStorage($backedUpFilters) {
458  if ($backedUpFilters !== NULL) {
459  $this->storage->setFileAndFolderNameFilters($backedUpFilters);
460  }
461  }
462 
469  public function setFileAndFolderNameFilters(array $filters) {
470  $this->fileAndFolderNameFilters = $filters;
471  }
472 
478  public function getRole() {
479  return $this->storage->getRole($this);
480  }
481 
491  public function getParentFolder() {
492  return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
493  }
494 }
addUploadedFile(array $uploadedFileData, $conflictMode='cancel')
Definition: Folder.php:289
getSubfolders($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS)
Definition: Folder.php:261
setFileAndFolderNameFilters(array $filters)
Definition: Folder.php:469
moveTo(Folder $targetFolder, $targetFolderName=NULL, $conflictMode='renameNewFile')
Definition: Folder.php:353
getReadablePath($rootId=NULL)
Definition: Folder.php:106
addFile($localFilePath, $fileName=NULL, $conflictMode='cancel')
Definition: Folder.php:277
__construct(ResourceStorage $storage, $identifier, $name)
Definition: Folder.php:84
prepareFiltersInStorage($filterMode)
Definition: Folder.php:416
getFileCount(array $filterMethods=array(), $recursive=FALSE)
Definition: Folder.php:231
updateProperties(array $properties)
Definition: Folder.php:400
getPublicUrl($relativeToCurrentScript=FALSE)
Definition: Folder.php:190
const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
Definition: Folder.php:71
copyTo(Folder $targetFolder, $targetFolderName=NULL, $conflictMode='renameNewFile')
Definition: Folder.php:341
restoreBackedUpFiltersInStorage($backedUpFilters)
Definition: Folder.php:457
getFiles($start=0, $numberOfItems=0, $filterMode=self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive=FALSE)
Definition: Folder.php:206