ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
35  public function __construct($a_title = "", $a_postvar = "")
36  {
37  global $DIC;
38 
39  $this->lng = $DIC->language();
40  parent::__construct($a_title, $a_postvar);
41  }
42 
48  public function setSuffix($a_value)
49  {
50  $this->suffix = $a_value;
51  }
52 
58  public function getSuffix()
59  {
60  return $this->suffix;
61  }
62 
68  public function setValue($a_value)
69  {
70  $this->value = str_replace(',', '.', $a_value);
71 
72  // empty strings are allowed
73  if ($this->value != "") {
74  // integer
75  if (!$this->areDecimalsAllowed()) {
76  $this->value = round($this->value);
77  }
78  // float
79  elseif ($this->getDecimals() > 0) {
80  // get rid of unwanted decimals
81  $this->value = round($this->value, $this->getDecimals());
82 
83  // pad value to specified format
84  $this->value = number_format($this->value, $this->getDecimals(), ".", "");
85  }
86  }
87  }
88 
94  public function getValue()
95  {
96  return $this->value;
97  }
98 
104  public function setMaxLength($a_maxlength)
105  {
106  $this->maxlength = $a_maxlength;
107  }
108 
114  public function getMaxLength()
115  {
116  return $this->maxlength;
117  }
118 
124  public function setMinvalueShouldBeGreater($a_bool)
125  {
126  $this->minvalueShouldBeGreater = $a_bool;
127  }
128 
134  public function minvalueShouldBeGreater()
135  {
137  }
138 
144  public function setMaxvalueShouldBeLess($a_bool)
145  {
146  $this->maxvalueShouldBeLess = $a_bool;
147  }
148 
154  public function maxvalueShouldBeLess()
155  {
157  }
158 
164  public function setSize($a_size)
165  {
166  $this->size = $a_size;
167  }
168 
174  public function setValueByArray($a_values)
175  {
176  $this->setValue($a_values[$this->getPostVar()]);
177  }
178 
184  public function getSize()
185  {
186  return $this->size;
187  }
188 
195  public function setMinValue($a_minvalue, $a_display_always = false)
196  {
197  $this->minvalue = $a_minvalue;
198  $this->minvalue_visible = (bool) $a_display_always;
199  }
200 
206  public function getMinValue()
207  {
208  return $this->minvalue;
209  }
210 
217  public function setMaxValue($a_maxvalue, $a_display_always = false)
218  {
219  $this->maxvalue = $a_maxvalue;
220  $this->maxvalue_visible = (bool) $a_display_always;
221  }
222 
228  public function getMaxValue()
229  {
230  return $this->maxvalue;
231  }
232 
238  public function setDecimals($a_decimals)
239  {
240  $this->decimals = (int) $a_decimals;
241  if ($this->decimals) {
242  $this->allowDecimals(true);
243  }
244  }
245 
251  public function getDecimals()
252  {
253  return $this->decimals;
254  }
255 
261  public function allowDecimals($a_value)
262  {
263  $this->allow_decimals = (bool) $a_value;
264  }
265 
271  public function areDecimalsAllowed()
272  {
273  return $this->allow_decimals;
274  }
275 
281  public function checkInput()
282  {
283  $lng = $this->lng;
284 
285  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
286  if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "") {
287  $this->setAlert($lng->txt("msg_input_is_required"));
288  return false;
289  }
290 
291  if (trim($_POST[$this->getPostVar()]) != "" &&
292  !is_numeric(str_replace(',', '.', $_POST[$this->getPostVar()]))) {
293  $this->minvalue_visible = true;
294  $this->maxvalue_visible = true;
295  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
296  return false;
297  }
298 
299  if ($this->minvalueShouldBeGreater()) {
300  if (trim($_POST[$this->getPostVar()]) != "" &&
301  $this->getMinValue() !== false &&
302  $_POST[$this->getPostVar()] <= $this->getMinValue()) {
303  $this->minvalue_visible = true;
304  $this->setAlert($lng->txt("form_msg_value_too_low"));
305  return false;
306  }
307  } else {
308  if (trim($_POST[$this->getPostVar()]) != "" &&
309  $this->getMinValue() !== false &&
310  $_POST[$this->getPostVar()] < $this->getMinValue()) {
311  $this->minvalue_visible = true;
312  $this->setAlert($lng->txt("form_msg_value_too_low"));
313  return false;
314  }
315  }
316 
317  if ($this->maxvalueShouldBeLess()) {
318  if (trim($_POST[$this->getPostVar()]) != "" &&
319  $this->getMaxValue() !== false &&
320  $_POST[$this->getPostVar()] >= $this->getMaxValue()) {
321  $this->maxvalue_visible = true;
322  $this->setAlert($lng->txt("form_msg_value_too_high"));
323  return false;
324  }
325  } else {
326  if (trim($_POST[$this->getPostVar()]) != "" &&
327  $this->getMaxValue() !== false &&
328  $_POST[$this->getPostVar()] > $this->getMaxValue()) {
329  $this->maxvalue_visible = true;
330  $this->setAlert($lng->txt("form_msg_value_too_high"));
331  return false;
332  }
333  }
334 
335  return $this->checkSubItemsInput();
336  }
337 
343  public function insert($a_tpl)
344  {
345  $html = $this->render();
346 
347  $a_tpl->setCurrentBlock("prop_generic");
348  $a_tpl->setVariable("PROP_GENERIC", $html);
349  $a_tpl->parseCurrentBlock();
350  }
351 
355  public function render()
356  {
357  $lng = $this->lng;
358 
359  $tpl = new ilTemplate("tpl.prop_number.html", true, true, "Services/Form");
360 
361  if (strlen($this->getValue())) {
362  $tpl->setCurrentBlock("prop_number_propval");
363  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
364  $tpl->parseCurrentBlock();
365  }
366  $tpl->setCurrentBlock("prop_number");
367 
368  $tpl->setVariable("POST_VAR", $this->getPostVar());
369  $tpl->setVariable("ID", $this->getFieldId());
370  $tpl->setVariable("SIZE", $this->getSize());
371  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
372  if (strlen($this->getSuffix())) {
373  $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
374  }
375  if ($this->getDisabled()) {
376  $tpl->setVariable(
377  "DISABLED",
378  " disabled=\"disabled\""
379  );
380  }
381 
382  /*
383  $tpl->setVariable("JS_DECIMALS_ALLOWED", (int)$this->areDecimalsAllowed());
384  */
385 
386  // constraints
387  if ($this->areDecimalsAllowed() && $this->getDecimals() > 0) {
388  $constraints = $lng->txt("form_format") . ": ###." . str_repeat("#", $this->getDecimals());
389  $delim = ", ";
390  }
391  if ($this->getMinValue() !== false && $this->minvalue_visible) {
392  $constraints .= $delim . $lng->txt("form_min_value") . ": " . (($this->minvalueShouldBeGreater()) ? "&gt; " : "") . $this->getMinValue();
393  $delim = ", ";
394  }
395  if ($this->getMaxValue() !== false && $this->maxvalue_visible) {
396  $constraints .= $delim . $lng->txt("form_max_value") . ": " . (($this->maxvalueShouldBeLess()) ? "&lt; " : "") . $this->getMaxValue();
397  $delim = ", ";
398  }
399  if ($constraints != "") {
400  $tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
401  }
402 
403  if ($this->getRequired()) {
404  $tpl->setVariable("REQUIRED", "required=\"required\"");
405  }
406 
407  $tpl->parseCurrentBlock();
408 
409  return $tpl->get();
410  }
411 
417  public function getPostValueForComparison()
418  {
420  if ($value != "") {
421  return (int) $value;
422  }
423  }
424 }
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.
global $DIC
Definition: saml.php:7
$tpl
Definition: ilias.php:10
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.
special template class to simplify handling of ITX/PEAR
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.
font size
Definition: langcheck.php:162
setMaxLength($a_maxlength)
Set Max Length.
getPostValueForComparison()
parse post value to make it comparable
render()
Insert property html.
This class represents a property that may include a sub form.
setValueByArray($a_values)
Set value by array.
$_POST["username"]
$html
Definition: example_001.php:87
getDecimals()
Get Decimal Places.
setMaxvalueShouldBeLess($a_bool)
Set maxvalueShouldBeLess.
maxvalueShouldBeLess()
Get maxvalueShouldBeLess.