Passed
Push — main ( 2edd53...9e1454 )
by Stefan
02:35
created

FormSelect::readAdditionalXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 string text for selectbutton     */
16
    protected string $strSelectBtnText; 
17
    
18
    /**
19
     * Create HTML select element.
20
     * If $wFlags contain SELECT_BTN property, the display of the selected
21
     * value is replaced by a button with text specified with setSelectBtnText.
22
     * (default text: 'Auswählen')
23
     * @see FormSelect::setSelectBtnText()
24
     *  
25
     * @param string $strName    name and id
26
     * @param int $iSize         size of list. 1 => dropdown list (default: 1)
27
     * @param int $wFlags        flags (default: 0)
28
     */
29
    public function __construct(string $strName, int $iSize = 1, int $wFlags = 0)
30
    {
31
        parent::__construct($strName, $iSize, $wFlags);
32
        if ($this->oFlags->isSet(FormFlags::SELECT_BTN)) {
33
            $this->strClass .= ' sbSelect';
34
            if ($iSize != 1) {
35
                trigger_error('SELECT_BTN must have size of 1!', E_USER_WARNING);
36
            }
37
        }
38
        $this->strSelectBtnText = 'Auswählen';
39
    }
40
    
41
    /**
42
     * {@inheritDoc}
43
     * @see \SKien\Formgenerator\FormElement::fromXML()
44
     */
45
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
46
    {
47
        $strName = self::getAttribString($oXMLElement, 'name', '');
48
        $iSize = self::getAttribInt($oXMLElement, 'size', 1);
49
        $wFlags = self::getAttribFlags($oXMLElement);
50
        $oFormElement = new self($strName, $iSize, $wFlags);
51
        $oFormParent->add($oFormElement);
52
        $oFormElement->readAdditionalXML($oXMLElement);
53
        return $oFormElement;
54
    }
55
    
56
    /**
57
     * {@inheritDoc}
58
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
59
     */
60
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
61
    {
62
        parent::readAdditionalXML($oXMLElement);
63
        if (($strSelectBtnText = self::getAttribString($oXMLElement, 'selectbtntext')) !== null) {
64
            $this->setSelectBtnText($strSelectBtnText);
65
        }
66
    }
67
    
68
    /**
69
     * Build the HTML code for the element
70
     * @return string
71
     */
72
    public function getHTML() : string
73
    {
74
        $this->processFlags();
75
        
76
        $strSelect = $this->oFG->getData()->getValue($this->strName);
77
        $aOptions = $this->oFG->getData()->getSelectOptions($this->strName);
78
        
79
        $strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : '');
80
        $strHTML  = '';
81
        $strHTML .= '       ';
82
        $strHTML .= '<div style="float: left; position: relative;';
83
        if (!empty($strWidth)) {
84
            $strHTML .= ' width: ' . $strWidth . ';';
85
        }
86
        $strHTML .= '">';
87
        if ($this->oFlags->isSet(FormFlags::SELECT_BTN)) {
88
            $strHTML .= '<button class="sbBtn">' . $this->strSelectBtnText . '</button>';
89
        }
90
        $strHTML .= '<select';
91
        $strHTML .= ' class="' . $this->strClass . '"';
92
        $strHTML .= ' name="' . $this->strName . '"';
93
        $strHTML .= ' id="' . $this->strName . '"';
94
        if ($this->size > 0) {
95
            $strHTML .= ' size="' . $this->size . '"';
96
        }
97
        $strHTML .= $this->buildStyle();
98
        $strHTML .= $this->buildAttributes();
99
        $strHTML .= $this->buildTabindex();
100
        $strHTML .= '>' . PHP_EOL;
101
102
        if (count($aOptions) > 0) {
103
            foreach ($aOptions as $strOption => $strValue) {
104
                $strHTML .= '           ';
105
                $strHTML .= '<option ';
106
                if ($strValue == $strSelect) {
107
                    $strHTML .= 'selected ';
108
                }
109
                $strHTML .= 'value="' . $strValue . '">' . $strOption . '</option>' . PHP_EOL;
110
            }
111
        } else if ($this->size == 1) {
112
            // dropdown selectlist without options... 
113
            trigger_error('empty select options set!', E_USER_NOTICE);
114
        }
115
            
116
        $strHTML .= '       </select></div>' . PHP_EOL;
117
        
118
        return $strHTML;
119
    }
120
    
121
    /**
122
     * set text for selectbutton.
123
     * @param string $strSelectBtnText
124
     */
125
    public function setSelectBtnText(string $strSelectBtnText) : void 
126
    {
127
        if (!$this->oFlags->isSet(FormFlags::SELECT_BTN)) {
128
            trigger_error('SELECT_BTN flag must be set!', E_USER_NOTICE);
129
        }
130
        $this->strSelectBtnText = $strSelectBtnText;
131
    }
132
}
133