Passed
Push — main ( cdbc18...f1cdfe )
by
unknown
02:56
created

AbstractFormSelectElement::renderSelectElement()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 18
nc 3
nop 3
dl 0
loc 29
ccs 18
cts 18
cp 1
crap 3
rs 9.6666
c 1
b 0
f 0
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 2
    public function __construct(
51
        NodeFactory $nodeFactory,
52
        array $data,
53
        FeatureFlagRepository $featureFlagRepository = null,
54
        MappingRepository $mappingRepository = null
55
    ) {
56 2
        parent::__construct($nodeFactory, $data);
57
58 2
        $this->featureFlagRepository = $featureFlagRepository ??
59
            GeneralUtility::makeInstance(FeatureFlagRepository::class);
60
61 2
        $this->mappingRepository = $mappingRepository ??
62
            GeneralUtility::makeInstance(MappingRepository::class);
63 2
    }
64
65
    /**
66
     * This will render a selector box element, or possibly a special construction with two selector boxes.
67
     */
68 2
    public function renderElement(array $optionElements): array
69
    {
70 2
        $resultArray = $this->initializeResultArray();
71 2
        $parameterArray = $this->data['parameterArray'];
72
73
        // Field configuration from TCA:
74 2
        $config = $parameterArray['fieldConf']['config'];
75
76 2
        $selectElement = $this->renderSelectElement($optionElements, $parameterArray, $config);
77
78 2
        $width = MathUtility::forceIntegerInRange(
79 2
            $config['width'] ?? $this->defaultInputWidth,
80 2
            $this->minimumInputWidth,
81 2
            $this->maxInputWidth
82
        );
83 2
        $maxWidth = $this->formMaxWidth($width);
84
85 2
        $html = [];
86 2
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
87 2
        $html[] = '<div class="form-control-wrap" style="max-width: ' . $maxWidth . 'px">';
88 2
        $html[] = '<div class="form-wizards-wrap">';
89 2
        $html[] = '<div class="form-wizards-element">';
90 2
        $html[] = $selectElement;
91 2
        $html[] = '</div>';
92 2
        $html[] = '</div>';
93 2
        $html[] = '</div>';
94
95 2
        $resultArray['html'] = implode(LF, $html);
96
97 2
        return $resultArray;
98
    }
99
100
    /**
101
     * Renders a <select> element
102
     */
103 2
    protected function renderSelectElement(
104
        array $options,
105
        array $parameterArray,
106
        array $config
107
    ): string {
108
        $attributes = [
109 2
            'id' => StringUtility::getUniqueId('tceforms-select-'),
110 2
            'name' => $parameterArray['itemFormElName'],
111 2
            'class' => 'form-control tceforms-select',
112 2
            'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
113 2
            'disabled' => (string) !empty($config['readOnly']),
114 2
            'size' => $config['size'],
115
        ];
116
117 2
        $optionElements = [];
118 2
        foreach ($options as $option) {
119 2
            $optionAttributes = [];
120 2
            if ($option['isSelected']) {
121 2
                $optionAttributes['selected'] = 'selected';
122
            }
123 2
            $optionElements[] = $this->renderOptionElement($option['value'], $option['name'], $optionAttributes);
124
        }
125
126 2
        $html = [];
127 2
        $html[] = '<select ' . GeneralUtility::implodeAttributes($attributes, true) . '>';
128 2
        $html[] = implode(LF, $optionElements);
129 2
        $html[] = '</select>';
130
131 2
        return implode(LF, $html);
132
    }
133
134
    /**
135
     * Renders a single <option> element
136
     */
137 2
    protected function renderOptionElement(string $value, string $label, array $attributes = []): string
138
    {
139 2
        $attributes['value'] = $value;
140
        $html = [
141 2
            '<option ' . GeneralUtility::implodeAttributes($attributes, true) . '>',
142 2
            htmlspecialchars($label, ENT_COMPAT, 'UTF-8', false),
143 2
            '</option>',
144
145
        ];
146
147 2
        return implode('', $html);
148
    }
149
150 1
    protected function getLanguageService(): LanguageService
151
    {
152 1
        return $GLOBALS['LANG'];
153
    }
154
}
155