e70c5f3bcb1d4b8666b1f1dd95a866dd0f98bcac
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / ViewHelpers / GridColumnClassAutoConfigurationViewHelper.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Form\ViewHelpers;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
19 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
20 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
22
23 /**
24 * Scope: frontend
25 * @api
26 */
27 class GridColumnClassAutoConfigurationViewHelper extends AbstractViewHelper
28 {
29 use CompileWithRenderStatic;
30
31 /**
32 * @var bool
33 */
34 protected $escapeOutput = false;
35
36 /**
37 * Initialize the arguments.
38 *
39 * @return void
40 * @internal
41 */
42 public function initializeArguments()
43 {
44 parent::initializeArguments();
45 $this->registerArgument('element', RootRenderableInterface::class, 'A RootRenderableInterface instance', true);
46 }
47
48 /**
49 * @param array $arguments
50 * @param \Closure $renderChildrenClosure
51 * @param RenderingContextInterface $renderingContext
52 * @return string
53 * @public
54 */
55 public static function renderStatic(
56 array $arguments,
57 \Closure $renderChildrenClosure,
58 RenderingContextInterface $renderingContext
59 ) {
60 $formElement = $arguments['element'];
61
62 $gridRowElement = $formElement->getParentRenderable();
63 $gridContainerElement = $gridRowElement->getParentRenderable();
64 $gridRowEChildElements = $gridRowElement->getElementsRecursively();
65
66 $gridContainerViewPortConfiguration = $gridContainerElement->getProperties()['gridColumnClassAutoConfiguration'];
67 if (empty($gridContainerViewPortConfiguration)) {
68 return '';
69 }
70
71 $gridSize = (int)$gridContainerViewPortConfiguration['gridSize'];
72
73 $columnsToCalculate = [];
74 $usedColumns = [];
75 foreach ($gridRowEChildElements as $childElement) {
76 if (empty($childElement->getProperties()['gridColumnClassAutoConfiguration'])) {
77 foreach ($gridContainerViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
78 $columnsToCalculate[$viewPortName]['elements']++;
79 }
80 } else {
81 $gridColumnViewPortConfiguration = $childElement->getProperties()['gridColumnClassAutoConfiguration'];
82 foreach ($gridContainerViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
83 $configuration = $gridColumnViewPortConfiguration['viewPorts'][$viewPortName];
84 if (
85 isset($configuration['numbersOfColumnsToUse'])
86 && (int)$configuration['numbersOfColumnsToUse'] > 0
87 ) {
88 $usedColumns[$viewPortName]['sum'] += (int)$configuration['numbersOfColumnsToUse'];
89 if ($childElement->getIdentifier() === $formElement->getIdentifier()) {
90 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = (int)$configuration['numbersOfColumnsToUse'];
91 if ($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] > $gridSize) {
92 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = $gridSize;
93 }
94 }
95 } else {
96 $columnsToCalculate[$viewPortName]['elements']++;
97 }
98 }
99 }
100 }
101
102 $classes = [];
103 foreach ($gridContainerViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
104 if (isset($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'])) {
105 $numbersOfColumnsToUse = $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'];
106 } else {
107 $restColumnsToDivide = $gridSize - $usedColumns[$viewPortName]['sum'];
108 $restElements = (int)$columnsToCalculate[$viewPortName]['elements'];
109
110 if ($restColumnsToDivide < 1) {
111 $restColumnsToDivide = 1;
112 }
113 if ($restElements < 1) {
114 $restElements = 1;
115 }
116 $numbersOfColumnsToUse = floor($restColumnsToDivide / $restElements);
117 }
118
119 $classes[] = str_replace(
120 '{@numbersOfColumnsToUse}',
121 $numbersOfColumnsToUse,
122 $configuration['classPattern']
123 );
124 }
125
126 return implode(' ', $classes);
127 }
128 }