[BUGFIX] Folder: getSubfolders() has no range parameters
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Folder.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Andreas Wolf <andreas.wolf@ikt-werk.de>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * A folder that groups files in a storage. This may be a folder on the local
31 * disk, a bucket in Amazon S3 or a user or a tag in Flickr.
32 *
33 * This object is not persisted in TYPO3 locally, but created on the fly by
34 * storage drivers for the folders they "offer".
35 *
36 * Some folders serve as a physical container for files (e.g. folders on the
37 * local disk, S3 buckets or Flickr users). Other folders just group files by a
38 * certain criterion, e.g. a tag.
39 * The way this is implemented depends on the storage driver.
40 *
41 * @author Andreas Wolf <andreas.wolf@ikt-werk.de>
42 * @author Ingmar Schlecht <ingmar@typo3.org>
43 * @package TYPO3
44 * @subpackage t3lib
45 */
46 class Folder implements \TYPO3\CMS\Core\Resource\FolderInterface {
47
48 /**
49 * The storage this folder belongs to.
50 *
51 * @var \TYPO3\CMS\Core\Resource\ResourceStorage
52 */
53 protected $storage;
54
55 /**
56 * The identifier of this folder to identify it on the storage.
57 * On some drivers, this is the path to the folder, but drivers could also just
58 * provide any other unique identifier for this folder on the specific storage.
59 *
60 * @var string
61 */
62 protected $identifier;
63
64 /**
65 * The name of this folder
66 *
67 * @var string
68 */
69 protected $name;
70
71 /**
72 * Initialization of the folder
73 *
74 * @param \TYPO3\CMS\Core\Resource\ResourceStorage $storage
75 * @param $identifier
76 * @param $name
77 */
78 public function __construct(\TYPO3\CMS\Core\Resource\ResourceStorage $storage, $identifier, $name) {
79 $this->storage = $storage;
80 $this->identifier = rtrim($identifier, '/') . '/';
81 $this->name = $name;
82 }
83
84 /**
85 * Returns the name of this folder.
86 *
87 * @return string
88 */
89 public function getName() {
90 return $this->name;
91 }
92
93 /**
94 * Sets a new name of the folder
95 * currently this does not trigger the "renaming process"
96 * as the name is more seen as a label
97 *
98 * @param string $name The new name
99 * @return void
100 */
101 public function setName($name) {
102 $this->name = $name;
103 }
104
105 /**
106 * Returns the storage this folder belongs to.
107 *
108 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
109 */
110 public function getStorage() {
111 return $this->storage;
112 }
113
114 /**
115 * Returns the path of this folder inside the storage. It depends on the
116 * type of storage whether this is a real path or just some unique identifier.
117 *
118 * @return string
119 */
120 public function getIdentifier() {
121 return $this->identifier;
122 }
123
124 /**
125 * Returns a combined identifier of this folder, i.e. the storage UID and
126 * the folder identifier separated by a colon ":".
127 *
128 * @return string Combined storage and folder identifier, e.g. StorageUID:folder/path/
129 */
130 public function getCombinedIdentifier() {
131 // @todo $this->properties is never defined nor used here
132 if (is_array($this->properties) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->properties['storage'])) {
133 $combinedIdentifier = $this->properties['storage'] . ':' . $this->getIdentifier();
134 } else {
135 $combinedIdentifier = $this->getStorage()->getUid() . ':' . $this->getIdentifier();
136 }
137 return $combinedIdentifier;
138 }
139
140 /**
141 * Returns a publicly accessible URL for this folder
142 *
143 * WARNING: Access to the folder may be restricted by further means, e.g. some
144 * web-based authentication. You have to take care of this yourself.
145 *
146 * @param boolean $relativeToCurrentScript Determines whether the URL returned should be relative to the current script, in case it is relative at all (only for the LocalDriver)
147 * @return string
148 */
149 public function getPublicUrl($relativeToCurrentScript = FALSE) {
150 return $this->getStorage()->getPublicUrl($this, $relativeToCurrentScript);
151 }
152
153 /**
154 * Returns a list of files in this folder, optionally filtered by the given pattern.
155 * For performance reasons the returned items can be limited to a given range
156 *
157 * @param integer $start The item to start at
158 * @param integer $numberOfItems The number of items to return
159 * @param boolean $useFilters
160 * @return \TYPO3\CMS\Core\Resource\File[]
161 */
162 public function getFiles($start = 0, $numberOfItems = 0, $useFilters = TRUE) {
163 // TODO fetch
164 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
165 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
166 $fileArray = $this->storage->getFileList($this->identifier, $start, $numberOfItems, $useFilters);
167 $fileObjects = array();
168 foreach ($fileArray as $fileInfo) {
169 $fileObjects[] = $factory->createFileObject($fileInfo);
170 }
171 return $fileObjects;
172 }
173
174 /**
175 * Returns amount of all files within this folder, optionally filtered by
176 * the given pattern
177 *
178 * @param array $filterMethods
179 * @return integer
180 */
181 public function getFileCount(array $filterMethods = array()) {
182 // TODO replace by call to count()
183 return count($this->storage->getFileList($this->identifier, 0, 0, $filterMethods));
184 }
185
186 /**
187 * Returns the object for a subfolder of the current folder, if it exists.
188 *
189 * @param string $name Name of the subfolder
190 * @return \TYPO3\CMS\Core\Resource\Folder
191 */
192 public function getSubfolder($name) {
193 if (!$this->storage->hasFolderInFolder($name, $this)) {
194 throw new \InvalidArgumentException('Folder "' . $name . '" does not exist in "' . $this->identifier . '"', 1329836110);
195 }
196 // TODO this will not work with non-hierarchical storages -> the identifier for subfolders is not composed of
197 // the current item's identifier for these
198 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
199 $factory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
200 $folderObject = $factory->createFolderObject($this->storage, $this->identifier . $name . '/', $name);
201 return $folderObject;
202 }
203
204 /**
205 * Returns a list of all subfolders
206 *
207 * @param integer $start The item to start at
208 * @param integer $numberOfItems The number of items to return
209 * @return \TYPO3\CMS\Core\Resource\Folder[]
210 */
211 public function getSubfolders($start = 0, $numberOfItems = 0) {
212 $folderObjects = array();
213 $folderArray = $this->storage->getFolderList($this->identifier, $start, $numberOfItems);
214 if (count($folderArray) > 0) {
215 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
216 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
217 // TODO this will not work with non-hierarchical storages
218 // -> the identifier for subfolders is not composed of the
219 // current item's identifier for these
220 foreach ($folderArray as $folder) {
221 $folderObjects[] = $factory->createFolderObject($this->storage, $this->identifier . $folder['name'] . '/', $folder['name']);
222 }
223 }
224 return $folderObjects;
225 }
226
227 /**
228 * Adds a file from the local server disk. If the file already exists and
229 * overwriting is disabled,
230 *
231 * @param string $localFilePath
232 * @param string $fileName
233 * @param string $conflictMode possible value are 'cancel', 'replace'
234 * @return \TYPO3\CMS\Core\Resource\File The file object
235 */
236 public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
237 $fileName = $fileName ? $fileName : basename($localFilePath);
238 return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
239 }
240
241 /**
242 * Adds an uploaded file into the Storage.
243 *
244 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
245 * @param string $conflictMode possible value are 'cancel', 'replace'
246 * @return \TYPO3\CMS\Core\Resource\File The file object
247 */
248 public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
249 return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
250 }
251
252 /**
253 * Renames this folder.
254 *
255 * @param string $newName
256 * @return \TYPO3\CMS\Core\Resource\Folder
257 */
258 public function rename($newName) {
259 return $this->storage->renameFolder($this, $newName);
260 }
261
262 /**
263 * Deletes this folder from its storage. This also means that this object becomes useless.
264 *
265 * @param boolean $deleteRecursively
266 * @return boolean TRUE if deletion succeeded
267 */
268 public function delete($deleteRecursively = TRUE) {
269 return $this->storage->deleteFolder($this, $deleteRecursively);
270 }
271
272 /**
273 * Creates a new blank file
274 *
275 * @param string $fileName
276 * @return \TYPO3\CMS\Core\Resource\File The new file object
277 */
278 public function createFile($fileName) {
279 return $this->storage->createFile($fileName, $this);
280 }
281
282 /**
283 * Creates a new folder
284 *
285 * @param string $folderName
286 * @return \TYPO3\CMS\Core\Resource\Folder The new folder object
287 */
288 public function createFolder($folderName) {
289 return $this->storage->createFolder($folderName, $this);
290 }
291
292 /**
293 * Copies folder to a target folder
294 *
295 * @param \TYPO3\CMS\Core\Resource\Folder $targetFolder Target folder to copy to.
296 * @param string $targetFolderName an optional destination fileName
297 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
298 * @return \TYPO3\CMS\Core\Resource\Folder New (copied) folder object.
299 */
300 public function copyTo(\TYPO3\CMS\Core\Resource\Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
301 return $this->storage->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
302 }
303
304 /**
305 * Moves folder to a target folder
306 *
307 * @param \TYPO3\CMS\Core\Resource\Folder $targetFolder Target folder to move to.
308 * @param string $targetFolderName an optional destination fileName
309 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
310 * @return \TYPO3\CMS\Core\Resource\Folder New (copied) folder object.
311 */
312 public function moveTo(\TYPO3\CMS\Core\Resource\Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
313 return $this->storage->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
314 }
315
316 /**
317 * Checks if a file exists in this folder
318 *
319 * @param string $name
320 * @return boolean
321 */
322 public function hasFile($name) {
323 return $this->storage->hasFileInFolder($name, $this);
324 }
325
326 /**
327 * Checks if a folder exists in this folder.
328 *
329 * @param string $name
330 * @return boolean
331 */
332 public function hasFolder($name) {
333 return $this->storage->hasFolderInFolder($name, $this);
334 }
335
336 /**
337 * Check if a file operation (= action) is allowed on this folder
338 *
339 * @param string $action Action that can be read, write or delete
340 * @return boolean
341 */
342 public function checkActionPermission($action) {
343 return $this->getStorage()->checkFolderActionPermission($action, $this);
344 }
345
346 /**
347 * Updates the properties of this folder, e.g. after re-indexing or moving it.
348 *
349 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
350 *
351 * @param array $properties
352 * @return void
353 * @internal
354 */
355 public function updateProperties(array $properties) {
356 // Setting identifier and name to update values
357 if (isset($properties['identifier'])) {
358 $this->identifier = $properties['identifier'];
359 }
360 if (isset($properties['name'])) {
361 $this->name = $properties['name'];
362 }
363 }
364
365 }
366
367
368 ?>