From 7dea7fcad0f16d2f0687e39aec89e6ffa90d5ecd Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 12 Aug 2013 14:20:06 +0200 Subject: [PATCH] Monitoring/Forms: Replace PHP DateTime usage with DateTimeFactory refs #4440 --- .../forms/Command/AcknowledgeForm.php | 27 ++++----- .../forms/Command/RescheduleNextCheckForm.php | 17 ++---- .../forms/Command/ScheduleDowntimeForm.php | 55 ++++--------------- .../forms/Command/AcknowledgeFormTest.php | 21 ++++++- .../Command/ScheduleDowntimeFormTest.php | 6 +- 5 files changed, 49 insertions(+), 77 deletions(-) diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index ef21ee1fb..05a758246 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -28,12 +28,11 @@ namespace Monitoring\Form\Command; -use \DateTime; use Icinga\Web\Form\Element\DateTimePicker; -use \DateInterval; use Icinga\Web\Form\Element\Note; use Icinga\Protocol\Commandpipe\Acknowledgement; use Icinga\Protocol\Commandpipe\Comment; +use Icinga\Util\DateTimeFactory; /** * Form for problem acknowledgements @@ -59,9 +58,9 @@ class AcknowledgeForm extends CommandForm 'textarea', 'comment', array( - 'label' => t('Comment'), - 'rows' => 4, - 'required' => true + 'label' => t('Comment'), + 'rows' => 4, + 'required' => true ) ); @@ -85,20 +84,17 @@ class AcknowledgeForm extends CommandForm 'checkbox', 'expire', array( - 'label' => t('Use expire time') + 'label' => t('Use expire time') ) ); if ($this->getRequest()->getPost('expire', '0') === '1') { - $now = new DateTime(); - $interval = new DateInterval('PT1H'); // Add 3600 seconds - $now->add($interval); - + $now = DateTimeFactory::create(); $expireTime = new DateTimePicker( array( 'name' => 'expiretime', 'label' => t('Expire time'), - 'value' => $now->format($this->getDateFormat()) + 'value' => $now->getTimestamp() + 3600 ) ); @@ -146,15 +142,15 @@ class AcknowledgeForm extends CommandForm /** * Add validator for dependent fields - * @see Form::preValidation - * @param array $data + * + * @param array $data + * @see \Icinga\Web\Form::preValidation() */ protected function preValidation(array $data) { if (isset($data['expire']) && intval($data['expire']) === 1) { $expireTime = $this->getElement('expiretime'); $expireTime->setRequired(true); - $expireTime->addValidator($this->createDateTimeValidator(), true); } } @@ -162,8 +158,7 @@ class AcknowledgeForm extends CommandForm { $expireTime = -1; if ($this->getValue('expire')) { - $time = new DateTime($this->getValue('expiretime')); - $expireTime = $time->getTimestamp(); + $expireTime = $this->getValue('expiretime'); } return new Acknowledgement( new Comment( diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php index 9d9d50a4c..a5016b2f1 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheckForm.php @@ -29,8 +29,9 @@ namespace Monitoring\Form\Command; use \DateTime; -use Icinga\Web\Form\Element\DateTimePicker; -use Zend_Form_Element_Checkbox; +use \Zend_Form_Element_Checkbox; +use \Icinga\Web\Form\Element\DateTimePicker; +use \Icinga\Util\DateTimeFactory; /** * Form for RescheduleNextCheck @@ -38,24 +39,18 @@ use Zend_Form_Element_Checkbox; class RescheduleNextCheckForm extends WithChildrenCommandForm { /** - * Interface method to build the form - * @see CommandForm::create + * Create the form's elements */ protected function create() { - - $now = new DateTime(); + $now = DateTimeFactory::create(); $dateElement = new DateTimePicker( array( 'name' => 'checktime', 'label' => t('Check time'), - 'value' => $now->format($this->getDateFormat()) + 'value' => $now->getTimestamp() ) ); - - $dateElement->setRequired(true); - $dateElement->addValidator($this->createDateTimeValidator(), true); - $this->addElement($dateElement); $checkBox = new Zend_Form_Element_Checkbox( diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index c2ece3fbd..84e71e7b4 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -28,26 +28,19 @@ namespace Monitoring\Form\Command; -use \DateTime; -use Icinga\Web\Form\Element\DateTimePicker; -use Icinga\Protocol\Commandpipe\Downtime; -use Icinga\Protocol\Commandpipe\Comment; -use \DateInterval; use \Zend_Form_Element_Text; use \Zend_Validate_GreaterThan; use \Zend_Validate_Digits; +use Icinga\Web\Form\Element\DateTimePicker; +use Icinga\Protocol\Commandpipe\Downtime; +use Icinga\Protocol\Commandpipe\Comment; +use Icinga\Util\DateTimeFactory; /** * Form for any ScheduleDowntime command */ class ScheduleDowntimeForm extends WithChildrenCommandForm { - /** - * Default endtime interval definition - * @see http://php.net/manual/de/class.dateinterval.php - */ - const DEFAULT_ENDTIME_INTERVAL = 'PT1H'; - /** * Type constant for fixed downtimes */ @@ -66,25 +59,6 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm $this->setName('ScheduleDowntimeForm'); } - /** - * Build an array of timestamps - * - * @return string[] - */ - private function generateDefaultTimestamps() - { - $out = array(); - - $dateTimeObject = new DateTime(); - $out[] = $dateTimeObject->format($this->getDateFormat()); - - $dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL); - $dateTimeObject->add($dateInterval); - $out[] = $dateTimeObject->format($this->getDateFormat()); - - return $out; - } - /** * Generate translated multi options based on type constants * @@ -144,28 +118,21 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ) ); - list($timestampStart, $timestampEnd) = $this->generateDefaultTimestamps(); - + $now = DateTimeFactory::create(); $dateTimeStart = new DateTimePicker( array( 'name' => 'starttime', 'label' => t('Start time'), - 'value' => $timestampStart + 'value' => $now->getTimestamp() ) ); - $dateTimeStart->setRequired(true); - $dateTimeStart->addValidator($this->createDateTimeValidator(), true); - $dateTimeEnd = new DateTimePicker( array( 'name' => 'endtime', 'label' => t('End time'), - 'value' => $timestampEnd + 'value' => $now->getTimestamp() + 3600 ) ); - $dateTimeEnd->setRequired(true); - $dateTimeEnd->addValidator($this->createDateTimeValidator(), true); - $this->addElements(array($dateTimeStart, $dateTimeEnd)); $this->addElement( @@ -304,12 +271,12 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm if ($this->getValue('type') === self::TYPE_FLEXIBLE) { $duration = ($this->getValue('hours')*3600) + ($this->getValue('minutes')*60); } - $starttime = new DateTime($this->getValue('starttime')); - $endtime = new DateTime($this->getValue('endtime')); + $starttime = $this->getValue('starttime'); + $endtime = $this->getValue('endtime'); $downtime = new Downtime( - $starttime->getTimestamp(), - $endtime->getTimestamp(), + $starttime, + $endtime, $comment, $duration, $this->getValue('triggered') diff --git a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php index cca9951c5..d8451cb44 100644 --- a/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/AcknowledgeFormTest.php @@ -1,23 +1,38 @@ new DateTimeZone('UTC'))); + } + public function testForm() { $formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS); diff --git a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php index 5cf27d395..5d5d43331 100644 --- a/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php +++ b/modules/monitoring/test/php/application/forms/Command/ScheduleDowntimeFormTest.php @@ -228,7 +228,7 @@ class ScheduleDowntimeFormTest extends BaseFormTest 'Assert negative hours to cause validation errors in flexible downtime' ); } - + public function testInvalidMinutesValueRecognitionInFlexibleDowntime() { $form = $this->getRequestForm(array( @@ -317,8 +317,8 @@ class ScheduleDowntimeFormTest extends BaseFormTest $form = $this->getRequestForm(array(), self::FORMCLASS); $form->buildForm(); - $time1 = strtotime($form->getElement('starttime')->getValue()); - $time2 = strtotime($form->getElement('endtime')->getValue()); + $time1 = $form->getElement('starttime')->getValue(); + $time2 = $form->getElement('endtime')->getValue(); $this->assertEquals(3600, ($time2 - $time1)); }