1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Radiogroup input. |
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 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
|
|
|
*/ |
36
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
37
|
|
|
{ |
38
|
|
|
$strName = self::getAttribString($oXMLElement, 'name', ''); |
39
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
40
|
|
|
$oFormElement = new self($strName, $wFlags); |
41
|
|
|
$oFormParent->add($oFormElement); |
42
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
43
|
|
|
return $oFormElement; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
* @see \SKien\Formgenerator\FormElement::readAdditionalXML() |
49
|
|
|
*/ |
50
|
|
|
public function readAdditionalXML(\DOMElement $oXMLElement) : void |
51
|
|
|
{ |
52
|
|
|
parent::readAdditionalXML($oXMLElement); |
53
|
|
|
$oOptions = $oXMLElement->getElementsByTagName('option'); |
54
|
|
|
if ($oOptions->length > 0) { |
55
|
|
|
$this->aOptions = []; |
56
|
|
|
foreach($oOptions as $oOption) { |
57
|
|
|
$this->aOptions[$oOption->nodeValue] = self::getAttribString($oOption, 'value'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Build the HTML-markup for the radio group. |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getHTML() : string |
67
|
|
|
{ |
68
|
|
|
$this->processFlags(); |
69
|
|
|
|
70
|
|
|
$strSelect = $this->oFG->getData()->getValue($this->strName); |
71
|
|
|
$aOptions = $this->aOptions ?: $this->oFG->getData()->getSelectOptions($this->strName); |
72
|
|
|
|
73
|
|
|
$strHTML = $this->buildContainerDiv(); |
74
|
|
|
|
75
|
|
|
// TODO: create surrogates for unchecked/readonly (see FormCheck) |
76
|
|
|
|
77
|
|
|
$iBtn = 0; |
78
|
|
|
$this->addStyle('float', 'left'); |
79
|
|
|
if (count($aOptions) > 0) { |
80
|
|
|
foreach ($aOptions as $strName => $strValue) { |
81
|
|
|
if ($strName !== '') { |
82
|
|
|
$strHTML .= '<input type="radio"'; |
83
|
|
|
$strHTML .= $this->buildStyle(); |
84
|
|
|
$strHTML .= $this->buildAttributes(); |
85
|
|
|
$strHTML .= $this->buildTabindex(); |
86
|
|
|
$strHTML .= ' id="' . $this->strName . ++$iBtn . '"'; |
87
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
88
|
|
|
if ($strSelect === $strValue) { |
89
|
|
|
$strHTML .= ' checked'; |
90
|
|
|
} |
91
|
|
|
$strHTML .= ' value="' . $strValue . '">'; |
92
|
|
|
$strHTML .= ' <label for="' . $this->strName . $iBtn . '"'; |
93
|
|
|
if ($this->oFlags->isSet(FormFlags::HORZ_ARRANGE)) { |
94
|
|
|
$strHTML .= ' style="float: left;"'; |
95
|
|
|
} |
96
|
|
|
$strHTML .= ' class="radio">' . $strName . '</label>' . PHP_EOL; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
// radiogroup without options... |
101
|
|
|
trigger_error('empty radiogroup defined!', E_USER_NOTICE); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
105
|
|
|
|
106
|
|
|
return $strHTML; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set the select options for the element. |
111
|
|
|
* If no selection options are passed to the element via this method, an |
112
|
|
|
* attempt is made in the getHTML () method to determine an assigned list |
113
|
|
|
* via the data provider. |
114
|
|
|
* @param array $aOptions |
115
|
|
|
*/ |
116
|
|
|
public function setSelectOptions(array $aOptions) : void |
117
|
|
|
{ |
118
|
|
|
$this->aOptions = $aOptions; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|