1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aoe\FeatureFlag\Form\Element; |
4
|
|
|
|
5
|
|
|
/*************************************************************** |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2021 AOE GmbH <[email protected]> |
9
|
|
|
* |
10
|
|
|
* All rights reserved |
11
|
|
|
* |
12
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
13
|
|
|
* free software; you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* The GNU General Public License can be found at |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
20
|
|
|
* |
21
|
|
|
* This script is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
27
|
|
|
***************************************************************/ |
28
|
|
|
|
29
|
|
|
use Aoe\FeatureFlag\Domain\Repository\FeatureFlagRepository; |
30
|
|
|
use Aoe\FeatureFlag\Domain\Repository\MappingRepository; |
31
|
|
|
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement; |
32
|
|
|
use TYPO3\CMS\Backend\Form\NodeFactory; |
33
|
|
|
use TYPO3\CMS\Core\Localization\LanguageService; |
34
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
35
|
|
|
use TYPO3\CMS\Core\Utility\MathUtility; |
36
|
|
|
use TYPO3\CMS\Core\Utility\StringUtility; |
37
|
|
|
|
38
|
|
|
abstract class AbstractFormSelectElement extends AbstractFormElement |
39
|
|
|
{ |
40
|
|
|
protected object $featureFlagRepository; |
41
|
|
|
|
42
|
|
|
protected object $mappingRepository; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Container objects give $nodeFactory down to other containers. |
46
|
|
|
* |
47
|
|
|
* @param FeatureFlagRepository|null $featureFlagRepository |
48
|
|
|
* @param MappingRepository|null $mappingRepository |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
NodeFactory $nodeFactory, |
52
|
|
|
array $data, |
53
|
|
|
FeatureFlagRepository $featureFlagRepository = null, |
54
|
|
|
MappingRepository $mappingRepository = null |
55
|
|
|
) { |
56
|
|
|
parent::__construct($nodeFactory, $data); |
57
|
|
|
|
58
|
|
|
$this->featureFlagRepository = $featureFlagRepository ?? |
59
|
|
|
GeneralUtility::makeInstance(FeatureFlagRepository::class); |
60
|
|
|
|
61
|
|
|
$this->mappingRepository = $mappingRepository ?? |
62
|
|
|
GeneralUtility::makeInstance(MappingRepository::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* This will render a selector box element, or possibly a special construction with two selector boxes. |
67
|
|
|
*/ |
68
|
|
|
public function renderElement(array $optionElements): array |
69
|
|
|
{ |
70
|
|
|
$resultArray = $this->initializeResultArray(); |
71
|
|
|
$parameterArray = $this->data['parameterArray']; |
72
|
|
|
|
73
|
|
|
// Field configuration from TCA: |
74
|
|
|
$config = $parameterArray['fieldConf']['config']; |
75
|
|
|
|
76
|
|
|
$selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config); |
77
|
|
|
|
78
|
|
|
$width = MathUtility::forceIntegerInRange( |
79
|
|
|
$config['width'] ?? $this->defaultInputWidth, |
80
|
|
|
$this->minimumInputWidth, |
81
|
|
|
$this->maxInputWidth |
82
|
|
|
); |
83
|
|
|
$maxWidth = $this->formMaxWidth($width); |
84
|
|
|
|
85
|
|
|
$html = []; |
86
|
|
|
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">'; |
87
|
|
|
$html[] = '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">'; |
88
|
|
|
$html[] = '<div class="form-wizards-wrap">'; |
89
|
|
|
$html[] = '<div class="form-wizards-element">'; |
90
|
|
|
$html[] = $selectElement; |
91
|
|
|
$html[] = '</div>'; |
92
|
|
|
$html[] = '</div>'; |
93
|
|
|
$html[] = '</div>'; |
94
|
|
|
|
95
|
|
|
$resultArray['html'] = implode(LF, $html); |
96
|
|
|
|
97
|
|
|
return $resultArray; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Renders a <select> element |
102
|
|
|
*/ |
103
|
|
|
protected function renderSelectElement( |
104
|
|
|
array $options, |
105
|
|
|
array $parameterArray, |
106
|
|
|
array $config |
107
|
|
|
): string { |
108
|
|
|
$attributes = [ |
109
|
|
|
'id' => StringUtility::getUniqueId('tceforms-select-'), |
110
|
|
|
'name' => $parameterArray['itemFormElName'], |
111
|
|
|
'class' => 'form-control tceforms-select', |
112
|
|
|
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config), |
113
|
|
|
'disabled' => (string) !empty($config['readOnly']), |
114
|
|
|
'size' => $config['size'], |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$optionElements = []; |
118
|
|
|
foreach ($options as $option) { |
119
|
|
|
$optionAttributes = []; |
120
|
|
|
if ($option['isSelected']) { |
121
|
|
|
$optionAttributes['selected'] = 'selected'; |
122
|
|
|
} |
123
|
|
|
$optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $optionAttributes); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$html = []; |
127
|
|
|
$html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>'; |
128
|
|
|
$html[] = implode(LF, $optionElements); |
129
|
|
|
$html[] = '</select>'; |
130
|
|
|
|
131
|
|
|
return implode(LF, $html); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Renders a single <option> element |
136
|
|
|
*/ |
137
|
|
|
protected function renderOptionElement(string $value, string $label, array $attributes = []): string |
138
|
|
|
{ |
139
|
|
|
$attributes['value'] = $value; |
140
|
|
|
$html = [ |
141
|
|
|
'<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>', |
142
|
|
|
htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false), |
143
|
|
|
'</option>', |
144
|
|
|
|
145
|
|
|
]; |
146
|
|
|
|
147
|
|
|
return implode('', $html); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function getLanguageService(): LanguageService |
151
|
|
|
{ |
152
|
|
|
return $GLOBALS['LANG']; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|