[TASK] Move and Namespace classes
[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 t3lib_file_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 * @return t3lib_file_Folder[]
208 */
209 public function getSubfolders() {
210 $folderObjects = array();
211 $folderArray = $this->storage->getFolderList($this->identifier);
212 if (count($folderArray) > 0) {
213 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
214 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
215 // TODO this will not work with non-hierarchical storages
216 // -> the identifier for subfolders is not composed of the
217 // current item's identifier for these
218 foreach ($folderArray as $folder) {
219 $folderObjects[] = $factory->createFolderObject($this->storage, ($this->identifier . $folder['name']) . '/', $folder['name']);
220 }
221 }
222 return $folderObjects;
223 }
224
225 /**
226 * Adds a file from the local server disk. If the file already exists and
227 * overwriting is disabled,
228 *
229 * @param string $localFilePath
230 * @param string $fileName
231 * @param string $conflictMode possible value are 'cancel', 'replace'
232 * @return \TYPO3\CMS\Core\Resource\File The file object
233 */
234 public function addFile($localFilePath, $fileName = NULL, $conflictMode = 'cancel') {
235 $fileName = $fileName ? $fileName : basename($localFilePath);
236 return $this->storage->addFile($localFilePath, $this, $fileName, $conflictMode);
237 }
238
239 /**
240 * Adds an uploaded file into the Storage.
241 *
242 * @param array $uploadedFileData contains information about the uploaded file given by $_FILES['file1']
243 * @param string $conflictMode possible value are 'cancel', 'replace'
244 * @return \TYPO3\CMS\Core\Resource\File The file object
245 */
246 public function addUploadedFile(array $uploadedFileData, $conflictMode = 'cancel') {
247 return $this->storage->addUploadedFile($uploadedFileData, $this, $uploadedFileData['name'], $conflictMode);
248 }
249
250 /**
251 * Renames this folder.
252 *
253 * @param string $newName
254 * @return \TYPO3\CMS\Core\Resource\Folder
255 */
256 public function rename($newName) {
257 return $this->storage->renameFolder($this, $newName);
258 }
259
260 /**
261 * Deletes this folder from its storage. This also means that this object becomes useless.
262 *
263 * @param boolean $deleteRecursively
264 * @return boolean TRUE if deletion succeeded
265 */
266 public function delete($deleteRecursively = TRUE) {
267 return $this->storage->deleteFolder($this, $deleteRecursively);
268 }
269
270 /**
271 * Creates a new blank file
272 *
273 * @param string $fileName
274 * @return \TYPO3\CMS\Core\Resource\File The new file object
275 */
276 public function createFile($fileName) {
277 return $this->storage->createFile($fileName, $this);
278 }
279
280 /**
281 * Creates a new folder
282 *
283 * @param string $folderName
284 * @return \TYPO3\CMS\Core\Resource\Folder The new folder object
285 */
286 public function createFolder($folderName) {
287 return $this->storage->createFolder($folderName, $this);
288 }
289
290 /**
291 * Copies folder to a target folder
292 *
293 * @param \TYPO3\CMS\Core\Resource\Folder $targetFolder Target folder to copy to.
294 * @param string $targetFolderName an optional destination fileName
295 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
296 * @return \TYPO3\CMS\Core\Resource\Folder New (copied) folder object.
297 */
298 public function copyTo(\TYPO3\CMS\Core\Resource\Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
299 return $this->storage->copyFolder($this, $targetFolder, $targetFolderName, $conflictMode);
300 }
301
302 /**
303 * Moves folder to a target folder
304 *
305 * @param \TYPO3\CMS\Core\Resource\Folder $targetFolder Target folder to move to.
306 * @param string $targetFolderName an optional destination fileName
307 * @param string $conflictMode "overrideExistingFile", "renameNewFile" or "cancel
308 * @return \TYPO3\CMS\Core\Resource\Folder New (copied) folder object.
309 */
310 public function moveTo(\TYPO3\CMS\Core\Resource\Folder $targetFolder, $targetFolderName = NULL, $conflictMode = 'renameNewFile') {
311 return $this->storage->moveFolder($this, $targetFolder, $targetFolderName, $conflictMode);
312 }
313
314 /**
315 * Checks if a file exists in this folder
316 *
317 * @param string $name
318 * @return boolean
319 */
320 public function hasFile($name) {
321 return $this->storage->hasFileInFolder($name, $this);
322 }
323
324 /**
325 * Checks if a folder exists in this folder.
326 *
327 * @param string $name
328 * @return boolean
329 */
330 public function hasFolder($name) {
331 return $this->storage->hasFolderInFolder($name, $this);
332 }
333
334 /**
335 * Check if a file operation (= action) is allowed on this folder
336 *
337 * @param string $action Action that can be read, write or delete
338 * @return boolean
339 */
340 public function checkActionPermission($action) {
341 return $this->getStorage()->checkFolderActionPermission($action, $this);
342 }
343
344 /**
345 * Updates the properties of this folder, e.g. after re-indexing or moving it.
346 *
347 * NOTE: This method should not be called from outside the File Abstraction Layer (FAL)!
348 *
349 * @param array $properties
350 * @return void
351 * @internal
352 */
353 public function updateProperties(array $properties) {
354 // Setting identifier and name to update values
355 if (isset($properties['identifier'])) {
356 $this->identifier = $properties['identifier'];
357 }
358 if (isset($properties['name'])) {
359 $this->name = $properties['name'];
360 }
361 }
362
363 }
364
365
366 ?>