[BUGFIX] Missing typehint in abstract FAL driver
[Packages/TYPO3.CMS.git] / t3lib / file / ProcessedFile.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2012 Benjamin Mack <benni@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28
29 /**
30 * Representation of a specific processing of a file.
31 *
32 * @author Benjamin Mack <benni@typo3.org>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class t3lib_file_ProcessedFile extends t3lib_file_AbstractFile {
37
38 /*********************************************
39 * FILE PROCESSING CONTEXTS
40 *********************************************/
41
42 /**
43 * Basic processing context to get a processed image with smaller
44 * width/height to render a preview
45 */
46 const CONTEXT_IMAGEPREVIEW = 'image.preview';
47
48 /**
49 * Standard processing context for the frontend, that was previously
50 * in tslib_cObj::getImgResource which only takes croping, masking and scaling
51 * into account
52 */
53 const CONTEXT_IMAGECROPSCALEMASK = 'image.cropscalemask';
54
55 /**
56 * Processing context
57 *
58 * @var string
59 */
60 protected $context;
61
62 /**
63 * check if the file is processed
64 *
65 * @var boolean
66 */
67 protected $processed;
68
69 /**
70 * Processing configuration
71 *
72 * @var array
73 */
74 protected $processingConfiguration;
75
76 /**
77 * Reference to the original File object underlying this FileReference.
78 *
79 * @var t3lib_file_File
80 */
81 protected $originalFile;
82
83 /**
84 * Constructor for a file processing object. Should normally not be used
85 * directly, use the corresponding factory methods instead.
86 *
87 * @param t3lib_file_File $originalFile
88 * @param string $context
89 * @param array $processingConfiguration
90 */
91 public function __construct(t3lib_file_File $originalFile, $context, array $processingConfiguration) {
92 $this->originalFile = $originalFile;
93 $this->context = $context;
94 $this->processingConfiguration = $processingConfiguration;
95 }
96
97 /*******************************
98 * VARIOUS FILE PROPERTY GETTERS
99 ************************
100
101 /**
102 * Returns the checksum that makes the processed configuration unique
103 * also takes the mtime and the uid into account, to find out if the
104 * process needs to be done again
105 *
106 * @return string
107 */
108 public function calculateChecksum() {
109 return t3lib_div::shortMD5(
110 $this->originalFile->getUid()
111 . $this->originalFile->getModificationTime() // take the modtime into account
112 . $this->context
113 . serialize($GLOBALS['TYPO3_CONF_VARS']['GFX']) // make sure to take any IM changes into account
114 . serialize($this->processingConfiguration)
115 );
116 }
117
118 /******************
119 * CONTENTS RELATED
120 ******************/
121
122 /**
123 * Replace the current file contents with the given string
124 *
125 * @param string $contents The contents to write to the file.
126 * @return t3lib_file_File The file object (allows chaining).
127 */
128 public function setContents($contents) {
129 throw new BadMethodCallException('Setting contents not possible for processed file.', 1305438528);
130 }
131
132 /****************************************
133 * STORAGE AND MANAGEMENT RELATED METHDOS
134 ****************************************/
135
136 /**
137 * Returns TRUE if this file is indexed
138 *
139 * @return boolean
140 */
141 public function isIndexed() {
142 return FALSE;
143 }
144
145 /*****************
146 * SPECIAL METHODS
147 *****************/
148 /**
149 * Returns TRUE if this file is already processed.
150 *
151 * @return boolean
152 */
153 public function isProcessed() {
154 return (bool) $this->processed;
155 }
156
157 /**
158 * Called when the processed file is processed
159 *
160 * @param boolean $isProcessed
161 * @return void
162 */
163 public function setProcessed($isProcessed) {
164 $this->processed = (boolean) $isProcessed;
165 }
166
167 /**
168 * @return t3lib_file_File
169 */
170 public function getOriginalFile() {
171 return $this->originalFile;
172 }
173
174 /**
175 * get the identifier of the file
176 * if there is no processed file in the file system (as the original file did not have to be modified e.g.
177 * when the original image is in the boundaries of the maxW/maxH stuff)
178 * then just return the identifier of the original file
179 *
180 * @return string
181 */
182 public function getIdentifier() {
183 if ($this->identifier) {
184 return $this->identifier;
185 } else {
186 return $this->originalFile->getIdentifier();
187 }
188 }
189
190 /**
191 * get the name of the file
192 * if there is no processed file in the file system (as the original file did not have to be modified e.g.
193 * when the original image is in the boundaries of the maxW/maxH stuff)
194 * then just return the name of the original file
195 *
196 * @return string
197 */
198 public function getName() {
199 if ($this->name) {
200 return $this->name;
201 } else {
202 return $this->originalFile->getName();
203 }
204 }
205
206 /**
207 * Updates properties of this object.
208 * This method is used to reconstitute settings from the
209 * database into this object after being intantiated.
210 *
211 * @param array $properties
212 */
213 public function updateProperties(array $properties) {
214 if ($properties['name']) {
215 $this->name = $properties['name'];
216 }
217 if ($properties['identifier']) {
218 $this->identifier = $properties['identifier'];
219 }
220 if (isset($properties['is_processed']) && $properties['is_processed'] > 0) {
221 $this->processed = TRUE;
222 }
223 if (t3lib_utility_Math::canBeInterpretedAsInteger($properties['storage'])) {
224 $this->setStorage($properties['storage']);
225 }
226 $this->properties = array_merge($this->properties, $properties);
227 }
228
229 /**
230 * basic array function for the DB update
231 * @return array
232 */
233 public function toArray() {
234 // @todo: define what we need here
235 return array(
236 'storage' => $this->getStorage()->getUid(),
237 'identifier' => $this->getIdentifier(),
238 'name' => $this->getName(),
239 'is_processed' => intval($this->processed),
240 'checksum' => $this->calculateChecksum(),
241 'context' => $this->context,
242 'configuration' => serialize($this->processingConfiguration),
243 'original' => $this->originalFile->getUid(),
244 'width' => $this->getProperty('width'),
245 'height' => $this->getProperty('height')
246 );
247 }
248 }
249
250 ?>