FusionDirectory
 All Data Structures Files Functions Variables
class_CompositeAttribute.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 
29 {
30  public $attributes;
31  protected $readFormat;
32  protected $writeFormat;
33  protected $linearRendering = FALSE;
34 
45  function __construct ($description, $ldapName, $attributes, $readFormat, $writeFormat, $acl = "", $label = NULL)
46  {
47  if ($label === NULL) {
48  $label = $ldapName;
49  }
50  parent::__construct($label, $description, $ldapName, FALSE, "", $acl);
51  $this->readFormat = $readFormat;
52  $this->writeFormat = $writeFormat;
53  $this->setAttributes($attributes);
54  }
55 
56  function setAttributes ($attributes)
57  {
58  $this->attributes = $attributes;
59  foreach ($this->attributes as &$attribute) {
60  $attribute->setAcl($this->getAcl());
61  }
62  unset($attribute);
63  }
64 
65  function setAcl ($acl)
66  {
67  parent::setAcl($acl);
68  foreach ($this->attributes as &$attribute) {
69  $attribute->setAcl($this->getAcl());
70  }
71  unset($attribute);
72  }
73 
74  function setParent (&$plugin)
75  {
76  parent::setParent($plugin);
77  foreach ($this->attributes as &$attribute) {
78  $attribute->setParent($plugin);
79  }
80  unset($attribute);
81  }
82 
83  function setManagedAttributes ($dontcare)
84  {
85  trigger_error('method setManagedAttributes is not supported for CompositeAttribute');
86  }
87 
88  function setLinearRendering ($bool)
89  {
90  $this->linearRendering = $bool;
91  }
92 
93  function readValues($value)
94  {
95  $res = preg_match($this->readFormat, $value, $m);
96  if ($res === 1) {
97  $m = array_slice($m, 1);
98  $values = array();
99  foreach (array_keys($this->attributes) as $name) {
100  if (isset($m[$name])) {
101  $values[] = $m[$name];
102  } else {
103  $values[] = '';
104  }
105  }
106  return $values;
107  } elseif ($res === FALSE) {
108  trigger_error('Error in preg_match : '.preg_last_error());
109  } elseif ($value !== "") { /* If an empty value does not match, we don't trigger an error */
110  trigger_error('String passed "'.$value.'"to Composite did not match format "'.$this->readFormat.'"');
111  }
112  return array_fill(0, count($this->attributes), '');
113  }
114 
115  function writeValues($values)
116  {
117  if ($this->writeFormat === FALSE) {
118  return $values;
119  } else {
120  return vsprintf($this->writeFormat, $values);
121  }
122  }
123 
124  function resetToDefault ()
125  {
126  foreach ($this->attributes as &$attribute) {
127  $attribute->resetToDefault();
128  }
129  unset($attribute);
130  }
131 
132  function inputValue ($value)
133  {
134  $values = $this->readValues($value);
135  $i = 0;
136  foreach ($this->attributes as &$attribute) {
137  $values[$i] = $attribute->inputValue($values[$i]);
138  $i++;
139  }
140  unset($attribute);
141  return $values;
142  }
143 
144  function loadPostValue ()
145  {
146  foreach ($this->attributes as &$attribute) {
147  $attribute->setDisabled($this->disabled);
148  $attribute->loadPostValue();
149  }
150  unset($attribute);
151  }
152 
153  function applyPostValue ()
154  {
155  foreach ($this->attributes as &$attribute) {
156  $attribute->setDisabled($this->disabled);
157  $attribute->applyPostValue();
158  }
159  unset($attribute);
160  }
161 
162  function setValue ($values)
163  {
164  if (!is_array($values)) {
165  $values = $this->inputValue($values);
166  }
167  reset($values);
168  foreach ($this->attributes as &$attribute) {
169  $attribute->setValue(current($values));
170  next($values);
171  }
172  unset($attribute);
173  reset($values);
174  }
175 
176  /* We always return the LDAP value as the composite attribute has nothing else */
177  function getValue ()
178  {
179  $values = array_map(
180  function ($a)
181  {
182  return $a->computeLdapValue();
183  },
184  $this->attributes
185  );
186  return $this->writeValues($values);
187  }
188 
189  function getArrayValue ()
190  {
191  $values = array_map(
192  function ($a)
193  {
194  return $a->displayValue($a->getValue());
195  },
196  $this->attributes
197  );
198  return $values;
199  }
200 
201  function check ()
202  {
203  $error = parent::check();
204  if (!empty($error)) {
205  return $error;
206  }
207  foreach ($this->attributes as &$attribute) {
208  $error = $attribute->check();
209  if (!empty($error)) {
210  return $error;
211  }
212  }
213  unset($attribute);
214  }
215 
216  function renderAttribute(&$attributes, $readOnly)
217  {
218  if ($this->visible) {
219  if ($this->linearRendering) {
220  parent::renderAttribute($attributes, $readOnly);
221  } else {
222  foreach ($this->attributes as &$attribute) {
223  $attribute->setDisabled($this->disabled);
224  $attribute->renderAttribute($attributes, $readOnly);
225  }
226  unset($attribute);
227  }
228  }
229  }
230 
231  function getForHtmlId()
232  {
233  // Label (if any) should point to the first attribute
234  if (isset($this->attributes[0])) {
235  return $this->attributes[0]->getForHtmlId();
236  } else {
237  return '';
238  }
239  }
240 
241  function serializeAttribute(&$attributes, $form = TRUE)
242  {
243  if ($form) {
244  if ($this->visible) {
245  foreach ($this->attributes as &$attribute) {
246  $attribute->setDisabled($this->disabled);
247  $attribute->serializeAttribute($attributes, $form);
248  }
249  unset($attribute);
250  }
251  } else {
252  parent::serializeAttribute($attributes, $form);
253  $subattributes = array();
254  foreach ($this->attributes as &$attribute) {
255  $attribute->setDisabled($this->disabled);
256  $attribute->serializeAttribute($subattributes, $form);
257  }
258  unset($attribute);
259  $attributes[$this->getLdapName()]['attributes'] = $subattributes;
260  $attributes[$this->getLdapName()]['attributes_order'] = array_keys($subattributes);
261  }
262  }
263 
264  function deserializeValue($values)
265  {
266  if ($this->visible) {
267  foreach ($this->attributes as &$attribute) {
268  $attribute->setDisabled($this->disabled);
269  $attribute->deserializeValue($values);
270  }
271  unset($attribute);
272  }
273  }
274 
275  function renderFormInput()
276  {
277  $display = "";
278  foreach ($this->attributes as &$attribute) {
279  $attribute->setDisabled($this->disabled);
280  if ($attribute->isVisible()) {
281  $display .= '<label for="'.$attribute->getForHtmlId().'">'.$attribute->getLabel().'</label>'." ".$attribute->renderFormInput()." ";
282  }
283  }
284  unset($attribute);
285  return $display;
286  }
287 
288  public function htmlIds()
289  {
290  $ret = array();
291  foreach ($this->attributes as &$attribute) {
292  $ret = array_merge($ret, $attribute->htmlIds());
293  }
294  unset($attribute);
295  return $ret;
296  }
297 }
This class allow to handle easily any kind of LDAP attribute.
This class allow to handle easily a composite attribute.
__construct($description, $ldapName, $attributes, $readFormat, $writeFormat, $acl="", $label=NULL)
The constructor of CompositeAttribute.