From 03f4a8eceb170be5eca22e8a62d62312bceb2b64 Mon Sep 17 00:00:00 2001 From: Marius Hein Date: Wed, 17 Jul 2013 14:08:07 +0200 Subject: [PATCH] Command masks: Implement missing forms [WIP] Fixes phpcs issues, missing features, fix controller using new config interface. refs #4355 --- library/Icinga/Web/Form.php | 7 +- library/Icinga/Web/Form/Confirmation.php | 61 ----- library/Icinga/Web/Form/Element/DateTime.php | 2 +- library/Icinga/Web/Form/Element/Note.php | 2 +- library/Icinga/Web/Form/SendCommand.php | 152 ----------- .../controllers/CommandController.php | 255 ++++++++++++++---- .../forms/Command/AbstractCommand.php | 18 +- .../application/forms/Command/Acknowledge.php | 50 +++- .../application/forms/Command/Comment.php | 2 +- .../Command/ConfirmationWithIdentifier.php | 127 +++++++++ .../forms/Command/DelayNotification.php | 58 ++++ .../forms/Command/RescheduleNextCheck.php | 23 +- .../forms/Command/ScheduleDowntime.php | 202 ++++++++++++++ .../WithChildrenCommand.php} | 40 ++- 14 files changed, 699 insertions(+), 300 deletions(-) delete mode 100644 library/Icinga/Web/Form/Confirmation.php delete mode 100644 library/Icinga/Web/Form/SendCommand.php create mode 100644 modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php create mode 100644 modules/monitoring/application/forms/Command/DelayNotification.php create mode 100644 modules/monitoring/application/forms/Command/ScheduleDowntime.php rename modules/monitoring/application/forms/{TestForm.php => Command/WithChildrenCommand.php} (65%) diff --git a/library/Icinga/Web/Form.php b/library/Icinga/Web/Form.php index 7b1e9dbab..00d96219d 100644 --- a/library/Icinga/Web/Form.php +++ b/library/Icinga/Web/Form.php @@ -75,6 +75,11 @@ abstract class Form extends \Zend_Form { if ($this->_isRendered === false) { $this->create(); + + // Empty action if not safe + if (!$this->getAction() && $this->getRequest()) { + $this->setAction($this->getRequest()->getRequestUri()); + } } return parent::render($view); } @@ -115,7 +120,7 @@ abstract class Form extends \Zend_Form $check = null; if ($data === null) { - $data = $this->getRequest()->getParams(); + $check = $this->getRequest()->getParams(); } elseif ($data instanceof \Zend_Controller_Request_Abstract) { $check = $data->getParams(); } else { diff --git a/library/Icinga/Web/Form/Confirmation.php b/library/Icinga/Web/Form/Confirmation.php deleted file mode 100644 index cea06b9ed..000000000 --- a/library/Icinga/Web/Form/Confirmation.php +++ /dev/null @@ -1,61 +0,0 @@ -message = $message; - $this->style = $style; - $this->init(); - } - - public function init() - { - $this->setMethod("post"); - - $note = new \Zend_Form_Element_Note("note"); - $note->setValue($this->message); - $this->addElement($note); - - if ($this->style === self::YES_NO) { - $this->addElement('submit', 'btn_yes', array( - 'label' => 'Yes' - ) - ); - $this->addElement('submit', 'btn_no', array( - 'label' => 'No' - ) - ); - } elseif ($this->style === self::OK_CANCEL) { - $this->addElement('submit', 'btn_ok', array( - 'label' => 'Ok' - ) - ); - $this->addElement('submit', 'btn_cancel', array( - 'label' => 'Cancel' - ) - ); - } else { - throw new ProgrammingError("Button style must be one of: YES_NO, OK_CANCEL"); - } - } - - public function isConfirmed() - { - if ($this->style === self::YES_NO) { - return $this->isSubmitted("btn_yes"); - } elseif ($this->style === self::OK_CANCEL) { - return $this->isSubmitted("btn_ok"); - } - } -} - -?> diff --git a/library/Icinga/Web/Form/Element/DateTime.php b/library/Icinga/Web/Form/Element/DateTime.php index f25b3f814..baa6807e3 100644 --- a/library/Icinga/Web/Form/Element/DateTime.php +++ b/library/Icinga/Web/Form/Element/DateTime.php @@ -37,4 +37,4 @@ class DateTime extends Zend_Form_Element_Xhtml * @var string */ public $helper = "formDateTime"; -} \ No newline at end of file +} diff --git a/library/Icinga/Web/Form/Element/Note.php b/library/Icinga/Web/Form/Element/Note.php index b093bdd23..77ec2a785 100644 --- a/library/Icinga/Web/Form/Element/Note.php +++ b/library/Icinga/Web/Form/Element/Note.php @@ -40,4 +40,4 @@ class Note extends Zend_Form_Element_Xhtml * @var string */ public $helper = 'formNote'; -} \ No newline at end of file +} diff --git a/library/Icinga/Web/Form/SendCommand.php b/library/Icinga/Web/Form/SendCommand.php deleted file mode 100644 index b3f66701a..000000000 --- a/library/Icinga/Web/Form/SendCommand.php +++ /dev/null @@ -1,152 +0,0 @@ -message = $message; - $this->init(); - } - - public function init() - { - $this->setMethod("post"); - - $note = new \Zend_Form_Element_Note("note"); - $note->setValue($this->message); - $this->addElement($note); - - $this->addElement("hidden", "servicelist"); - $this->addElement("hidden", "hostlist"); - } - - public function setHosts($hosts) - { - $this->setDefault("hostlist", $hosts); - } - - public function getHosts() - { - return $this->getValue("hostlist"); - } - - public function setServices($services) - { - $this->setDefault("servicelist", $services); - } - - public function getServices() - { - return $this->getValue("servicelist"); - } - - public function addCheckbox($id, $label, $checked) - { - $this->addElement("checkbox", $id, array( - 'checked' => $checked, - 'label' => $label - ) - ); - } - - public function isChecked($id) - { - return $this->getElement($id)->isChecked(); - } - - public function addSubmitButton($label) - { - $this->addElement("submit", "btn_submit", array( - 'label' => $label - ) - ); - } - - public function addDatePicker($id, $label, $value = "") - { - $date = new Date($id); - $date->setValue($value); - $this->addElement($date, $id, array( - 'label' => $label - ) - ); - } - - public function getDate($id) - { - return $this->getValue($id); - } - - public function addTimePicker($id, $label, $value = "") - { - $time = new Time($id); - $time->setValue($value); - $this->addElement($time, $id, array( - 'label' => $label - ) - ); - } - - public function getTime($id) - { - return $this->getValue($id); - } - - public function addTextBox($id, $label, $value = "", $readonly = false, $multiline = false) - { - $options = array('label' => $label, 'value' => $value); - if ($readonly) { - $options['readonly'] = 1; - } - $this->addElement($multiline ? "textarea" : "text", $id, $options); - } - - public function getText($id) - { - return $this->getValue($id); - } - - public function addChoice($id, $label, $values) - { - $this->addElement("select", $id, array( - 'label' => $label - ) - ); - $this->getElement($id)->setMultiOptions($values); - } - - public function getChoice($id) - { - return $this->getElement($id)->options[$_POST[$id]]; - } - - /** - * @TODO: "min", "max" and "step" seem to have no effect :( - */ - public function addNumberBox($id, $label, $value = "", $min = 0, $max = -1, $step = "any") - { - $number = new Number($id); - $number->setValue($value); - $this->addElement($number, $id, array( - 'label' => $label, - 'step' => $step, - 'min' => $min, - 'max' => $max - ) - ); - } - - public function getNumber($id) - { - return $this->getValue($id); - } -} - -?> \ No newline at end of file diff --git a/modules/monitoring/application/controllers/CommandController.php b/modules/monitoring/application/controllers/CommandController.php index 09f6cdbd2..9ab70a5d9 100644 --- a/modules/monitoring/application/controllers/CommandController.php +++ b/modules/monitoring/application/controllers/CommandController.php @@ -38,8 +38,11 @@ use Icinga\Exception\MissingParameterException; use Monitoring\Form\Command\Acknowledge; use Monitoring\Form\Command\Comment as CommentForm; use Monitoring\Form\Command\Confirmation; +use Monitoring\Form\Command\ConfirmationWithIdentifier; use Monitoring\Form\Command\CustomNotification; +use Monitoring\Form\Command\DelayNotification; use Monitoring\Form\Command\RescheduleNextCheck; +use Monitoring\Form\Command\ScheduleDowntime; use Monitoring\Form\Command\SubmitPassiveCheckResult; /** @@ -62,30 +65,33 @@ class Monitoring_CommandController extends ModuleActionController */ public function init() { -// if ($this->_request->isPost()) { -// // We do not need to display a view.. -// $this->_helper->viewRenderer->setNoRender(true); -// // ..nor the overall site layout in case its a POST request. -// $this->_helper->layout()->disableLayout(); -// -// $instance = $this->_request->getPost("instance"); -// $target_config = Config::getInstance()->getModuleConfig("instances", "monitoring"); -// if ($instance) { -// if (isset($target_config[$instance])) { -// $this->target = new CommandPipe($target_config[$instance]); -// } else { -// throw new ConfigurationError("Instance $instance is not configured"); -// } -// } else { -// $target_info = $target_config->current(); // Take the very first section -// if ($target_info === false) { -// throw new ConfigurationError("Not any instances are configured yet"); -// } else { -// $this->target = new CommandPipe($target_info); -// } -// } -// } + if ($this->_request->isPost()) { + // Save time and memory. We're only working on post + $this->_helper->viewRenderer->setNoRender(true); + $this->_helper->layout()->disableLayout(); + $instance = $this->_request->getPost("instance"); + + $targetConfig = Config::module('monitoring', 'instances'); + + if ($instance) { + if ($targetConfig->get($instance)) { + $this->target = new CommandPipe($targetConfig->get($instance)); + } else { + throw new ConfigurationError('Instance is not configured: '. $instance); + } + } else { + $targetInfo = $targetConfig->current(); // Take the very first section + + if ($targetInfo === false) { + throw new ConfigurationError("Not any instances are configured yet"); + } else { + $this->target = new CommandPipe($targetInfo); + } + } + } + + // Reduce template writing mess $this->_helper->viewRenderer->setRender(self::DEFAULT_VIEW_SCRIPT); } @@ -133,6 +139,10 @@ class Monitoring_CommandController extends ModuleActionController // Commands for hosts / services // ------------------------------------------------------------------------ + /** + * Handle command disableactivechecks + * @throws Icinga\Exception\ProgrammingError + */ public function disableactivechecksAction() { $form = new Confirmation(); @@ -141,11 +151,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Disable active checks for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enableactivechecks + * @throws Icinga\Exception\ProgrammingError + */ public function enableactivechecksAction() { $form = new Confirmation(); @@ -154,11 +168,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Enable active checks for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command reschedulenextcheck + * @throws Icinga\Exception\ProgrammingError + */ public function reschedulenextcheckAction() { $form = new RescheduleNextCheck(); @@ -166,11 +184,15 @@ class Monitoring_CommandController extends ModuleActionController $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command submitpassivecheckresult + * @throws Icinga\Exception\ProgrammingError + */ public function submitpassivecheckresultAction() { $type = SubmitPassiveCheckResult::TYPE_SERVICE; @@ -181,11 +203,15 @@ class Monitoring_CommandController extends ModuleActionController $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command stopobsessing + * @throws Icinga\Exception\ProgrammingError + */ public function stopobsessingAction() { $form = new Confirmation(); @@ -194,11 +220,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Stop obsessing over this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command startobsessing + * @throws Icinga\Exception\ProgrammingError + */ public function startobsessingAction() { $form = new Confirmation(); @@ -207,11 +237,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Start obsessing over this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command stopacceptingpassivechecks + * @throws Icinga\Exception\ProgrammingError + */ public function stopacceptingpassivechecksAction() { $form = new Confirmation(); @@ -220,11 +254,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Passive checks for this object will be omitted.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command startacceptingpassivechecks + * @throws Icinga\Exception\ProgrammingError + */ public function startacceptingpassivechecksAction() { $form = new Confirmation(); @@ -233,11 +271,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Passive checks for this object will be accepted.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command disablenotifications + * @throws Icinga\Exception\ProgrammingError + */ public function disablenotificationsAction() { $form = new Confirmation(); @@ -246,11 +288,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Notifications for this object will be disabled.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enablenotifications + * @throws Icinga\Exception\ProgrammingError + */ public function enablenotificationsAction() { $form = new Confirmation(); @@ -259,32 +305,62 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Notifications for this object will be enabled.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command sendcustomnotification + * @throws Icinga\Exception\ProgrammingError + */ public function sendcustomnotificationAction() { $form = new CustomNotification(); $form->setRequest($this->getRequest()); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command scheduledowntime + * @throws Icinga\Exception\ProgrammingError + */ public function scheduledowntimeAction() { + $form = new ScheduleDowntime(); + $form->setRequest($this->getRequest()); + $form->setWithChildren(false); + $this->view->form = $form; + if ($form->isValid(null) && $this->getRequest()->isPost()) { + throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); + } } + /** + * Handle command scheduledowntimeswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function scheduledowntimeswithchildrenAction() { + $form = new ScheduleDowntime(); + $form->setRequest($this->getRequest()); + $form->setWithChildren(true); + $this->view->form = $form; + if ($form->isValid(null) && $this->getRequest()->isPost()) { + throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); + } } + /** + * Handle command removedowntimeswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function removedowntimeswithchildrenAction() { $form = new Confirmation(); @@ -293,11 +369,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Remove downtime(s) from this host and its services.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command disablenotificationswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function disablenotificationswithchildrenAction() { $form = new Confirmation(); @@ -306,11 +386,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Notifications for this host and its services will be disabled.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enablenotificationswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function enablenotificationswithchildrenAction() { $form = new Confirmation(); @@ -319,25 +403,33 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Notifications for this host and its services will be enabled.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command reschedulenextcheckwithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function reschedulenextcheckwithchildrenAction() { $form = new RescheduleNextCheck(); $form->setRequest($this->getRequest()); - $form->setWithChildred(true); + $form->setWithChildren(true); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command disableactivecheckswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function disableactivecheckswithchildrenAction() { $form = new Confirmation(); @@ -346,11 +438,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Disable active checks for this host and its services.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enableactivecheckswithchildren + * @throws Icinga\Exception\ProgrammingError + */ public function enableactivecheckswithchildrenAction() { $form = new Confirmation(); @@ -359,11 +455,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Enable active checks for this host and its services.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command disableeventhandler + * @throws Icinga\Exception\ProgrammingError + */ public function disableeventhandlerAction() { $form = new Confirmation(); @@ -372,11 +472,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Disable event handler for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enableeventhandler + * @throws Icinga\Exception\ProgrammingError + */ public function enableeventhandlerAction() { $form = new Confirmation(); @@ -385,11 +489,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Enable event handler for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command disableflapdetection + * @throws Icinga\Exception\ProgrammingError + */ public function disableflapdetectionAction() { $form = new Confirmation(); @@ -398,11 +506,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Disable flapping detection for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command enableflapdetection + * @throws Icinga\Exception\ProgrammingError + */ public function enableflapdetectionAction() { $form = new Confirmation(); @@ -411,11 +523,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Enable flapping detection for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command addcomment + * @throws Icinga\Exception\ProgrammingError + */ public function addcommentAction() { $form = new CommentForm(); @@ -423,11 +539,15 @@ class Monitoring_CommandController extends ModuleActionController $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command resetattributes + * @throws Icinga\Exception\ProgrammingError + */ public function resetattributesAction() { $form = new Confirmation(); @@ -436,11 +556,15 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Reset modified attributes to its default.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command acknowledgeproblem + * @throws Icinga\Exception\ProgrammingError + */ public function acknowledgeproblemAction() { $form = new Acknowledge(); @@ -448,11 +572,15 @@ class Monitoring_CommandController extends ModuleActionController $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command removeacknowledgement + * @throws Icinga\Exception\ProgrammingError + */ public function removeacknowledgementAction() { $form = new Confirmation(); @@ -461,18 +589,45 @@ class Monitoring_CommandController extends ModuleActionController $form->addNote(t('Remove problem acknowledgement for this object.')); $this->view->form = $form; - if ($form->isValid($this->getRequest()) && $this->getRequest()->isPost()) { + if ($form->isValid(null) && $this->getRequest()->isPost()) { throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); } } + /** + * Handle command delaynotification + * @throws Icinga\Exception\ProgrammingError + */ public function delaynotificationAction() { + $form = new DelayNotification(); + $form->setRequest($this->getRequest()); + $this->view->form = $form; + + if ($form->isValid(null) && $this->getRequest()->isPost()) { + throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); + } } + /** + * Handle command removedowntime + * @throws Icinga\Exception\ProgrammingError + */ public function removedowntimeAction() { - // DOWNTIME ID + $form = new ConfirmationWithIdentifier(); + $form->setRequest($this->getRequest()); + + $form->setSubmitLabel(t('Delete downtime')); + $form->setFieldName('downtimeid'); + $form->setFieldLabel(t('Downtime id')); + $form->addNote(t('Delete a single downtime with the id shown above')); + + $this->view->form = $form; + + if ($form->isValid(null) && $this->getRequest()->isPost()) { + throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); + } } } diff --git a/modules/monitoring/application/forms/Command/AbstractCommand.php b/modules/monitoring/application/forms/Command/AbstractCommand.php index d00354b0c..6b7549e06 100644 --- a/modules/monitoring/application/forms/Command/AbstractCommand.php +++ b/modules/monitoring/application/forms/Command/AbstractCommand.php @@ -30,7 +30,7 @@ namespace Monitoring\Form\Command; use Icinga\Web\Form; use Icinga\Web\Form\Element\Note; -use Zend_Form_Element_Hidden; +use \Zend_Form_Element_Hidden; /** * Class AbstractCommand @@ -123,6 +123,18 @@ abstract class AbstractCommand extends Form return $this->notes; } + /** + * Create an instance name containing hidden field + * @return Zend_Form_Element_Hidden + */ + private function createInstanceHiddenField() + { + $field = new Zend_Form_Element_Hidden('instance'); + $value = $this->getRequest()->getParam($field->getName()); + $field->setValue($value); + return $field; + } + /** * Add elements to this form (used by extending classes) */ @@ -161,6 +173,8 @@ abstract class AbstractCommand extends Form ); $this->addElement($submitButton); } + + $this->addElement($this->createInstanceHiddenField()); } /** @@ -199,4 +213,4 @@ abstract class AbstractCommand extends Form return $authorField; } -} \ No newline at end of file +} diff --git a/modules/monitoring/application/forms/Command/Acknowledge.php b/modules/monitoring/application/forms/Command/Acknowledge.php index ecf4c2315..7e6863366 100644 --- a/modules/monitoring/application/forms/Command/Acknowledge.php +++ b/modules/monitoring/application/forms/Command/Acknowledge.php @@ -28,6 +28,11 @@ namespace Monitoring\Form\Command; +use Icinga\Web\Form\Element\DateTime; +use \DateTime as PhpDateTime; +use \DateInterval; +use Icinga\Web\Form\Element\Note; + /** * Form for acknowledge commands */ @@ -59,6 +64,48 @@ class Acknowledge extends AbstractCommand ) ); + $expireCheck = $this->createElement( + 'checkbox', + 'expire', + array( + 'label' => t('Use expire time'), + 'value' => false + ) + ); + + $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('Y-m-d H:i:s') + ) + ); + + $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->addDisplayGroup( + array( + 'expirenote', + 'expire', + 'expiretime' + ), + 'expire_group', + array( + 'legend' => t('Expire acknowledgement') + ) + ); + $this->addElement( 'checkbox', 'sticky', @@ -81,5 +128,4 @@ class Acknowledge extends AbstractCommand parent::create(); } - -} \ No newline at end of file +} diff --git a/modules/monitoring/application/forms/Command/Comment.php b/modules/monitoring/application/forms/Command/Comment.php index d0a83f771..7c265582d 100644 --- a/modules/monitoring/application/forms/Command/Comment.php +++ b/modules/monitoring/application/forms/Command/Comment.php @@ -60,4 +60,4 @@ class Comment extends AbstractCommand parent::create(); } -} \ No newline at end of file +} diff --git a/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php b/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php new file mode 100644 index 000000000..94b5e5c75 --- /dev/null +++ b/modules/monitoring/application/forms/Command/ConfirmationWithIdentifier.php @@ -0,0 +1,127 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Form\Command; + +use \Zend_Form_Element_Hidden; + +/** + * Form to handle confirmations with a single value processed + */ +class ConfirmationWithIdentifier extends Confirmation +{ + /** + * Identifier for data field + * @var string + */ + private $fieldName = 'objectid'; + + /** + * Label for the field + * + * Human readable sting, must be translated before. + * + * @var string + */ + private $fieldLabel; + + /** + * Setter for field label + * @param string $fieldLabel + */ + public function setFieldLabel($fieldLabel) + { + $this->fieldLabel = $fieldLabel; + } + + /** + * Getter for field label + * @return string + */ + public function getFieldLabel() + { + return $this->fieldLabel; + } + + /** + * Setter for field name + * @param string $fieldName + */ + public function setFieldName($fieldName) + { + $this->fieldName = $fieldName; + } + + /** + * Getter for field name + * @return string + */ + public function getFieldName() + { + return $this->fieldName; + } + + /** + * Create corresponding field for object configuration + * @return Zend_Form_Element_Hidden + */ + private function createObjectField() + { + $value = $this->getRequest()->getParam($this->getFieldName()); + $fieldLabel = $this->getFieldLabel(); + + $hiddenField = new Zend_Form_Element_Hidden($this->getFieldName()); + $hiddenField->removeDecorator('Label'); + + $hiddenField->addDecorator( + 'Callback', + array( + 'callback' => function () use ($value, $fieldLabel) { + return sprintf( + '%s %s "%s"', + $fieldLabel, + t('is'), + (isset($value)) ? $value : t('unset') + ); + } + ) + ); + + return $hiddenField; + } + + /** + * Interface method to build the form + * @see Form::create() + */ + protected function create() + { + $this->addElement($this->createObjectField()); + parent::create(); + } +} diff --git a/modules/monitoring/application/forms/Command/DelayNotification.php b/modules/monitoring/application/forms/Command/DelayNotification.php new file mode 100644 index 000000000..5af1ccc13 --- /dev/null +++ b/modules/monitoring/application/forms/Command/DelayNotification.php @@ -0,0 +1,58 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Form\Command; + +/** + * Form to handle DelayNotification command + */ +class DelayNotification extends AbstractCommand +{ + /** + * Interface method to build the form + * @see Form::create() + */ + protected function create() + { + $this->addElement( + 'text', + 'minutes', + array( + 'label' => t('Notification delay'), + 'style' => 'width: 80px;', + 'value' => 0 + ) + ); + + $this->addNote('Delay next notification in minutes from now'); + + $this->setSubmitLabel(t('Delay notification')); + + parent::create(); + } +} diff --git a/modules/monitoring/application/forms/Command/RescheduleNextCheck.php b/modules/monitoring/application/forms/Command/RescheduleNextCheck.php index be864cb6f..fbc48f1b1 100644 --- a/modules/monitoring/application/forms/Command/RescheduleNextCheck.php +++ b/modules/monitoring/application/forms/Command/RescheduleNextCheck.php @@ -35,29 +35,8 @@ use DateTime as PhpDateTime; /** * Form for RescheduleNextCheck */ -class RescheduleNextCheck extends AbstractCommand +class RescheduleNextCheck extends WithChildrenCommand { - - private $withChildren = false; - - /** - * Setter for withChildren - * @param bool $flag - */ - public function setWithChildred($flag = true) - { - $this->withChildren = $flag; - } - - /** - * Getter for withChildren - * @return bool - */ - public function getWithChildren() - { - return $this->withChildren; - } - /** * Interface method to build the form * @see Form::create() diff --git a/modules/monitoring/application/forms/Command/ScheduleDowntime.php b/modules/monitoring/application/forms/Command/ScheduleDowntime.php new file mode 100644 index 000000000..0bf006de6 --- /dev/null +++ b/modules/monitoring/application/forms/Command/ScheduleDowntime.php @@ -0,0 +1,202 @@ + + * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 + * @author Icinga Development Team + */ +// {{{ICINGA_LICENSE_HEADER}}} + +namespace Monitoring\Form\Command; + +use Icinga\Web\Form\Element\DateTime; +use \DateTime as PhpDateTime; +use \DateInterval; +use \Zend_Form_Element_Text; + +/** + * Form for any ScheduleDowntime command + */ +class ScheduleDowntime extends WithChildrenCommand +{ + /** + * Default endtime interval definition + * @see http://php.net/manual/de/class.dateinterval.php + */ + const DEFAULT_ENDTIME_INTERVAL = 'PT1H'; + + /** + * Default time format + * TODO(mh): Should be configurable on a central place (#4424) + */ + const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; + + /** + * Type constant for fixed downtimes + */ + const TYPE_FIXED = 'fixed'; + + /** + * Type constant for flexible downtimes + */ + const TYPE_FLEXIBLE = 'flexible'; + + /** + * Build an array of timestamps + * @return string[] + */ + private function generateDefaultTimestamps() + { + $out = array(); + + $dateTimeObject = new PhpDateTime(); + $out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); + + $dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL); + $dateTimeObject->add($dateInterval); + $out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); + + return $out; + } + + /** + * Generate translated multi options based on type constants + * @return array + */ + private function getDowntimeTypeOptions() + { + return array( + self::TYPE_FIXED => t('Fixed'), + self::TYPE_FLEXIBLE => t('Flexible') + ); + } + + /** + * Interface method to build the form + * @see Form::create() + */ + protected function create() + { + $this->addElement($this->createAuthorField()); + + $this->addElement( + 'textarea', + 'comment', + array( + 'label' => t('Comment'), + 'rows' => 4 + ) + ); + + $this->addElement( + 'text', + 'triggered', + array( + 'label' => t('Triggered by') + ) + ); + + list($timestampStart, $timestampEnd) = $this->generateDefaultTimestamps(); + + $dateTimeStart = new DateTime( + array( + 'name' => 'starttime', + 'label' => t('Start time'), + 'value' => $timestampStart + ) + ); + + $dateTimeEnd = new DateTime( + array( + 'name' => 'endtime', + 'label' => t('End time'), + 'value' => $timestampEnd + ) + ); + + $this->addElements(array($dateTimeStart, $dateTimeEnd)); + + $this->addElement( + 'select', + 'type', + array( + 'label' => t('Downtime type'), + 'multiOptions' => $this->getDowntimeTypeOptions() + ) + ); + + $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'); + } + ) + ); + + $this->addElements(array($hoursText, $minutesText)); + + if ($this->getWithChildren() === true) { + $this->addNote(t('Schedule downtime for host and its services.')); + } else { + + $this->addElement( + 'select', + 'childobjects', + array( + 'label' => t('Child objects'), + 'multiOptions' => array( + 0 => t('Do nothing with child objects'), + 1 => t('Schedule triggered downtime for all child objects'), + 2 => t('Schedule non-triggered downtime for all child objects') + ) + ) + ); + + $this->addNote(t('Schedule downtime for this object.')); + } + + $this->setSubmitLabel(t('Schedule downtime')); + + parent::create(); + } +} diff --git a/modules/monitoring/application/forms/TestForm.php b/modules/monitoring/application/forms/Command/WithChildrenCommand.php similarity index 65% rename from modules/monitoring/application/forms/TestForm.php rename to modules/monitoring/application/forms/Command/WithChildrenCommand.php index c68784240..2b9db6f8e 100644 --- a/modules/monitoring/application/forms/TestForm.php +++ b/modules/monitoring/application/forms/Command/WithChildrenCommand.php @@ -2,32 +2,58 @@ // {{{ICINGA_LICENSE_HEADER}}} /** * This file is part of Icinga 2 Web. - * + * * Icinga 2 Web - Head for multiple monitoring backends. * Copyright (C) 2013 Icinga Development Team - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * + * * @copyright 2013 Icinga Development Team * @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2 * @author Icinga Development Team */ // {{{ICINGA_LICENSE_HEADER}}} -namespace Monitoring\Form; +namespace Monitoring\Form\Command; -class TestForm +/** + * Class handle specific command flags + */ +abstract class WithChildrenCommand extends AbstractCommand { + /** + * Flag if we handle child objects as well + * @var bool + */ + private $withChildren = false; + + /** + * Setter for withChildren + * @param bool $flag + */ + public function setWithChildren($flag = true) + { + $this->withChildren = $flag; + } + + /** + * Getter for withChildren + * @return bool + */ + public function getWithChildren() + { + return $this->withChildren; + } }