Commit 3ae050ad authored by Côme Chilliet's avatar Côme Chilliet

Merge branch '6083-improve-objectsattribute' into '1.4-dev'

Resolve "Improve ObjectsAttribute"

See merge request fusiondirectory/fd!758
parents 3405bb14 406234de
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \file class_ObjectSelectAttribute.inc
* Source code for ObjectSelectAttribute
*/
/*! \brief This class allows to handle a select attribute which allow to choose an object
*
*/
class ObjectSelectAttribute extends SelectAttribute
{
protected $objectType;
protected $storeAttr;
protected $displayAttr;
protected $objectFilter = '';
function __construct (string $label, string $description, string $ldapName, bool $required, array $objectTypes, string $storeAttr = 'dn', string $displayAttr = 'nameAttr', $objectFilter = '', $acl = '')
{
parent::__construct($label, $description, $ldapName, $required, [], '', NULL, $acl);
$this->objectTypes = $objectTypes;
$this->storeAttr = $storeAttr;
$this->displayAttr = $displayAttr;
$this->objectFilter = $objectFilter;
}
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
$this->updateChoices();
}
}
function updateChoices ()
{
$choices = [];
$outputs = [];
foreach ($this->objectTypes as $objectType) {
try {
$infos = objects::infos($this->objectType);
$realStoreAttr = $this->storeAttr;
if (in_array($this->storeAttr, ['nameAttr','mainAttr'])) {
$realStoreAttr = $infos[$this->storeAttr];
}
$realDisplayAttr = $this->displayAttr;
if (in_array($this->displayAttr, ['nameAttr','mainAttr'])) {
$realDisplayAttr = $infos[$this->displayAttr];
}
$attributes = [
$realStoreAttr => (($realStoreAttr == 'dn') ? 'raw' : '1'),
$realDisplayAttr => (($realDisplayAttr == 'dn') ? 'raw' : '1'),
];
$objects = objects::ls($objectType, $attributes, NULL, $this->objectFilter);
array_push($choices, ...array_column($objects, $realStoreAttr));
array_push($outputs, ...array_column($objects, $realDisplayAttr));
} catch (NonExistingObjectTypeException $e) {
continue;
}
}
$this->setChoices($choices, $outputs);
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2016 FusionDirectory
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -20,7 +21,7 @@
/*!
* \file class_SelectAttribute.inc
* Source code for the Select attributes
* Source code for SelectAttribute
*/
/*! \brief This class allow to handle easily a Select LDAP attribute with a set of choices
......@@ -247,35 +248,3 @@ class SelectAttribute extends Attribute
}
}
}
/*! \brief This class allows to handle a select attribute which allow to choose an object
*
*/
class ObjectSelectAttribute extends SelectAttribute
{
protected $objectType;
protected $objectAttrs = NULL;
protected $objectFilter = '';
function __construct ($label, $description, $ldapName, $required, $objectType, $objectAttrs = NULL, $objectFilter = '', $acl = "")
{
parent::__construct($label, $description, $ldapName, $required, [], "", NULL, $acl);
$this->objectType = $objectType;
$this->objectAttrs = $objectAttrs;
$this->objectFilter = $objectFilter;
}
function setParent (simplePlugin &$plugin)
{
parent::setParent($plugin);
if (is_object($this->plugin)) {
$this->updateChoices();
}
}
function updateChoices ()
{
$objects = objects::ls($this->objectType, $this->objectAttrs, NULL, $this->objectFilter);
$this->setChoices(array_keys($objects), array_values($objects));
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Attribute showing a button
*/
class ButtonAttribute extends Attribute
{
protected $buttonText = NULL;
protected $action;
function __construct ($label, $description, $ldapName, $buttonText, $action = NULL, $defaultValue = "", $acl = "")
{
parent::__construct($label, $description, $ldapName, FALSE, $defaultValue, $acl);
$this->buttonText = $buttonText;
if ($action === NULL) {
$action = 'handle_'.$ldapName;
}
$this->action = $action;
}
function renderFormInput (): string
{
$id = $this->getHtmlId();
$display = $this->renderInputField(
'submit', $id,
['value' => '{literal}'.htmlentities($this->buttonText, ENT_COMPAT, 'UTF-8').'{/literal}']
);
return $this->renderAcl($display);
}
function loadPostValue ()
{
$this->postValue = FALSE;
if ($this->isVisible() && isset($_POST[$this->getHtmlId()])) {
$this->setPostValue(TRUE);
}
}
function applyPostValue ()
{
if (!$this->disabled && $this->isVisible() && $this->postValue) {
$func = $this->action;
$this->plugin->$func();
}
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*! \brief This class allows to handle an attribute with a popup for selection
*
* It looks like a SetAttribute, but clicking "Add" will open a dialog that allow to select one or more items.
* It stores their dn as values, but displays the cn.
*
*/
abstract class DialogAttribute extends SetAttribute
{
protected $dialogClass = NULL;
/*! \brief The constructor of DialogAttribute
*
* \param string $label The label to show for this attribute
* \param string $description A more detailed description for the attribute
* \param string $ldapName The name of the attribute in the LDAP (If it's not in the ldap, still provide a unique name)
* \param boolean $required Is this attribute mandatory or not
* \param array $defaultValue The default value for this attribute
* \param string $acl The name of the acl for this attribute if he does not use its own. (Leave empty if he should use its own like most attributes do)
*/
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = "")
{
Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
$this->attribute = FALSE;
}
function addPostValue ($value)
{
$this->addValue($value, NULL);
}
function delPostValue ($key)
{
$this->removeValue($key);
}
abstract function addValue (string $dn, $attrs = NULL);
function searchAndRemove ($value)
{
$row = array_search($value, $this->value);
if ($row !== FALSE) {
$this->removeValue($row);
}
}
protected function removeValue ($row)
{
unset($this->value[$row]);
}
abstract function getFilterBlackList ();
function getFilterWhiteList ()
{
return [];
}
function loadPostValue ()
{
parent::loadPostValue();
if ($this->isVisible()) {
$id = $this->getHtmlId();
if (isset($_POST['add'.$id.'_dialog'])) {
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
} elseif (isset($_POST['add'.$id]) && isset($_POST[$id]) && $this->isTemplate()) {
$this->addPostValue($_POST[$id]);
}
}
}
function applyPostValue ()
{
}
function renderButtons ()
{
$id = $this->getHtmlId();
$buttons = '';
$dialogButtonValue = '{msgPool type=addButton}';
if ($this->isTemplate()) {
$buttons .= $this->renderInputField(
'text', $id,
['value' => $this->editingValue, 'class' => 'subattribute']
);
$buttons .= $this->renderInputField(
'submit', 'add'.$id,
[
'value' => '{msgPool type=addButton}',
'formnovalidate' => 'formnovalidate',
'class' => 'subattribute',
]
);
$dialogButtonValue = _('Add (dialog)');
}
$buttons .= $this->renderInputField(
'submit', 'add'.$id.'_dialog',
[
'class' => 'dialog subattribute',
'value' => $dialogButtonValue,
'formnovalidate' => 'formnovalidate',
]
);
$buttons .= $this->renderInputField(
'submit', 'del'.$id,
[
'value' => '{msgPool type=delButton}',
'formnovalidate' => 'formnovalidate',
'class' => 'subattribute',
]
);
return $buttons;
}
public function htmlIds (): array
{
$id = $this->getHtmlId();
$ids = ['add'.$id.'_dialog','del'.$id,'row'.$id];
if ($this->isTemplate()) {
$ids[] = $id;
$ids[] = 'add'.$id;
}
return $ids;
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Attribute showing a button which triggers a dialog
*/
class DialogButtonAttribute extends ButtonAttribute
{
protected $dialogClass = NULL;
function __construct ($label, $description, $ldapName, $buttonText, $dialogClass, $defaultValue = '', $acl = '')
{
parent::__construct($label, $description, $ldapName, $buttonText, NULL, $defaultValue, $acl);
$this->dialogClass = $dialogClass;
}
function applyPostValue ()
{
if (!$this->disabled && $this->isVisible() && $this->postValue) {
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
}
}
function getFilterBlackList ()
{
return [];
}
function getFilterWhiteList ()
{
return [];
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief An OrderedArrayAttribute which uses a dialog to create/edit values
*/
abstract class DialogOrderedArrayAttribute extends OrderedArrayAttribute
{
protected $dialogClass;
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $acl = "")
{
Attribute::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
$this->edit_enabled = TRUE;
$this->attribute = FALSE;
}
function getFilterWhiteList ()
{
return [];
}
function loadPostValue ()
{
if ($this->isVisible()) {
parent::loadPostValue();
parent::applyPostValue();
if (isset($_POST['add'.$this->getHtmlId().'_dialog'])) {
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this));
}
}
}
protected function handleEdit ($key)
{
$this->editingValue = $this->value[$key];
$this->delPostValue($key);
$this->plugin->openDialog(new $this->dialogClass($this->plugin, $this, $this->editingValue));
}
function applyPostValue ()
{
}
function addValue (string $dn, $attrs = NULL)
{
if ($attrs !== NULL) {
$this->value[] = $attrs;
$this->reIndexValues();
}
}
function renderButtons ()
{
$id = $this->getHtmlId();
return $this->renderInputField(
'submit', 'add'.$id.'_dialog',
[
'class' => 'dialog subattribute',
'value' => '{msgPool type=addButton}',
'formnovalidate' => 'formnovalidate'
]
);
}
public function htmlIds (): array
{
$ids = parent::htmlIds();
$ids[0] .= '_dialog';
return $ids;
}
protected function getAttributeArrayValue ($key, $value)
{
/* Convert text value to displayable array value */
die('Pure virtual method');
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*! \brief Generic dialog base class
*/
class GenericDialog
{
protected $dialogClass = "";
protected $dialog;
protected $attribute;
protected $post_cancel = 'add_cancel';
protected $post_finish = 'add_finish';
function __construct ($simplePlugin, $attribute)
{
$this->attribute = $attribute;
$this->dialog = new $this->dialogClass();
}
function execute ()
{
if (isset($_POST[$this->post_cancel])) {
return $this->handle_cancel();
}
if (isset($_POST[$this->post_finish]) || isset($_GET[$this->post_finish])) {
return $this->handle_finish();
}
return $this->dialog_execute();
}
function dialog_execute ()
{
return $this->dialog->execute();
}
function handle_finish ()
{
trigger_error('empty function');
return FALSE;
}
function handle_cancel ()
{
return FALSE;
}
}
<?php
/*
This code is part of FusionDirectory (http://www.fusiondirectory.org/)
Copyright (C) 2012-2020 FusionDirectory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*!
* \brief Base class for DialogAttribute using a GenericDialog derived dialog
*/
class GenericDialogAttribute extends DialogAttribute
{
protected $displays = [];
protected $store_attr = 'dn';
protected $display_attr = 'cn';
function __construct ($label, $description, $ldapName, $required = FALSE, $defaultValue = [], $store_attr = 'dn', $display_attr = 'cn', $acl = '')
{
$this->store_attr = $store_attr;
$this->display_attr = $display_attr;
parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
}
function getFilterBlackList ()
{
return [$this->store_attr => $this->getValue()];
}
function addValue (string $dn, $attrs = NULL)
{
if (
($this->isTemplate() && ($attrs === NULL)) ||
($this->store_attr == 'dn')
) {
$value = $dn;
} else {
$value = $attrs[$this->store_attr][0];
}
if (!in_array($value, $this->value)) {
end($this->value);
$new_key = key($this->value) + 1;
$this->value[$new_key] = $value;
$this->fillDisplayValueFrom($new_key, $attrs);
}
}
protected function removeValue ($row)
{
unset($this->displays[$row]);
unset($this->value[$row]);
}
protected function sortValues ()
{
asort($this->value);
}
function getDisplayValues ()
{
foreach (array_keys($this->value) as $i) {
if (!isset($this->displays[$i])) {
$this->fillDisplayValue($i);
}
}
return $this->displays;
}
function setDisplayValues (array $array)
{
$this->displays = $array;
}
protected function fillDisplayValue ($i)
{
global $config;
$ldap = $config->get_ldap_link();
$value = $this->value[$i];
if ($this->store_attr == 'dn') {
$ldap->cat($value, $this->ldapAttributesToGet());
} else {
$ldap->cd($config->current['BASE']);
$ldap->search('('.$this->store_attr.'='.ldap_escape_f($value).')', $this->ldapAttributesToGet());
}
$attrs = $ldap->fetch(TRUE);
if (empty($attrs) && $this->isTemplate()) {
$this->fillDisplayValueFrom($i, NULL);
} else {
$this->fillDisplayValueFrom($i, $attrs);
}
}
protected function ldapAttributesToGet ()
{
return [$this->display_attr];
}
protected function fillDisplayValueFrom ($i, $attrs)
{
if ($this->isTemplate() && ($attrs === NULL)) {
$this->displays[$i] = $this->value[$i];
} elseif (!isset($attrs[$this->display_attr])) {
unset($this->value[$i]);
} else {
if ($this->display_attr == 'dn') {
$this->displays[$i] = $attrs['dn'];
} else {
$this->displays