ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
5include_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 function __construct($a_title = "", $a_postvar = "")
36 {
37 parent::__construct($a_title, $a_postvar);
38 }
39
45 function setSuffix($a_value)
46 {
47 $this->suffix = $a_value;
48 }
49
55 function getSuffix()
56 {
57 return $this->suffix;
58 }
59
65 function setValue($a_value)
66 {
67 $this->value = str_replace(',', '.', $a_value);
68
69 // empty strings are allowed
70 if($this->value != "")
71 {
72 // integer
73 if(!$this->areDecimalsAllowed())
74 {
75 $this->value = round($this->value);
76 }
77 // float
78 else if($this->getDecimals() > 0)
79 {
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 function getValue()
95 {
96 return $this->value;
97 }
98
104 function setMaxLength($a_maxlength)
105 {
106 $this->maxlength = $a_maxlength;
107 }
108
114 function getMaxLength()
115 {
116 return $this->maxlength;
117 }
118
125 {
126 $this->minvalueShouldBeGreater = $a_bool;
127 }
128
135 {
137 }
138
144 function setMaxvalueShouldBeLess($a_bool)
145 {
146 $this->maxvalueShouldBeLess = $a_bool;
147 }
148
155 {
157 }
158
164 function setSize($a_size)
165 {
166 $this->size = $a_size;
167 }
168
174 function setValueByArray($a_values)
175 {
176 $this->setValue($a_values[$this->getPostVar()]);
177 }
178
184 function getSize()
185 {
186 return $this->size;
187 }
188
195 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 function getMinValue()
207 {
208 return $this->minvalue;
209 }
210
217 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 function getMaxValue()
229 {
230 return $this->maxvalue;
231 }
232
238 function setDecimals($a_decimals)
239 {
240 $this->decimals = (int)$a_decimals;
241 if($this->decimals)
242 {
243 $this->allowDecimals(true);
244 }
245 }
246
252 function getDecimals()
253 {
254 return $this->decimals;
255 }
256
262 function allowDecimals($a_value)
263 {
264 $this->allow_decimals = (bool)$a_value;
265 }
266
273 {
275 }
276
282 function checkInput()
283 {
284 global $lng;
285
287 if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "")
288 {
289 $this->setAlert($lng->txt("msg_input_is_required"));
290 return false;
291 }
292
293 if (trim($_POST[$this->getPostVar()]) != "" &&
294 ! is_numeric(str_replace(',', '.', $_POST[$this->getPostVar()])))
295 {
296 $this->minvalue_visible = true;
297 $this->maxvalue_visible = true;
298 $this->setAlert($lng->txt("form_msg_numeric_value_required"));
299 return false;
300 }
301
302 if ($this->minvalueShouldBeGreater())
303 {
304 if (trim($_POST[$this->getPostVar()]) != "" &&
305 $this->getMinValue() !== false &&
306 $_POST[$this->getPostVar()] <= $this->getMinValue())
307 {
308 $this->minvalue_visible = true;
309 $this->setAlert($lng->txt("form_msg_value_too_low"));
310 return false;
311 }
312 }
313 else
314 {
315 if (trim($_POST[$this->getPostVar()]) != "" &&
316 $this->getMinValue() !== false &&
317 $_POST[$this->getPostVar()] < $this->getMinValue())
318 {
319 $this->minvalue_visible = true;
320 $this->setAlert($lng->txt("form_msg_value_too_low"));
321 return false;
322 }
323 }
324
325 if ($this->maxvalueShouldBeLess())
326 {
327 if (trim($_POST[$this->getPostVar()]) != "" &&
328 $this->getMaxValue() !== false &&
329 $_POST[$this->getPostVar()] >= $this->getMaxValue())
330 {
331 $this->maxvalue_visible = true;
332 $this->setAlert($lng->txt("form_msg_value_too_high"));
333 return false;
334 }
335 }
336 else
337 {
338 if (trim($_POST[$this->getPostVar()]) != "" &&
339 $this->getMaxValue() !== false &&
340 $_POST[$this->getPostVar()] > $this->getMaxValue())
341 {
342 $this->maxvalue_visible = true;
343 $this->setAlert($lng->txt("form_msg_value_too_high"));
344 return false;
345 }
346 }
347
348 return $this->checkSubItemsInput();
349 }
350
356 function insert(&$a_tpl)
357 {
358 $html = $this->render();
359
360 $a_tpl->setCurrentBlock("prop_generic");
361 $a_tpl->setVariable("PROP_GENERIC", $html);
362 $a_tpl->parseCurrentBlock();
363 }
364
368 function render()
369 {
370 global $lng;
371
372 $tpl = new ilTemplate("tpl.prop_number.html", true, true, "Services/Form");
373
374 if (strlen($this->getValue()))
375 {
376 $tpl->setCurrentBlock("prop_number_propval");
377 $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
378 $tpl->parseCurrentBlock();
379 }
380 $tpl->setCurrentBlock("prop_number");
381
382 $tpl->setVariable("POST_VAR", $this->getPostVar());
383 $tpl->setVariable("ID", $this->getFieldId());
384 $tpl->setVariable("SIZE", $this->getSize());
385 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
386 if (strlen($this->getSuffix())) $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
387 if ($this->getDisabled())
388 {
389 $tpl->setVariable("DISABLED",
390 " disabled=\"disabled\"");
391 }
392
393 /*
394 $tpl->setVariable("JS_DECIMALS_ALLOWED", (int)$this->areDecimalsAllowed());
395 */
396
397 // constraints
398 if ($this->areDecimalsAllowed() && $this->getDecimals() > 0)
399 {
400 $constraints = $lng->txt("form_format").": ###.".str_repeat("#", $this->getDecimals());
401 $delim = ", ";
402 }
403 if ($this->getMinValue() !== false && $this->minvalue_visible)
404 {
405 $constraints.= $delim.$lng->txt("form_min_value").": ".(($this->minvalueShouldBeGreater()) ? "&gt; " : "").$this->getMinValue();
406 $delim = ", ";
407 }
408 if ($this->getMaxValue() !== false && $this->maxvalue_visible)
409 {
410 $constraints.= $delim.$lng->txt("form_max_value").": ".(($this->maxvalueShouldBeLess()) ? "&lt; " : "").$this->getMaxValue();
411 $delim = ", ";
412 }
413 if ($constraints != "")
414 {
415 $tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
416 }
417
418 $tpl->parseCurrentBlock();
419
420 return $tpl->get();
421 }
422
429 {
431 if($value != "")
432 {
433 return (int)$value;
434 }
435 }
436}
437?>
global $tpl
Definition: ilias.php:8
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
getFieldId()
Get Post Variable.
This class represents a number property in a property form.
minvalueShouldBeGreater()
Get minvalueShouldBeGreater.
setMaxValue($a_maxvalue, $a_display_always=false)
Set Maximum Value.
setSuffix($a_value)
Set suffix.
checkInput()
Check input, strip slashes etc.
__construct($a_title="", $a_postvar="")
Constructor.
setValue($a_value)
Set Value.
setDecimals($a_decimals)
Set Decimal Places.
getMaxLength()
Get Max Length.
getMinValue()
Get Minimum Value.
setMaxLength($a_maxlength)
Set Max Length.
allowDecimals($a_value)
Toggle Decimals.
insert(&$a_tpl)
Insert property html.
getDecimals()
Get Decimal Places.
setMinvalueShouldBeGreater($a_bool)
Set minvalueShouldBeGreater.
setValueByArray($a_values)
Set value by array.
setMinValue($a_minvalue, $a_display_always=false)
Set Minimum Value.
getPostValueForComparison()
parse post value to make it comparable
setMaxvalueShouldBeLess($a_bool)
Set maxvalueShouldBeLess.
maxvalueShouldBeLess()
Get maxvalueShouldBeLess.
render()
Insert property html.
setSize($a_size)
Set Size.
getMaxValue()
Get Maximum Value.
This class represents a property that may include a sub form.
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
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
$_POST['username']
Definition: cron.php:12
$html
Definition: example_001.php:87
global $lng
Definition: privfeed.php:40