1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Meter element. |
8
|
|
|
* |
9
|
|
|
* The meter element represents a scalar measurement within a known range |
10
|
|
|
* (defined by min/max), or a fractional value (also known as a gauge). <br/> |
11
|
|
|
* The element is particularly suitable when it is desired that the color |
12
|
|
|
* display should change dependent on certain boundaries. |
13
|
|
|
* |
14
|
|
|
* > The meter element should not be used to indicate progress (as in a progress bar). Use |
15
|
|
|
* > a progress element instead. |
16
|
|
|
* > The meter element also does not represent a scalar value of arbitrary range unless |
17
|
|
|
* > at least a maximum is known. |
18
|
|
|
* > |
19
|
|
|
* > Examples: |
20
|
|
|
* > - disk usage |
21
|
|
|
* > - temperature within defined boundaries |
22
|
|
|
* > - relevance of a query result |
23
|
|
|
* > - fraction of a voting population to have selected a particular candidate. |
24
|
|
|
* |
25
|
|
|
* In addition to the min / max range, a lower and upper bound (low/high) and an optimum |
26
|
|
|
* value for checking and color coding can optionally be specified. |
27
|
|
|
* |
28
|
|
|
* <b>Usage without optimum value: </b><br/> |
29
|
|
|
* If the value to be displayed is below the lower or above the upper bound, the output |
30
|
|
|
* is in a different color than within the bounds. |
31
|
|
|
* |
32
|
|
|
* <b>Usage including optimum value: </b><br/> |
33
|
|
|
* This means that the color display changes between three categories (bad / middle / |
34
|
|
|
* good), depending on the value. |
35
|
|
|
* 1. *bad-middle-good*, if the optimum value is between the min and low bound. |
36
|
|
|
* 2. *good-middle-bad*, if the optimum value is between the high and max bound. |
37
|
|
|
* |
38
|
|
|
* @SKienImage FormMeter.png |
39
|
|
|
* |
40
|
|
|
* @package Formgenerator |
41
|
|
|
* @author Stefanius <[email protected]> |
42
|
|
|
* @copyright MIT License - see the LICENSE file for details |
43
|
|
|
*/ |
44
|
|
|
class FormMeter extends FormInput |
45
|
|
|
{ |
46
|
|
|
/** default min value */ |
47
|
|
|
protected const MIN_DEFAULT = 0; |
48
|
|
|
/** default max value */ |
49
|
|
|
protected const MAX_DEFAULT = 1.0; |
50
|
|
|
|
51
|
|
|
/** @var float value */ |
52
|
|
|
protected float $fltValue = 0; |
53
|
|
|
/** @var float min value */ |
54
|
|
|
protected float $fltMin; |
55
|
|
|
/** @var float max value */ |
56
|
|
|
protected float $fltMax; |
57
|
|
|
/** @var float low value */ |
58
|
|
|
protected ?float $fltLow = null; |
59
|
|
|
/** @var float high value */ |
60
|
|
|
protected ?float $fltHigh = null; |
61
|
|
|
/** @var float optimum value */ |
62
|
|
|
protected ?float $fltOpt = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a range slider. |
66
|
|
|
* @param string $strName name of the Element |
67
|
|
|
* @param string $strWidth width |
68
|
|
|
* @param float $fltMin min value |
69
|
|
|
* @param float $fltMax max value |
70
|
|
|
* @param int $wFlags any combination of FormFlag constants |
71
|
|
|
*/ |
72
|
|
|
public function __construct(string $strName, string $strWidth, float $fltMin = self::MIN_DEFAULT, float $fltMax = self::MAX_DEFAULT, int $wFlags = 0) |
73
|
|
|
{ |
74
|
|
|
parent::__construct($strName, $strWidth, $wFlags); |
75
|
|
|
$this->addFlags(FormFlags::READ_ONLY); |
76
|
|
|
$this->fltMin = $fltMin; |
77
|
|
|
$this->fltMax = $fltMax; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
83
|
|
|
* @internal l |
84
|
|
|
*/ |
85
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
86
|
|
|
{ |
87
|
|
|
$strName = self::getAttribString($oXMLElement, 'name', ''); |
88
|
|
|
$strWidth = self::getAttribString($oXMLElement, 'width', ''); |
89
|
|
|
$fltMin = self::getAttribFloat($oXMLElement, 'min', self::MIN_DEFAULT); |
90
|
|
|
$fltMax = self::getAttribFloat($oXMLElement, 'max', self::MAX_DEFAULT); |
91
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
92
|
|
|
$oFormElement = new self($strName, $strWidth, $fltMin, $fltMax, $wFlags); |
|
|
|
|
93
|
|
|
$oFormParent->add($oFormElement); |
94
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
95
|
|
|
return $oFormElement; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
* @see \SKien\Formgenerator\FormElement::readAdditionalXML() |
101
|
|
|
* @internal |
102
|
|
|
*/ |
103
|
|
|
public function readAdditionalXML(\DOMElement $oXMLElement) : void |
104
|
|
|
{ |
105
|
|
|
parent::readAdditionalXML($oXMLElement); |
106
|
|
|
$this->fltLow = self::getAttribFloat($oXMLElement, 'low'); |
107
|
|
|
$this->fltHigh = self::getAttribFloat($oXMLElement, 'high'); |
108
|
|
|
$this->fltopt = self::getAttribFloat($oXMLElement, 'optimum'); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set min/max value for the slider. |
113
|
|
|
* @param float $fltMin |
114
|
|
|
* @param float $fltMax |
115
|
|
|
*/ |
116
|
|
|
public function setMinMax(?float $fltMin, ?float $fltMax) : void |
117
|
|
|
{ |
118
|
|
|
$this->fltMin = $fltMin ?? $this->fltMin; |
119
|
|
|
$this->fltMax = $fltMax ?? $this->fltMax; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the upper/lower bound of the measured range and the optimum value. |
124
|
|
|
* @param float $fltLow |
125
|
|
|
* @param float $fltHigh |
126
|
|
|
* @param float $fltOpt |
127
|
|
|
*/ |
128
|
|
|
public function setMessureRange(?float $fltLow, ?float $fltHigh, ?float $fltOpt = null) : void |
129
|
|
|
{ |
130
|
|
|
$this->fltLow = $fltLow; |
131
|
|
|
$this->fltHigh = $fltHigh; |
132
|
|
|
$this->fltOpt = $fltOpt; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* get value from dataprovider. |
137
|
|
|
* Value have to be retrieved earlier because it may be needed for the value-label |
138
|
|
|
* before the value of the range element is set. |
139
|
|
|
*/ |
140
|
|
|
protected function onParentSet() : void |
141
|
|
|
{ |
142
|
|
|
$value = $this->oFG->getData()->getValue($this->strName); |
143
|
|
|
$this->fltValue = $value ? floatval($value) : $this->fltMin; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritDoc} |
148
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
149
|
|
|
*/ |
150
|
|
|
protected function buildValue() : string |
151
|
|
|
{ |
152
|
|
|
$strHTML = ' value="' . $this->fltValue . '"'; |
153
|
|
|
return $strHTML; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Build the HTML-notation for the input element. |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
* @see \SKien\Formgenerator\FormElement::getHTML() |
161
|
|
|
* @internal |
162
|
|
|
*/ |
163
|
|
|
public function getHTML() : string |
164
|
|
|
{ |
165
|
|
|
$this->addAttribute('min', (string)$this->fltMin); |
166
|
|
|
$this->addAttribute('max', (string)$this->fltMax); |
167
|
|
|
$this->addAttribute('low', (string)$this->fltLow); |
168
|
|
|
$this->addAttribute('high', (string)$this->fltHigh); |
169
|
|
|
$this->addAttribute('optimum', (string)$this->fltOpt); |
170
|
|
|
$this->addAttribute('autocomplete', 'off'); |
171
|
|
|
|
172
|
|
|
if (!empty($this->size)) { |
173
|
|
|
$this->addStyle('width', $this->size); |
174
|
|
|
} |
175
|
|
|
$this->processFlags(); |
176
|
|
|
|
177
|
|
|
$strInnerHTML = $this->getAttribute('title'); |
178
|
|
|
if ($strInnerHTML === null) { |
179
|
|
|
$strInnerHTML = $this->buildTitle(); |
180
|
|
|
$this->addAttribute('title', $strInnerHTML); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$this->strID = $this->strID ?: $this->strName; |
184
|
|
|
|
185
|
|
|
$strHTML = $this->buildContainerDiv(); |
186
|
|
|
$strHTML .= '<meter '; |
187
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
188
|
|
|
$strHTML .= FormElement::buildClass(); // direct call of FormElement - we don't want 'Input_OK' ... |
189
|
|
|
$strHTML .= $this->buildID(); |
190
|
|
|
$strHTML .= $this->buildStyle(); |
191
|
|
|
$strHTML .= $this->buildAttributes(); |
192
|
|
|
$strHTML .= $this->buildValue(); |
193
|
|
|
$strHTML .= '>' . $strInnerHTML . '</meter>'; |
194
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
195
|
|
|
|
196
|
|
|
return $strHTML; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Build a title from value and min/max settings. |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
|
|
protected function buildTitle() : string |
204
|
|
|
{ |
205
|
|
|
return $this->fltValue . ' / ' . $this->fltMax; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|