FusionDirectory
class_ObjectSelectAttribute.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 $objectTypes;
33  protected $storeAttr;
34  protected $displayAttr;
35  protected $objectFilter = '';
36 
37  function __construct (string $label, string $description, string $ldapName, bool $required, array $objectTypes, string $storeAttr = 'dn', string $displayAttr = 'nameAttr', $objectFilter = '', $acl = '')
38  {
39  parent::__construct($label, $description, $ldapName, $required, [], '', NULL, $acl);
40  $this->objectTypes = $objectTypes;
41  $this->storeAttr = $storeAttr;
42  $this->displayAttr = $displayAttr;
43  $this->objectFilter = $objectFilter;
44  }
45 
46  function setParent (&$plugin)
47  {
48  parent::setParent($plugin);
49  if (is_object($this->plugin)) {
50  $this->updateChoices();
51  }
52  }
53 
54  function updateChoices ()
55  {
56  $choices = [];
57  $outputs = [];
58  foreach ($this->objectTypes as $objectType) {
59  try {
60  $infos = objects::infos($objectType);
61  $realStoreAttr = $this->storeAttr;
62  if (in_array($this->storeAttr, ['nameAttr','mainAttr'])) {
63  $realStoreAttr = $infos[$this->storeAttr];
64  }
65  $realDisplayAttr = $this->displayAttr;
66  if (in_array($this->displayAttr, ['nameAttr','mainAttr'])) {
67  $realDisplayAttr = $infos[$this->displayAttr];
68  }
69  $attributes = [
70  $realStoreAttr => (($realStoreAttr == 'dn') ? 'raw' : '1'),
71  $realDisplayAttr => (($realDisplayAttr == 'dn') ? 'raw' : '1'),
72  ];
73  $objects = objects::ls($objectType, $attributes, NULL, $this->objectFilter);
74  foreach (array_column($objects, $realStoreAttr) as $choice) {
75  $choices[] = $choice;
76  }
77  foreach (array_column($objects, $realDisplayAttr) as $output) {
78  $outputs[] = $output;
79  }
80  } catch (NonExistingObjectTypeException $e) {
81  continue;
82  }
83  }
84  $this->setChoices($choices, $outputs);
85  }
86 }
This class allow to handle easily a Select LDAP attribute with a set of choices.
This class allows to handle a select attribute which allow to choose an object.
static ls($types, $attrs=NULL, string $ou=NULL, string $filter='', bool $checkAcl=FALSE, string $scope='subtree', bool $templateSearch=FALSE, bool $sizeLimit=FALSE)
Get list of object of objectTypes from $types in $ou.
setChoices(array $choices, array $outputs=NULL)
Set the options of the select attribute.