FusionDirectory
class_IntAttribute.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2019 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 
25 {
26  protected $min;
27  protected $max;
28  protected $step = 1;
29 
41  function __construct ($label, $description, $ldapName, $required, $min, $max, $defaultValue = "", $acl = "")
42  {
43  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
44  $this->min = ($min === FALSE ? FALSE : $this->inputValue($min));
45  $this->max = ($max === FALSE ? FALSE : $this->inputValue($max));
46  }
47 
48  public function getExample ()
49  {
50  if (($this->min !== FALSE) && ($this->max !== FALSE)) {
51  return sprintf(_('An integer between %d and %d'), $this->min, $this->max);
52  } elseif ($this->min !== FALSE) {
53  return sprintf(_('An integer larger than %d'), $this->min);
54  } elseif ($this->max !== FALSE) {
55  return sprintf(_('An integer smaller than %d'), $this->max);
56  }
57  }
58 
59  function setStep ($step)
60  {
61  $this->step = $step;
62  }
63 
64  function inputValue ($value)
65  {
66  if (!$this->isRequired() && empty($value) && !is_numeric($value)) {
67  // value is "" or array()
68  return "";
69  }
70  if ($this->isTemplate()) {
71  return $value;
72  } else {
73  return intval($value);
74  }
75  }
76 
77  function check ()
78  {
79  $error = parent::check();
80  if (!empty($error)) {
81  return $error;
82  } elseif ($this->value !== '') {
83  if (!is_numeric($this->value)) {
84  return new SimplePluginCheckError(
85  $this,
86  SimplePluginCheckError::invalidValue(sprintf(_('"%s" is not an number'), $this->getValue()))
87  );
88  }
89  if (($this->min !== FALSE) && ($this->value < $this->min)) {
90  return new SimplePluginCheckError(
91  $this,
92  SimplePluginCheckError::invalidValue(sprintf(_('%s is smaller than %s'), $this->getValue(), $this->min))
93  );
94  }
95  if (($this->max !== FALSE) && ($this->value > $this->max)) {
96  return new SimplePluginCheckError(
97  $this,
98  SimplePluginCheckError::invalidValue(sprintf(_('%s is larger than %s'), $this->getValue(), $this->max))
99  );
100  }
101  }
102  }
103 
104  function renderFormInput (): string
105  {
106  $id = $this->getHtmlId();
107  $attributes = [
108  'value' => $this->getValue()
109  ];
110  if ($this->min !== FALSE) {
111  $attributes['min'] = $this->min;
112  }
113  if ($this->max !== FALSE) {
114  $attributes['max'] = $this->max;
115  }
116  if ($this->step !== FALSE) {
117  $attributes['step'] = $this->step;
118  }
119  if (!empty($this->managedAttributes)) {
120  $js = $this->managedAttributesJS();
121  $attributes['onChange'] = 'javascript:'.$js;
122  }
123  if ($this->isSubAttribute) {
124  $attributes['class'] = 'subattribute';
125  } elseif ($this->isRequired()) {
126  $attributes['required'] = 'required';
127  }
128  $display = $this->renderInputField('number', $id, $attributes);
129  return $this->renderAcl($display);
130  }
131 
132  function renderTemplateInput (): string
133  {
134  $id = $this->getHtmlId();
135  $attributes = [
136  'value' => $this->getValue()
137  ];
138  if ($this->isSubAttribute) {
139  $attributes['class'] = 'subattribute';
140  }
141  $display = $this->renderInputField('text', $id, $attributes);
142  return $this->renderAcl($display);
143  }
144 }
145 
150 {
151 
166  function __construct ($label, $description, $ldapName, $required, $min, $max, $defaultValue = 0.0, $acl = "")
167  {
168  parent::__construct($label, $description, $ldapName, $required, $min, $max, $defaultValue, $acl);
169 
170  $this->step = 0.01;
171  }
172 
173  public function getExample ()
174  {
175  if (($this->min !== FALSE) && ($this->max !== FALSE)) {
176  return sprintf(_('A float between %f and %f'), $this->min, $this->max);
177  } elseif ($this->min !== FALSE) {
178  return sprintf(_('A float larger than %f'), $this->min);
179  } elseif ($this->max !== FALSE) {
180  return sprintf(_('A float smaller than %f'), $this->max);
181  }
182  }
183 
184  function inputValue ($value)
185  {
186  if (!$this->isRequired() && empty($value) && !is_numeric($value)) {
187  // value is "" or array()
188  return "";
189  }
190  if ($this->isTemplate()) {
191  return $value;
192  } else {
193  return floatval($value);
194  }
195  }
196 }
This class allow to handle easily an Float 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 invalidValue(string $error)
Format error message for invalid value.
Error returned by check method of SimplePlugin.
renderAcl(string $display)
Add ACL information around display.
This class allow to handle easily an Integer LDAP attribute.
This class allow to handle easily any kind of LDAP attribute.