33 protected $outputs = NULL;
52 function __construct ($label, $description, $ldapName, $required = FALSE, $choices = [], $defaultValue =
"", $outputs = NULL, $acl =
"")
54 parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
63 function setChoices (array $choices, array $outputs = NULL)
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'));
72 if ($this->isTemplate() && !in_array(
'%askme%', $choices)) {
73 $choices[] =
'%askme%';
74 if (is_array($outputs)) {
75 $outputs[] =
'%askme%';
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];
83 $this->defaultValue =
'';
86 if (is_array($outputs)) {
89 if (!in_array($this->value, $this->choices)) {
92 if (!in_array($this->postValue, $this->choices)) {
94 $this->postValue = $this->defaultValue;
105 $values = array_values($values);
107 foreach ($this->choices as $choice) {
108 $this->outputs[$choice] = $values[$i++];
116 return $this->choices;
123 return $this->outputs;
126 function setHiddenChoices (array $choices)
128 $this->hiddenChoices = $choices;
131 function setRequired (
bool $bool)
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[
'']);
140 } elseif (!$this->isRequired() && !in_array(
'', $this->choices, TRUE)) {
141 $this->choices[] =
'';
142 if (($this->outputs !== NULL) && !isset($this->outputs[
''])) {
143 $this->outputs[
''] = _(
'None');
148 function displayValue ($value):
string 150 if ($this->outputs !== NULL) {
151 if (isset($this->outputs[$value])) {
152 return $this->outputs[$value];
154 trigger_error(
"No display value set for '$value' in ".$this->getLabel());
164 $error = parent::check();
165 if (!empty($error)) {
168 if (!$this->disabled && !in_array($this->value, $this->choices)) {
177 function renderFormInput ():
string 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)) {
186 if ($this->outputs !== NULL) {
189 foreach ($smartyChoices as $choice) {
190 $smartyOutputs[] = $this->outputs[$choice];
193 $smartyOutputs = $smartyChoices;
196 if (!empty($currentValue) && !in_array($currentValue, $smartyChoices)) {
197 $smartyChoices[] = $currentValue;
198 $smartyOutputs[] = $currentValue;
201 $key = array_search(
'', $smartyOutputs, TRUE);
202 if ($key !== FALSE) {
203 $smartyOutputs[$key] =
' ';
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" ';
212 if ($this->size > 1) {
213 $display .=
'size="'.$this->size.
'" ';
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).
'"';
222 if ($this->isSubAttribute) {
223 $display .=
'class="subattribute" ';
226 $display .=
'{html_options values=$'.$id.
'_choices output=$'.$id.
'_outputs selected=$'.$id.
'_selected}';
227 $display .=
'</select>';
231 function serializeAttribute (array &$attributes,
bool $form = TRUE)
233 if (!$form || $this->visible) {
234 parent::serializeAttribute($attributes, $form);
236 if ($this->outputs !== NULL) {
237 $tmpOutputs = array_values($this->outputs);
239 $tmpOutputs = $this->choices;
241 $attributes[$this->getLdapName()][
'choices'] = array_combine($this->choices, $tmpOutputs);
251 function setParent (&$plugin)
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%';
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.
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.