From 3ffacc70b6b89205fdb847d6e64ae999238962d1 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 13 Aug 2018 11:21:54 +0200 Subject: [PATCH] IcingaServiceSet: do not delete on title click fixes #1560 --- application/forms/RemoveLinkForm.php | 27 ++++++++++------ library/Director/Objects/IcingaServiceSet.php | 24 ++++++++++++++ .../Table/IcingaServiceSetServiceTable.php | 32 ++++++++----------- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/application/forms/RemoveLinkForm.php b/application/forms/RemoveLinkForm.php index 7cc45b55..e70920e2 100644 --- a/application/forms/RemoveLinkForm.php +++ b/application/forms/RemoveLinkForm.php @@ -3,7 +3,6 @@ namespace Icinga\Module\Director\Forms; use dipl\Html\Icon; -use dipl\Web\Url; use Icinga\Module\Director\Web\Form\DirectorForm; class RemoveLinkForm extends DirectorForm @@ -12,10 +11,13 @@ class RemoveLinkForm extends DirectorForm private $title; + private $onSuccessAction; + public function __construct($label, $title, $action, $params = []) { parent::__construct([ - 'style' => 'float: right' + 'style' => 'float: right', + 'data-base-target' => '_self' ]); $this->label = $label; $this->title = $title; @@ -25,24 +27,31 @@ class RemoveLinkForm extends DirectorForm $this->setAction($action); } + public function runOnSuccess($action) + { + $this->onSuccessAction = $action; + + return $this; + } + public function setup() { $this->setAttrib('class', 'inline'); - //$this->setDecorators(['Form', 'FormElements']); -// 'class' => 'icon-cancel', -// 'style' => 'float: right; font-weight: normal', -// 'title' => $this->translate('Remove this set from this host') - $this->addHtml(Icon::create('cancel')); $this->addSubmitButton($this->label, [ 'class' => 'link-button', 'title' => $this->title, - 'data-base-target' => '_next' ]); } public function onSuccess() { - // nothing. + if ($this->onSuccessAction !== null) { + $func = $this->onSuccessAction; + $func(); + $this->redirectOnSuccess( + $this->translate('Service Set has been removed') + ); + } } } diff --git a/library/Director/Objects/IcingaServiceSet.php b/library/Director/Objects/IcingaServiceSet.php index 12f400c4..21988e19 100644 --- a/library/Director/Objects/IcingaServiceSet.php +++ b/library/Director/Objects/IcingaServiceSet.php @@ -109,6 +109,30 @@ class IcingaServiceSet extends IcingaObject return $services; } + public function onDelete() + { + $hostId = $this->get('host_id'); + if ($hostId) { + $deleteIds = []; + foreach ($this->getServiceObjects() as $service) { + $deleteIds[] = (int) $service->get('id'); + } + + if (! empty($deleteIds)) { + $db = $this->getDb(); + $db->delete( + 'icinga_host_service_blacklist', + $db->quoteInto( + sprintf('host_id = %s AND service_id IN (?)', $hostId), + $deleteIds + ) + ); + } + } + + parent::onDelete(); + } + public function renderToConfig(IcingaConfig $config) { if ($this->get('assign_filter') === null && $this->isTemplate()) { diff --git a/library/Director/Web/Table/IcingaServiceSetServiceTable.php b/library/Director/Web/Table/IcingaServiceSetServiceTable.php index 60e8fdf3..fbcbd168 100644 --- a/library/Director/Web/Table/IcingaServiceSetServiceTable.php +++ b/library/Director/Web/Table/IcingaServiceSetServiceTable.php @@ -8,6 +8,7 @@ use Icinga\Module\Director\Objects\IcingaServiceSet; use dipl\Html\HtmlElement; use dipl\Html\Link; use dipl\Web\Table\ZfQueryBasedTable; +use dipl\Web\Url; class IcingaServiceSetServiceTable extends ZfQueryBasedTable { @@ -157,29 +158,22 @@ class IcingaServiceSetServiceTable extends ZfQueryBasedTable ] ); } else { - // !!! - $deleteLink = Link::create( - $this->translate('Remove'), - 'director/host/removeset', - [ - 'name' => $this->host->getObjectName(), - 'setId' => $this->set->get('id') - ], - [ - 'class' => 'icon-cancel', - 'style' => 'float: right; font-weight: normal', - 'title' => $this->translate('Remove this set from this host') - ] - ); $deleteLink = new RemoveLinkForm( $this->translate('Remove'), $this->translate('Remove this set from this host'), - 'director/host/removeset', - [ - 'name' => $this->host->getObjectName(), - 'setId' => $this->set->get('id') - ] + Url::fromPath('director/host/services', [ + 'name' => $this->host->getObjectName() + ]) ); + $hostId = $this->host->get('id'); + $setName = $this->set->getObjectName(); + $db = $this->set->getConnection(); + $deleteLink->runOnSuccess(function () use ($db, $hostId, $setName) { + IcingaServiceSet::load([ + 'host_id' => $hostId, + 'object_name' => $setName + ], $db)->delete(); + }); $deleteLink->handleRequest(); }