Skip to content

Commit

Permalink
Move removal command actions into the downtime controllers
Browse files Browse the repository at this point in the history
refs #8902
  • Loading branch information
majentsch committed May 7, 2015
1 parent b186ce2 commit 6ddb004
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 102 deletions.
33 changes: 11 additions & 22 deletions modules/monitoring/application/controllers/DowntimeController.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
41 changes: 15 additions & 26 deletions modules/monitoring/application/controllers/DowntimesController.php
Expand Up @@ -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;
}
}
}
4 changes: 2 additions & 2 deletions modules/monitoring/application/controllers/ListController.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down
Expand Up @@ -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.
Expand All @@ -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',
Expand All @@ -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;
}
}
@@ -0,0 +1,36 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */

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 DeleteDowntimeQuickCommandForm extends DeleteDowntimeCommandForm
{
/**
* (non-PHPDoc)
* @see \Icinga\Web\Form::addSubmitButton() For the method documentation.
*/
public function addSubmitButton()
{
$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;
}
}
Expand Up @@ -3,26 +3,54 @@

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
*
* @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
Expand Down
Expand Up @@ -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';
Expand Down
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 6ddb004

Please sign in to comment.