Passed
Push — main ( 753b8f...3b9663 )
by Stefan
02:22
created

FormSelect::setSelectOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * HTML select element.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormSelect extends FormInput
14
{
15
    /** @var array available select option */
16
    protected ?array $aOptions = null;
17
    /** @var string text for selectbutton     */
18
    protected string $strSelectBtnText;
19
20
    /**
21
     * Create HTML select element.
22
     * If $wFlags contain SELECT_BTN property, the display of the selected
23
     * value is replaced by a button with text specified with setSelectBtnText.
24
     * (default text: 'Auswählen')
25
     * @see FormSelect::setSelectBtnText()
26
     *
27
     * @param string $strName    name and id
28
     * @param int $iSize         size of list. 1 => dropdown list (default: 1)
29
     * @param int $wFlags        flags (default: 0)
30
     */
31
    public function __construct(string $strName, int $iSize = 1, int $wFlags = 0)
32
    {
33
        parent::__construct($strName, $iSize, $wFlags);
34
        if ($this->oFlags->isSet(FormFlags::SELECT_BTN)) {
35
            $this->strClass .= ' sbSelect';
36
            if ($iSize != 1) {
37
                trigger_error('SELECT_BTN must have size of 1!', E_USER_WARNING);
38
            }
39
        }
40
        $this->strSelectBtnText = 'Auswählen';
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     * @see \SKien\Formgenerator\FormElement::fromXML()
46
     */
47
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
48
    {
49
        $strName = self::getAttribString($oXMLElement, 'name', '');
50
        $iSize = self::getAttribInt($oXMLElement, 'size', 1);
51
        $wFlags = self::getAttribFlags($oXMLElement);
52
        $oFormElement = new self($strName, $iSize, $wFlags);
53
        $oFormParent->add($oFormElement);
54
        $oFormElement->readAdditionalXML($oXMLElement);
55
        return $oFormElement;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
61
     */
62
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
63
    {
64
        parent::readAdditionalXML($oXMLElement);
65
        if (($strSelectBtnText = self::getAttribString($oXMLElement, 'selectbtntext')) !== null) {
66
            $this->setSelectBtnText($strSelectBtnText);
67
        }
68
        $oOptions = $oXMLElement->getElementsByTagName('option');
69
        if ($oOptions->length > 0) {
70
            $this->aOptions = [];
71
            foreach($oOptions as $oOption) {
72
                $this->aOptions[$oOption->nodeValue] = self::getAttribString($oOption, 'value');
73
            }
74
        }
75
    }
76
77
    /**
78
     * Build the HTML code for the element
79
     * @return string
80
     */
81
    public function getHTML() : string
82
    {
83
        $this->processFlags();
84
85
        $strSelect = $this->oFG->getData()->getValue($this->strName);
86
        $aOptions = $this->aOptions ?: $this->oFG->getData()->getSelectOptions($this->strName);
87
88
        $strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : '');
89
        $strHTML  = '';
90
        $strHTML .= '       ';
91
        $strHTML .= '<div style="float: left; position: relative;';
92
        if (!empty($strWidth)) {
93
            $strHTML .= ' width: ' . $strWidth . ';';
94
        }
95
        $strHTML .= '">';
96
        if ($this->oFlags->isSet(FormFlags::SELECT_BTN)) {
97
            $strHTML .= '<button class="sbBtn">' . $this->strSelectBtnText . '</button>';
98
        }
99
        $strHTML .= '<select';
100
        $strHTML .= ' class="' . $this->strClass . '"';
101
        $strHTML .= ' name="' . $this->strName . '"';
102
        $strHTML .= ' id="' . $this->strName . '"';
103
        if ($this->size > 0) {
104
            $strHTML .= ' size="' . $this->size . '"';
105
        }
106
        $strHTML .= $this->buildStyle();
107
        $strHTML .= $this->buildAttributes();
108
        $strHTML .= $this->buildTabindex();
109
        $strHTML .= '>' . PHP_EOL;
110
111
        if (count($aOptions) > 0) {
112
            foreach ($aOptions as $strOption => $strValue) {
113
                $strHTML .= '           ';
114
                $strHTML .= '<option ';
115
                if ($strValue == $strSelect) {
116
                    $strHTML .= 'selected ';
117
                }
118
                if (strlen($strOption) == 0) {
119
                    // to prevent HTML5 validation error "Element option without attribute label must not be empty."
120
                    $strOption = '&nbsp;';
121
                }
122
                $strHTML .= 'value="' . $strValue . '">' . $strOption . '</option>' . PHP_EOL;
123
            }
124
        } else {
125
            // dropdown selectlist without options...
126
            trigger_error('empty select options set!', E_USER_NOTICE);
127
        }
128
129
        $strHTML .= '       </select></div>' . PHP_EOL;
130
131
        return $strHTML;
132
    }
133
134
    /**
135
     * set text for selectbutton.
136
     * @param string $strSelectBtnText
137
     */
138
    public function setSelectBtnText(string $strSelectBtnText) : void
139
    {
140
        if (!$this->oFlags->isSet(FormFlags::SELECT_BTN)) {
141
            trigger_error('SELECT_BTN flag must be set!', E_USER_NOTICE);
142
        }
143
        $this->strSelectBtnText = $strSelectBtnText;
144
    }
145
146
    /**
147
     * Set the select options for the element.
148
     * If no selection options are passed to the element via this method, an
149
     * attempt is made in the getHTML () method to determine an assigned list
150
     * via the data provider.
151
     * @param array $aOptions
152
     */
153
    public function setSelectOptions(array $aOptions) : void
154
    {
155
        $this->aOptions = $aOptions;
156
    }
157
}
158