From d316b31cbb5358df479a7370b6152196909a4294 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Thu, 30 Apr 2015 16:50:55 +0200 Subject: [PATCH] Fix downtime removal command in detail view Use removal command for single downtimes in detail view. Move removing single and multiple downtimees into seperate forms. refs #8902 --- .../controllers/DowntimeController.php | 18 ++-- .../controllers/DowntimesController.php | 4 +- .../controllers/ListController.php | 4 +- .../Object/DeleteDowntimeCommandForm.php | 87 ++++++++++-------- .../Object/DeleteDowntimeQuickCommandForm.php | 36 -------- .../Object/DeleteDowntimesCommandForm.php | 89 +++++++++++++++++++ .../Controller/MonitoredObjectController.php | 4 +- 7 files changed, 155 insertions(+), 87 deletions(-) delete mode 100644 modules/monitoring/application/forms/Command/Object/DeleteDowntimeQuickCommandForm.php create mode 100644 modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php diff --git a/modules/monitoring/application/controllers/DowntimeController.php b/modules/monitoring/application/controllers/DowntimeController.php index 086e39f10d..7a769b4d64 100644 --- a/modules/monitoring/application/controllers/DowntimeController.php +++ b/modules/monitoring/application/controllers/DowntimeController.php @@ -4,7 +4,7 @@ use Icinga\Module\Monitoring\Controller; use Icinga\Module\Monitoring\Object\Service; use Icinga\Module\Monitoring\Object\Host; -use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeQuickCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm; use Icinga\Web\Url; use Icinga\Web\Widget\Tabextension\DashboardAction; @@ -100,11 +100,17 @@ private function createDelDowntimeForm() { $this->assertPermission('monitoring/command/downtime/delete'); - $delDowntimeForm = new DeleteDowntimeQuickCommandForm(); - $delDowntimeForm->setDowntimes(array($this->downtime)) - ->populate( - array('redirect' => Url::fromPath('monitoring/list/downtimes')) - ); + $delDowntimeForm = new DeleteDowntimeCommandForm(); + $delDowntimeForm->setAction( + Url::fromPath('monitoring/downtime/show') + ->setParam('downtime_id', $this->downtime->id) + ); + $delDowntimeForm->populate( + array( + 'redirect' => Url::fromPath('monitoring/list/downtimes'), + 'downtime_id' => $this->downtime->id + ) + ); $delDowntimeForm->handleRequest(); return $delDowntimeForm; } diff --git a/modules/monitoring/application/controllers/DowntimesController.php b/modules/monitoring/application/controllers/DowntimesController.php index 608e3c9fc3..28ba76cb40 100644 --- a/modules/monitoring/application/controllers/DowntimesController.php +++ b/modules/monitoring/application/controllers/DowntimesController.php @@ -5,7 +5,7 @@ use Icinga\Module\Monitoring\Controller; use Icinga\Module\Monitoring\Object\Service; use Icinga\Module\Monitoring\Object\Host; -use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimesCommandForm; use Icinga\Web\Url; use Icinga\Web\Widget\Tabextension\DashboardAction; @@ -107,7 +107,7 @@ public function removeAllAction() $this->view->downtimes = $this->downtimes; $this->view->listAllLink = Url::fromPath('monitoring/list/downtimes') ->setQueryString($this->filter->toQueryString()); - $delDowntimeForm = new DeleteDowntimeCommandForm(); + $delDowntimeForm = new DeleteDowntimesCommandForm(); $delDowntimeForm->setTitle($this->view->translate('Remove all Downtimes')); $delDowntimeForm->addDescription(sprintf( $this->translate('Confirm removal of %d downtimes.'), diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index b0b0d9f2ea..e6147c31bc 100644 --- a/modules/monitoring/application/controllers/ListController.php +++ b/modules/monitoring/application/controllers/ListController.php @@ -4,7 +4,7 @@ use Icinga\Module\Monitoring\Controller; use Icinga\Module\Monitoring\Backend; use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm; -use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeQuickCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeCommandForm; use Icinga\Web\Url; use Icinga\Web\Widget\Tabextension\DashboardAction; use Icinga\Web\Widget\Tabextension\OutputFormat; @@ -294,7 +294,7 @@ public function downtimesAction() )); if ($this->Auth()->hasPermission('monitoring/command/downtime/delete')) { - $this->view->delDowntimeForm = new DeleteDowntimeQuickCommandForm(); + $this->view->delDowntimeForm = new DeleteDowntimeCommandForm(); } } diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php index 72728cd5a3..8862c2b530 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php @@ -12,8 +12,6 @@ */ class DeleteDowntimeCommandForm extends CommandForm { - protected $downtimes; - /** * (non-PHPDoc) * @see \Zend_Form::init() For the method documentation. @@ -22,50 +20,76 @@ public function init() { $this->setAttrib('class', 'inline'); } - - /** + + /** * (non-PHPDoc) * @see \Icinga\Web\Form::createElements() For the method documentation. */ public function createElements(array $formData = array()) { - $this->addElements(array( + $this->addElements( array( - 'hidden', - 'redirect', array( - 'decorators' => array('ViewHelper') + 'hidden', + 'downtime_id', + array( + 'decorators' => array('ViewHelper') + ) + ), + array( + 'hidden', + 'redirect', + array( + 'decorators' => array('ViewHelper') + ) ) ) - )); + ); return $this; } - + /** * (non-PHPDoc) - * @see \Icinga\Web\Form::getSubmitLabel() For the method documentation. + * @see \Icinga\Web\Form::addSubmitButton() For the method documentation. */ - public function getSubmitLabel() + public function addSubmitButton() { - return $this->translatePlural('Remove', 'Remove All', count($this->downtimes)); + $this->addElement( + 'button', + 'btn_submit', + array( + 'ignore' => true, + 'escape' => false, + 'type' => 'submit', + 'class' => 'link-like', + 'label' => $this->getView()->icon('trash'), + 'title' => $this->translate('Delete this downtime'), + 'decorators' => array('ViewHelper') + ) + ); + return $this; } - + /** * (non-PHPDoc) - * @see \Icinga\Web\Form::onSuccess() For the method documentation. + * @see \Icinga\Web\Form::onSuccess() For the method documentation. */ public function onSuccess() { - foreach ($this->downtimes as $downtime) { - $delDowntime = new DeleteDowntimeCommand(); - $delDowntime->setDowntimeId($downtime->id); - $delDowntime->setDowntimeType( - isset($downtime->service_description) ? - DeleteDowntimeCommand::DOWNTIME_TYPE_SERVICE : - DeleteDowntimeCommand::DOWNTIME_TYPE_HOST - ); - $this->getTransport($this->request)->send($delDowntime); - } + $id = $this->getElement('downtime_id')->getValue(); + + // Presence of downtime id, only delete this specific downtime + $firstDowntime = $this->downtimes[0]; + + $delDowntime = new DeleteDowntimeCommand(); + $delDowntime->setDowntimeId($id); + $delDowntime->setDowntimeType( + isset($firstDowntime->service_description) ? + DeleteDowntimeCommand::DOWNTIME_TYPE_SERVICE : + DeleteDowntimeCommand::DOWNTIME_TYPE_HOST + ); + $this->getTransport($this->request)->send($delDowntime); + $redirect = $this->getElement('redirect')->getValue(); if (! empty($redirect)) { $this->setRedirectUrl($redirect); @@ -73,17 +97,4 @@ public function onSuccess() Notification::success($this->translate('Deleting downtime.')); return true; } - - /** - * Set the downtimes to be deleted upon success - * - * @param type $downtimes - * - * @return $this - */ - public function setDowntimes($downtimes) - { - $this->downtimes = $downtimes; - return $this; - } } diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeQuickCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeQuickCommandForm.php deleted file mode 100644 index adbfcae43d..0000000000 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeQuickCommandForm.php +++ /dev/null @@ -1,36 +0,0 @@ -addElement( - 'button', - 'btn_submit', - array( - 'ignore' => true, - 'escape' => false, - 'type' => 'submit', - 'class' => 'link-like', - 'label' => $this->getView()->icon('trash'), - 'title' => $this->translate('Delete this downtime'), - 'decorators' => array('ViewHelper') - ) - ); - return $this; - } -} diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php new file mode 100644 index 0000000000..f460f3601c --- /dev/null +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimesCommandForm.php @@ -0,0 +1,89 @@ +setAttrib('class', 'inline'); + } + + /** + * (non-PHPDoc) + * @see \Icinga\Web\Form::createElements() For the method documentation. + */ + public function createElements(array $formData = array()) + { + $this->addElements(array( + array( + 'hidden', + 'redirect', + array( + 'decorators' => array('ViewHelper') + ) + ) + )); + return $this; + } + + /** + * (non-PHPDoc) + * @see \Icinga\Web\Form::getSubmitLabel() For the method documentation. + */ + public function getSubmitLabel() + { + return $this->translatePlural('Remove', 'Remove All', count($this->downtimes)); + } + + /** + * (non-PHPDoc) + * @see \Icinga\Web\Form::onSuccess() For the method documentation. + */ + public function onSuccess() + { + foreach ($this->downtimes as $downtime) { + $delDowntime = new DeleteDowntimeCommand(); + $delDowntime->setDowntimeId($downtime->id); + $delDowntime->setDowntimeType( + isset($downtime->service_description) ? + DeleteDowntimeCommand::DOWNTIME_TYPE_SERVICE : + DeleteDowntimeCommand::DOWNTIME_TYPE_HOST + ); + $this->getTransport($this->request)->send($delDowntime); + } + $redirect = $this->getElement('redirect')->getValue(); + if (! empty($redirect)) { + $this->setRedirectUrl($redirect); + } + Notification::success($this->translate('Deleting downtime.')); + return true; + } + + /** + * Set the downtimes to be deleted upon success + * + * @param type $downtimes + * + * @return $this + */ + public function setDowntimes($downtimes) + { + $this->downtimes = $downtimes; + return $this; + } +} diff --git a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php index 42060d2f03..94d9f6399e 100644 --- a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php +++ b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php @@ -89,9 +89,7 @@ public function showAction() } if (! empty($this->object->downtimes) && $auth->hasPermission('monitoring/command/downtime/delete')) { $delDowntimeForm = new DeleteDowntimeCommandForm(); - $delDowntimeForm - ->setDowntimes($this->object->downtimes) - ->handleRequest(); + $delDowntimeForm->handleRequest(); $this->view->delDowntimeForm = $delDowntimeForm; } $this->view->object = $this->object;