FusionDirectory
 All Data Structures Files Functions Variables
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 
41  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = "", $acl = "", $regexp = "", $example = NULL)
42  {
43  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
44  $this->pattern = $regexp;
45  $this->example = ($example === NULL ? $defaultValue : $example);
46  }
47 
48  function setExample ($example)
49  {
50  $this->example = $example;
51  }
52 
53  function setPattern ($pattern)
54  {
55  $this->pattern = $pattern;
56  }
57 
58  function renderFormInput ()
59  {
60  $id = $this->getHtmlId();
61  $attributes = array(
62  'value' => '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}'
63  );
64  if (!empty($this->managedAttributes)) {
65  $js = $this->managedAttributesJS();
66  $attributes['onChange'] = 'javascript:'.htmlentities($js, ENT_COMPAT, 'UTF-8');
67  }
68  if ($this->autocomplete !== NULL) {
69  $attributes['autocomplete'] = ($this->autocomplete ? 'on' : 'off' );
70  }
71  $display = $this->renderInputField('text', $id, $attributes);
72  return $this->renderAcl($display);
73  }
74 
75  function fixPostValue ($value)
76  {
77  /* Replace CRLF by LF, to avoid non-ASCII chars in multiline values (mainly useful for textarea) */
78  return str_replace(array("\r\n", "\r"), "\n", $value);
79  }
80 
81  function check ()
82  {
83  $error = parent::check();
84  if (!empty($error)) {
85  return $error;
86  } else {
87  if ($this->value !== "") {
88  return $this->validate();
89  }
90  }
91  }
92 
93  function validate ()
94  {
95  if (($this->pattern !== "") && !preg_match($this->pattern, $this->value)) {
96  return msgPool::invalid($this->getLabel(), $this->value, $this->pattern, htmlentities($this->example));
97  }
98  }
99 
100  function setAutocomplete ($bool)
101  {
102  $this->autocomplete = $bool;
103  }
104 
105  function getAutocomplete ()
106  {
107  return $this->autocomplete;
108  }
109 
110  function checkValue($value)
111  {
112  if (!is_scalar($value) && (!is_object($value) || !method_exists($value, '__toString' ))) {
113  throw new InvalidValueException(sprintf(_('StringAttribute "%s" was set to a non-compatible value'), $this->getLabel()));
114  }
115  }
116 
117  function setValue ($value)
118  {
119  if ($this->trim) {
120  return parent::setValue(trim($value));
121  } else {
122  return parent::setValue($value);
123  }
124  }
125 }
126 
131 {
132  protected $trim = TRUE;
133 }
134 
139 {
140  function renderFormInput ()
141  {
142  $id = $this->getHtmlId();
143  $display = '<textarea name="'.$id.'" id="'.$id.'"'.
144  ($this->disabled ? ' disabled="disabled"' : '').'>'.
145  '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}</textarea>';
146  return $this->renderAcl($display);
147  }
148 }
149 
154 {
155  protected $autocomplete = FALSE;
156 
157  function renderFormInput ()
158  {
159  $id = $this->getHtmlId();
160  $display = $this->renderInputField(
161  'password', $id,
162  array(
163  'value' => '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}',
164  'autocomplete' => ($this->autocomplete ? 'on' : 'off'),
165  )
166  );
167  if ($this->autocomplete === FALSE) {
168  $display = '{literal}<input autocomplete="off" value="foolautocompleteworkaround" type="text" style="display:none;"/>{/literal}'.$display;
169  }
170  return $this->renderAcl($display);
171  }
172 
173  function renderTemplateInput ()
174  {
175  return parent::renderFormInput();
176  }
177 }
renderAcl($display)
Add ACL information around display.
Exception class which can be thrown if an attribute is set to a value with a non-compatible type...
This class allow to handle easily any kind of LDAP attribute.
static invalid($name, $data="", $regex="", $example="")
Display error about invalid characters.
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.
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 a String LDAP attribute.