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