Monitoring/Forms: Replace PHP DateTime usage with DateTimeFactory
refs #4440
This commit is contained in:
parent
2c217d1d06
commit
7dea7fcad0
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -1,23 +1,38 @@
|
|||
<?php
|
||||
|
||||
|
||||
|
||||
namespace Test\Monitoring\Forms\Command;
|
||||
|
||||
require_once __DIR__.'/BaseFormTest.php';
|
||||
$base = __DIR__.'/../../../../../../../';
|
||||
require_once $base.'modules/monitoring/application/forms/Command/CommandForm.php';
|
||||
require_once $base . 'library/Icinga/Util/ConfigAwareFactory.php';
|
||||
require_once $base . 'library/Icinga/Util/DateTimeFactory.php';
|
||||
require_once realpath($base.'modules/monitoring/application/forms/Command/WithChildrenCommandForm.php');
|
||||
require_once realpath($base.'modules/monitoring/application/forms/Command/AcknowledgeForm.php');
|
||||
|
||||
use Monitoring\Form\Command\AcknowledgeForm;
|
||||
use \DateTimeZone;
|
||||
use \Zend_View;
|
||||
use \Zend_Test_PHPUnit_ControllerTestCase;
|
||||
use Monitoring\Form\Command\AcknowledgeForm;
|
||||
use Icinga\Util\DateTimeFactory;
|
||||
|
||||
class AcknowledgeFormTest extends BaseFormTest
|
||||
{
|
||||
const FORMCLASS = "Monitoring\Form\Command\AcknowledgeForm";
|
||||
|
||||
/**
|
||||
* Set up the default time zone
|
||||
*
|
||||
* Utilizes singleton DateTimeFactory
|
||||
*
|
||||
* @backupStaticAttributes enabled
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
date_default_timezone_set('UTC');
|
||||
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('UTC')));
|
||||
}
|
||||
|
||||
public function testForm()
|
||||
{
|
||||
$formWithoutExpiration = $this->getRequestForm(array(), self::FORMCLASS);
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue