MultiEdit: make it independent of an object type

This commit is contained in:
Thomas Gelf 2016-10-14 18:32:34 +00:00
parent 6a54e00402
commit 23ef9a707c
4 changed files with 130 additions and 68 deletions

View File

@ -6,4 +6,8 @@ use Icinga\Module\Director\Web\Controller\ObjectsController;
class HostsController extends ObjectsController class HostsController extends ObjectsController
{ {
protected $multiEdit = array(
'imports',
'groups'
);
} }

View File

@ -2,8 +2,10 @@
namespace Icinga\Module\Director\Forms; namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\FormLoader;
use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader; use Icinga\Module\Director\Web\Form\IcingaObjectFieldLoader;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use Zend_Form_Element as ZfElement; use Zend_Form_Element as ZfElement;
class IcingaMultiEditForm extends DirectorObjectForm class IcingaMultiEditForm extends DirectorObjectForm
@ -12,6 +14,10 @@ class IcingaMultiEditForm extends DirectorObjectForm
private $elementGroupMap; private $elementGroupMap;
private $relatedForm;
private $propertiesToPick;
public function setObjects($objects) public function setObjects($objects)
{ {
$this->objects = $objects; $this->objects = $objects;
@ -20,17 +26,34 @@ class IcingaMultiEditForm extends DirectorObjectForm
return $this; return $this;
} }
public function pickElementsFrom(QuickForm $form, $properties)
{
$this->relatedForm = $form;
$this->propertiesToPick = $properties;
return $this;
}
public function setup() public function setup()
{ {
$object = $this->object; $object = $this->object;
$this->addImportsElement();
// $this->addDisabledElement();
$loader = new IcingaObjectFieldLoader($object); $loader = new IcingaObjectFieldLoader($object);
$loader->addFieldsToForm($this); $loader->addFieldsToForm($this);
$this->makeVariants($this->getElement('imports')); if ($form = $this->relatedForm) {
// $this->makeVariants($this->getElement('disabled')); $form->setDb($object->getConnection())
->setObject($object)
->prepareElements();
} else {
$this->propertiesToPick = array();
}
foreach ($this->propertiesToPick as $property) {
if ($el = $form->getElement($property)) {
$this->makeVariants($el);
}
}
foreach ($this->getElements() as $el) { foreach ($this->getElements() as $el) {
$name = $el->getName(); $name = $el->getName();
if (substr($name, 0, 4) === 'var_') { if (substr($name, 0, 4) === 'var_') {
@ -41,6 +64,27 @@ class IcingaMultiEditForm extends DirectorObjectForm
$this->setButtons(); $this->setButtons();
} }
public function onSuccess()
{
foreach ($this->getValues() as $key => $value) {
$this->setSubmittedMultiValue($key, $value);
}
$modified = $this->storeModifiedObjects();
if ($modified === 0) {
$msg = $this->translate('No object has been modified');
} elseif ($modified === 1) {
$msg = $this->translate('One object has been modified');
} else {
$msg = sprintf(
$this->translate('%d objects have been modified'),
$modified
);
}
$this->redirectOnSuccess($msg);
}
/** /**
* No default objects behaviour * No default objects behaviour
*/ */
@ -48,9 +92,8 @@ class IcingaMultiEditForm extends DirectorObjectForm
{ {
} }
public function onSuccess() protected function setSubmittedMultiValue($key, $value)
{ {
foreach ($this->getValues() as $key => $value) {
$parts = preg_split('/_/', $key); $parts = preg_split('/_/', $key);
$objectsSum = array_pop($parts); $objectsSum = array_pop($parts);
$valueSum = array_pop($parts); $valueSum = array_pop($parts);
@ -77,6 +120,8 @@ class IcingaMultiEditForm extends DirectorObjectForm
} }
} }
protected function storeModifiedObjects()
{
$modified = 0; $modified = 0;
foreach ($this->objects as $object) { foreach ($this->objects as $object) {
if ($object->hasBeenModified()) { if ($object->hasBeenModified()) {
@ -85,18 +130,7 @@ class IcingaMultiEditForm extends DirectorObjectForm
} }
} }
if ($modified === 0) { return $modified;
$msg = $this->translate('No object has been modified');
} elseif ($modified === 1) {
$msg = $this->translate('One object has been modified');
} else {
$msg = sprintf(
$this->translate('%d objects have been modified'),
$modified
);
}
$this->redirectOnSuccess($msg);
} }
protected function getDisplayGroupForElement(ZfElement $element) protected function getDisplayGroupForElement(ZfElement $element)
@ -107,17 +141,46 @@ class IcingaMultiEditForm extends DirectorObjectForm
$name = $element->getName(); $name = $element->getName();
if (array_key_exists($name, $this->elementGroupMap)) { if (array_key_exists($name, $this->elementGroupMap)) {
return $this->getDisplayGroup($this->elementGroupMap[$name]); $groupName = $this->elementGroupMap[$name];
if ($group = $this->getDisplayGroup($groupName)) {
return $group;
} elseif ($this->relatedForm) {
return $this->stealDisplayGroup($groupName, $this->relatedForm);
}
} else { } else {
return null; return null;
} }
} }
protected function stealDisplayGroup($name, $form)
{
if ($group = $this->relatedForm->getDisplayGroup($name)) {
$group = clone($group);
$group->setElements(array());
$this->_displayGroups[$name] = $group;
$this->_order[$name] = $this->_displayGroups[$name]->getOrder();
$this->_orderUpdated = true;
return $group;
}
return null;
}
protected function resolveDisplayGroups() protected function resolveDisplayGroups()
{ {
$this->elementGroupMap = array(); $this->elementGroupMap = array();
if ($form = $this->relatedForm) {
$this->extractFormDisplayGroups($form, true);
}
foreach ($this->getDisplayGroups() as $group) { $this->extractFormDisplayGroups($this);
}
protected function extractFormDisplayGroups($form, $clone = false)
{
foreach ($form->getDisplayGroups() as $group) {
$groupName = $group->getName(); $groupName = $group->getName();
foreach ($group->getElements() as $name => $e) { foreach ($group->getElements() as $name => $e) {
$this->elementGroupMap[$name] = $groupName; $this->elementGroupMap[$name] = $groupName;
@ -127,10 +190,6 @@ class IcingaMultiEditForm extends DirectorObjectForm
protected function makeVariants(ZfElement $element) protected function makeVariants(ZfElement $element)
{ {
if (! $element) {
return $this;
}
$key = $element->getName(); $key = $element->getName();
$this->removeElement($key); $this->removeElement($key);
$label = $element->getLabel(); $label = $element->getLabel();
@ -190,22 +249,6 @@ class IcingaMultiEditForm extends DirectorObjectForm
return ' (' . count($list) . ')'; return ' (' . count($list) . ')';
} }
protected function enumTemplates()
{
$object = $this->object();
$tpl = $this->db()->enumIcingaTemplates($object->getShortTableName());
if (empty($tpl)) {
return array();
}
if (empty($tpl)) {
return array();
}
$tpl = array_combine($tpl, $tpl);
return $tpl;
}
protected function db() protected function db()
{ {
if ($this->db === null) { if ($this->db === null) {

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Web\Controller; namespace Icinga\Module\Director\Web\Controller;
use Icinga\Exception\NotFoundError;
use Icinga\Data\Filter\Filter; use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Table\IcingaObjectTable; use Icinga\Module\Director\Web\Table\IcingaObjectTable;
@ -12,6 +13,8 @@ abstract class ObjectsController extends ActionController
protected $isApified = true; protected $isApified = true;
protected $multiEdit = array();
protected $globalTypes = array( protected $globalTypes = array(
'ApiUser', 'ApiUser',
'Zone', 'Zone',
@ -196,6 +199,13 @@ abstract class ObjectsController extends ActionController
public function editAction() public function editAction()
{ {
$type = ucfirst($this->getType());
if (empty($this->multiEdit)) {
throw new NotFoundError('Cannot edit multiple "%s" instances', $type);
}
$formName = 'icinga' . $type;
$this->singleTab($this->translate('Multiple objects')); $this->singleTab($this->translate('Multiple objects'));
$filter = Filter::fromQueryString($this->params->toString()); $filter = Filter::fromQueryString($this->params->toString());
$dummy = $this->dummyObject(); $dummy = $this->dummyObject();
@ -209,9 +219,14 @@ abstract class ObjectsController extends ActionController
} }
} }
} }
$this->view->title = sprintf($this->translate('Modify %d objects'), count($objects)); $this->view->title = sprintf(
$this->translate('Modify %d objects'),
count($objects)
);
$this->view->form = $this->loadForm('IcingaMultiEdit') $this->view->form = $this->loadForm('IcingaMultiEdit')
->setObjects($objects) ->setObjects($objects)
->pickElementsFrom($this->loadForm($formName), $this->multiEdit)
->handleRequest(); ->handleRequest();
$this->setViewScript('objects/form'); $this->setViewScript('objects/form');

View File

@ -457,7 +457,7 @@ abstract class DirectorObjectForm extends QuickForm
{ {
$this->object = $object; $this->object = $object;
if ($this->db === null) { if ($this->db === null) {
$this->setDb($db); $this->setDb($object->getConnection());
} }
return $this; return $this;