Passed
Push — main ( 7b0991...9b4de3 )
by Felix
02:59
created

AbstractFormSelectElement   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Test Coverage

Coverage 96.08%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 139
ccs 49
cts 51
cp 0.9608
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getLanguageService() 0 3 1
A __construct() 0 13 1
A renderSelectElement() 0 29 3
A renderOptionElement() 0 11 1
A renderElement() 0 30 1
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 2
    public function __construct(
58
        NodeFactory $nodeFactory,
59
        array $data,
60
        FeatureFlagRepository $featureFlagRepository = null,
61
        MappingRepository $mappingRepository = null
62
    ) {
63 2
        parent::__construct($nodeFactory, $data);
64
65 2
        $this->featureFlagRepository = $featureFlagRepository ??
66
            GeneralUtility::makeInstance(FeatureFlagRepository::class);
67
68 2
        $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 2
    public function renderElement(array $optionElements)
79
    {
80 2
        $resultArray = $this->initializeResultArray();
81 2
        $parameterArray = $this->data['parameterArray'];
82
83
        // Field configuration from TCA:
84 2
        $config = $parameterArray['fieldConf']['config'];
85
86 2
        $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
87
88 2
        $width = MathUtility::forceIntegerInRange(
89 2
            $config['width'] ?? $this->defaultInputWidth,
90 2
            $this->minimumInputWidth,
91 2
            $this->maxInputWidth
92
        );
93 2
        $maxWidth = $this->formMaxWidth($width);
94
95 2
        $html = [];
96 2
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
97 2
        $html[] =   '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">';
98 2
        $html[] =       '<div class="form-wizards-wrap">';
99 2
        $html[] =           '<div class="form-wizards-element">';
100 2
        $html[] =               $selectElement;
101 2
        $html[] =           '</div>';
102 2
        $html[] =   '</div>';
103 2
        $html[] = '</div>';
104
105 2
        $resultArray['html'] = implode(LF, $html);
106
107 2
        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 2
    protected function renderSelectElement(
119
        array $options,
120
        array $parameterArray,
121
        array $config
122
    ) {
123
        $attributes = [
124 2
            'id' => StringUtility::getUniqueId('tceforms-select-'),
125 2
            'name' => $parameterArray['itemFormElName'],
126
            'class' => 'form-control tceforms-select',
127 2
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
128 2
            'disabled' => !empty($config['readOnly']),
129 2
            'size' => (int)$config['size']
130
        ];
131
132 2
        $optionElements = [];
133 2
        foreach ($options as $option) {
134 2
            $optionAttributes = [];
135 2
            if ($option['isSelected']) {
136 2
                $optionAttributes['selected'] = 'selected';
137
            }
138 2
            $optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $optionAttributes);
139
        }
140
141 2
        $html = [];
142 2
        $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
143 2
        $html[] =   implode(LF, $optionElements);
144 2
        $html[] = '</select>';
145
146 2
        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 2
    protected function renderOptionElement($value, $label, array $attributes = [])
158
    {
159 2
        $attributes['value'] = $value;
160
        $html = [
161 2
            '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
162 2
            htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
163
            '</option>'
164
165
        ];
166
167 2
        return implode('', $html);
168
    }
169
170
    /**
171
     * @return LanguageService
172
     */
173 1
    protected function getLanguageService()
174
    {
175 1
        return $GLOBALS['LANG'];
176
    }
177
}
178