|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Static Text element. |
|
9
|
|
|
* |
|
10
|
|
|
* The FormStatic element can be used for different purposes: |
|
11
|
|
|
* - just for the normal output of a text |
|
12
|
|
|
* - as a label that is assigned to a specific input element |
|
13
|
|
|
* - set the `$strLabelFor` param in the constructor or use the `setLabelFor()` method |
|
14
|
|
|
* - As a hint |
|
15
|
|
|
* - set the `FormFlags::HINT` flag |
|
16
|
|
|
* - To display an error message |
|
17
|
|
|
* - set the `FormFlags::ERROR` flag |
|
18
|
|
|
* - As an information field |
|
19
|
|
|
* - set the `FormFlags::INFO` flag |
|
20
|
|
|
* |
|
21
|
|
|
* @SKienImage FormStatic.png |
|
22
|
|
|
* |
|
23
|
|
|
* @package Formgenerator |
|
24
|
|
|
* @author Stefanius <[email protected]> |
|
25
|
|
|
* @copyright MIT License - see the LICENSE file for details |
|
26
|
|
|
*/ |
|
27
|
|
|
class FormStatic extends FormElement |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var string the text to display */ |
|
30
|
|
|
protected string $strText; |
|
31
|
|
|
/** @var string the if set, label with for attribute is created */ |
|
32
|
|
|
protected string $strLabelFor = ''; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a static text element. |
|
36
|
|
|
* @param string $strText text to display |
|
37
|
|
|
* @param int $wFlags any combination of FormFlag constants |
|
38
|
|
|
* @param string $strLabelFor crate label for this element |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(string $strText, int $wFlags = 0, string $strLabelFor = '') |
|
41
|
|
|
{ |
|
42
|
|
|
$this->strText = $strText; |
|
43
|
|
|
$this->strLabelFor = $strLabelFor; |
|
44
|
|
|
parent::__construct($wFlags); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritDoc} |
|
49
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
|
50
|
|
|
* @internal |
|
51
|
|
|
*/ |
|
52
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
|
53
|
|
|
{ |
|
54
|
|
|
$strText = self::getAttribString($oXMLElement, 'text', ''); |
|
55
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
|
56
|
|
|
$strLabelFor = self::getAttribString($oXMLElement, 'for', ''); |
|
57
|
|
|
$oFormElement = new self($strText, $wFlags, $strLabelFor); |
|
58
|
|
|
$oFormParent->add($oFormElement); |
|
59
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
|
60
|
|
|
|
|
61
|
|
|
return $oFormElement; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the control for which a label should be created. |
|
66
|
|
|
* @param string $strLabelFor |
|
67
|
|
|
*/ |
|
68
|
|
|
public function setLabelFor(string $strLabelFor) : void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->strLabelFor = $strLabelFor; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Build the HTML-notation for the static text |
|
75
|
|
|
* @return string |
|
76
|
|
|
* @internal |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getHTML() : string |
|
79
|
|
|
{ |
|
80
|
|
|
// no container div! |
|
81
|
|
|
$this->addStyle('float', 'left'); |
|
82
|
|
|
$strWidth = ($this->oParent ? $this->oParent->getColWidth($this->iCol) : ''); |
|
83
|
|
|
if (!empty($strWidth)) { |
|
84
|
|
|
$this->addStyle('width', $strWidth); |
|
85
|
|
|
} |
|
86
|
|
|
if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
|
87
|
|
|
$this->addStyle('text-align', 'right'); |
|
88
|
|
|
} else if ($this->oFlags->isSet(FormFlags::ALIGN_CENTER)) { |
|
89
|
|
|
$this->addStyle('text-align', 'center'); |
|
90
|
|
|
} |
|
91
|
|
|
if ($this->oFlags->isSet(FormFlags::BOLD)) { |
|
92
|
|
|
$this->addStyle('font-weight', 'bold'); |
|
93
|
|
|
} |
|
94
|
|
|
$strSep = ''; |
|
95
|
|
|
if (strlen($this->strClass) !== 0) { |
|
96
|
|
|
$strSep = ' '; |
|
97
|
|
|
} |
|
98
|
|
|
if ($this->oFlags->isSet(FormFlags::ERROR)) { |
|
99
|
|
|
$this->strClass .= $strSep . 'error'; |
|
100
|
|
|
} else if ($this->oFlags->isSet(FormFlags::HINT)) { |
|
101
|
|
|
$this->strClass = $strSep . 'hint'; |
|
102
|
|
|
} else if ($this->oFlags->isSet(FormFlags::INFO)) { |
|
103
|
|
|
$this->strClass = $strSep . 'forminfo'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$strHTML = ''; |
|
107
|
|
|
$strHTML .= '<div'; |
|
108
|
|
|
$strHTML .= $this->buildID(); |
|
109
|
|
|
$strHTML .= $this->buildClass(); |
|
110
|
|
|
$strHTML .= $this->buildStyle(); |
|
111
|
|
|
$strHTML .= $this->buildAttributes(); |
|
112
|
|
|
$strHTML .= '>'; |
|
113
|
|
|
if (strlen($this->strLabelFor) > 0 ) { |
|
114
|
|
|
$strHTML .= '<label for="' . $this->strLabelFor . '">' . $this->strText . '</label>'; |
|
115
|
|
|
} else { |
|
116
|
|
|
$strHTML .= $this->strText; |
|
117
|
|
|
} |
|
118
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
|
119
|
|
|
|
|
120
|
|
|
return $strHTML; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|