31 protected $readFormat;
32 protected $writeFormat;
33 protected $linearRendering = FALSE;
45 function __construct (
string $description,
string $ldapName, array $attributes, $readFormat, $writeFormat,
string $acl =
'',
string $label = NULL)
47 if ($label === NULL) {
50 parent::__construct($label, $description, $ldapName, FALSE,
'', $acl);
51 $this->readFormat = $readFormat;
52 $this->writeFormat = $writeFormat;
53 $this->setAttributes($attributes);
56 function setAttributes (array $attributes)
58 $this->attributes = $attributes;
59 foreach ($this->attributes as &$attribute) {
60 $attribute->setAcl($this->getAcl());
61 $attribute->setParent($this->plugin);
66 function setIsSubAttribute (
bool $bool)
68 parent::setIsSubAttribute($bool);
69 foreach ($this->attributes as &$attribute) {
70 $attribute->setIsSubAttribute($this->isSubAttribute);
75 function setAcl (
string $acl)
78 foreach ($this->attributes as &$attribute) {
79 $attribute->setAcl($this->getAcl());
84 function setParent (&$plugin)
86 parent::setParent($plugin);
87 foreach ($this->attributes as &$attribute) {
88 $attribute->setParent($plugin);
93 function setManagedAttributes (array $dontcare)
95 trigger_error(
'method setManagedAttributes is not supported for CompositeAttribute');
98 function setLinearRendering (
bool $bool)
100 $this->linearRendering = $bool;
103 function loadValue (array $attrs)
105 parent::loadValue($attrs);
106 foreach ($this->attributes as $attribute) {
107 $attribute->setInitialValue($attribute->getValue());
111 function readValues (
string $value): array
113 $res = preg_match($this->readFormat, $value, $m);
115 $m = array_slice($m, 1);
117 foreach (array_keys($this->attributes) as $name) {
118 if (isset($m[$name])) {
119 $values[] = $m[$name];
125 } elseif ($res === FALSE) {
126 trigger_error(
'Error in preg_match : '.preg_last_error());
127 } elseif ($value !==
"") {
128 trigger_error(
'String passed "'.$value.
'"to Composite did not match format "'.$this->readFormat.
'"');
130 return array_fill(0, count($this->attributes),
'');
133 function writeValues (array $values)
135 if ($this->writeFormat === FALSE) {
138 return vsprintf($this->writeFormat, $values);
142 function resetToDefault ()
144 foreach ($this->attributes as &$attribute) {
145 $attribute->resetToDefault();
150 function inputValue ($value)
152 $values = $this->readValues($value);
154 foreach ($this->attributes as &$attribute) {
155 $values[$i] = $attribute->inputValue($values[$i]);
162 function loadPostValue ()
164 foreach ($this->attributes as &$attribute) {
165 $attribute->setDisabled($this->disabled);
166 $attribute->loadPostValue();
171 function applyPostValue ()
173 foreach ($this->attributes as &$attribute) {
174 $attribute->setDisabled($this->disabled);
175 $attribute->applyPostValue();
180 function setValue ($values)
182 if (!is_array($values)) {
183 $values = $this->inputValue($values);
186 foreach ($this->attributes as &$attribute) {
187 $attribute->setValue(current($values));
200 return $a->computeLdapValue();
204 return $this->writeValues($values);
207 function getArrayValue (): array
212 return $a->displayValue($a->getValue());
220 $error = parent::check();
221 if (!empty($error)) {
224 foreach ($this->attributes as &$attribute) {
225 $error = $attribute->check();
226 if (!empty($error)) {
233 function renderAttribute (array &$attributes,
bool $readOnly,
bool $readable,
bool $writable)
235 if ($this->visible) {
236 if ($this->linearRendering) {
237 parent::renderAttribute($attributes, $readOnly, $readable, $writable);
239 foreach ($this->attributes as &$attribute) {
240 $attribute->setDisabled($this->disabled);
241 $attribute->renderAttribute($attributes, $readOnly, $readable, $writable);
248 function getForHtmlId ():
string 251 if (isset($this->attributes[0])) {
252 return $this->attributes[0]->getForHtmlId();
258 function serializeAttribute (array &$attributes,
bool $form = TRUE)
261 if ($this->visible) {
262 foreach ($this->attributes as &$attribute) {
263 $attribute->setDisabled($this->disabled);
264 $attribute->serializeAttribute($attributes, $form);
269 parent::serializeAttribute($attributes, $form);
271 foreach ($this->attributes as &$attribute) {
272 $attribute->setDisabled($this->disabled);
273 $attribute->serializeAttribute($subattributes, $form);
276 $attributes[$this->getLdapName()][
'attributes'] = $subattributes;
277 $attributes[$this->getLdapName()][
'attributes_order'] = array_keys($subattributes);
281 function renderFormInput ():
string 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().
" ";
294 public function htmlIds (): array
297 foreach ($this->attributes as &$attribute) {
298 $ret = array_merge($ret, $attribute->htmlIds());
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.