FusionDirectory
class_BooleanAttribute.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  public $trueValue;
27  public $falseValue;
28  protected $templatable = TRUE;
29 
41  function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = FALSE, $acl = "", $trueValue = "TRUE", $falseValue = "FALSE")
42  {
43  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
44  $this->trueValue = $trueValue;
45  $this->falseValue = $falseValue;
46  }
47 
48  function setTemplatable ($bool)
49  {
50  $this->templatable = $bool;
51  }
52 
53  protected function isTemplatable ()
54  {
55  /* Allow to set to %askme% if we are not (de)activating other fields */
56  return (!$this->submitForm && empty($this->managedAttributes) && $this->templatable);
57  }
58 
59  function inputValue ($value)
60  {
61  if ($this->isTemplate() && $this->isTemplatable() && ($value === '%askme%')) {
62  return $value;
63  }
64  return ($value == $this->trueValue);
65  }
66 
67  function loadPostValue ()
68  {
69  if ($this->isVisible()) {
70  if ($this->isTemplate() && $this->isTemplatable()) {
71  if (!isset($_POST[$this->getHtmlId()])) {
72  $this->setPostValue(FALSE);
73  } elseif ($_POST[$this->getHtmlId()] === '%askme%') {
74  $this->setPostValue('%askme%');
75  } else {
76  $this->setPostValue($_POST[$this->getHtmlId()] == 'TRUE');
77  }
78  } else {
79  $this->setPostValue(isset($_POST[$this->getHtmlId()]));
80  }
81  }
82  }
83 
84  function computeLdapValue ()
85  {
86  if ($this->isTemplate() && $this->isTemplatable() && ($this->value === '%askme%')) {
87  return $this->value;
88  } else {
89  return ($this->value ? $this->trueValue : $this->falseValue);
90  }
91  }
92 
93  function displayValue ($value): string
94  {
95  if ($this->isTemplate() && $this->isTemplatable() && ($value === '%askme%')) {
96  return $value;
97  } else {
98  return ($value ? _('yes') : _('no'));
99  }
100  }
101 
102  function renderFormInput (): string
103  {
104  $id = $this->getHtmlId();
105  $attributes = ($this->value ? ['checked' => 'checked'] : []);
106  if ($this->submitForm) {
107  $js = 'document.mainform.submit();';
108  $attributes['onChange'] = 'javascript:'.$js;
109  } elseif (!empty($this->managedAttributes)) {
110  $js = $this->managedAttributesJS();
111  $attributes['onChange'] = 'javascript:'.$js;
112  }
113  $display = $this->renderInputField('checkbox', $id, $attributes);
114  return $this->renderAcl($display);
115  }
116 
117  function renderTemplateInput (): string
118  {
119  if ($this->isTemplatable()) {
120  $id = $this->getHtmlId();
121  if ($this->getValue() === '%askme%') {
122  $selected = '%askme%';
123  } elseif ($this->getValue()) {
124  $selected = 'TRUE';
125  } else {
126  $selected = 'FALSE';
127  }
128  $display = '<select name="'.$id.'" id="'.$id.'" ';
129  if ($this->disabled) {
130  $display .= 'disabled="disabled" ';
131  }
132  $display .= '>';
133  $display .= '{html_options values=array("FALSE","TRUE","%askme%") output=array("FALSE","TRUE","%askme%") selected="'.$selected.'"}';
134  $display .= '</select>';
135  return $this->renderAcl($display);
136  } else {
137  return $this->renderFormInput();
138  }
139  }
140 
141  protected function managedAttributesJS (): string
142  {
143  $js = '';
144  $id = $this->getHtmlId();
145  foreach ($this->managedAttributes as $array) {
146  foreach ($array as $value => $attributes) {
147  if (isset($this->managedAttributesMultipleValues[$value])) {
148  trigger_error('Multiple values are not available for boolean attributes');
149  } else {
150  $js .= 'disableAttributes = (document.getElementById('.json_encode($id).').checked == '.($value ? 'true' : 'false').');'."\n";
151  }
152  foreach ($attributes as $attribute) {
153  foreach ($this->plugin->attributesAccess[$attribute]->htmlIds() as $htmlId) {
154  $js .= 'if (document.getElementById('.json_encode($htmlId).')) { document.getElementById('.json_encode($htmlId).').disabled = disableAttributes; }'."\n";
155  }
156  }
157  }
158  }
159  return $js;
160  }
161 
166  function deserializeValue ($value)
167  {
168  if ($this->disabled) {
169  return new SimplePluginError(
170  $this,
171  htmlescape(sprintf(_('Attribute %s is disabled, its value could not be set'), $this->getLdapName()))
172  );
173  }
174  if ($value === $this->trueValue) {
175  $this->setValue(TRUE);
176  } elseif ($value === $this->falseValue) {
177  $this->setValue(FALSE);
178  } elseif (is_bool($value)) {
179  $this->setValue($value);
180  } elseif ($value === 1) {
181  $this->setValue(TRUE);
182  } elseif ($value === 0) {
183  $this->setValue(FALSE);
184  } else {
185  return new SimplePluginError(
186  $this,
187  htmlescape(sprintf(_('"%s" is not a valid value, should be "%s" or "%s"'), $value, $this->getLdapName(), $this->trueValue, $this->falseValue))
188  );
189  }
190 
191  /* No error */
192  return '';
193  }
194 
195  function serializeAttribute (array &$attributes, bool $form = TRUE)
196  {
197  if (!$form || $this->visible) {
198  parent::serializeAttribute($attributes, $form);
199  $attributes[$this->getLdapName()]['choices'] = [
200  $this->trueValue => 'True',
201  $this->falseValue => 'False',
202  ];
203  }
204  }
205 }
206 
211 {
212  private $objectclasses;
213 
214 
225  function __construct ($label, $description, $ldapName, $required, $objectclasses, $defaultValue = FALSE, $acl = "")
226  {
227  if (is_array($objectclasses)) {
228  $this->objectclasses = $objectclasses;
229  } else {
230  $this->objectclasses = [$objectclasses];
231  }
232  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
233  $this->setInLdap(FALSE);
234  }
235 
236  function loadValue (array $attrs)
237  {
238  if (isset($attrs['objectClass'])) {
239  $missing_oc = array_udiff($this->objectclasses, $attrs['objectClass'], 'strcasecmp');
240  $this->setValue(empty($missing_oc));
241  } else {
242  $this->resetToDefault();
243  }
244  $this->initialValue = $this->value;
245  }
246 
247  function fillLdapValue (array &$attrs)
248  {
249  if ($this->getValue()) {
250  $attrs['objectClass'] = array_merge_unique($this->objectclasses, $attrs['objectClass']);
251  } else {
252  $attrs['objectClass'] = array_remove_entries($this->objectclasses, $attrs['objectClass']);
253  }
254  }
255 }
htmlescape(string $str)
Escape string for HTML output.
Definition: php_setup.inc:32
fillLdapValue(array &$attrs)
Fill LDAP value in the attrs array.
loadValue(array $attrs)
If in LDAP, loads this attribute value from the attrs array.
Error returned by any method of SimplePlugin.
deserializeValue($value)
Apply value from RPC requests.
This class allow to handle easily a Boolean LDAP attribute.
renderAcl(string $display)
Add ACL information around display.
array_merge_unique(array $ar1, array $ar2)
Definition: functions.inc:275
resetToDefault()
Reset this attribute to its default value.
array_remove_entries(array $needles, array $haystack)
Definition: functions.inc:239
__construct($label, $description, $ldapName, $required, $objectclasses, $defaultValue=FALSE, $acl="")
The constructor of ObjectClassBooleanAttribute.
This class allow to handle easily any kind of LDAP attribute.
This class allow to handle easily a Boolean LDAP attribute that triggers a set of objectclasses...
__construct($label, $description, $ldapName, $required=FALSE, $defaultValue=FALSE, $acl="", $trueValue="TRUE", $falseValue="FALSE")
The constructor of BooleanAttribute.