1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Group of radio buttons. |
8
|
|
|
* |
9
|
|
|
* @package Formgenerator |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class FormRadioGroup extends FormInput |
14
|
|
|
{ |
15
|
|
|
/** @var array<string,string> available select option */ |
16
|
|
|
protected ?array $aOptions = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a radio group. |
20
|
|
|
* @param string $strName name AND id of the element |
21
|
|
|
* @param int $wFlags (default: 0) |
22
|
|
|
*/ |
23
|
|
|
public function __construct(string $strName, int $wFlags = 0) |
24
|
|
|
{ |
25
|
|
|
$this->oFlags = new FormFlags($wFlags); |
26
|
|
|
$this->strName = $strName; |
27
|
|
|
if ($this->oFlags->isSet(FormFlags::READ_ONLY | FormFlags::DISABLED)) { |
28
|
|
|
$this->addAttribute('disabled'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
35
|
|
|
* @internal |
36
|
|
|
*/ |
37
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
38
|
|
|
{ |
39
|
|
|
$strName = self::getAttribString($oXMLElement, 'name'); |
40
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
41
|
|
|
$oFormElement = new self($strName, $wFlags); |
42
|
|
|
$oFormParent->add($oFormElement); |
43
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
44
|
|
|
return $oFormElement; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
* @see \SKien\Formgenerator\FormElement::readAdditionalXML() |
50
|
|
|
* @internal |
51
|
|
|
*/ |
52
|
|
|
public function readAdditionalXML(\DOMElement $oXMLElement) : void |
53
|
|
|
{ |
54
|
|
|
parent::readAdditionalXML($oXMLElement); |
55
|
|
|
$oOptions = $oXMLElement->getElementsByTagName('option'); |
56
|
|
|
if ($oOptions->length > 0) { |
57
|
|
|
$this->aOptions = []; |
58
|
|
|
foreach ($oOptions as $oOption) { |
59
|
|
|
$this->aOptions[$oOption->nodeValue] = self::getAttribString($oOption, 'value'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Build the HTML-markup for the radio group. |
66
|
|
|
* @return string |
67
|
|
|
* @internal l |
68
|
|
|
*/ |
69
|
|
|
public function getHTML() : string |
70
|
|
|
{ |
71
|
|
|
$this->processFlags(); |
72
|
|
|
|
73
|
|
|
$strSelect = $this->oFG->getData()->getValue($this->strName); |
74
|
|
|
$aOptions = $this->aOptions ?: $this->oFG->getData()->getSelectOptions($this->strName); |
75
|
|
|
|
76
|
|
|
$strHTML = $this->buildContainerDiv(); |
77
|
|
|
|
78
|
|
|
$iBtn = 0; |
79
|
|
|
$this->addStyle('float', 'left'); |
80
|
|
|
if (count($aOptions) > 0) { |
81
|
|
|
foreach ($aOptions as $strName => $strValue) { |
82
|
|
|
if ($strName !== '') { |
83
|
|
|
$strHTML .= '<input type="radio"'; |
84
|
|
|
$strHTML .= $this->buildStyle(); |
85
|
|
|
$strHTML .= $this->buildAttributes(); |
86
|
|
|
$strHTML .= $this->buildTabindex(); |
87
|
|
|
$strHTML .= ' id="' . $this->strName . ++$iBtn . '"'; |
88
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
89
|
|
|
if ($strSelect === $strValue) { |
90
|
|
|
$strHTML .= ' checked'; |
91
|
|
|
} |
92
|
|
|
$strHTML .= ' value="' . $strValue . '">'; |
93
|
|
|
$strHTML .= ' <label for="' . $this->strName . $iBtn . '"'; |
94
|
|
|
if ($this->oFlags->isSet(FormFlags::HORZ_ARRANGE)) { |
95
|
|
|
$strHTML .= ' style="float: left;"'; |
96
|
|
|
} |
97
|
|
|
$strHTML .= ' class="radio">' . $strName . '</label>' . PHP_EOL; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} else { |
101
|
|
|
// radiogroup without options... |
102
|
|
|
trigger_error('empty radiogroup defined!', E_USER_NOTICE); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
106
|
|
|
|
107
|
|
|
return $strHTML; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set the select options for the element. |
112
|
|
|
* If no selection options are passed to the element via this method, an |
113
|
|
|
* attempt is made to determine an assigned list via the data provider. |
114
|
|
|
* @param array<string,string> $aOptions |
115
|
|
|
*/ |
116
|
|
|
public function setSelectOptions(array $aOptions) : void |
117
|
|
|
{ |
118
|
|
|
$this->aOptions = $aOptions; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|