[BUGFIX] Show full folder path in file list title
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Folder.php
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
17 use TYPO3\CMS\Core\Utility\PathUtility;
18
19 /**
20 * A folder that groups files in a storage. This may be a folder on the local
21 * disk, a bucket in Amazon S3 or a user or a tag in Flickr.
22 *
23 * This object is not persisted in TYPO3 locally, but created on the fly by
24 * storage drivers for the folders they "offer".
25 *
26 * Some folders serve as a physical container for files (e.g. folders on the
27 * local disk, S3 buckets or Flickr users). Other folders just group files by a
28 * certain criterion, e.g. a tag.
29 * The way this is implemented depends on the storage driver.
30 *
31 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
32 * @author Ingmar Schlecht <ingmar@typo3.org>
33 */
34 class Folder implements FolderInterface {
35
36 /**
37 * The storage this folder belongs to.
38 *
39 * @var ResourceStorage
40 */
41 protected $storage;
42
43 /**
44 * The identifier of this folder to identify it on the storage.
45 * On some drivers, this is the path to the folder, but drivers could also just
46 * provide any other unique identifier for this folder on the specific storage.
47 *
48 * @var string
49 */
50 protected $identifier;
51
52 /**
53 * The name of this folder
54 *
55 * @var string
56 */
57 protected $name;
58
59 /**
60 * The filters this folder should use for a file list.
61 *
62 * @var callable[]
63 */
64 protected $fileAndFolderNameFilters = array();
65
66 /**
67 * Modes for filter usage in getFiles()/getFolders()
68 */
69 const FILTER_MODE_NO_FILTERS = 0;
70 // Merge local filters into storage's filters
71 const FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS = 1;
72 // Only use the filters provided by the storage
73 const FILTER_MODE_USE_STORAGE_FILTERS = 2;
74 // Only use the filters provided by the current class
75 const FILTER_MODE_USE_OWN_FILTERS = 3;
76
77 /**
78 * Initialization of the folder
79 *
80 * @param ResourceStorage $storage
81 * @param $identifier
82 * @param $name
83 */
84 public function __construct(ResourceStorage $storage, $identifier, $name) {
85 $this->storage = $storage;
86 $this->identifier = rtrim($identifier, '/') . '/';
87 $this->name = $name;
88 }
89
90 /**
91 * Returns the name of this folder.
92 *
93 * @return string
94 */
95 public function getName() {
96 return $this->name;
97 }
98
99 /**
100 * Returns the full path of this folder, from the root.
101 *
102 * @param string $rootId ID of the root folder, NULL to auto-detect
103 *
104 * @return string
105 */
106 public function getReadablePath($rootId = NULL) {
107 if ($rootId === NULL) {
108 $rootId = $this->storage->getRootLevelFolder()->getIdentifier();
109 }
110 $readablePath = '';
111 if ($this->identifier !== $rootId) {
112 $readablePath = $this->getParentFolder()->getReadablePath($rootId);
113 }
114 return $readablePath . $this->name . '/';
115 }
116
117 /**
118 * Sets a new name of the folder
119 * currently this does not trigger the "renaming process"
120 * as the name is more seen as a label
121 *
122 * @param string $name The new name
123 * @return void
124 */
125 public function setName($name) {
126 $this->name = $name;
127 }
128
129 /**
130 * Returns the storage this folder belongs to.
131 *
132 * @return ResourceStorage
133 */
134 public function getStorage() {
135 return $this->storage;
136 }
137
138 /**
139 * Returns the path of this folder inside the storage. It depends on the
140 * type of storage whether this is a real path or just some unique identifier.
141 *
142 * @return string
143 */
144 public function getIdentifier() {
145 return $this->identifier;
146 }
147
148 /**
149 * Get hashed identifier
150 *
151 * @return string
152 */
153 public function getHashedIdentifier() {
154 return $this->storage->hashFileIdentifier($this->identifier);
155 }
156
157 /**
158 * Returns a combined identifier of this folder, i.e. the storage UID and
159 * the folder identifier separated by a colon ":".
160 *
161 * @return string Combined storage and folder identifier, e.g. StorageUID:folder/path/
162 */
163 public function getCombinedIdentifier() {
164 return $this->getStorage()->getUid() . ':' . $this->getIdentifier();
165 }
166
167 /**
168 * Returns a publicly accessible URL for this folder
169 *
170 * WARNING: Access to the folder may be restricted by further means, e.g. some
171 * web-based authentication. You have to take care of this yourself.
172 *
173 * @param bool $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
174 * @return string
175 */
176 public function getPublicUrl($relativeToCurrentScript = FALSE) {
177 return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
178 }
179
180 /**
181 * Returns a list of files in this folder, optionally filtered. There are several filter modes available, see the
182 * FILTER_MODE_* constants for more information.
183 *
184 * For performance reasons the returned items can also be limited to a given range
185 *
186 * @param int $start The item to start at
187 * @param int $numberOfItems The number of items to return
188 * @param int $filterMode The filter mode to use for the file list.
189 * @param bool $recursive
190 * @param string $sort Property name used to sort the items.
191 * Among them may be: '' (empty, no sorting), name,
192 * fileext, size, tstamp and rw.
193 * If a driver does not support the given property, it
194 * should fall back to "name".
195 * @param bool $sortRev TRUE to indicate reverse sorting (last to first)
196 * @return \TYPO3\CMS\Core\Resource\File[]
197 */
198 public function getFiles($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, $recursive = FALSE, $sort = '', $sortRev = FALSE) {
199 // Fallback for compatibility with the old method signature variable $useFilters that was used instead of $filterMode
200 if ($filterMode === FALSE) {
201 $useFilters = FALSE;
202 $backedUpFilters = array();
203 } else {
204 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
205 }
206
207 $fileObjects = $this->storage->getFilesInFolder($this, $start, $numberOfItems, $useFilters, $recursive, $sort, $sortRev);
208
209 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
210
211 return $fileObjects;
212 }
213
214 /**
215 * Returns amount of all files within this folder, optionally filtered by
216 * the given pattern
217 *
218 * @param array $filterMethods
219 * @param bool $recursive
220 * @return int
221 */
222 public function getFileCount(array $filterMethods = array(), $recursive = FALSE) {
223 return $this->storage->countFilesInFolder($this, TRUE, $recursive);
224 }
225
226 /**
227 * Returns the object for a subfolder of the current folder, if it exists.
228 *
229 * @param string $name Name of the subfolder
230 * @return Folder
231 * @throws \InvalidArgumentException
232 */
233 public function getSubfolder($name) {
234 if (!$this->storage->hasFolderInFolder($name, $this)) {
235 throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
236 }
237 /** @var $factory ResourceFactory */
238 $factory = ResourceFactory::getInstance();
239 $folderObject = $factory->createFolderObject($this->storage, $this->identifier . $name . '/', $name);
240 return $folderObject;
241 }
242
243 /**
244 * Returns a list of subfolders
245 *
246 * @param int $start The item to start at
247 * @param int $numberOfItems The number of items to return
248 * @param int $filterMode The filter mode to use for the file list.
249 * @return Folder[]
250 */
251 public function getSubfolders($start = 0, $numberOfItems = 0, $filterMode = self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS) {
252 list($backedUpFilters, $useFilters) = $this->prepareFiltersInStorage($filterMode);
253 $folderObjects = $this->storage->getFoldersInFolder($this, $start, $numberOfItems, $useFilters);
254 $this->restoreBackedUpFiltersInStorage($backedUpFilters);
255 return $folderObjects;
256 }
257
258 /**
259 * Adds a file from the local server disk. If the file already exists and
260 * overwriting is disabled,
261 *
262 * @param string $localFilePath
263 * @param string $fileName
264 * @param string $conflictMode possible value are 'cancel', 'replace', 'changeName'
265 * @return File The file object
266 */
267 public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
268 $fileName = $fileName ? $fileName : PathUtility::basename($localFilePath);
269 return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
270 }
271
272 /**
273 * Adds an uploaded file into the Storage.
274 *
275 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
276 * @param string $conflictMode possible value are 'cancel', 'replace'
277 * @return File The file object
278 */
279 public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
280 return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
281 }
282
283 /**
284 * Renames this folder.
285 *
286 * @param string $newName
287 * @return Folder
288 */
289 public function rename($newName) {
290 return $this->storage->renameFolder($this, $newName);
291 }
292
293 /**
294 * Deletes this folder from its storage. This also means that this object becomes useless.
295 *
296 * @param bool $deleteRecursively
297 * @return bool TRUE if deletion succeeded
298 */
299 public function delete($deleteRecursively = TRUE) {
300 return $this->storage->deleteFolder($this, $deleteRecursively);
301 }
302
303 /**
304 * Creates a new blank file
305 *
306 * @param string $fileName
307 * @return File The new file object
308 */
309 public function createFile($fileName) {
310 return $this->storage->createFile($fileName, $this);
311 }
312
313 /**
314 * Creates a new folder
315 *
316 * @param string $folderName
317 * @return Folder The new folder object
318 */
319 public function createFolder($folderName) {
320 return $this->storage->createFolder($folderName, $this);
321 }
322
323 /**
324 * Copies folder to a target folder
325 *
326 * @param Folder $targetFolder Target folder to copy to.
327 * @param string $targetFolderName an optional destination fileName
328 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
329 * @return Folder New (copied) folder object.
330 */
331 public function copyTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
332 return $targetFolder->getStorage()->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
333 }
334
335 /**
336 * Moves folder to a target folder
337 *
338 * @param Folder $targetFolder Target folder to move to.
339 * @param string $targetFolderName an optional destination fileName
340 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
341 * @return Folder New (copied) folder object.
342 */
343 public function moveTo(Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
344 return $targetFolder->getStorage()->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
345 }
346
347 /**
348 * Checks if a file exists in this folder
349 *
350 * @param string $name
351 * @return bool
352 */
353 public function hasFile($name) {
354 return $this->storage->hasFileInFolder($name, $this);
355 }
356
357 /**
358 * Checks if a folder exists in this folder.
359 *
360 * @param string $name
361 * @return bool
362 */
363 public function hasFolder($name) {
364 return $this->storage->hasFolderInFolder($name, $this);
365 }
366
367 /**
368 * Check if a file operation (= action) is allowed on this folder
369 *
370 * @param string $action Action that can be read, write or delete
371 * @return bool
372 */
373 public function checkActionPermission($action) {
374 return $this->getStorage()->checkFolderActionPermission($action, $this);
375 }
376
377 /**
378 * Updates the properties of this folder, e.g. after re-indexing or moving it.
379 *
380 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
381 *
382 * @param array $properties
383 * @return void
384 * @internal
385 */
386 public function updateProperties(array $properties) {
387 // Setting identifier and name to update values
388 if (isset($properties['identifier'])) {
389 $this->identifier = $properties['identifier'];
390 }
391 if (isset($properties['name'])) {
392 $this->name = $properties['name'];
393 }
394 }
395
396 /**
397 * Prepares the filters in this folder's storage according to a set filter mode.
398 *
399 * @param int $filterMode The filter mode to use; one of the FILTER_MODE_* constants
400 * @return array The backed up filters as an array (NULL if filters were not backed up) and whether to use filters or not (bool)
401 */
402 protected function prepareFiltersInStorage($filterMode) {
403 $backedUpFilters = NULL;
404 $useFilters = TRUE;
405
406 switch ($filterMode) {
407 case self::FILTER_MODE_USE_OWN_FILTERS:
408 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
409 $this->storage->setFileAndFolderNameFilters($this->fileAndFolderNameFilters);
410
411 break;
412
413 case self::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS:
414 if (count($this->fileAndFolderNameFilters) > 0) {
415 $backedUpFilters = $this->storage->getFileAndFolderNameFilters();
416 foreach ($this->fileAndFolderNameFilters as $filter) {
417 $this->storage->addFileAndFolderNameFilter($filter);
418 }
419 }
420
421 break;
422
423 case self::FILTER_MODE_USE_STORAGE_FILTERS:
424 // nothing to do here
425
426 break;
427
428 case self::FILTER_MODE_NO_FILTERS:
429 $useFilters = FALSE;
430
431 break;
432 }
433 return array($backedUpFilters, $useFilters);
434 }
435
436 /**
437 * Restores the filters of a storage.
438 *
439 * @param array $backedUpFilters The filters to restore; might be NULL if no filters have been backed up, in
440 * which case this method does nothing.
441 * @see prepareFiltersInStorage()
442 */
443 protected function restoreBackedUpFiltersInStorage($backedUpFilters) {
444 if ($backedUpFilters !== NULL) {
445 $this->storage->setFileAndFolderNameFilters($backedUpFilters);
446 }
447 }
448
449 /**
450 * Sets the filters to use when listing files. These are only used if the filter mode is one of
451 * FILTER_MODE_USE_OWN_FILTERS and FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS
452 *
453 * @param array $filters
454 */
455 public function setFileAndFolderNameFilters(array $filters) {
456 $this->fileAndFolderNameFilters = $filters;
457 }
458
459 /**
460 * Returns the role of this folder (if any). See FolderInterface::ROLE_* constants for possible values.
461 *
462 * @return int
463 */
464 public function getRole() {
465 return $this->storage->getRole($this);
466 }
467
468 /**
469 * Returns the parent folder.
470 *
471 * In non-hierarchical storages, that always is the root folder.
472 *
473 * The parent folder of the root folder is the root folder.
474 *
475 * @return Folder
476 */
477 public function getParentFolder() {
478 return $this->getStorage()->getFolder($this->getStorage()->getFolderIdentifierFromFileIdentifier($this->getIdentifier()));
479 }
480
481 }