5edae4d7e514e843258de735c1ec49267ef019cf
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / DataProcessing / GalleryProcessor.php
1 <?php
2 namespace TYPO3\CMS\Frontend\DataProcessing;
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\Resource\FileInterface;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
20 use TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException;
21
22 /**
23 * This data processor will calculate rows, columns and dimensions for a gallery
24 * based on several settings and can be used for f.i. the CType "textmedia"
25 *
26 * The output will be an array which contains the rows and columns,
27 * including the file references and the calculated width and height for each media element,
28 * but also some more information of the gallery, like position, width and counters
29 *
30 * Example TypoScript configuration:
31 *
32 * 10 = TYPO3\CMS\Frontend\DataProcessing\GalleryProcessor
33 * 10 {
34 * filesProcessedDataKey = files
35 * mediaOrientation.field = imageorient
36 * numberOfColumns.field = imagecols
37 * equalMediaHeight.field = imageheight
38 * equalMediaWidth.field = imagewidth
39 * columnSpacing = 0
40 * borderEnabled.field = imageborder
41 * borderPadding = 0
42 * borderWidth = 0
43 * maxGalleryWidth = {$styles.content.mediatext.maxW}
44 * maxGalleryWidthInText = {$styles.content.mediatext.maxWInText}
45 * as = gallery
46 * }
47 *
48 * Output example:
49 *
50 * gallery {
51 * position {
52 * horizontal = center
53 * vertical = above
54 * noWrap = FALSE
55 * }
56 * width = 600
57 * count {
58 * files = 2
59 * columns = 1
60 * rows = 2
61 * }
62 * rows {
63 * 1 {
64 * columns {
65 * 1 {
66 * media = TYPO3\CMS\Core\Resource\FileReference
67 * dimensions {
68 * width = 600
69 * height = 400
70 * }
71 * }
72 * }
73 * }
74 * 2 {
75 * columns {
76 * 1 {
77 * media = TYPO3\CMS\Core\Resource\FileReference
78 * dimensions {
79 * width = 600
80 * height = 400
81 * }
82 * }
83 * }
84 * }
85 * }
86 * columnSpacing = 0
87 * border {
88 * enabled = FALSE
89 * width = 0
90 * padding = 0
91 * }
92 * }
93 *
94 */
95 class GalleryProcessor implements DataProcessorInterface
96 {
97 /**
98 * The content object renderer
99 *
100 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
101 */
102 protected $contentObjectRenderer;
103
104 /**
105 * The processor configuration
106 *
107 * @var array
108 */
109 protected $processorConfiguration;
110
111 /**
112 * Matching the tt_content field towards the imageOrient option
113 *
114 * @var array
115 */
116 protected $availableGalleryPositions = [
117 'horizontal' => [
118 'center' => [0, 8],
119 'right' => [1, 9, 17, 25],
120 'left' => [2, 10, 18, 26]
121 ],
122 'vertical' => [
123 'above' => [0, 1, 2],
124 'intext' => [17, 18, 25, 26],
125 'below' => [8, 9, 10]
126 ]
127 ];
128
129 /**
130 * Storage for processed data
131 *
132 * @var array
133 */
134 protected $galleryData = [
135 'position' => [
136 'horizontal' => '',
137 'vertical' => '',
138 'noWrap' => false
139 ],
140 'width' => 0,
141 'count' => [
142 'files' => 0,
143 'columns' => 0,
144 'rows' => 0,
145 ],
146 'columnSpacing' => 0,
147 'border' => [
148 'enabled' => false,
149 'width' => 0,
150 'padding' => 0,
151 ],
152 'rows' => []
153 ];
154
155 /**
156 * @var int
157 */
158 protected $numberOfColumns;
159
160 /**
161 * @var int
162 */
163 protected $mediaOrientation;
164
165 /**
166 * @var int
167 */
168 protected $maxGalleryWidth;
169
170 /**
171 * @var int
172 */
173 protected $maxGalleryWidthInText;
174
175 /**
176 * @var int
177 */
178 protected $equalMediaHeight;
179
180 /**
181 * @var int
182 */
183 protected $equalMediaWidth;
184
185 /**
186 * @var int
187 */
188 protected $columnSpacing;
189
190 /**
191 * @var bool
192 */
193 protected $borderEnabled;
194
195 /**
196 * @var int
197 */
198 protected $borderWidth;
199
200 /**
201 * @var int
202 */
203 protected $borderPadding;
204
205 /**
206 * The (filtered) media files to be used in the gallery
207 *
208 * @var FileInterface[]
209 */
210 protected $fileObjects = [];
211
212 /**
213 * The calculated dimensions for each media element
214 *
215 * @var array
216 */
217 protected $mediaDimensions = [];
218
219 /**
220 * Process data for a gallery, for instance the CType "textmedia"
221 *
222 * @param ContentObjectRenderer $cObj The content object renderer, which contains data of the content element
223 * @param array $contentObjectConfiguration The configuration of Content Object
224 * @param array $processorConfiguration The configuration of this processor
225 * @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
226 * @return array the processed data as key/value store
227 * @throws ContentRenderingException
228 */
229 public function process(
230 ContentObjectRenderer $cObj,
231 array $contentObjectConfiguration,
232 array $processorConfiguration,
233 array $processedData
234 ) {
235 if (isset($processorConfiguration['if.']) && !$cObj->checkIf($processorConfiguration['if.'])) {
236 return $processedData;
237 }
238
239 $this->contentObjectRenderer = $cObj;
240 $this->processorConfiguration = $processorConfiguration;
241
242 $filesProcessedDataKey = (string)$cObj->stdWrapValue(
243 'filesProcessedDataKey',
244 $processorConfiguration,
245 'files'
246 );
247 if (isset($processedData[$filesProcessedDataKey]) && is_array($processedData[$filesProcessedDataKey])) {
248 $this->fileObjects = $processedData[$filesProcessedDataKey];
249 $this->galleryData['count']['files'] = count($this->fileObjects);
250 } else {
251 throw new ContentRenderingException('No files found for key ' . $filesProcessedDataKey . ' in $processedData.', 1436809789);
252 }
253
254 $this->numberOfColumns = (int)$this->getConfigurationValue('numberOfColumns', 'imagecols');
255 $this->mediaOrientation = (int)$this->getConfigurationValue('mediaOrientation', 'imageorient');
256 $this->maxGalleryWidth = (int)$this->getConfigurationValue('maxGalleryWidth') ?: 600;
257 $this->maxGalleryWidthInText = (int)$this->getConfigurationValue('maxGalleryWidthInText') ?: 300;
258 $this->equalMediaHeight = (int)$this->getConfigurationValue('equalMediaHeight', 'imageheight');
259 $this->equalMediaWidth = (int)$this->getConfigurationValue('equalMediaWidth', 'imagewidth');
260 $this->columnSpacing = (int)$this->getConfigurationValue('columnSpacing');
261 $this->borderEnabled = (bool)$this->getConfigurationValue('borderEnabled', 'imageborder');
262 $this->borderWidth = (int)$this->getConfigurationValue('borderWidth');
263 $this->borderPadding = (int)$this->getConfigurationValue('borderPadding');
264
265 $this->determineGalleryPosition();
266 $this->determineMaximumGalleryWidth();
267
268 $this->calculateRowsAndColumns();
269 $this->calculateMediaWidthsAndHeights();
270
271 $this->prepareGalleryData();
272
273 $targetFieldName = (string)$cObj->stdWrapValue(
274 'as',
275 $processorConfiguration,
276 'gallery'
277 );
278
279 $processedData[$targetFieldName] = $this->galleryData;
280
281 return $processedData;
282 }
283
284 /**
285 * Get configuration value from processorConfiguration
286 * with when $dataArrayKey fallback to value from cObj->data array
287 *
288 * @param string $key
289 * @param string|NULL $dataArrayKey
290 * @return string
291 */
292 protected function getConfigurationValue($key, $dataArrayKey = null)
293 {
294 $defaultValue = '';
295 if ($dataArrayKey && isset($this->contentObjectRenderer->data[$dataArrayKey])) {
296 $defaultValue = $this->contentObjectRenderer->data[$dataArrayKey];
297 }
298 return $this->contentObjectRenderer->stdWrapValue(
299 $key,
300 $this->processorConfiguration,
301 $defaultValue
302 );
303 }
304
305 /**
306 * Define the gallery position
307 *
308 * Gallery has a horizontal and a vertical position towards the text
309 * and a possible wrapping of the text around the gallery.
310 *
311 * @return void
312 */
313 protected function determineGalleryPosition()
314 {
315 foreach ($this->availableGalleryPositions as $positionDirectionKey => $positionDirectionValue) {
316 foreach ($positionDirectionValue as $positionKey => $positionArray) {
317 if (in_array($this->mediaOrientation, $positionArray, true)) {
318 $this->galleryData['position'][$positionDirectionKey] = $positionKey;
319 }
320 }
321 }
322
323 if ($this->mediaOrientation === 25 || $this->mediaOrientation === 26) {
324 $this->galleryData['position']['noWrap'] = true;
325 }
326 }
327
328 /**
329 * Get the gallery width based on vertical position
330 *
331 * @return void
332 */
333 protected function determineMaximumGalleryWidth()
334 {
335 if ($this->galleryData['position']['vertical'] === 'intext') {
336 $this->galleryData['width'] = $this->maxGalleryWidthInText;
337 } else {
338 $this->galleryData['width'] = $this->maxGalleryWidth;
339 }
340 }
341
342 /**
343 * Calculate the amount of rows and columns
344 *
345 * @return void
346 */
347 protected function calculateRowsAndColumns()
348 {
349
350 // If no columns defined, set it to 1
351 $columns = max((int)$this->numberOfColumns, 1);
352
353 // When more columns than media elements, set the columns to the amount of media elements
354 if ($columns > $this->galleryData['count']['files']) {
355 $columns = $this->galleryData['count']['files'];
356 }
357
358 if ($columns === 0) {
359 $columns = 1;
360 }
361
362 // Calculate the rows from the amount of files and the columns
363 $rows = ceil($this->galleryData['count']['files'] / $columns);
364
365 $this->galleryData['count']['columns'] = $columns;
366 $this->galleryData['count']['rows'] = (int)$rows;
367 }
368
369 /**
370 * Calculate the width/height of the media elements
371 *
372 * Based on the width of the gallery, defined equal width or height by a user, the spacing between columns and
373 * the use of a border, defined by user, where the border width and padding are taken into account
374 *
375 * File objects MUST already be filtered. They need a height and width to be shown in the gallery
376 *
377 * @return void
378 */
379 protected function calculateMediaWidthsAndHeights()
380 {
381 $columnSpacingTotal = ($this->galleryData['count']['columns'] - 1) * $this->columnSpacing;
382
383 $galleryWidthMinusBorderAndSpacing = max($this->galleryData['width'] - $columnSpacingTotal, 1);
384
385 if ($this->borderEnabled) {
386 $borderPaddingTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderPadding;
387 $borderWidthTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderWidth;
388 $galleryWidthMinusBorderAndSpacing = $galleryWidthMinusBorderAndSpacing - $borderPaddingTotal - $borderWidthTotal;
389 }
390
391 // User entered a predefined height
392 if ($this->equalMediaHeight) {
393 $mediaScalingCorrection = 1;
394 $maximumRowWidth = 0;
395
396 // Calculate the scaling correction when the total of media elements is wider than the gallery width
397 for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
398 $totalRowWidth = 0;
399 for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
400 $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
401 if ($fileKey > $this->galleryData['count']['files'] - 1) {
402 break 2;
403 }
404 $currentMediaScaling = $this->equalMediaHeight / max($this->getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'height'), 1);
405 $totalRowWidth += $this->getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'width') * $currentMediaScaling;
406 }
407 $maximumRowWidth = max($totalRowWidth, $maximumRowWidth);
408 $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
409 $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
410 }
411
412 // Set the corrected dimensions for each media element
413 foreach ($this->fileObjects as $key => $fileObject) {
414 $mediaHeight = floor($this->equalMediaHeight / $mediaScalingCorrection);
415 $mediaWidth = floor(
416 $this->getCroppedDimensionalProperty($fileObject, 'width') * ($mediaHeight / max($this->getCroppedDimensionalProperty($fileObject, 'height'), 1))
417 );
418 $this->mediaDimensions[$key] = [
419 'width' => $mediaWidth,
420 'height' => $mediaHeight
421 ];
422 }
423
424 // Recalculate gallery width
425 $this->galleryData['width'] = floor($maximumRowWidth / $mediaScalingCorrection);
426
427 // User entered a predefined width
428 } elseif ($this->equalMediaWidth) {
429 $mediaScalingCorrection = 1;
430
431 // Calculate the scaling correction when the total of media elements is wider than the gallery width
432 $totalRowWidth = $this->galleryData['count']['columns'] * $this->equalMediaWidth;
433 $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
434 $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
435
436 // Set the corrected dimensions for each media element
437 foreach ($this->fileObjects as $key => $fileObject) {
438 $mediaWidth = floor($this->equalMediaWidth / $mediaScalingCorrection);
439 $mediaHeight = floor(
440 $this->getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalProperty($fileObject, 'width'), 1))
441 );
442 $this->mediaDimensions[$key] = [
443 'width' => $mediaWidth,
444 'height' => $mediaHeight
445 ];
446 }
447
448 // Recalculate gallery width
449 $this->galleryData['width'] = floor($totalRowWidth / $mediaScalingCorrection);
450
451 // Automatic setting of width and height
452 } else {
453 $maxMediaWidth = (int)($galleryWidthMinusBorderAndSpacing / $this->galleryData['count']['columns']);
454
455 foreach ($this->fileObjects as $key => $fileObject) {
456 $mediaWidth = min($maxMediaWidth, $this->getCroppedDimensionalProperty($fileObject, 'width'));
457 $mediaHeight = floor(
458 $this->getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalProperty($fileObject, 'width'), 1))
459 );
460 $this->mediaDimensions[$key] = [
461 'width' => $mediaWidth,
462 'height' => $mediaHeight
463 ];
464 }
465 }
466 }
467
468 /**
469 * When retrieving the height or width for a media file
470 * a possible cropping needs to be taken into account.
471 *
472 * @param FileInterface $fileObject
473 * @param string $dimensionalProperty 'width' or 'height'
474 * @return int
475 */
476 protected function getCroppedDimensionalProperty(FileInterface $fileObject, $dimensionalProperty)
477 {
478 if (!$fileObject->hasProperty('crop') || empty($fileObject->getProperty('crop'))) {
479 return $fileObject->getProperty($dimensionalProperty);
480 }
481 $croppingConfiguration = json_decode($fileObject->getProperty('crop'), true);
482 return (int)$croppingConfiguration[$dimensionalProperty];
483 }
484
485 /**
486 * Prepare the gallery data
487 *
488 * Make an array for rows, columns and configuration
489 *
490 * @return void
491 */
492 protected function prepareGalleryData()
493 {
494 for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
495 for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
496 $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
497
498 $this->galleryData['rows'][$row]['columns'][$column] = [
499 'media' => $this->fileObjects[$fileKey],
500 'dimensions' => [
501 'width' => $this->mediaDimensions[$fileKey]['width'],
502 'height' => $this->mediaDimensions[$fileKey]['height']
503 ]
504 ];
505 }
506 }
507
508 $this->galleryData['columnSpacing'] = $this->columnSpacing;
509 $this->galleryData['border']['enabled'] = $this->borderEnabled;
510 $this->galleryData['border']['width'] = $this->borderWidth;
511 $this->galleryData['border']['padding'] = $this->borderPadding;
512 }
513 }