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
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManager; |
37
|
|
|
|
38
|
|
|
abstract class AbstractFormSelectElement extends AbstractFormElement |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var FeatureFlagRepository |
42
|
|
|
*/ |
43
|
|
|
protected $featureFlagRepository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var MappingRepository |
47
|
|
|
*/ |
48
|
|
|
protected $mappingRepository; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Container objects give $nodeFactory down to other containers. |
52
|
|
|
* |
53
|
|
|
* @param NodeFactory $nodeFactory |
54
|
|
|
* @param array $data |
55
|
|
|
* @param FeatureFlagRepository|null $featureFlagRepository |
56
|
|
|
* @param MappingRepository|null $mappingRepository |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
NodeFactory $nodeFactory, |
60
|
|
|
array $data, |
61
|
|
|
FeatureFlagRepository $featureFlagRepository = null, |
62
|
|
|
MappingRepository $mappingRepository = null |
63
|
|
|
) { |
64
|
|
|
parent::__construct($nodeFactory, $data); |
65
|
|
|
|
66
|
|
|
$this->featureFlagRepository = $featureFlagRepository ?? |
67
|
|
|
GeneralUtility::makeInstance(ObjectManager::class)->get(FeatureFlagRepository::class); |
68
|
|
|
|
69
|
|
|
$this->mappingRepository = $mappingRepository ?? |
70
|
|
|
GeneralUtility::makeInstance(ObjectManager::class)->get(MappingRepository::class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This will render a selector box element, or possibly a special construction with two selector boxes. |
75
|
|
|
* |
76
|
|
|
* @param array $optionElements |
77
|
|
|
* @return array As defined in initializeResultArray() of AbstractNode |
78
|
|
|
*/ |
79
|
|
|
public function renderElement(array $optionElements) |
80
|
|
|
{ |
81
|
|
|
$resultArray = $this->initializeResultArray(); |
82
|
|
|
$parameterArray = $this->data['parameterArray']; |
83
|
|
|
|
84
|
|
|
// Field configuration from TCA: |
85
|
|
|
$config = $parameterArray['fieldConf']['config']; |
86
|
|
|
|
87
|
|
|
$selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config); |
88
|
|
|
|
89
|
|
|
$width = MathUtility::forceIntegerInRange( |
90
|
|
|
$config['width'] ?: $this->defaultInputWidth, |
91
|
|
|
$this->minimumInputWidth, |
92
|
|
|
$this->maxInputWidth |
93
|
|
|
); |
94
|
|
|
$maxWidth = $this->formMaxWidth($width); |
95
|
|
|
|
96
|
|
|
$html = []; |
97
|
|
|
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">'; |
98
|
|
|
$html[] = '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">'; |
99
|
|
|
$html[] = '<div class="form-wizards-wrap">'; |
100
|
|
|
$html[] = '<div class="form-wizards-element">'; |
101
|
|
|
$html[] = $selectElement; |
102
|
|
|
$html[] = '</div>'; |
103
|
|
|
$html[] = '</div>'; |
104
|
|
|
$html[] = '</div>'; |
105
|
|
|
|
106
|
|
|
$resultArray['html'] = implode(LF, $html); |
107
|
|
|
|
108
|
|
|
return $resultArray; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Renders a <select> element |
113
|
|
|
* |
114
|
|
|
* @param array $options |
115
|
|
|
* @param array $parameterArray |
116
|
|
|
* @param array $config Field configuration |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function renderSelectElement( |
120
|
|
|
array $options, |
121
|
|
|
array $parameterArray, |
122
|
|
|
array $config |
123
|
|
|
) { |
124
|
|
|
$attributes = [ |
125
|
|
|
'id' => StringUtility::getUniqueId('tceforms-select-'), |
126
|
|
|
'name' => $parameterArray['itemFormElName'], |
127
|
|
|
'class' => 'form-control tceforms-select', |
128
|
|
|
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config), |
129
|
|
|
'disabled' => !empty($config['readOnly']), |
130
|
|
|
'size' => (int)$config['size'] |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$optionElements = []; |
134
|
|
|
foreach ($options as $option) { |
135
|
|
|
$attributes = []; |
136
|
|
|
if ($option['isSelected']) { |
137
|
|
|
$attributes['selected'] = 'selected'; |
138
|
|
|
} |
139
|
|
|
$optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $attributes); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$html = []; |
143
|
|
|
$html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>'; |
144
|
|
|
$html[] = implode(LF, $optionElements); |
145
|
|
|
$html[] = '</select>'; |
146
|
|
|
|
147
|
|
|
return implode(LF, $html); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Renders a single <option> element |
152
|
|
|
* |
153
|
|
|
* @param string $value The option value |
154
|
|
|
* @param string $label The option label |
155
|
|
|
* @param array $attributes Map of attribute names and values |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function renderOptionElement($value, $label, array $attributes = []) |
159
|
|
|
{ |
160
|
|
|
$attributes['value'] = $value; |
161
|
|
|
$html = [ |
162
|
|
|
'<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>', |
163
|
|
|
htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false), |
164
|
|
|
'</option>' |
165
|
|
|
|
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
return implode('', $html); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return LanguageService |
173
|
|
|
*/ |
174
|
|
|
protected function getLanguageService() |
175
|
|
|
{ |
176
|
|
|
return $GLOBALS['LANG']; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|