|
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<string,string> 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 (if no ID specified, name is used also as ID) |
|
28
|
|
|
* @param int $iSize size of list. 1 => dropdown list (default: 1) |
|
29
|
|
|
* @param int $wFlags any combination of FormFlag constants |
|
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
|
|
|
* @internal |
|
47
|
|
|
*/ |
|
48
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
|
49
|
|
|
{ |
|
50
|
|
|
$strName = self::getAttribString($oXMLElement, 'name'); |
|
51
|
|
|
$iSize = self::getAttribInt($oXMLElement, 'size', 1); |
|
52
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
|
53
|
|
|
$oFormElement = new self($strName, $iSize, $wFlags); |
|
54
|
|
|
$oFormParent->add($oFormElement); |
|
55
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
|
56
|
|
|
return $oFormElement; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritDoc} |
|
61
|
|
|
* @see \SKien\Formgenerator\FormElement::readAdditionalXML() |
|
62
|
|
|
* @internal |
|
63
|
|
|
*/ |
|
64
|
|
|
public function readAdditionalXML(\DOMElement $oXMLElement) : void |
|
65
|
|
|
{ |
|
66
|
|
|
parent::readAdditionalXML($oXMLElement); |
|
67
|
|
|
if (self::hasAttrib($oXMLElement, 'selectbtntext')) { |
|
68
|
|
|
$this->setSelectBtnText(self::getAttribString($oXMLElement, 'selectbtntext')); |
|
69
|
|
|
} |
|
70
|
|
|
$oOptions = $oXMLElement->getElementsByTagName('option'); |
|
71
|
|
|
if ($oOptions->length > 0) { |
|
72
|
|
|
$this->aOptions = []; |
|
73
|
|
|
foreach ($oOptions as $oOption) { |
|
74
|
|
|
$this->aOptions[$oOption->nodeValue] = self::getAttribString($oOption, 'value'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Build the HTML code for the element. |
|
81
|
|
|
* @return string |
|
82
|
|
|
* @internal |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getHTML() : string |
|
85
|
|
|
{ |
|
86
|
|
|
$this->processFlags(); |
|
87
|
|
|
|
|
88
|
|
|
$strSelect = $this->oFG->getData()->getValue($this->strName); |
|
89
|
|
|
$aOptions = $this->aOptions ?: $this->oFG->getData()->getSelectOptions($this->strName); |
|
90
|
|
|
|
|
91
|
|
|
$strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : ''); |
|
92
|
|
|
$strHTML = ''; |
|
93
|
|
|
$strHTML .= ' '; |
|
94
|
|
|
$strHTML .= '<div style="float: left; position: relative;'; |
|
95
|
|
|
if (!empty($strWidth)) { |
|
96
|
|
|
$strHTML .= ' width: ' . $strWidth . ';'; |
|
97
|
|
|
} |
|
98
|
|
|
$strHTML .= '">'; |
|
99
|
|
|
if ($this->oFlags->isSet(FormFlags::SELECT_BTN)) { |
|
100
|
|
|
$strHTML .= '<button class="sbBtn">' . $this->strSelectBtnText . '</button>'; |
|
101
|
|
|
} |
|
102
|
|
|
$strHTML .= '<select'; |
|
103
|
|
|
$strHTML .= ' class="' . $this->strClass . '"'; |
|
104
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
|
105
|
|
|
$strHTML .= ' id="' . $this->strName . '"'; |
|
106
|
|
|
if ($this->size > 0) { |
|
107
|
|
|
$strHTML .= ' size="' . $this->size . '"'; |
|
108
|
|
|
} |
|
109
|
|
|
$strHTML .= $this->buildStyle(); |
|
110
|
|
|
$strHTML .= $this->buildAttributes(); |
|
111
|
|
|
$strHTML .= $this->buildTabindex(); |
|
112
|
|
|
$strHTML .= '>' . PHP_EOL; |
|
113
|
|
|
|
|
114
|
|
|
if (count($aOptions) > 0) { |
|
115
|
|
|
foreach ($aOptions as $strOption => $strValue) { |
|
116
|
|
|
$strHTML .= ' '; |
|
117
|
|
|
$strHTML .= '<option '; |
|
118
|
|
|
if ($strValue == $strSelect) { |
|
119
|
|
|
$strHTML .= 'selected '; |
|
120
|
|
|
} |
|
121
|
|
|
if (strlen($strOption) == 0) { |
|
122
|
|
|
// to prevent HTML5 validation error "Element option without attribute label must not be empty." |
|
123
|
|
|
$strOption = ' '; |
|
124
|
|
|
} |
|
125
|
|
|
$strHTML .= 'value="' . $strValue . '">' . $strOption . '</option>' . PHP_EOL; |
|
126
|
|
|
} |
|
127
|
|
|
} else { |
|
128
|
|
|
// dropdown selectlist without options... |
|
129
|
|
|
trigger_error('empty select options set!', E_USER_NOTICE); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$strHTML .= ' </select></div>' . PHP_EOL; |
|
133
|
|
|
|
|
134
|
|
|
return $strHTML; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Set text for selectbutton. |
|
139
|
|
|
* @param string $strSelectBtnText |
|
140
|
|
|
*/ |
|
141
|
|
|
public function setSelectBtnText(string $strSelectBtnText) : void |
|
142
|
|
|
{ |
|
143
|
|
|
if (!$this->oFlags->isSet(FormFlags::SELECT_BTN)) { |
|
144
|
|
|
trigger_error('SELECT_BTN flag must be set!', E_USER_NOTICE); |
|
145
|
|
|
} |
|
146
|
|
|
$this->strSelectBtnText = $strSelectBtnText; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Set the select options for the element. |
|
151
|
|
|
* If no selection options are passed to the element via this method, an |
|
152
|
|
|
* attempt is made in the getHTML () method to determine an assigned list |
|
153
|
|
|
* via the data provider. |
|
154
|
|
|
* @see AbstractFormData::getSelectOptions() |
|
155
|
|
|
* @param array<string,string> $aOptions |
|
156
|
|
|
*/ |
|
157
|
|
|
public function setSelectOptions(array $aOptions) : void |
|
158
|
|
|
{ |
|
159
|
|
|
$this->aOptions = $aOptions; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|