From fa7379adc7eaf7af2654ac89b434f2976a200d56 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 2 Aug 2013 16:55:28 +0200 Subject: [PATCH] Enable dynamic form elements Expire time of the AcknowledgeForm and hours as well as minutes of the ScheduleDowntimeForm are now dynamic. refs #4439 --- library/Icinga/Web/Form.php | 1 - .../forms/Command/AcknowledgeForm.php | 50 +++++++++----- .../forms/Command/ScheduleDowntimeForm.php | 68 +++++++++++-------- 3 files changed, 72 insertions(+), 47 deletions(-) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 6d2be2fe0..f2d9af954 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -27,7 +27,6 @@ namespace Icinga\Web; use Icinga\Exception\ProgrammingError; use Icinga\Web\Form\InvalidCSRFTokenException; -use Zend_Form_Exception; use Zend_View_Interface; /** diff --git a/modules/monitoring/application/forms/Command/AcknowledgeForm.php b/modules/monitoring/application/forms/Command/AcknowledgeForm.php index 61e9bb46e..6ebb29937 100644 --- a/modules/monitoring/application/forms/Command/AcknowledgeForm.php +++ b/modules/monitoring/application/forms/Command/AcknowledgeForm.php @@ -39,6 +39,14 @@ use Icinga\Protocol\Commandpipe\Comment; */ class AcknowledgeForm extends ConfirmationForm { + /** + * Initialize form + */ + public function init() + { + $this->setName('AcknowledgeForm'); + } + /** * Interface method to build the form * @see ConfirmationForm::create @@ -66,6 +74,13 @@ class AcknowledgeForm extends ConfirmationForm ) ); + $expireNote = new Note( + array( + 'name' => 'expirenote', + 'value' => t('If the acknowledgement should expire, check the box and enter an expiration timestamp.') + ) + ); + $expireCheck = $this->createElement( 'checkbox', 'expire', @@ -74,26 +89,25 @@ class AcknowledgeForm extends ConfirmationForm ) ); - $now = new PhpDateTime(); - $interval = new DateInterval('PT1H'); // Add 3600 seconds - $now->add($interval); + if ($this->getRequest()->getPost('expire', '0') === '1') { + $now = new PhpDateTime(); + $interval = new DateInterval('PT1H'); // Add 3600 seconds + $now->add($interval); - $expireTime = new DateTime( - array( - 'name' => 'expiretime', - 'label' => t('Expire time'), - 'value' => $now->format($this->getDateFormat()) - ) - ); + $expireTime = new DateTime( + array( + 'name' => 'expiretime', + 'label' => t('Expire time'), + 'value' => $now->format($this->getDateFormat()) + ) + ); + + $this->addElements(array($expireNote, $expireCheck, $expireTime)); + } else { + $this->addElements(array($expireNote, $expireCheck)); + } - $expireNote = new Note( - array( - 'name' => 'expirenote', - 'value' => t('If the acknowledgement should expire, check the box and enter an expiration timestamp.') - ) - ); - - $this->addElements(array($expireNote, $expireCheck, $expireTime)); + $this->enableAutoSubmit(array('expire')); $this->addDisplayGroup( array( diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php index cab8fae0e..dd0764be9 100644 --- a/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php +++ b/modules/monitoring/application/forms/Command/ScheduleDowntimeForm.php @@ -58,6 +58,14 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm */ const TYPE_FLEXIBLE = 'flexible'; + /** + * Initialize form + */ + public function init() + { + $this->setName('ScheduleDowntimeForm'); + } + /** * Build an array of timestamps * @@ -178,37 +186,41 @@ class ScheduleDowntimeForm extends WithChildrenCommandForm ) ) ); + $this->enableAutoSubmit(array('type')); - $hoursText = new Zend_Form_Element_Text('hours'); - $hoursText->setLabel(t('Flexible duration')); - $hoursText->setAttrib('style', 'width: 40px;'); - $hoursText->setValue(1); - $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); - $hoursText->addDecorator( - 'Callback', - array( - 'callback' => function () { - return t('Hours'); - } - ) - ); + + if ($this->getRequest()->getPost('type') === self::TYPE_FLEXIBLE) { + $hoursText = new Zend_Form_Element_Text('hours'); + $hoursText->setLabel(t('Flexible duration')); + $hoursText->setAttrib('style', 'width: 40px;'); + $hoursText->setValue(1); + $hoursText->addDecorator('HtmlTag', array('tag' => 'dd', 'openOnly' => true)); + $hoursText->addDecorator( + 'Callback', + array( + 'callback' => function () { + return t('Hours'); + } + ) + ); - $minutesText = new Zend_Form_Element_Text('minutes'); - $minutesText->setLabel(t('Minutes')); - $minutesText->setAttrib('style', 'width: 40px;'); - $minutesText->setValue(0); - $minutesText->removeDecorator('HtmlTag'); - $minutesText->removeDecorator('Label'); - $minutesText->addDecorator( - 'Callback', - array( - 'callback' => function () { - return t('Minutes'); - } - ) - ); + $minutesText = new Zend_Form_Element_Text('minutes'); + $minutesText->setLabel(t('Minutes')); + $minutesText->setAttrib('style', 'width: 40px;'); + $minutesText->setValue(0); + $minutesText->removeDecorator('HtmlTag'); + $minutesText->removeDecorator('Label'); + $minutesText->addDecorator( + 'Callback', + array( + 'callback' => function () { + return t('Minutes'); + } + ) + ); - $this->addElements(array($hoursText, $minutesText)); + $this->addElements(array($hoursText, $minutesText)); + } if ($this->getWithChildren() === true) { $this->addNote(t('Schedule downtime for host and its services.'));