Commit e2ed8f86 authored by Côme Chilliet's avatar Côme Chilliet

🚜 fix(attributes) Split dialogAttributes into files

This file was crowded

issue #6083
parent 3405bb14
<?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[$i] = $attrs[$this->display_attr][0];
}
}
}
function foreignKeyUpdate ($oldvalue, $newvalue, array $source)
{
foreach ($this->value as $key => &$value) {
if (($source['FIELD'] == 'dn') && ($source['MODE'] == 'move')) {
if ($newvalue === NULL) {
if (preg_match('/'.preg_quote($oldvalue, '/').'$/', $value)) {
$this->removeValue($key);
}
} else {
$value = preg_replace('/'.preg_quote($oldvalue, '/').'$/', $newvalue, $value, -1, $count);
if ($count > 0) {
/* Update display */
$this->fillDisplayValue($key);
}
}
} elseif ($value == $oldvalue) {
if ($newvalue === NULL) {
$this->removeValue($key);
} elseif ($source['MODE'] == 'copy') {
$this->value[] = $newvalue;
} elseif ($source['MODE'] == 'move') {
$value = $newvalue;
/* Update display */
$this->fillDisplayValue($key);
}
}
}
unset($value);
}
}
<?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 for selection using a selectManagement class
*/
class GenericSelectManagementDialog extends GenericDialog
{
protected $dialogClass = 'selectManagement';
function __construct ($simplePlugin, $attribute)
{
$this->attribute = $attribute;
$this->dialog = new $this->dialogClass(...$this->attribute->getSelectManagementParameters());
}
function handle_finish ()
{
$result = $this->dialog->detectPostActions();
if (isset($result['targets'])) {
if ($this->dialog instanceof selectManagement) {
$headpage = $this->dialog->listing;
} else {
$headpage = $this->dialog->getHeadpage();
}
foreach ($result['targets'] as $dn) {
$entry = $headpage->getEntry($dn);
$this->attribute->addValue($dn, $entry);
}
}
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 GenericDialog using a simplePlugin based class as dialog
*/
class GenericSimplePluginDialog extends GenericDialog
{
protected $initialDialogValue = NULL;
function __construct ($simplePlugin, $attribute, $value = [])
{
$this->attribute = $attribute;
$this->dialog = new $this->dialogClass('new');
$base = $simplePlugin->dn;
if ($base == 'new') {
$base = $simplePlugin->base;
}
$this->dialog->base = $base;
$this->dialog->set_acl_category($simplePlugin->acl_category);
if (!empty($value)) {
$this->initialDialogValue = $value;
foreach ($this->dialog->attributes as $attr) {
if (isset($value[$attr])) {
$this->dialog->$attr = $value[$attr];
}
}
}
}
function buttons