[CLEANUP] Remove/Replace non-standard phpdoc annotations
[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 */
54 public static function renderStatic(
55 array $arguments,
56 \Closure $renderChildrenClosure,
57 RenderingContextInterface $renderingContext
58 ) {
59 $formElement = $arguments['element'];
60
61 $gridRowElement = $formElement->getParentRenderable();
62 $gridContainerElement = $gridRowElement->getParentRenderable();
63 $gridRowEChildElements = $gridRowElement->getElementsRecursively();
64
65 if ($gridContainerElement instanceof GridContainerInterface) {
66 $gridViewPortConfiguration = $gridContainerElement->getProperties()['gridColumnClassAutoConfiguration'];
67 } else {
68 $gridViewPortConfiguration = $gridRowElement->getProperties()['gridColumnClassAutoConfiguration'];
69 }
70
71 if (empty($gridViewPortConfiguration)) {
72 return '';
73 }
74 $gridSize = (int)$gridViewPortConfiguration['gridSize'];
75
76 $columnsToCalculate = [];
77 $usedColumns = [];
78 foreach ($gridRowEChildElements as $childElement) {
79 if (empty($childElement->getProperties()['gridColumnClassAutoConfiguration'])) {
80 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
81 $columnsToCalculate[$viewPortName]['elements']++;
82 }
83 } else {
84 $gridColumnViewPortConfiguration = $childElement->getProperties()['gridColumnClassAutoConfiguration'];
85 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
86 $configuration = $gridColumnViewPortConfiguration['viewPorts'][$viewPortName];
87 if (
88 isset($configuration['numbersOfColumnsToUse'])
89 && (int)$configuration['numbersOfColumnsToUse'] > 0
90 ) {
91 $usedColumns[$viewPortName]['sum'] += (int)$configuration['numbersOfColumnsToUse'];
92 if ($childElement->getIdentifier() === $formElement->getIdentifier()) {
93 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = (int)$configuration['numbersOfColumnsToUse'];
94 if ($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] > $gridSize) {
95 $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'] = $gridSize;
96 }
97 }
98 } else {
99 $columnsToCalculate[$viewPortName]['elements']++;
100 }
101 }
102 }
103 }
104
105 $classes = [];
106 foreach ($gridViewPortConfiguration['viewPorts'] as $viewPortName => $configuration) {
107 if (isset($usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'])) {
108 $numbersOfColumnsToUse = $usedColumns[$viewPortName]['concreteNumbersOfColumnsToUse'];
109 } else {
110 $restColumnsToDivide = $gridSize - $usedColumns[$viewPortName]['sum'];
111 $restElements = (int)$columnsToCalculate[$viewPortName]['elements'];
112
113 if ($restColumnsToDivide < 1) {
114 $restColumnsToDivide = 1;
115 }
116 if ($restElements < 1) {
117 $restElements = 1;
118 }
119 $numbersOfColumnsToUse = floor($restColumnsToDivide / $restElements);
120 }
121
122 $classes[] = str_replace(
123 '{@numbersOfColumnsToUse}',
124 $numbersOfColumnsToUse,
125 $configuration['classPattern']
126 );
127 }
128
129 return implode(' ', $classes);
130 }
131 }