IcingaTimeperiodRangeForm: delete through the...
IcingaTimePeriod and it's Ranges fixes #1089
This commit is contained in:
parent
fc9425ee89
commit
2109d138d5
|
@ -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();
|
||||
|
|
|
@ -857,7 +857,7 @@ abstract class DbObject
|
|||
return $result !== false;
|
||||
}
|
||||
|
||||
protected function createWhere()
|
||||
public function createWhere()
|
||||
{
|
||||
if ($id = $this->getAutoincId()) {
|
||||
return $this->db->quoteInto(
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue