diff --git a/library/Icinga/Web/Form/Element/DateTimePicker.php b/library/Icinga/Web/Form/Element/DateTimePicker.php index 2173c6c8a..f48742ea6 100644 --- a/library/Icinga/Web/Form/Element/DateTimePicker.php +++ b/library/Icinga/Web/Form/Element/DateTimePicker.php @@ -112,6 +112,16 @@ class DateTimePicker extends FormElement return $this->max; } + /** + * Get the expected date and time format of any user input + * + * @return string + */ + public function getFormat() + { + return $this->local ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; + } + /** * Is the date and time valid? * @@ -127,7 +137,7 @@ class DateTimePicker extends FormElement } if (! $value instanceof DateTime) { - $format = $this->local === true ? 'Y-m-d\TH:i:s' : DateTime::RFC3339; + $format = $this->getFormat(); $dateTime = DateTime::createFromFormat($format, $value); if ($dateTime === false) { $dateTime = DateTime::createFromFormat(substr($format, 0, strrpos($format, ':')), $value); diff --git a/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php index 05d1451d9..046b6fee0 100644 --- a/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/ScheduleHostDowntimeCommandForm.php @@ -61,6 +61,22 @@ class ScheduleHostDowntimeCommandForm extends ScheduleServiceDowntimeCommandForm */ public function onSuccess() { + $end = $this->getValue('end')->getTimestamp(); + if ($end <= $this->getValue('start')->getTimestamp()) { + $endElement = $this->_elements['end']; + $endElement->setValue($endElement->getValue()->format($endElement->getFormat())); + $endElement->addError($this->translate('The end time must be greater than the start time')); + return false; + } + + $now = new DateTime; + if ($end <= $now->getTimestamp()) { + $endElement = $this->_elements['end']; + $endElement->setValue($endElement->getValue()->format($endElement->getFormat())); + $endElement->addError($this->translate('A downtime must not be in the past')); + return false; + } + foreach ($this->objects as $object) { /** @var \Icinga\Module\Monitoring\Object\Host $object */ if (($childHostsEl = $this->getElement('child_hosts')) !== null) { diff --git a/modules/monitoring/application/forms/Command/Object/ScheduleServiceDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/ScheduleServiceDowntimeCommandForm.php index 9e6efb5ca..14ac85120 100644 --- a/modules/monitoring/application/forms/Command/Object/ScheduleServiceDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/ScheduleServiceDowntimeCommandForm.php @@ -197,6 +197,22 @@ class ScheduleServiceDowntimeCommandForm extends ObjectsCommandForm */ public function onSuccess() { + $end = $this->getValue('end')->getTimestamp(); + if ($end <= $this->getValue('start')->getTimestamp()) { + $endElement = $this->_elements['end']; + $endElement->setValue($endElement->getValue()->format($endElement->getFormat())); + $endElement->addError($this->translate('The end time must be greater than the start time')); + return false; + } + + $now = new DateTime; + if ($end <= $now->getTimestamp()) { + $endElement = $this->_elements['end']; + $endElement->setValue($endElement->getValue()->format($endElement->getFormat())); + $endElement->addError($this->translate('A downtime must not be in the past')); + return false; + } + foreach ($this->objects as $object) { /** @var \Icinga\Module\Monitoring\Object\Service $object */ $downtime = new ScheduleServiceDowntimeCommand();