ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNumberInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 {
33  protected $value;
34  protected $maxlength = 200;
35  protected $size = 40;
36  protected $suffix;
37  protected $minvalue = false;
38  protected $minvalueShouldBeGreater = false;
39  protected $maxvalue = false;
40  protected $maxvalueShouldBeLess = false;
41 
48  function __construct($a_title = "", $a_postvar = "")
49  {
50  parent::__construct($a_title, $a_postvar);
51  }
52 
58  function setSuffix($a_value)
59  {
60  $this->suffix = $a_value;
61  }
62 
68  function getSuffix()
69  {
70  return $this->suffix;
71  }
72 
78  function setValue($a_value)
79  {
80  $this->value = str_replace(',', '.', $a_value);
81  }
82 
88  function getValue()
89  {
90  return $this->value;
91  }
92 
98  function setMaxLength($a_maxlength)
99  {
100  $this->maxlength = $a_maxlength;
101  }
102 
108  function getMaxLength()
109  {
110  return $this->maxlength;
111  }
112 
118  function setMinvalueShouldBeGreater($a_bool)
119  {
120  $this->minvalueShouldBeGreater = $a_bool;
121  }
122 
129  {
131  }
132 
138  function setMaxvalueShouldBeLess($a_bool)
139  {
140  $this->maxvalueShouldBeLess = $a_bool;
141  }
142 
149  {
151  }
152 
158  function setSize($a_size)
159  {
160  $this->size = $a_size;
161  }
162 
168  function setValueByArray($a_values)
169  {
170  $this->setValue($a_values[$this->getPostVar()]);
171  }
172 
178  function getSize()
179  {
180  return $this->size;
181  }
182 
188  function setMinValue($a_minvalue)
189  {
190  $this->minvalue = $a_minvalue;
191  }
192 
198  function getMinValue()
199  {
200  return $this->minvalue;
201  }
202 
208  function setMaxValue($a_maxvalue)
209  {
210  $this->maxvalue = $a_maxvalue;
211  }
212 
218  function getMaxValue()
219  {
220  return $this->maxvalue;
221  }
222 
228  function setDecimals($a_decimals)
229  {
230  $this->decimals = $a_decimals;
231  }
232 
238  function getDecimals()
239  {
240  return $this->decimals;
241  }
242 
248  function checkInput()
249  {
250  global $lng;
251 
252  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
253  if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "")
254  {
255  $this->setAlert($lng->txt("msg_input_is_required"));
256 
257  return false;
258  }
259 
260  if (trim($_POST[$this->getPostVar()]) != "" &&
261  ! is_numeric(str_replace(',', '.', $_POST[$this->getPostVar()])))
262  {
263  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
264 
265  return false;
266  }
267 
268  if ($this->minvalueShouldBeGreater())
269  {
270  if (trim($_POST[$this->getPostVar()]) != "" &&
271  $this->getMinValue() !== false &&
272  $_POST[$this->getPostVar()] <= $this->getMinValue())
273  {
274  $this->setAlert($lng->txt("form_msg_value_too_low"));
275 
276  return false;
277  }
278  }
279  else
280  {
281  if (trim($_POST[$this->getPostVar()]) != "" &&
282  $this->getMinValue() !== false &&
283  $_POST[$this->getPostVar()] < $this->getMinValue())
284  {
285  $this->setAlert($lng->txt("form_msg_value_too_low"));
286 
287  return false;
288  }
289  }
290 
291  if ($this->maxvalueShouldBeLess())
292  {
293  if (trim($_POST[$this->getPostVar()]) != "" &&
294  $this->getMaxValue() !== false &&
295  $_POST[$this->getPostVar()] >= $this->getMaxValue())
296  {
297  $this->setAlert($lng->txt("form_msg_value_too_high"));
298 
299  return false;
300  }
301  }
302  else
303  {
304  if (trim($_POST[$this->getPostVar()]) != "" &&
305  $this->getMaxValue() !== false &&
306  $_POST[$this->getPostVar()] > $this->getMaxValue())
307  {
308  $this->setAlert($lng->txt("form_msg_value_too_high"));
309 
310  return false;
311  }
312  }
313 
314  return $this->checkSubItemsInput();
315  }
316 
322  function insert(&$a_tpl)
323  {
324  global $lng;
325 
326  if (strlen($this->getValue()))
327  {
328  $a_tpl->setCurrentBlock("prop_number_propval");
329  $a_tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
330  $a_tpl->parseCurrentBlock();
331  }
332  $a_tpl->setCurrentBlock("prop_number");
333 
334  $a_tpl->setVariable("POST_VAR", $this->getPostVar());
335  $a_tpl->setVariable("ID", $this->getFieldId());
336  $a_tpl->setVariable("SIZE", $this->getSize());
337  $a_tpl->setVariable("MAXLENGTH", $this->getMaxLength());
338  if (strlen($this->getSuffix())) $a_tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
339  if ($this->getDisabled())
340  {
341  $a_tpl->setVariable("DISABLED",
342  " disabled=\"disabled\"");
343  }
344 
345  // constraints
346  if ($this->getDecimals() > 0)
347  {
348  $constraints = $lng->txt("form_format").": ###.".str_repeat("#", $this->getDecimals());
349  $delim = ", ";
350  }
351  if ($this->getMinValue() !== false)
352  {
353  $constraints.= $delim.$lng->txt("form_min_value").": ".(($this->minvalueShouldBeGreater()) ? "&gt; " : "").$this->getMinValue();
354  $delim = ", ";
355  }
356  if ($this->getMaxValue() !== false)
357  {
358  $constraints.= $delim.$lng->txt("form_max_value").": ".(($this->maxvalueShouldBeLess()) ? "&lt; " : "").$this->getMaxValue();
359  $delim = ", ";
360  }
361  if ($constraints != "")
362  {
363  $a_tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
364  }
365 
366  $a_tpl->parseCurrentBlock();
367  }
368 }