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