FusionDirectory
 All Data Structures Files Functions Variables
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  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 
30 {
31  protected $choices;
32  protected $outputs = NULL;
33  protected $size = 1;
34 
46  function __construct ($label, $description, $ldapName, $required = FALSE, $choices = array(), $defaultValue = "", $outputs = NULL, $acl = "")
47  {
48  if (!in_array($defaultValue, $choices, TRUE) && isset($choices[0])) {
49  $defaultValue = $choices[0];
50  }
51  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
52  $this->setChoices($choices, $outputs);
53  }
54 
60  function setChoices ($choices, $outputs = NULL)
61  {
62  $this->outputs = NULL;
63  if (!$this->isRequired() && !in_array('', $choices, TRUE)) {
64  array_unshift($choices, '');
65  if (is_array($outputs)) {
66  array_unshift($outputs, _('None'));
67  }
68  }
69  $this->choices = $choices;
70  if (!in_array($this->defaultValue, $this->choices, TRUE) && isset($this->choices[0])) {
71  $this->defaultValue = $this->choices[0];
72  }
73  if (is_array($outputs)) {
74  $this->setDisplayChoices($outputs);
75  }
76  if (!in_array($this->value, $this->choices)) {
77  $this->resetToDefault();
78  }
79  }
80 
85  function setDisplayChoices ($values)
86  {
87  $this->outputs = array();
88  $values = array_values($values);
89  $i = 0;
90  foreach ($this->choices as $choice) {
91  $this->outputs[$choice] = $values[$i++];
92  }
93  }
94 
97  function getChoices ()
98  {
99  return $this->choices;
100  }
101 
104  function getDisplayChoices ()
105  {
106  return $this->outputs;
107  }
108 
109  function setRequired ($bool)
110  {
111  parent::setRequired($bool);
112  $key = array_search("", $this->choices, TRUE);
113  if ($this->isRequired() && ($key !== FALSE)) {
114  unset($this->choices[$key]);
115  unset($this->outputs[""]);
116  } elseif (!$this->isRequired() && !in_array("", $this->choices, TRUE)) {
117  $this->choices[] = "";
118  if (!isset($this->output[""])) {
119  $this->output[""] = _("None");
120  }
121  }
122  }
123 
124  function displayValue ($value)
125  {
126  if ($this->outputs !== NULL) {
127  if (isset($this->outputs[$value])) {
128  return $this->outputs[$value];
129  } else {
130  trigger_error("No display value set for '$value' in ".$this->getLabel());
131  return $value;
132  }
133  } else {
134  return $value;
135  }
136  }
137 
138  function check ()
139  {
140  $error = parent::check();
141  if (!empty($error)) {
142  return $error;
143  } else {
144  if (!$this->disabled && !in_array($this->value, $this->choices)) {
145  return sprintf(_('The value "%s" for field "%s" is not in the list of possible choices'), $this->value, $this->getLabel());
146  }
147  }
148  }
149 
150  function renderFormInput ()
151  {
152  $smarty = get_smarty();
153  $id = $this->getHtmlId();
154  $smarty->assign($id."_choices", $this->choices);
155  if ($this->outputs !== NULL) {
156  $outputs = array_values($this->outputs);
157  } else {
158  $outputs = $this->choices;
159  }
160  $key = array_search('', $outputs, TRUE);
161  if ($key !== FALSE) {
162  $outputs[$key] = '&nbsp;';
163  }
164  $smarty->assign($id."_outputs", $outputs);
165  $smarty->assign($id."_selected", $this->getValue());
166  $display = '<select name="'.$id.'" id="'.$id.'" ';
167  if ($this->disabled || (count($this->choices) == 0)) {
168  $display .= 'disabled="disabled" ';
169  }
170  if ($this->size > 1) {
171  $display .= 'size="'.$this->size.'" ';
172  }
173  if ($this->submitForm) {
174  $js = 'document.mainform.submit();';
175  $display .= 'onChange="javascript:'.htmlentities($js, ENT_COMPAT, 'UTF-8').'"';
176  } elseif (!empty($this->managedAttributes)) {
177  $js = $this->managedAttributesJS();
178  $display .= 'onChange="javascript:'.htmlentities($js, ENT_COMPAT, 'UTF-8').'"';
179  }
180  $display .= '>';
181  $display .= '{html_options values=$'.$id.'_choices output=$'.$id.'_outputs selected=$'.$id.'_selected}';
182  $display .= '</select>';
183  return $this->renderAcl($display);
184  }
185 
186  function serializeAttribute(&$attributes, $form = TRUE)
187  {
188  if (!$form || $this->visible) {
189  parent::serializeAttribute($attributes, $form);
190 
191  if ($this->outputs !== NULL) {
192  $outputs = array_values($this->outputs);
193  } else {
194  $outputs = $this->choices;
195  }
196  $attributes[$this->getLdapName()]['choices'] = array_combine($this->choices, $outputs);
197  }
198  }
199 
201  function setSize($size)
202  {
203  $this->size = $size;
204  }
205 }
206 
211 {
212  protected $objectType;
213  protected $objectAttrs = NULL;
214  protected $objectFilter = '';
215 
216  function __construct ($label, $description, $ldapName, $required, $objectType, $objectAttrs = NULL, $objectFilter = '', $acl = "")
217  {
218  parent::__construct($label, $description, $ldapName, $required, array(), "", NULL, $acl);
219  $this->objectType = $objectType;
220  $this->objectAttrs = $objectAttrs;
221  $this->objectFilter = $objectFilter;
222  }
223 
224  function setParent (&$plugin)
225  {
226  parent::setParent($plugin);
227  if (is_object($this->plugin)) {
228  $this->updateChoices();
229  }
230  }
231 
232  function updateChoices()
233  {
234  $objects = objects::ls($this->objectType, $this->objectAttrs, NULL, $this->objectFilter);
235  $this->setChoices(array_keys($objects), array_values($objects));
236  }
237 }
This class allow to handle easily a Select LDAP attribute with a set of choices.
renderAcl($display)
Add ACL information around display.
getChoices()
Get the choices.
This class allows to handle a select attribute which allow to choose an object.
This class allow to handle easily any kind of LDAP attribute.
getDisplayChoices()
Get the displayed choices (keys are choices)
setDisplayChoices($values)
Set the display options of the select attribute.
& get_smarty()
Get global smarty object.
Definition: functions.inc:953
setChoices($choices, $outputs=NULL)
Set the options of the select attribute.
__construct($label, $description, $ldapName, $required=FALSE, $choices=array(), $defaultValue="", $outputs=NULL, $acl="")
The constructor of SelectAttribute.
setSize($size)
Set the size of the HTML input tag, useful to display several options on the screen instead of just o...
static ls($types, $attrs=NULL, $ou=NULL, $filter= '', $checkAcl=FALSE, $scope= 'subtree')
Get list of object of objectTypes from $types in $ou.
resetToDefault()
Reset this attribute to its default value.