Merge branch 'bugfix/warn-about-illogical-dates-10847'

fixes #10847
This commit is contained in:
Marius Hein 2016-04-19 11:12:45 +02:00
commit 6faed28e1b
3 changed files with 43 additions and 1 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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();