From cd13b8124bb1663f78a75ef2f9e54d10decb343d Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 10 Apr 2019 16:25:06 +0200 Subject: [PATCH] IcingaScheduledDowntimeRangeForm: new form --- .../IcingaScheduledDowntimeRangeForm.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 application/forms/IcingaScheduledDowntimeRangeForm.php diff --git a/application/forms/IcingaScheduledDowntimeRangeForm.php b/application/forms/IcingaScheduledDowntimeRangeForm.php new file mode 100644 index 00000000..aced5c0a --- /dev/null +++ b/application/forms/IcingaScheduledDowntimeRangeForm.php @@ -0,0 +1,110 @@ +addHidden('scheduled_downtime_id', $this->downtime->get('id')); + $this->addElement('text', 'range_key', [ + 'label' => $this->translate('Day(s)'), + 'description' => $this->translate( + 'Might be, monday, tuesday, 2016-01-28 - have a look at the documentation for more examples' + ), + ]); + + $this->addElement('text', 'range_value', [ + 'label' => $this->translate('Timeperiods'), + 'description' => $this->translate( + 'One or more time periods, e.g. 00:00-24:00 or 00:00-09:00,17:00-24:00' + ), + ]); + + $this->setButtons(); + } + + public function setScheduledDowntime(IcingaScheduledDowntime $downtime) + { + $this->downtime = $downtime; + $this->setDb($downtime->getConnection()); + return $this; + } + + /** + * @param IcingaScheduledDowntimeRange $object + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + protected function deleteObject($object) + { + $key = $object->get('range_key'); + $downtime = $this->downtime; + $downtime->ranges()->remove($key); + $downtime->store(); + $msg = sprintf( + $this->translate('Time range "%s" has been removed from %s'), + $key, + $downtime->getObjectName() + ); + + $url = $this->getSuccessUrl()->without( + ['range', 'range_type'] + ); + + $this->setSuccessUrl($url); + $this->redirectOnSuccess($msg); + } + + /** + * @throws \Icinga\Module\Director\Exception\DuplicateKeyException + */ + public function onSuccess() + { + $object = $this->object(); + if ($object->hasBeenModified()) { + $this->downtime->ranges()->setRange( + $this->getValue('range_key'), + $this->getValue('range_value') + ); + } + + if ($this->downtime->hasBeenModified()) { + if (! $object->hasBeenLoadedFromDb()) { + $this->setHttpResponseCode(201); + } + + $msg = sprintf( + $object->hasBeenLoadedFromDb() + ? $this->translate('The %s has successfully been stored') + : $this->translate('A new %s has successfully been created'), + $this->translate($this->getObjectShortClassName()) + ); + + $this->downtime->store($this->db); + } else { + if ($this->isApiRequest()) { + $this->setHttpResponseCode(304); + } + $msg = $this->translate('No action taken, object has not been modified'); + } + if ($object instanceof IcingaObject) { + $this->setSuccessUrl( + 'director/' . strtolower($this->getObjectShortClassName()), + $object->getUrlParams() + ); + } + + $this->redirectOnSuccess($msg); + } +}