ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilNumberInputGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected ?float $value = null;
30 protected int $maxlength = 200;
31 protected int $size = 40;
32 protected string $suffix = "";
33 protected ?float $minvalue = null;
34 protected bool $minvalueShouldBeGreater = false;
35 protected bool $minvalue_visible = false;
36 protected ?float $maxvalue = null;
37 protected bool $maxvalueShouldBeLess = false;
38 protected bool $maxvalue_visible = false;
39 protected int $decimals = 0;
40 protected bool $allow_decimals = false;
41 protected bool $client_side_validation = false;
42
43 public function __construct(
44 string $a_title = "",
45 string $a_postvar = ""
46 ) {
47 global $DIC;
48
49 $this->lng = $DIC->language();
50 parent::__construct($a_title, $a_postvar);
51 }
52
53 public function setSuffix(string $a_value): void
54 {
55 $this->suffix = $a_value;
56 }
57
58 public function getSuffix(): string
59 {
60 return $this->suffix;
61 }
62
63 public function setValue(?string $a_value): void
64 {
65 if ($a_value == "" || is_null($a_value)) {
66 $this->value = null;
67 return;
68 }
69 $this->value = (float) str_replace(',', '.', $a_value);
70
71 // integer
72 if (!$this->areDecimalsAllowed()) {
73 $this->value = round($this->value);
74 }
75 // float
76 elseif ($this->getDecimals() > 0) {
77 // get rid of unwanted decimals
78 $this->value = round($this->value, $this->getDecimals());
79
80 // pad value to specified format
81 $this->value = (float) number_format($this->value, $this->getDecimals(), ".", "");
82 }
83 }
84
85 public function getValue(): ?float
86 {
87 return $this->value;
88 }
89
90 public function unserializeData(string $a_data): void
91 {
92 $data = unserialize($a_data);
93
94 // BT 35716: table filters with numeric input fields should accept 0
95 if ($data || $data === 0 || $data === 0. || $data === '0') {
96 $this->setValue((string) $data);
97 } else {
98 $this->setValue(null);
99 }
100 }
101
102 public function setMaxLength(int $a_maxlength): void
103 {
104 $this->maxlength = $a_maxlength;
105 }
106
107 public function getMaxLength(): int
108 {
109 return $this->maxlength;
110 }
111
112 // true if the minimum value should be greater than minvalue
113 public function setMinvalueShouldBeGreater(bool $a_bool): void
114 {
115 $this->minvalueShouldBeGreater = $a_bool;
116 }
117
118 public function minvalueShouldBeGreater(): bool
119 {
121 }
122
123 // true if the maximum value should be less than maxvalue
124 public function setMaxvalueShouldBeLess(bool $a_bool): void
125 {
126 $this->maxvalueShouldBeLess = $a_bool;
127 }
128
129 public function maxvalueShouldBeLess(): bool
130 {
132 }
133
134 public function setSize(int $a_size): void
135 {
136 $this->size = $a_size;
137 }
138
139 public function setValueByArray(array $a_values): void
140 {
141 $this->setValue((string) ($a_values[$this->getPostVar()] ?? ""));
142 }
143
144 public function getSize(): int
145 {
146 return $this->size;
147 }
148
149 public function setMinValue(
150 float $a_minvalue,
151 bool $a_display_always = false
152 ): void {
153 $this->minvalue = $a_minvalue;
154 $this->minvalue_visible = $a_display_always;
155 }
156
157 public function getMinValue(): ?float
158 {
159 return $this->minvalue;
160 }
161
162 public function setMaxValue(
163 float $a_maxvalue,
164 bool $a_display_always = false
165 ): void {
166 $this->maxvalue = $a_maxvalue;
167 $this->maxvalue_visible = $a_display_always;
168 }
169
170 public function getMaxValue(): ?float
171 {
172 return $this->maxvalue;
173 }
174
175 public function setDecimals(int $a_decimals): void
176 {
177 $this->decimals = $a_decimals;
178 if ($this->decimals) {
179 $this->allowDecimals(true);
180 }
181 }
182
183 public function getDecimals(): int
184 {
185 return $this->decimals;
186 }
187
188 public function allowDecimals(bool $a_value): void
189 {
190 $this->allow_decimals = $a_value;
191 }
192
193 public function areDecimalsAllowed(): bool
194 {
195 return $this->allow_decimals;
196 }
197
198 public function checkInput(): bool
199 {
201
202 $val = trim($this->str($this->getPostVar()));
203 if ($this->getRequired() && $val == "") {
204 $this->setAlert($lng->txt("msg_input_is_required"));
205 return false;
206 }
207 $val = str_replace(',', '.', $val);
208
209 if ($val != "" && !is_numeric($val)) {
210 $this->minvalue_visible = true;
211 $this->maxvalue_visible = true;
212 $this->setAlert($lng->txt("form_msg_numeric_value_required"));
213 return false;
214 }
215
216 if ($this->minvalueShouldBeGreater()) {
217 if ($val != "" && $this->getMinValue() !== null &&
218 $val <= $this->getMinValue()) {
219 $this->minvalue_visible = true;
220 $this->setAlert($lng->txt("form_msg_value_too_low"));
221 return false;
222 }
223 } else {
224 if ($val != "" &&
225 $this->getMinValue() !== null &&
226 $val < $this->getMinValue()) {
227 $this->minvalue_visible = true;
228 $this->setAlert($lng->txt("form_msg_value_too_low"));
229 return false;
230 }
231 }
232
233 if ($this->maxvalueShouldBeLess()) {
234 if ($val != "" &&
235 $this->getMaxValue() !== null &&
236 $val >= $this->getMaxValue()) {
237 $this->maxvalue_visible = true;
238 $this->setAlert($lng->txt("form_msg_value_too_high"));
239 return false;
240 }
241 } else {
242 if ($val != "" &&
243 $this->getMaxValue() !== null &&
244 $val > $this->getMaxValue()) {
245 $this->maxvalue_visible = true;
246 $this->setAlert($lng->txt("form_msg_value_too_high"));
247 return false;
248 }
249 }
250
251 return $this->checkSubItemsInput();
252 }
253
254 public function getInput(): ?float
255 {
256 $value = $this->str($this->getPostVar());
257 if (trim($value) == "") {
258 return null;
259 }
260 return (float) str_replace(',', '.', $value);
261 }
262
263 public function insert(ilTemplate $a_tpl): void
264 {
265 $html = $this->render();
266
267 $a_tpl->setCurrentBlock("prop_generic");
268 $a_tpl->setVariable("PROP_GENERIC", $html);
269 $a_tpl->parseCurrentBlock();
270 }
271
272 public function render(): string
273 {
275
276 $tpl = new ilTemplate("tpl.prop_number.html", true, true, "components/ILIAS/Form");
277
278 if (strlen((string) $this->getValue())) {
279 $tpl->setCurrentBlock("prop_number_propval");
280 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput((string) $this->getValue()));
281 $tpl->parseCurrentBlock();
282 }
283
284 if ($this->getInfo() !== '') {
285 $tpl->setCurrentBlock('described_by_description');
286 $tpl->setVariable('DESCRIBED_BY_DESCRIPTION_FIELD_ID', $this->getFieldId());
287 $tpl->parseCurrentBlock();
288 }
289
290 // constraints
291 $constraints = "";
292 $delim = "";
293 if ($this->areDecimalsAllowed() && $this->getDecimals() > 0) {
294 $constraints = $lng->txt("form_format") . ": ###." . str_repeat("#", $this->getDecimals());
295 $delim = ", ";
296 }
297 if ($this->getMinValue() !== null && $this->minvalue_visible) {
298 $constraints .= $delim . $lng->txt("form_min_value") . ": " . (($this->minvalueShouldBeGreater()) ? "&gt; " : "") . $this->getMinValue();
299 $delim = ", ";
300 }
301 if ($this->getMaxValue() !== null && $this->maxvalue_visible) {
302 $constraints .= $delim . $lng->txt("form_max_value") . ": " . (($this->maxvalueShouldBeLess()) ? "&lt; " : "") . $this->getMaxValue();
303 $delim = ", ";
304 }
305
306 if ($constraints !== "") {
307 $tpl->setCurrentBlock('described_by_constraint');
308 $tpl->setVariable('DESCRIBED_BY_CONSTRAINT_FIELD_ID', $this->getFieldId());
309 $tpl->parseCurrentBlock();
310 }
311
312 $tpl->setCurrentBlock("prop_number");
313
314 $tpl->setVariable("POST_VAR", $this->getPostVar());
315 $tpl->setVariable("ID", $this->getFieldId());
316 $tpl->setVariable("SIZE", $this->getSize());
317 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
318 if (strlen($this->getSuffix())) {
319 $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
320 }
321 if ($this->getDisabled()) {
322 $tpl->setVariable(
323 "DISABLED",
324 " disabled=\"disabled\""
325 );
326 }
327
328 if ($this->client_side_validation) {
329 $tpl->setVariable("JS_DECIMALS_ALLOWED", (int) $this->areDecimalsAllowed());
330 $tpl->setVariable("JS_ID", $this->getFieldId());
331 }
332
333 if ($constraints !== '') {
334 $tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
335 $tpl->setVariable(
336 "CONSTRAINT_FOR_ID",
337 $this->getFieldId()
338 );
339 }
340
341 if ($this->getRequired()) {
342 $tpl->setVariable("REQUIRED", "required=\"required\"");
343 }
344
345 $tpl->parseCurrentBlock();
346
347 return $tpl->get();
348 }
349
350 public function getPostValueForComparison(): ?float
351 {
352 return $this->getInput();
353 }
354
355 public function setClientSideValidation(bool $validate): void
356 {
357 $this->client_side_validation = $validate;
358 }
359}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a number property in a property form.
setMaxValue(float $a_maxvalue, bool $a_display_always=false)
checkInput()
Check input, strip slashes etc.
setMaxLength(int $a_maxlength)
setMinValue(float $a_minvalue, bool $a_display_always=false)
allowDecimals(bool $a_value)
unserializeData(string $a_data)
setDecimals(int $a_decimals)
setMaxvalueShouldBeLess(bool $a_bool)
insert(ilTemplate $a_tpl)
setValueByArray(array $a_values)
setSuffix(string $a_value)
__construct(string $a_title="", string $a_postvar="")
setClientSideValidation(bool $validate)
setValue(?string $a_value)
setMinvalueShouldBeGreater(bool $a_bool)
This class represents a property that may include a sub form.
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26