[BUGFIX] Streamline EXT:fluid_styled_content typoscript
[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 $defaultValue = '';
294 if ($dataArrayKey && isset($this->contentObjectRenderer->data[$dataArrayKey])) {
295 $defaultValue = $this->contentObjectRenderer->data[$dataArrayKey];
296 }
297 return $this->contentObjectRenderer->stdWrapValue(
298 $key,
299 $this->processorConfiguration,
300 $defaultValue
301 );
302 }
303
304 /**
305 * Define the gallery position
306 *
307 * Gallery has a horizontal and a vertical position towards the text
308 * and a possible wrapping of the text around the gallery.
309 *
310 * @return void
311 */
312 protected function determineGalleryPosition() {
313 foreach ($this->availableGalleryPositions as $positionDirectionKey => $positionDirectionValue) {
314 foreach ($positionDirectionValue as $positionKey => $positionArray) {
315 if (in_array($this->mediaOrientation, $positionArray, TRUE)) {
316 $this->galleryData['position'][$positionDirectionKey] = $positionKey;
317 }
318 }
319 }
320
321 if ($this->mediaOrientation === 25 || $this->mediaOrientation === 26) {
322 $this->galleryData['position']['noWrap'] = TRUE;
323 }
324 }
325
326 /**
327 * Get the gallery width based on vertical position
328 *
329 * @return void
330 */
331 protected function determineMaximumGalleryWidth() {
332 if ($this->galleryData['position']['vertical'] === 'intext') {
333 $this->galleryData['width'] = $this->maxGalleryWidthInText;
334 } else {
335 $this->galleryData['width'] = $this->maxGalleryWidth;
336 }
337 }
338
339 /**
340 * Calculate the amount of rows and columns
341 *
342 * @return void
343 */
344 protected function calculateRowsAndColumns() {
345
346 // If no columns defined, set it to 1
347 $columns = max((int)$this->numberOfColumns, 1);
348
349 // When more columns than media elements, set the columns to the amount of media elements
350 if ($columns > $this->galleryData['count']['files']) {
351 $columns = $this->galleryData['count']['files'];
352 }
353
354 if ($columns === 0) {
355 $columns = 1;
356 }
357
358 // Calculate the rows from the amount of files and the columns
359 $rows = ceil($this->galleryData['count']['files'] / $columns);
360
361 $this->galleryData['count']['columns'] = $columns;
362 $this->galleryData['count']['rows'] = (int)$rows;
363 }
364
365 /**
366 * Calculate the width/height of the media elements
367 *
368 * Based on the width of the gallery, defined equal width or height by a user, the spacing between columns and
369 * the use of a border, defined by user, where the border width and padding are taken into account
370 *
371 * File objects MUST already be filtered. They need a height and width to be shown in the gallery
372 *
373 * @return void
374 */
375 protected function calculateMediaWidthsAndHeights() {
376
377 $columnSpacingTotal = ($this->galleryData['count']['columns'] - 1) * $this->columnSpacing;
378
379 $galleryWidthMinusBorderAndSpacing = max($this->galleryData['width'] - $columnSpacingTotal, 1);
380
381 if ($this->borderEnabled) {
382 $borderPaddingTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderPadding;
383 $borderWidthTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderWidth;
384 $galleryWidthMinusBorderAndSpacing = $galleryWidthMinusBorderAndSpacing - $borderPaddingTotal - $borderWidthTotal;
385 }
386
387 // User entered a predefined height
388 if ($this->equalMediaHeight) {
389 $mediaScalingCorrection = 1;
390 $maximumRowWidth = 0;
391
392 // Calculate the scaling correction when the total of media elements is wider than the gallery width
393 for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
394 $totalRowWidth = 0;
395 for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
396 $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
397 if ($fileKey > $this->galleryData['count']['files'] - 1) {
398 break 2;
399 }
400 $currentMediaScaling = $this->equalMediaHeight / max($this->getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'height'), 1);
401 $totalRowWidth += $this->getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'width') * $currentMediaScaling;
402 }
403 $maximumRowWidth = max($totalRowWidth, $maximumRowWidth);
404 $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
405 $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
406 }
407
408 // Set the corrected dimensions for each media element
409 foreach ($this->fileObjects as $key => $fileObject) {
410 $mediaHeight = floor($this->equalMediaHeight / $mediaScalingCorrection);
411 $mediaWidth = floor(
412 $this->getCroppedDimensionalProperty($fileObject, 'width') * ($mediaHeight / max($this->getCroppedDimensionalProperty($fileObject, 'height'), 1))
413 );
414 $this->mediaDimensions[$key] = [
415 'width' => $mediaWidth,
416 'height' => $mediaHeight
417 ];
418 }
419
420 // Recalculate gallery width
421 $this->galleryData['width'] = floor($maximumRowWidth / $mediaScalingCorrection);
422
423 // User entered a predefined width
424 } elseif ($this->equalMediaWidth) {
425 $mediaScalingCorrection = 1;
426
427 // Calculate the scaling correction when the total of media elements is wider than the gallery width
428 $totalRowWidth = $this->galleryData['count']['columns'] * $this->equalMediaWidth;
429 $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
430 $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
431
432 // Set the corrected dimensions for each media element
433 foreach ($this->fileObjects as $key => $fileObject) {
434 $mediaWidth = floor($this->equalMediaWidth / $mediaScalingCorrection);
435 $mediaHeight = floor(
436 $this->getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalProperty($fileObject, 'width'), 1))
437 );
438 $this->mediaDimensions[$key] = [
439 'width' => $mediaWidth,
440 'height' => $mediaHeight
441 ];
442 }
443
444 // Recalculate gallery width
445 $this->galleryData['width'] = floor($totalRowWidth / $mediaScalingCorrection);
446
447 // Automatic setting of width and height
448 } else {
449 $maxMediaWidth = (int)($galleryWidthMinusBorderAndSpacing / $this->galleryData['count']['columns']);
450
451 foreach ($this->fileObjects as $key => $fileObject) {
452 $mediaWidth = min($maxMediaWidth, $this->getCroppedDimensionalProperty($fileObject, 'width'));
453 $mediaHeight = floor(
454 $this->getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->getCroppedDimensionalProperty($fileObject, 'width'), 1))
455 );
456 $this->mediaDimensions[$key] = [
457 'width' => $mediaWidth,
458 'height' => $mediaHeight
459 ];
460 }
461 }
462 }
463
464 /**
465 * When retrieving the height or width for a media file
466 * a possible cropping needs to be taken into account.
467 *
468 * @param FileInterface $fileObject
469 * @param string $dimensionalProperty 'width' or 'height'
470 * @return int
471 */
472 protected function getCroppedDimensionalProperty(FileInterface $fileObject, $dimensionalProperty) {
473 if (!$fileObject->hasProperty('crop') || empty($fileObject->getProperty('crop'))) {
474 return $fileObject->getProperty($dimensionalProperty);
475 }
476 $croppingConfiguration = json_decode($fileObject->getProperty('crop'), TRUE);
477 return (int)$croppingConfiguration[$dimensionalProperty];
478 }
479
480 /**
481 * Prepare the gallery data
482 *
483 * Make an array for rows, columns and configuration
484 *
485 * @return void
486 */
487 protected function prepareGalleryData() {
488 for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
489
490 for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
491
492 $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
493
494 $this->galleryData['rows'][$row]['columns'][$column] = [
495 'media' => $this->fileObjects[$fileKey],
496 'dimensions' => [
497 'width' => $this->mediaDimensions[$fileKey]['width'],
498 'height' => $this->mediaDimensions[$fileKey]['height']
499 ]
500 ];
501 }
502 }
503
504 $this->galleryData['columnSpacing'] = $this->columnSpacing;
505 $this->galleryData['border']['enabled'] = $this->borderEnabled;
506 $this->galleryData['border']['width'] = $this->borderWidth;
507 $this->galleryData['border']['padding'] = $this->borderPadding;
508 }
509 }