f1140f52c162f127b93266c14ae4ec8b51d320cf
[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\FormElements\GridContainerInterface;
20 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
21 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
23
24 /**
25 * Scope: frontend
26 * @api
27 */
28 class GridColumnClassAutoConfigurationViewHelper extends AbstractViewHelper
29 {
30 use CompileWithRenderStatic;
31
32 /**
33 * @var bool
34 */
35 protected $escapeOutput = false;
36
37 /**
38 * Initialize the arguments.
39 *
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 if ($gridContainerElement instanceof GridContainerInterface) {
67 $gridViewPortConfiguration = $gridContainerElement->getProperties()['gridColumnClassAutoConfiguration'];
68 } else {
69 $gridViewPortConfiguration = $gridRowElement->getProperties()['gridColumnClassAutoConfiguration'];
70 }
71
72 if (empty($gridViewPortConfiguration)) {
73 return '';
74 }
75 $gridSize = (int)$gridViewPortConfiguration['gridSize'];
76
77 $columnsToCalculate = [];
78 $usedColumns = [];
79 foreach ($gridRowEChildElements as $childElement) {
80 if (empty($childElement->getProperties()['gridColumnClassAutoConfiguration'])) {
81 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
82 $columnsToCalculate[$viewPortName]['elements']++;
83 }
84 } else {
85 $gridColumnViewPortConfiguration = $childElement->getProperties()['gridColumnClassAutoConfiguration'];
86 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
87 $configuration = $gridColumnViewPortConfiguration['viewPorts'][$viewPortName];
88 if (
89 isset($configuration['numbersOfColumnsToUse'])
90 && (int)$configuration['numbersOfColumnsToUse'] > 0
91 ) {
92 $usedColumns[$viewPortName]['sum'] += (int)$configuration['numbersOfColumnsToUse'];
93 if ($childElement->getIdentifier() === $formElement->getIdentifier()) {
94 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = (int)$configuration['numbersOfColumnsToUse'];
95 if ($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] > $gridSize) {
96 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = $gridSize;
97 }
98 }
99 } else {
100 $columnsToCalculate[$viewPortName]['elements']++;
101 }
102 }
103 }
104 }
105
106 $classes = [];
107 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
108 if (isset($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'])) {
109 $numbersOfColumnsToUse = $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'];
110 } else {
111 $restColumnsToDivide = $gridSize - $usedColumns[$viewPortName]['sum'];
112 $restElements = (int)$columnsToCalculate[$viewPortName]['elements'];
113
114 if ($restColumnsToDivide < 1) {
115 $restColumnsToDivide = 1;
116 }
117 if ($restElements < 1) {
118 $restElements = 1;
119 }
120 $numbersOfColumnsToUse = floor($restColumnsToDivide / $restElements);
121 }
122
123 $classes[] = str_replace(
124 '{@numbersOfColumnsToUse}',
125 $numbersOfColumnsToUse,
126 $configuration['classPattern']
127 );
128 }
129
130 return implode(' ', $classes);
131 }
132 }