From 2109d138d53e2aa5956e1dc191c7bf22c02ee9c0 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 21 Aug 2017 22:41:37 +0200 Subject: [PATCH] IcingaTimeperiodRangeForm: delete through the... IcingaTimePeriod and it's Ranges fixes #1089 --- .../forms/IcingaTimePeriodRangeForm.php | 24 +++++++++++++++++++ library/Director/Data/Db/DbObject.php | 2 +- .../Objects/IcingaTimePeriodRanges.php | 13 +++++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/application/forms/IcingaTimePeriodRangeForm.php b/application/forms/IcingaTimePeriodRangeForm.php index cb46d539..035026db 100644 --- a/application/forms/IcingaTimePeriodRangeForm.php +++ b/application/forms/IcingaTimePeriodRangeForm.php @@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Forms; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaTimePeriod; +use Icinga\Module\Director\Objects\IcingaTimePeriodRange; use Icinga\Module\Director\Web\Form\DirectorObjectForm; class IcingaTimePeriodRangeForm extends DirectorObjectForm @@ -40,6 +41,29 @@ class IcingaTimePeriodRangeForm extends DirectorObjectForm return $this; } + /** + * @param IcingaTimePeriodRange $object + */ + protected function deleteObject($object) + { + $key = $object->get('range_key'); + $period = $this->period; + $period->ranges()->remove($key); + $period->store(); + $msg = sprintf( + 'Time period range "%s" has been removed from %s', + $key, + $period->getObjectName() + ); + + $url = $this->getSuccessUrl()->without( + ['range', 'range_type'] + ); + + $this->setSuccessUrl($url); + $this->redirectOnSuccess($msg); + } + public function onSuccess() { $object = $this->object(); diff --git a/library/Director/Data/Db/DbObject.php b/library/Director/Data/Db/DbObject.php index e1d934a1..2a117642 100644 --- a/library/Director/Data/Db/DbObject.php +++ b/library/Director/Data/Db/DbObject.php @@ -857,7 +857,7 @@ abstract class DbObject return $result !== false; } - protected function createWhere() + public function createWhere() { if ($id = $this->getAutoincId()) { return $this->db->quoteInto( diff --git a/library/Director/Objects/IcingaTimePeriodRanges.php b/library/Director/Objects/IcingaTimePeriodRanges.php index d0a75dc8..7ffb117b 100644 --- a/library/Director/Objects/IcingaTimePeriodRanges.php +++ b/library/Director/Objects/IcingaTimePeriodRanges.php @@ -2,7 +2,7 @@ namespace Icinga\Module\Director\Objects; -use Icinga\Exception\ProgrammingError; +use Exception; use Iterator; use Countable; use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer; @@ -11,8 +11,10 @@ use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1; class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRenderer { + /** @var IcingaTimePeriodRange[] */ protected $storedRanges = array(); + /** @var IcingaTimePeriodRange[] */ protected $ranges = array(); protected $modified = false; @@ -211,6 +213,7 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere )->where('o.timeperiod_id = ?', (int) $this->object->id) ->order('o.range_key'); + /** @var IcingaTimePeriodRange $class */ $class = $this->getClass(); $this->ranges = $class::loadAll($connection, $query, 'range_key'); $this->storedRanges = array(); @@ -224,13 +227,17 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere public function store() { + $db = $this->object->getConnection(); foreach ($this->ranges as $range) { $range->timeperiod_id = $this->object->id; - $range->store($this->object->getConnection()); + $range->store($db); } foreach (array_diff(array_keys($this->storedRanges), array_keys($this->ranges)) as $delete) { - $this->storedRanges[$delete]->delete(); + $db->getDbAdapter()->delete( + 'icinga_timeperiod_range', + $this->storedRanges[$delete]->createWhere() + ); } $this->storedRanges = $this->ranges;