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