FusionDirectory
class_StringAttribute.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 
25 {
26  protected $pattern;
27  protected $example;
28  protected $autocomplete = NULL;
29  protected $trim = FALSE;
30  protected $inputType = 'text';
31  protected $html5pattern = NULL;
32 
44  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "", $regexp = "", $example = NULL)
45  {
46  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
47  $this->setPattern($regexp);
48  $this->example = ($example === NULL ? $defaultValue : $example);
49  }
50 
51  function setExample ($example)
52  {
53  $this->example = $example;
54  }
55 
56  public function getExample ()
57  {
58  return $this->example;
59  }
60 
61  function setPattern ($pattern)
62  {
63  $this->pattern = $pattern;
64  if (preg_match('/^(.)\^(.*)\$\1$/', $pattern, $m)) {
65  /* Only convert anchored patterns
66  * First and last characters are the same means no modifiers are in use
67  */
68  $this->html5pattern = $m[2];
69  } else {
70  $this->html5pattern = NULL;
71  }
72  }
73 
74  function renderFormInput (): string
75  {
76  $id = $this->getHtmlId();
77  $attributes = [
78  'value' => $this->getValue()
79  ];
80  if (!empty($this->managedAttributes)) {
81  $js = $this->managedAttributesJS();
82  $attributes['onChange'] = 'javascript:'.$js;
83  }
84  if ($this->autocomplete !== NULL) {
85  $attributes['autocomplete'] = $this->autocomplete;
86  }
87  if ($this->html5pattern !== NULL) {
88  $attributes['pattern'] = $this->html5pattern;
89  }
90  if ($this->isSubAttribute) {
91  $attributes['class'] = 'subattribute';
92  } elseif ($this->isRequired()) {
93  $attributes['required'] = 'required';
94  }
95  $display = $this->renderInputField($this->inputType, $id, $attributes);
96  return $this->renderAcl($display);
97  }
98 
99  function renderTemplateInput (): string
100  {
101  $id = $this->getHtmlId();
102  $attributes = [
103  'value' => $this->getValue()
104  ];
105  if ($this->autocomplete !== NULL) {
106  $attributes['autocomplete'] = $this->autocomplete;
107  }
108  if ($this->isSubAttribute) {
109  $attributes['class'] = 'subattribute';
110  }
111  $display = $this->renderInputField('text', $id, $attributes);
112  return $this->renderAcl($display);
113  }
114 
115  function fixPostValue ($value)
116  {
117  /* Replace CRLF by LF, to avoid non-ASCII chars in multiline values (mainly useful for textarea) */
118  return str_replace(["\r\n", "\r"], "\n", $value);
119  }
120 
121  function check ()
122  {
123  $error = parent::check();
124  if (!empty($error)) {
125  return $error;
126  } else {
127  if ($this->value !== "") {
128  return $this->validate();
129  }
130  }
131  }
132 
133  function validate ()
134  {
135  if (($this->pattern !== '') && !preg_match($this->pattern, $this->value)) {
136  return new SimplePluginCheckError(
137  $this,
138  SimplePluginCheckError::invalidValue(sprintf('"%s"', $this->getValue()))
139  );
140  }
141  }
142 
143  function setAutocomplete ($autocomplete)
144  {
145  if (is_bool($autocomplete)) {
146  $this->autocomplete = ($autocomplete ? 'on' : 'off');
147  } else {
148  $this->autocomplete = $autocomplete;
149  }
150  }
151 
152  function getAutocomplete ()
153  {
154  return $this->autocomplete;
155  }
156 
157  function checkValue ($value)
158  {
159  if (!is_scalar($value) && (!is_object($value) || !method_exists($value, '__toString'))) {
160  throw new InvalidValueException(sprintf(_('StringAttribute "%s" was set to a non-compatible value'), $this->getLabel()));
161  }
162  }
163 
164  function setValue ($value)
165  {
166  if ($this->trim) {
167  return parent::setValue(trim($value));
168  } else {
169  return parent::setValue($value);
170  }
171  }
172 }
173 
178 {
179  protected $trim = TRUE;
180 }
181 
186 {
187  function renderFormInput (): string
188  {
189  $id = $this->getHtmlId();
190  $display = '<textarea name="'.$id.'" id="'.$id.'"'.
191  ($this->disabled ? ' disabled="disabled"' : '').
192  ($this->isSubAttribute ? ' class="subattribute"' : ($this->isRequired() ? ' required="required"' : '')).
193  '>'.
194  '{literal}'.htmlescape($this->getValue()).'{/literal}</textarea>';
195  return $this->renderAcl($display);
196  }
197 
198  function renderTemplateInput (): string
199  {
200  $id = $this->getHtmlId();
201  $display = '<textarea name="'.$id.'" id="'.$id.'"'.
202  ($this->disabled ? ' disabled="disabled"' : '').
203  ($this->isSubAttribute ? ' class="subattribute"' : '').
204  '>'.
205  '{literal}'.htmlescape($this->getValue()).'{/literal}</textarea>';
206  return $this->renderAcl($display);
207  }
208 }
209 
214 {
215  protected $autocomplete = 'new-password';
216  protected $inputType = 'password';
217 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
Exception class which can be thrown if an attribute is set to a value with a non-compatible type...
static invalidValue(string $error)
Format error message for invalid value.
Error returned by check method of SimplePlugin.
This class allow to handle easily a String LDAP attribute that appears as a text area.
This class allow to handle easily a String LDAP attribute that contains a password.
renderAcl(string $display)
Add ACL information around display.
This class allow to handle easily a String LDAP attribute that appears as a text area.
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue="", $acl="", $regexp="", $example=NULL)
The constructor of StringAttribute.
This class allow to handle easily any kind of LDAP attribute.
This class allow to handle easily a String LDAP attribute.