ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilNumberInputGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once("./Services/Form/classes/class.ilSubEnabledFormPropertyGUI.php");
6 
15 {
16  protected $value;
17  protected $maxlength = 200;
18  protected $size = 40;
19  protected $suffix;
20  protected $minvalue = false;
21  protected $minvalueShouldBeGreater = false;
22  protected $minvalue_visible = false;
23  protected $maxvalue = false;
24  protected $maxvalueShouldBeLess = false;
25  protected $maxvalue_visible = false;
26  protected $decimals;
27  protected $allow_decimals = false;
28  protected $client_side_validation = false;
29 
36  public function __construct($a_title = "", $a_postvar = "")
37  {
38  global $DIC;
39 
40  $this->lng = $DIC->language();
41  parent::__construct($a_title, $a_postvar);
42  }
43 
49  public function setSuffix($a_value)
50  {
51  $this->suffix = $a_value;
52  }
53 
59  public function getSuffix()
60  {
61  return $this->suffix;
62  }
63 
69  public function setValue($a_value)
70  {
71  $this->value = str_replace(',', '.', $a_value);
72 
73  // empty strings are allowed
74  if ($this->value != "") {
75  // integer
76  if (!$this->areDecimalsAllowed()) {
77  $this->value = round($this->value);
78  }
79  // float
80  elseif ($this->getDecimals() > 0) {
81  // get rid of unwanted decimals
82  $this->value = round($this->value, $this->getDecimals());
83 
84  // pad value to specified format
85  $this->value = number_format($this->value, $this->getDecimals(), ".", "");
86  }
87  }
88  }
89 
95  public function getValue()
96  {
97  return $this->value;
98  }
99 
105  public function setMaxLength($a_maxlength)
106  {
107  $this->maxlength = $a_maxlength;
108  }
109 
115  public function getMaxLength()
116  {
117  return $this->maxlength;
118  }
119 
125  public function setMinvalueShouldBeGreater($a_bool)
126  {
127  $this->minvalueShouldBeGreater = $a_bool;
128  }
129 
135  public function minvalueShouldBeGreater()
136  {
138  }
139 
145  public function setMaxvalueShouldBeLess($a_bool)
146  {
147  $this->maxvalueShouldBeLess = $a_bool;
148  }
149 
155  public function maxvalueShouldBeLess()
156  {
158  }
159 
165  public function setSize($a_size)
166  {
167  $this->size = $a_size;
168  }
169 
175  public function setValueByArray($a_values)
176  {
177  $this->setValue($a_values[$this->getPostVar()]);
178  }
179 
185  public function getSize()
186  {
187  return $this->size;
188  }
189 
196  public function setMinValue($a_minvalue, $a_display_always = false)
197  {
198  $this->minvalue = $a_minvalue;
199  $this->minvalue_visible = (bool) $a_display_always;
200  }
201 
207  public function getMinValue()
208  {
209  return $this->minvalue;
210  }
211 
218  public function setMaxValue($a_maxvalue, $a_display_always = false)
219  {
220  $this->maxvalue = $a_maxvalue;
221  $this->maxvalue_visible = (bool) $a_display_always;
222  }
223 
229  public function getMaxValue()
230  {
231  return $this->maxvalue;
232  }
233 
239  public function setDecimals($a_decimals)
240  {
241  $this->decimals = (int) $a_decimals;
242  if ($this->decimals) {
243  $this->allowDecimals(true);
244  }
245  }
246 
252  public function getDecimals()
253  {
254  return $this->decimals;
255  }
256 
262  public function allowDecimals($a_value)
263  {
264  $this->allow_decimals = (bool) $a_value;
265  }
266 
272  public function areDecimalsAllowed()
273  {
274  return $this->allow_decimals;
275  }
276 
282  public function checkInput()
283  {
284  $lng = $this->lng;
285 
286  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
287  if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "") {
288  $this->setAlert($lng->txt("msg_input_is_required"));
289  return false;
290  }
291 
292  if (trim($_POST[$this->getPostVar()]) != "" &&
293  !is_numeric(str_replace(',', '.', $_POST[$this->getPostVar()]))) {
294  $this->minvalue_visible = true;
295  $this->maxvalue_visible = true;
296  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
297  return false;
298  }
299 
300  if ($this->minvalueShouldBeGreater()) {
301  if (trim($_POST[$this->getPostVar()]) != "" &&
302  $this->getMinValue() !== false &&
303  $_POST[$this->getPostVar()] <= $this->getMinValue()) {
304  $this->minvalue_visible = true;
305  $this->setAlert($lng->txt("form_msg_value_too_low"));
306  return false;
307  }
308  } else {
309  if (trim($_POST[$this->getPostVar()]) != "" &&
310  $this->getMinValue() !== false &&
311  $_POST[$this->getPostVar()] < $this->getMinValue()) {
312  $this->minvalue_visible = true;
313  $this->setAlert($lng->txt("form_msg_value_too_low"));
314  return false;
315  }
316  }
317 
318  if ($this->maxvalueShouldBeLess()) {
319  if (trim($_POST[$this->getPostVar()]) != "" &&
320  $this->getMaxValue() !== false &&
321  $_POST[$this->getPostVar()] >= $this->getMaxValue()) {
322  $this->maxvalue_visible = true;
323  $this->setAlert($lng->txt("form_msg_value_too_high"));
324  return false;
325  }
326  } else {
327  if (trim($_POST[$this->getPostVar()]) != "" &&
328  $this->getMaxValue() !== false &&
329  $_POST[$this->getPostVar()] > $this->getMaxValue()) {
330  $this->maxvalue_visible = true;
331  $this->setAlert($lng->txt("form_msg_value_too_high"));
332  return false;
333  }
334  }
335 
336  return $this->checkSubItemsInput();
337  }
338 
344  public function insert($a_tpl)
345  {
346  $html = $this->render();
347 
348  $a_tpl->setCurrentBlock("prop_generic");
349  $a_tpl->setVariable("PROP_GENERIC", $html);
350  $a_tpl->parseCurrentBlock();
351  }
352 
356  public function render()
357  {
358  $lng = $this->lng;
359 
360  $tpl = new ilTemplate("tpl.prop_number.html", true, true, "Services/Form");
361 
362  if (strlen($this->getValue())) {
363  $tpl->setCurrentBlock("prop_number_propval");
364  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
365  $tpl->parseCurrentBlock();
366  }
367  $tpl->setCurrentBlock("prop_number");
368 
369  $tpl->setVariable("POST_VAR", $this->getPostVar());
370  $tpl->setVariable("ID", $this->getFieldId());
371  $tpl->setVariable("SIZE", $this->getSize());
372  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
373  if (strlen($this->getSuffix())) {
374  $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
375  }
376  if ($this->getDisabled()) {
377  $tpl->setVariable(
378  "DISABLED",
379  " disabled=\"disabled\""
380  );
381  }
382 
383  if ($this->client_side_validation) {
384  $tpl->setVariable("JS_DECIMALS_ALLOWED", (int) $this->areDecimalsAllowed());
385  $tpl->setVariable("JS_ID", $this->getFieldId());
386  }
387 
388 
389  // constraints
390  if ($this->areDecimalsAllowed() && $this->getDecimals() > 0) {
391  $constraints = $lng->txt("form_format") . ": ###." . str_repeat("#", $this->getDecimals());
392  $delim = ", ";
393  }
394  if ($this->getMinValue() !== false && $this->minvalue_visible) {
395  $constraints .= $delim . $lng->txt("form_min_value") . ": " . (($this->minvalueShouldBeGreater()) ? "&gt; " : "") . $this->getMinValue();
396  $delim = ", ";
397  }
398  if ($this->getMaxValue() !== false && $this->maxvalue_visible) {
399  $constraints .= $delim . $lng->txt("form_max_value") . ": " . (($this->maxvalueShouldBeLess()) ? "&lt; " : "") . $this->getMaxValue();
400  $delim = ", ";
401  }
402  if ($constraints != "") {
403  $tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
404  }
405 
406  if ($this->getRequired()) {
407  $tpl->setVariable("REQUIRED", "required=\"required\"");
408  }
409 
410  $tpl->parseCurrentBlock();
411 
412  return $tpl->get();
413  }
414 
420  public function getPostValueForComparison()
421  {
423  if ($value != "") {
424  return (int) $value;
425  }
426  }
427 
428  public function setClientSideValidation(bool $validate) : void
429  {
430  $this->client_side_validation = $validate;
431  }
432 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
setMinvalueShouldBeGreater($a_bool)
Set minvalueShouldBeGreater.
setValue($a_value)
Set Value.
getMinValue()
Get Minimum Value.
getPostVar()
Get Post Variable.
getMaxLength()
Get Max Length.
__construct($a_title="", $a_postvar="")
Constructor.
insert($a_tpl)
Insert property html.
setAlert($a_alert)
Set Alert Text.
minvalueShouldBeGreater()
Get minvalueShouldBeGreater.
allowDecimals($a_value)
Toggle Decimals.
setDecimals($a_decimals)
Set Decimal Places.
setMinValue($a_minvalue, $a_display_always=false)
Set Minimum Value.
setSuffix($a_value)
Set suffix.
getMaxValue()
Get Maximum Value.
checkInput()
Check input, strip slashes etc.
This class represents a number property in a property form.
getFieldId()
Get Post Variable.
global $DIC
Definition: goto.php:24
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
setMaxValue($a_maxvalue, $a_display_always=false)
Set Maximum Value.
setSize($a_size)
Set Size.
setMaxLength($a_maxlength)
Set Max Length.
getPostValueForComparison()
parse post value to make it comparable
render()
Insert property html.
__construct(Container $dic, ilPlugin $plugin)
This class represents a property that may include a sub form.
setValueByArray($a_values)
Set value by array.
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$_POST["username"]
getDecimals()
Get Decimal Places.
setMaxvalueShouldBeLess($a_bool)
Set maxvalueShouldBeLess.
maxvalueShouldBeLess()
Get maxvalueShouldBeLess.
setClientSideValidation(bool $validate)