IcingaTimeperiodRangeForm: delete through the...

IcingaTimePeriod and it's Ranges

fixes #1089
This commit is contained in:
Thomas Gelf 2017-08-21 22:41:37 +02:00
parent fc9425ee89
commit 2109d138d5
3 changed files with 35 additions and 4 deletions

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaTimePeriod; use Icinga\Module\Director\Objects\IcingaTimePeriod;
use Icinga\Module\Director\Objects\IcingaTimePeriodRange;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaTimePeriodRangeForm extends DirectorObjectForm class IcingaTimePeriodRangeForm extends DirectorObjectForm
@ -40,6 +41,29 @@ class IcingaTimePeriodRangeForm extends DirectorObjectForm
return $this; 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() public function onSuccess()
{ {
$object = $this->object(); $object = $this->object();

View File

@ -857,7 +857,7 @@ abstract class DbObject
return $result !== false; return $result !== false;
} }
protected function createWhere() public function createWhere()
{ {
if ($id = $this->getAutoincId()) { if ($id = $this->getAutoincId()) {
return $this->db->quoteInto( return $this->db->quoteInto(

View File

@ -2,7 +2,7 @@
namespace Icinga\Module\Director\Objects; namespace Icinga\Module\Director\Objects;
use Icinga\Exception\ProgrammingError; use Exception;
use Iterator; use Iterator;
use Countable; use Countable;
use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer; use Icinga\Module\Director\IcingaConfig\IcingaConfigRenderer;
@ -11,8 +11,10 @@ use Icinga\Module\Director\IcingaConfig\IcingaLegacyConfigHelper as c1;
class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRenderer class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRenderer
{ {
/** @var IcingaTimePeriodRange[] */
protected $storedRanges = array(); protected $storedRanges = array();
/** @var IcingaTimePeriodRange[] */
protected $ranges = array(); protected $ranges = array();
protected $modified = false; protected $modified = false;
@ -211,6 +213,7 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
)->where('o.timeperiod_id = ?', (int) $this->object->id) )->where('o.timeperiod_id = ?', (int) $this->object->id)
->order('o.range_key'); ->order('o.range_key');
/** @var IcingaTimePeriodRange $class */
$class = $this->getClass(); $class = $this->getClass();
$this->ranges = $class::loadAll($connection, $query, 'range_key'); $this->ranges = $class::loadAll($connection, $query, 'range_key');
$this->storedRanges = array(); $this->storedRanges = array();
@ -224,13 +227,17 @@ class IcingaTimePeriodRanges implements Iterator, Countable, IcingaConfigRendere
public function store() public function store()
{ {
$db = $this->object->getConnection();
foreach ($this->ranges as $range) { foreach ($this->ranges as $range) {
$range->timeperiod_id = $this->object->id; $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) { 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; $this->storedRanges = $this->ranges;