[BUGFIX] Fallback of FAL properties does not work
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / FileReference.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Ingmar Schlecht <ingmar@typo3.org>
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 * Representation of a specific usage of a file with possibilities to override certain
31 * properties of the original file just for this usage of the file.
32 *
33 * It acts as a decorator over the original file in the way that most method calls are
34 * directly passed along to the original file object.
35 *
36 * All file related methods are directly passed along; only meta data functionality is adopted
37 * in this decorator class to priorities possible overrides for the metadata for this specific usage
38 * of the file.
39 *
40 * @author Ingmar Schlecht <ingmar@typo3.org>
41 */
42 class FileReference implements \TYPO3\CMS\Core\Resource\FileInterface {
43
44 /**
45 * Various properties of the FileReference. Note that these information can be different
46 * to the ones found in the originalFile.
47 *
48 * @var array
49 */
50 protected $propertiesOfFileReference;
51
52 /**
53 * The identifier of this file to identify it on the storage.
54 * On some drivers, this is the path to the file, but drivers could also just
55 * provide any other unique identifier for this file on the specific storage.
56 *
57 * @var string
58 */
59 protected $uidOfFileReference;
60
61 /**
62 * The file name of this file. It's either the fileName of the original underlying file,
63 * or the overlay file name supplied by the user for this particular usage (FileReference) of the file.
64 *
65 * @var string
66 */
67 protected $name;
68
69 /**
70 * The FileRepository object. Is needed e.g. for the delete() method to delete the usage record
71 * (sys_file_reference record) of this file usage.
72 *
73 * @var \TYPO3\CMS\Core\Resource\FileRepository
74 */
75 protected $fileRepository;
76
77 /**
78 * Reference to the original File object underlying this FileReference.
79 *
80 * @var \TYPO3\CMS\Core\Resource\File
81 */
82 protected $originalFile;
83
84 /**
85 * Defines properties that are merged with the parent object (File) if
86 * the value is not defined (NULL). Thus, FileReference properties act
87 * as overlays for the defined File properties.
88 *
89 * @var array
90 */
91 protected $parentFallbackProperties = array(
92 'title' => 'title',
93 'description' => 'description',
94 'alternative' => 'alternative',
95 );
96
97 /**
98 * Constructor for a file in use object. Should normally not be used
99 * directly, use the corresponding factory methods instead.
100 *
101 * @param array $fileReferenceData
102 * @param \TYPO3\CMS\Core\Resource\ResourceFactory $factory
103 */
104 public function __construct(array $fileReferenceData, $factory = NULL) {
105 $this->propertiesOfFileReference = $fileReferenceData;
106 if (!$fileReferenceData['uid_local']) {
107 throw new \InvalidArgumentException('Incorrect reference to original file given for FileReference.', 1300098528);
108 }
109 if (!$factory) {
110 /** @var $factory \TYPO3\CMS\Core\Resource\ResourceFactory */
111 $factory = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
112 }
113 $this->originalFile = $factory->getFileObject($fileReferenceData['uid_local']);
114 $this->fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
115 if (!is_object($this->originalFile)) {
116 throw new \RuntimeException('Original File not found for FileReference.', 1300098529);
117 }
118 $this->name = $fileReferenceData['name'] !== '' ? $fileReferenceData['name'] : $this->originalFile->getName();
119 }
120
121 /*******************************
122 * VARIOUS FILE PROPERTY GETTERS
123 *******************************/
124 /**
125 * Returns true if the given key exists for this file.
126 *
127 * @param string $key The property to be looked up
128 * @return boolean
129 */
130 public function hasProperty($key) {
131 return array_key_exists($key, $this->propertiesOfFileReference);
132 }
133
134 /**
135 * Gets a property, falling back to values of the parent.
136 *
137 * @param string $key The property to be looked up
138 * @return mixed
139 */
140 public function getProperty($key) {
141 $value = $this->getReferenceProperty($key);
142
143 if ($value === NULL && !empty($this->parentFallbackProperties[$key])) {
144 $value = $this->originalFile->getProperty($key);
145 }
146
147 return $value;
148 }
149
150 /**
151 * Gets a property.
152 *
153 * @param string $key The property to be looked up
154 * @return mixed
155 * @throws \InvalidArgumentException
156 */
157 public function getReferenceProperty($key) {
158 if (!$this->hasProperty($key)) {
159 throw new \InvalidArgumentException('Property "' . $key . '" was not found.', 1314226805);
160 }
161 return $this->propertiesOfFileReference[$key];
162 }
163
164 /**
165 * Gets all properties, falling back to values of the parent.
166 *
167 * @return array
168 */
169 public function getProperties() {
170 $properties = $this->getReferenceProperties();
171 $keys = array_keys($properties);
172
173 foreach ($this->parentFallbackProperties as $localKey => $parentKey) {
174 if (array_key_exists($localKey, $keys) && $properties[$localKey] === NULL) {
175 $properties[$localKey] = $this->originalFile->getProperty($parentKey);
176 }
177 }
178 }
179
180 /**
181 * Gets all properties.
182 *
183 * @return array
184 */
185 public function getReferenceProperties() {
186 return \TYPO3\CMS\Core\Utility\GeneralUtility::array_merge_recursive_overrule(
187 $this->originalFile->getProperties(),
188 $this->propertiesOfFileReference
189 );
190 }
191
192 /**
193 * Returns the name of this file
194 *
195 * @return string
196 */
197 public function getName() {
198 return $this->originalFile->getName();
199 }
200
201 /**
202 * Returns the title text to this image
203 *
204 * TODO: Possibly move this to the image domain object instead
205 *
206 * @return string
207 */
208 public function getTitle() {
209 return $this->getProperty('title');
210 }
211
212 /**
213 * Returns the alternative text to this image
214 *
215 * TODO: Possibly move this to the image domain object instead
216 *
217 * @return string
218 */
219 public function getAlternative() {
220 return $this->getProperty('alternative');
221 }
222
223 /**
224 * Returns the description text to this file
225 *
226 * TODO: Possibly move this to the image domain object instead
227 *
228 * @return string
229 */
230 public function getDescription() {
231 return $this->getProperty('description');
232 }
233
234 /**
235 * Returns the link that should be active when clicking on this image
236 *
237 * TODO: Move this to the image domain object instead
238 *
239 * @return string
240 */
241 public function getLink() {
242 return $this->propertiesOfFileReference['link'];
243 }
244
245 /**
246 * Returns the uid of this File In Use
247 *
248 * @return integer
249 */
250 public function getUid() {
251 return (int) $this->propertiesOfFileReference['uid'];
252 }
253
254 /**
255 * Returns the size of this file
256 *
257 * @return integer
258 */
259 public function getSize() {
260 return (int) $this->originalFile->getSize();
261 }
262
263 /**
264 * Returns the Sha1 of this file
265 *
266 * @return string
267 */
268 public function getSha1() {
269 return $this->originalFile->getSha1();
270 }
271
272 /**
273 * Get the file extension of this file
274 *
275 * @return string The file extension
276 */
277 public function getExtension() {
278 return $this->originalFile->getExtension();
279 }
280
281 /**
282 * Get the MIME type of this file
283 *
284 * @return array file information
285 */
286 public function getMimeType() {
287 return $this->originalFile->getMimeType();
288 }
289
290 /**
291 * Returns the modification time of the file as Unix timestamp
292 *
293 * @return integer
294 */
295 public function getModificationTime() {
296 return (int) $this->originalFile->getModificationTime();
297 }
298
299 /**
300 * Returns the creation time of the file as Unix timestamp
301 *
302 * @return integer
303 */
304 public function getCreationTime() {
305 return (int) $this->originalFile->getCreationTime();
306 }
307
308 /**
309 * Returns the fileType of this file
310 *
311 * @return integer $fileType
312 */
313 public function getType() {
314 return (int) $this->originalFile->getType();
315 }
316
317 /******************
318 * CONTENTS RELATED
319 ******************/
320 /**
321 * Get the contents of this file
322 *
323 * @return string File contents
324 */
325 public function getContents() {
326 return $this->originalFile->getContents();
327 }
328
329 /**
330 * Replace the current file contents with the given string
331 *
332 * @param string $contents The contents to write to the file.
333 * @return \TYPO3\CMS\Core\Resource\File The file object (allows chaining).
334 */
335 public function setContents($contents) {
336 return $this->originalFile->setContents($contents);
337 }
338
339 /****************************************
340 * STORAGE AND MANAGEMENT RELATED METHDOS
341 ****************************************/
342 /**
343 * Get the storage the original file is located in
344 *
345 * @return \TYPO3\CMS\Core\Resource\ResourceStorage
346 */
347 public function getStorage() {
348 return $this->originalFile->getStorage();
349 }
350
351 /**
352 * Returns the identifier of the underlying original file
353 *
354 * @return string
355 */
356 public function getIdentifier() {
357 return $this->originalFile->getIdentifier();
358 }
359
360 /**
361 * Returns a combined identifier of the underlying original file
362 *
363 * @return string Combined storage and file identifier, e.g. StorageUID:path/and/fileName.png
364 */
365 public function getCombinedIdentifier() {
366 return $this->originalFile->getCombinedIdentifier();
367 }
368
369 /**
370 * Deletes only this particular FileReference from the persistence layer
371 * (database table sys_file_reference) but leaves the original file untouched.
372 *
373 * @return boolean TRUE if deletion succeeded
374 */
375 public function delete() {
376 // TODO: Implement this function. This should only delete the
377 // FileReference (sys_file_reference) record, not the file itself.
378 throw new \BadMethodCallException('Function not implemented FileReference::delete().', 1333754461);
379 return $this->fileRepository->removeUsageRecord($this);
380 }
381
382 /**
383 * Renames the fileName in this particular usage.
384 *
385 * @param string $newName The new name
386 * @return \TYPO3\CMS\Core\Resource\FileReference
387 */
388 public function rename($newName) {
389 // TODO: Implement this function. This should only rename the
390 // FileReference (sys_file_reference) record, not the file itself.
391 throw new \BadMethodCallException('Function not implemented FileReference::rename().', 1333754473);
392 return $this->fileRepository->renameUsageRecord($this, $newName);
393 }
394
395 /*****************
396 * SPECIAL METHODS
397 *****************/
398 /**
399 * Returns a publicly accessible URL for this file
400 *
401 * WARNING: Access to the file may be restricted by further means, e.g.
402 * some web-based authentication. You have to take care of this yourself.
403 *
404 * @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)
405 * @return string
406 */
407 public function getPublicUrl($relativeToCurrentScript = FALSE) {
408 return $this->originalFile->getPublicUrl($relativeToCurrentScript);
409 }
410
411 /**
412 * Returns TRUE if this file is indexed.
413 * This is always true for FileReference objects, as they rely on a
414 * sys_file_reference record to be present, which in turn can only exist if
415 * the original file is indexed.
416 *
417 * @return boolean
418 */
419 public function isIndexed() {
420 return TRUE;
421 }
422
423 /**
424 * Returns a path to a local version of this file to process it locally (e.g. with some system tool).
425 * If the file is normally located on a remote storages, this creates a local copy.
426 * If the file is already on the local system, this only makes a new copy if $writable is set to TRUE.
427 *
428 * @param boolean $writable Set this to FALSE if you only want to do read operations on the file.
429 * @return string
430 */
431 public function getForLocalProcessing($writable = TRUE) {
432 return $this->originalFile->getForLocalProcessing($writable);
433 }
434
435 /**
436 * Returns an array representation of the file.
437 * (This is used by the generic listing module vidi when displaying file records.)
438 *
439 * @return array Array of main data of the file. Don't rely on all data to be present here, it's just a selection of the most relevant information.
440 */
441 public function toArray() {
442 $array = array_merge($this->originalFile->toArray(), $this->propertiesOfFileReference);
443 return $array;
444 }
445
446 /**
447 * Gets the original file being referenced.
448 *
449 * @return \TYPO3\CMS\Core\Resource\File
450 */
451 public function getOriginalFile() {
452 return $this->originalFile;
453 }
454
455 }
456
457
458 ?>