diff --git a/modules/monitoring/application/controllers/DowntimeController.php b/modules/monitoring/application/controllers/DowntimeController.php index 26a88ae3bc..086e39f10d 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\DeleteDowntimeCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeQuickCommandForm; use Icinga\Web\Url; use Icinga\Web\Widget\Tabextension\DashboardAction; @@ -85,38 +85,27 @@ public function showAction() $this->view->stateName = isset($this->downtime->service_description) ? Service::getStateText($this->downtime->service_state) : Host::getStateText($this->downtime->host_state); - $this->view->delDowntimeForm = $this->createDelDowntimeForm(); $this->view->listAllLink = Url::fromPath('monitoring/list/downtimes'); $this->view->showHostLink = Url::fromPath('monitoring/host/show') ->setParam('host', $this->downtime->host); $this->view->showServiceLink = Url::fromPath('monitoring/service/show') ->setParam('host', $this->downtime->host) ->setParam('service', $this->downtime->service_description); + if ($this->hasPermission('monitoring/command/downtime/delete')) { + $this->view->delDowntimeForm = $this->createDelDowntimeForm(); + } } private function createDelDowntimeForm() { - $delDowntimeForm = new DeleteDowntimeCommandForm(); - $delDowntimeForm->setObjects($this->downtime); - $delDowntimeForm->populate( - array( - 'downtime_id' => $this->downtime->id, - 'redirect' => Url::fromPath('monitoring/list/downtimes') - ) - ); - if (! $this->isService) { - $delDowntimeForm->setAction( - $this->view->url('monitoring/host/delete-downtime', - array('host' => $this->downtime->host_name)) - ); - } else { - $delDowntimeForm->setAction( - $this->view->url('monitoring/service/delete-downtime', array( - 'host' => $this->downtime->host_name, - 'service' => $this->downtime->service_description - )) + $this->assertPermission('monitoring/command/downtime/delete'); + + $delDowntimeForm = new DeleteDowntimeQuickCommandForm(); + $delDowntimeForm->setDowntimes(array($this->downtime)) + ->populate( + array('redirect' => Url::fromPath('monitoring/list/downtimes')) ); - } + $delDowntimeForm->handleRequest(); return $delDowntimeForm; } } diff --git a/modules/monitoring/application/controllers/DowntimesController.php b/modules/monitoring/application/controllers/DowntimesController.php index fe4e9f5bb5..608e3c9fc3 100644 --- a/modules/monitoring/application/controllers/DowntimesController.php +++ b/modules/monitoring/application/controllers/DowntimesController.php @@ -94,39 +94,28 @@ public function init() public function showAction() { - if (false === $this->downtimes) { - return; - } $this->view->downtimes = $this->downtimes; $this->view->listAllLink = Url::fromPath('monitoring/list/downtimes') ->setQueryString($this->filter->toQueryString()); - $this->view->removeAllLink = Url::fromPath('monitoring/downtimes/removeAll') + $this->view->removeAllLink = Url::fromPath('monitoring/downtimes/remove-all') ->setParams($this->params); } public function removeAllAction() { + $this->assertPermission('monitoring/command/downtime/delete'); + $this->view->downtimes = $this->downtimes; + $this->view->listAllLink = Url::fromPath('monitoring/list/downtimes') + ->setQueryString($this->filter->toQueryString()); $delDowntimeForm = new DeleteDowntimeCommandForm(); - $delDowntimeForm->setObjects($this->downtimes); - $delDowntimeForm->populate( - array( - 'downtime_id' => $this->downtime->id, - 'redirect' => Url::fromPath('monitoring/list/downtimes') - ) - ); - if (! $this->isService) { - $delDowntimeForm->setAction( - $this->view->url('monitoring/host/delete-downtime', - array('host' => $this->downtime->host_name)) - ); - } else { - $delDowntimeForm->setAction( - $this->view->url('monitoring/service/delete-downtime', array( - 'host' => $this->downtime->host_name, - 'service' => $this->downtime->service_description - )) - ); - } - return $delDowntimeForm; + $delDowntimeForm->setTitle($this->view->translate('Remove all Downtimes')); + $delDowntimeForm->addDescription(sprintf( + $this->translate('Confirm removal of %d downtimes.'), + count($this->downtimes) + )); + $delDowntimeForm->setDowntimes($this->downtimes) + ->setRedirectUrl(Url::fromPath('monitoring/list/downtimes')) + ->handleRequest(); + $this->view->delDowntimeForm = $delDowntimeForm; } -} +} \ No newline at end of file diff --git a/modules/monitoring/application/controllers/ListController.php b/modules/monitoring/application/controllers/ListController.php index e6147c31bc..b0b0d9f2ea 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\DeleteDowntimeCommandForm; +use Icinga\Module\Monitoring\Forms\Command\Object\DeleteDowntimeQuickCommandForm; 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 DeleteDowntimeCommandForm(); + $this->view->delDowntimeForm = new DeleteDowntimeQuickCommandForm(); } } diff --git a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php index d240251cf7..72728cd5a3 100644 --- a/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeCommandForm.php @@ -4,13 +4,16 @@ namespace Icinga\Module\Monitoring\Forms\Command\Object; use Icinga\Module\Monitoring\Command\Object\DeleteDowntimeCommand; +use \Icinga\Module\Monitoring\Forms\Command\CommandForm; use Icinga\Web\Notification; /** * Form for deleting host or service downtimes */ -class DeleteDowntimeCommandForm extends ObjectsCommandForm +class DeleteDowntimeCommandForm extends CommandForm { + protected $downtimes; + /** * (non-PHPDoc) * @see \Zend_Form::init() For the method documentation. @@ -27,14 +30,6 @@ public function init() public function createElements(array $formData = array()) { $this->addElements(array( - array( - 'hidden', - 'downtime_id', - array( - 'required' => true, - 'decorators' => array('ViewHelper') - ) - ), array( 'hidden', 'redirect', @@ -45,48 +40,50 @@ public function createElements(array $formData = array()) )); return $this; } - + /** * (non-PHPDoc) - * @see \Icinga\Web\Form::addSubmitButton() For the method documentation. + * @see \Icinga\Web\Form::getSubmitLabel() For the method documentation. */ - public function addSubmitButton() + public function getSubmitLabel() { - $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; + 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->objects as $object) { - /** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */ + foreach ($this->downtimes as $downtime) { $delDowntime = new DeleteDowntimeCommand(); - $delDowntime - ->setObject($object) - ->setDowntimeId($this->getElement('downtime_id')->getValue()); + $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..')); + 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 new file mode 100644 index 0000000000..adbfcae43d --- /dev/null +++ b/modules/monitoring/application/forms/Command/Object/DeleteDowntimeQuickCommandForm.php @@ -0,0 +1,36 @@ +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/library/Monitoring/Command/Object/DeleteDowntimeCommand.php b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php index 52ad99e1be..96dc8f7556 100644 --- a/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php +++ b/modules/monitoring/library/Monitoring/Command/Object/DeleteDowntimeCommand.php @@ -3,19 +3,22 @@ namespace Icinga\Module\Monitoring\Command\Object; +use Icinga\Module\Monitoring\Command\IcingaCommand; + /** * Delete a host or service downtime */ -class DeleteDowntimeCommand extends ObjectCommand +class DeleteDowntimeCommand extends IcingaCommand { /** - * (non-PHPDoc) - * @see \Icinga\Module\Monitoring\Command\Object\ObjectCommand::$allowedObjects For the property documentation. + * Downtime for a host + */ + const DOWNTIME_TYPE_HOST = 'host'; + + /** + * Downtime for a service */ - protected $allowedObjects = array( - self::TYPE_HOST, - self::TYPE_SERVICE - ); + const DOWNTIME_TYPE_SERVICE = 'service'; /** * ID of the downtime that is to be deleted @@ -23,6 +26,31 @@ class DeleteDowntimeCommand extends ObjectCommand * @var int */ protected $downtimeId; + + /** + * + * @var type + */ + protected $downtimeType = self::DOWNTIME_TYPE_HOST; + + /** + * Set the downtime type, either host or service + * + * @param string $type the downtime type + */ + public function setDowntimeType($type) + { + $this->downtimeType = $type; + } + + /** + * + * @return type + */ + public function getDowntimeType() + { + return $this->downtimeType; + } /** * Set the ID of the downtime that is to be deleted diff --git a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php index cf88d7d2e9..83fbd4364d 100644 --- a/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php +++ b/modules/monitoring/library/Monitoring/Command/Renderer/IcingaCommandFileCommandRenderer.php @@ -337,7 +337,7 @@ public function renderDeleteComment(DeleteCommentCommand $command) public function renderDeleteDowntime(DeleteDowntimeCommand $command) { - if ($command->getObject()->getType() === $command::TYPE_HOST) { + if ($command->getDowntimeType() === 'host') { $commandString = 'DEL_HOST_DOWNTIME'; } else { $commandString = 'DEL_SVC_DOWNTIME'; diff --git a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php index 04102e9d2a..42060d2f03 100644 --- a/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php +++ b/modules/monitoring/library/Monitoring/Web/Controller/MonitoredObjectController.php @@ -90,7 +90,7 @@ public function showAction() if (! empty($this->object->downtimes) && $auth->hasPermission('monitoring/command/downtime/delete')) { $delDowntimeForm = new DeleteDowntimeCommandForm(); $delDowntimeForm - ->setObjects($this->object) + ->setDowntimes($this->object->downtimes) ->handleRequest(); $this->view->delDowntimeForm = $delDowntimeForm; } @@ -147,16 +147,6 @@ public function deleteCommentAction() $this->handleCommandForm(new DeleteCommentCommandForm()); } - /** - * Delete a downtime - */ - public function deleteDowntimeAction() - { - $this->assertHttpMethod('POST'); - $this->assertPermission('monitoring/command/downtime/delete'); - $this->handleCommandForm(new DeleteDowntimeCommandForm()); - } - /** * Create tabs */