FusionDirectory
 All Data Structures Files Functions Variables
class_IntAttribute.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
24 class IntAttribute extends Attribute
25 {
26  protected $min;
27  protected $max;
28  protected $step = 1;
29  protected $example;
30 
42  function __construct ($label, $description, $ldapName, $required, $min, $max, $defaultValue = "", $acl = "")
43  {
44  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
45  $this->min = ($min === FALSE ? FALSE : $this->inputValue($min));
46  $this->max = ($max === FALSE ? FALSE : $this->inputValue($max));
47  $this->example = "";
48 
49  if (($min !== FALSE) && ($max !== FALSE)) {
50  $this->example = sprintf(_("An integer between %d and %d"), $min, $max);
51  } elseif ($min !== FALSE) {
52  $this->example = sprintf(_("An integer larger than %d"), $min);
53  } elseif ($max !== FALSE) {
54  $this->example = sprintf(_("An integer smaller than %d"), $max);
55  }
56  }
57 
58  function setStep ($step)
59  {
60  $this->step = $step;
61  }
62 
63  function inputValue ($value)
64  {
65  if (!$this->isRequired() && empty($value) && !is_numeric($value)) {
66  // value is "" or array()
67  return "";
68  }
69  if (is_object($this->plugin) && $this->plugin->is_template) {
70  return $value;
71  } else {
72  return intval($value);
73  }
74  }
75 
76  function check ()
77  {
78  $error = parent::check();
79  if (!empty($error)) {
80  return $error;
81  } else {
82  if (!is_numeric($this->value) && (!empty($this->value) || $this->isRequired())) {
83  return msgPool::invalid($this->getLabel(), $this->value, "/./", $this->example);
84  }
85  if ((($this->min !== FALSE) && ($this->value < $this->min))
86  || (($this->max !== FALSE) && ($this->value > $this->max))) {
87  return msgPool::invalid($this->getLabel(), $this->value, "/./", $this->example);
88  }
89  }
90  }
91 
92  function renderFormInput ()
93  {
94  $id = $this->getHtmlId();
95  $attributes = array(
96  'value' => '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}'
97  );
98  if ($this->min !== FALSE) {
99  $attributes['min'] = $this->min;
100  }
101  if ($this->max !== FALSE) {
102  $attributes['max'] = $this->max;
103  }
104  if ($this->step !== FALSE) {
105  $attributes['step'] = $this->step;
106  }
107  if (!empty($this->managedAttributes)) {
108  $js = $this->managedAttributesJS();
109  $attributes['onChange'] = 'javascript:'.htmlentities($js, ENT_COMPAT, 'UTF-8');
110  }
111  $display = $this->renderInputField('number', $id, $attributes);
112  return $this->renderAcl($display);
113  }
114 
115  function renderTemplateInput ()
116  {
117  $id = $this->getHtmlId();
118  $display = $this->renderInputField(
119  'text', $id,
120  array(
121  'value' => '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}'
122  )
123  );
124  return $this->renderAcl($display);
125  }
126 }
127 
132 {
133 
148  function __construct ($label, $description, $ldapName, $required, $min, $max, $defaultValue = 0.0, $acl = "")
149  {
150  parent::__construct($label, $description, $ldapName, $required, $min, $max, $defaultValue, $acl);
151 
152  $this->step = 0.01;
153 
154  $this->example = "";
155  if (($min !== FALSE) && ($max !== FALSE)) {
156  $this->example = sprintf(_("A float between %f and %f"), $min, $max);
157  } elseif ($min !== FALSE) {
158  $this->example = sprintf(_("A float larger than %f"), $min);
159  } elseif ($max !== FALSE) {
160  $this->example = sprintf(_("A float smaller than %f"), $max);
161  }
162  }
163 
164  function inputValue ($value)
165  {
166  if (!$this->isRequired() && empty($value) && !is_numeric($value)) {
167  // value is "" or array()
168  return "";
169  }
170  if (is_object($this->plugin) && $this->plugin->is_template) {
171  return $value;
172  } else {
173  return floatval($value);
174  }
175  }
176 }
renderAcl($display)
Add ACL information around display.
This class allow to handle easily an Float LDAP attribute.
This class allow to handle easily any kind of LDAP attribute.
__construct($label, $description, $ldapName, $required, $min, $max, $defaultValue="", $acl="")
The constructor of IntAttribute.
__construct($label, $description, $ldapName, $required, $min, $max, $defaultValue=0.0, $acl="")
The constructor of FloatAttribute.
static invalid($name, $data="", $regex="", $example="")
Display error about invalid characters.
This class allow to handle easily an Integer LDAP attribute.