1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SKien\Formgenerator; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Range slider element |
8
|
|
|
* |
9
|
|
|
* @package Formgenerator |
10
|
|
|
* @author Stefanius <[email protected]> |
11
|
|
|
* @copyright MIT License - see the LICENSE file for details |
12
|
|
|
*/ |
13
|
|
|
class FormRange extends FormInput |
14
|
|
|
{ |
15
|
|
|
/** @var int value */ |
16
|
|
|
protected int $iValue = 0; |
17
|
|
|
/** @var int step */ |
18
|
|
|
protected int $iStep = 1; |
19
|
|
|
/** @var int min value */ |
20
|
|
|
protected int $iMin = 0; |
21
|
|
|
/** @var int max value */ |
22
|
|
|
protected int $iMax = 100; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $strName name of input |
26
|
|
|
* @param string $strWidth width |
27
|
|
|
* @param int $iMin |
28
|
|
|
* @param int $iMax |
29
|
|
|
* @param int $wFlags flags (default = 0) |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $strName, string $strWidth, int $iMin = 0, int $iMax = 100, int $wFlags = 0) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($strName, $strWidth, $wFlags); |
34
|
|
|
$this->strType = 'range'; |
35
|
|
|
$this->iMin = $iMin; |
36
|
|
|
$this->iMax = $iMax; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritDoc} |
41
|
|
|
* @see \SKien\Formgenerator\FormElement::fromXML() |
42
|
|
|
*/ |
43
|
|
|
static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement |
44
|
|
|
{ |
45
|
|
|
$strName = self::getAttribString($oXMLElement, 'name', ''); |
46
|
|
|
$strWidth = self::getAttribString($oXMLElement, 'width', ''); |
47
|
|
|
$iMin = self::getAttribInt($oXMLElement, 'min', 0); |
48
|
|
|
$iMax = self::getAttribInt($oXMLElement, 'max', 100); |
49
|
|
|
$wFlags = self::getAttribFlags($oXMLElement); |
50
|
|
|
$oFormElement = new self($strName, $strWidth, $iMin, $iMax, $wFlags); |
51
|
|
|
$oFormParent->add($oFormElement); |
52
|
|
|
$oFormElement->readAdditionalXML($oXMLElement); |
53
|
|
|
return $oFormElement; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
* @see \SKien\Formgenerator\FormElement::readAdditionalXML() |
59
|
|
|
*/ |
60
|
|
|
public function readAdditionalXML(\DOMElement $oXMLElement) : void |
61
|
|
|
{ |
62
|
|
|
parent::readAdditionalXML($oXMLElement); |
63
|
|
|
if (($iStep = self::getAttribInt($oXMLElement, 'step')) !== null) { |
64
|
|
|
$this->setStep($iStep); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set min/max value accepted by input field. |
70
|
|
|
* @param int $iMin - set to null, if no limitation wanted |
71
|
|
|
* @param int $iMax - set to null, if no limitation wanted |
72
|
|
|
*/ |
73
|
|
|
public function setMinMax(?int $iMin, ?int $iMax) : void |
74
|
|
|
{ |
75
|
|
|
$this->iMin = $iMin ?? $this->iMin; |
76
|
|
|
$this->iMax = $iMax ?? $this->iMax; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set step width to be performed by slider. |
81
|
|
|
* @param int $iStep |
82
|
|
|
*/ |
83
|
|
|
public function setStep(int $iStep) : void |
84
|
|
|
{ |
85
|
|
|
$this->iStep = $iStep; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* get value from dataprovider. |
90
|
|
|
* Value have to be retrieved earlier because it may be needed for the value-label |
91
|
|
|
* before the value of the range element is set. |
92
|
|
|
*/ |
93
|
|
|
protected function onParentSet() : void |
94
|
|
|
{ |
95
|
|
|
$this->iValue = intval($this->oFG->getData()->getValue($this->strName)) ?? $this->iMin; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritDoc} |
100
|
|
|
* @see \SKien\Formgenerator\FormElement::buildValue() |
101
|
|
|
*/ |
102
|
|
|
protected function buildValue() : string |
103
|
|
|
{ |
104
|
|
|
$strHTML = ' value="' . $this->iValue . '"'; |
105
|
|
|
return $strHTML; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Build the HTML-notation for the input element. |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
* @see \SKien\Formgenerator\FormElement::getHTML() |
113
|
|
|
*/ |
114
|
|
|
public function getHTML() : string |
115
|
|
|
{ |
116
|
|
|
$this->addAttribute('step', (string)$this->iStep); |
117
|
|
|
$this->addAttribute('min', (string)$this->iMin); |
118
|
|
|
$this->addAttribute('max', (string)$this->iMax); |
119
|
|
|
$this->addAttribute('autocomplete', 'off'); |
120
|
|
|
|
121
|
|
|
$this->processFlags(); |
122
|
|
|
$strHTML = $this->buildContainerDiv('display: flex;'); |
123
|
|
|
|
124
|
|
|
$this->strID = $this->strID ?: $this->strName; |
125
|
|
|
|
126
|
|
|
if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && !$this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
127
|
|
|
$strHTML .= $this->buildValueLabel(); |
128
|
|
|
} |
129
|
|
|
$strHTML .= '<div class="slider" style="width: ' . $this->size . ';">'; |
130
|
|
|
$strHTML .= '<input'; |
131
|
|
|
$strHTML .= ' type="' . $this->strType . '"'; |
132
|
|
|
$strHTML .= ' name="' . $this->strName . '"'; |
133
|
|
|
$strHTML .= $this->buildClass(); |
134
|
|
|
$strHTML .= $this->buildID(); |
135
|
|
|
$strHTML .= $this->buildStyle(); |
136
|
|
|
$strHTML .= $this->buildTabindex(); |
137
|
|
|
$strHTML .= $this->buildAttributes(); |
138
|
|
|
$strHTML .= $this->buildValue(); |
139
|
|
|
$strHTML .= '>'; |
140
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
141
|
|
|
if ($this->oFlags->isSet(FormFlags::SHOW_VALUE) && $this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
142
|
|
|
$strHTML .= $this->buildValueLabel(); |
143
|
|
|
} |
144
|
|
|
$strHTML .= '</div>' . PHP_EOL; |
145
|
|
|
|
146
|
|
|
return $strHTML; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Build the label to display the actual value selected by the range element. |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function buildValueLabel() : string |
154
|
|
|
{ |
155
|
|
|
// if min is negative it can be longer than the max value... |
156
|
|
|
$iLen = (strlen((string)$this->iMin) > strlen((string)$this->iMax)) ? strlen((string)$this->iMin) : strlen((string)$this->iMax); |
157
|
|
|
|
158
|
|
|
$strHTML = '<label class="slider_label"'; |
159
|
|
|
$strHTML .= ' for="' . $this->strID . '"'; |
160
|
|
|
$strHTML .= ' style="width: ' . $iLen . 'em;'; |
161
|
|
|
if ($this->oFlags->isSet(FormFlags::ALIGN_RIGHT)) { |
162
|
|
|
$strHTML .= ' text-align: right;'; |
163
|
|
|
} |
164
|
|
|
$strHTML .= '">'; |
165
|
|
|
$strHTML .= $this->iValue; |
166
|
|
|
$strHTML .= '</label>' . PHP_EOL; |
167
|
|
|
|
168
|
|
|
return $strHTML; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|