FusionDirectory
class_SelectAttribute.inc
Go to the documentation of this file.
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2012-2020 FusionDirectory
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
31 {
32  protected $choices;
33  protected $outputs = NULL;
34  protected $size = 1;
35 
39  protected $hiddenChoices = [];
40 
52  function __construct ($label, $description, $ldapName, $required = FALSE, $choices = [], $defaultValue = "", $outputs = NULL, $acl = "")
53  {
54  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
55  $this->setChoices($choices, $outputs);
56  }
57 
63  function setChoices (array $choices, array $outputs = NULL)
64  {
65  $this->outputs = NULL;
66  if (!$this->isRequired() && !in_array('', $choices, TRUE)) {
67  array_unshift($choices, '');
68  if (is_array($outputs)) {
69  array_unshift($outputs, _('None'));
70  }
71  }
72  if ($this->isTemplate() && !in_array('%askme%', $choices)) {
73  $choices[] = '%askme%';
74  if (is_array($outputs)) {
75  $outputs[] = '%askme%';
76  }
77  }
78  $this->choices = $choices;
79  if (!in_array($this->defaultValue, $this->choices, TRUE)) {
80  if (isset($this->choices[0])) {
81  $this->defaultValue = $this->choices[0];
82  } else {
83  $this->defaultValue = '';
84  }
85  }
86  if (is_array($outputs)) {
87  $this->setDisplayChoices($outputs);
88  }
89  if (!in_array($this->value, $this->choices)) {
90  $this->resetToDefault();
91  }
92  if (!in_array($this->postValue, $this->choices)) {
93  /* We may be called between readPostValue and applyPostValue */
94  $this->postValue = $this->defaultValue;
95  }
96  }
97 
102  function setDisplayChoices (array $values)
103  {
104  $this->outputs = [];
105  $values = array_values($values);
106  $i = 0;
107  foreach ($this->choices as $choice) {
108  $this->outputs[$choice] = $values[$i++];
109  }
110  }
111 
114  function getChoices ()
115  {
116  return $this->choices;
117  }
118 
121  function getDisplayChoices ()
122  {
123  return $this->outputs;
124  }
125 
126  function setHiddenChoices (array $choices)
127  {
128  $this->hiddenChoices = $choices;
129  }
130 
131  function setRequired (bool $bool)
132  {
133  parent::setRequired($bool);
134  $key = array_search("", $this->choices, TRUE);
135  if ($this->isRequired() && ($key !== FALSE)) {
136  unset($this->choices[$key]);
137  if ($this->outputs !== NULL) {
138  unset($this->outputs['']);
139  }
140  } elseif (!$this->isRequired() && !in_array('', $this->choices, TRUE)) {
141  $this->choices[] = '';
142  if (($this->outputs !== NULL) && !isset($this->outputs[''])) {
143  $this->outputs[''] = _('None');
144  }
145  }
146  }
147 
148  function displayValue ($value): string
149  {
150  if ($this->outputs !== NULL) {
151  if (isset($this->outputs[$value])) {
152  return $this->outputs[$value];
153  } else {
154  trigger_error("No display value set for '$value' in ".$this->getLabel());
155  return $value;
156  }
157  } else {
158  return $value;
159  }
160  }
161 
162  function check ()
163  {
164  $error = parent::check();
165  if (!empty($error)) {
166  return $error;
167  } else {
168  if (!$this->disabled && !in_array($this->value, $this->choices)) {
169  return new SimplePluginCheckError(
170  $this,
171  SimplePluginCheckError::invalidValue(sprintf(_('"%s" is not in the list of possible choices'), $this->value))
172  );
173  }
174  }
175  }
176 
177  function renderFormInput (): string
178  {
179  $smarty = get_smarty();
180  $id = $this->getHtmlId();
181  $smartyChoices = array_values(array_diff($this->choices, $this->hiddenChoices));
182  $currentValue = $this->getValue();
183  if (in_array($currentValue, $this->hiddenChoices)) {
184  $currentValue = '';
185  }
186  if ($this->outputs !== NULL) {
187  /* Make sure choices and outputs are in the same order */
188  $smartyOutputs = [];
189  foreach ($smartyChoices as $choice) {
190  $smartyOutputs[] = $this->outputs[$choice];
191  }
192  } else {
193  $smartyOutputs = $smartyChoices;
194  }
195 
196  if (!empty($currentValue) && !in_array($currentValue, $smartyChoices)) {
197  $smartyChoices[] = $currentValue;
198  $smartyOutputs[] = $currentValue;
199  }
200 
201  $key = array_search('', $smartyOutputs, TRUE);
202  if ($key !== FALSE) {
203  $smartyOutputs[$key] = '&nbsp;';
204  }
205  $smarty->assign($id.'_choices', $smartyChoices);
206  $smarty->assign($id.'_outputs', $smartyOutputs);
207  $smarty->assign($id.'_selected', $currentValue);
208  $display = '<select name="'.$id.'" id="'.$id.'" ';
209  if ($this->disabled || (count($this->choices) == 0)) {
210  $display .= 'disabled="disabled" ';
211  }
212  if ($this->size > 1) {
213  $display .= 'size="'.$this->size.'" ';
214  }
215  if ($this->submitForm) {
216  $js = 'document.mainform.submit();';
217  $display .= 'onChange="javascript:'.htmlescape($js).'"';
218  } elseif (!empty($this->managedAttributes)) {
219  $js = $this->managedAttributesJS();
220  $display .= 'onChange="javascript:'.htmlescape($js).'"';
221  }
222  if ($this->isSubAttribute) {
223  $display .= 'class="subattribute" ';
224  }
225  $display .= '>';
226  $display .= '{html_options values=$'.$id.'_choices output=$'.$id.'_outputs selected=$'.$id.'_selected}';
227  $display .= '</select>';
228  return $this->renderAcl($display);
229  }
230 
231  function serializeAttribute (array &$attributes, bool $form = TRUE)
232  {
233  if (!$form || $this->visible) {
234  parent::serializeAttribute($attributes, $form);
235 
236  if ($this->outputs !== NULL) {
237  $tmpOutputs = array_values($this->outputs);
238  } else {
239  $tmpOutputs = $this->choices;
240  }
241  $attributes[$this->getLdapName()]['choices'] = array_combine($this->choices, $tmpOutputs);
242  }
243  }
244 
246  function setSize ($size)
247  {
248  $this->size = $size;
249  }
250 
251  function setParent (&$plugin)
252  {
253  parent::setParent($plugin);
254  if ($this->isTemplate() && !in_array('%askme%', $this->choices)) {
255  $this->choices[] = '%askme%';
256  if ($this->outputs !== NULL) {
257  $this->outputs['%askme%'] = '%askme%';
258  }
259  }
260  }
261 }
This class allow to handle easily a Select LDAP attribute with a set of choices.
getChoices()
Get the choices.
$hiddenChoices
Valid values which should be hidden from rendered input.
setDisplayChoices(array $values)
Set the display options of the select attribute.
getDisplayChoices()
Get the displayed choices (keys are choices)
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
setSize($size)
Set the size of the HTML input tag, useful to display several options on the screen instead of just o...
static invalidValue(string $error)
Format error message for invalid value.
Error returned by check method of SimplePlugin.
renderAcl(string $display)
Add ACL information around display.
resetToDefault()
Reset this attribute to its default value.
This class allow to handle easily any kind of LDAP attribute.
__construct($label, $description, $ldapName, $required=FALSE, $choices=[], $defaultValue="", $outputs=NULL, $acl="")
The constructor of SelectAttribute.
setChoices(array $choices, array $outputs=NULL)
Set the options of the select attribute.