Merge branch 'bugfix/delete-multiple-downtimes-and-comments-8624'

fixes 
This commit is contained in:
Matthias Jentsch 2015-05-07 17:36:49 +02:00
commit cf8376f478
20 changed files with 264 additions and 160 deletions

View File

@ -64,25 +64,26 @@ class Monitoring_CommentController extends Controller
*/
public function showAction()
{
$listCommentsLink = Url::fromPath('monitoring/list/comments')
->setQueryString('comment_type=(comment|ack)');
$this->view->comment = $this->comment;
if ($this->hasPermission('monitoring/command/comment/delete')) {
$this->view->delCommentForm = $this->createDelCommentForm();
$this->view->delCommentForm->populate(
array(
'redirect' => $listCommentsLink,
'comment_id' => $this->comment->id,
'comment_is_service' => isset($this->comment->service_description)
)
);
}
}
/**
* Receive DeleteCommentCommandForm post from other controller
*/
public function removeAction()
{
$this->assertHttpMethod('POST');
$this->createDelCommentForm();
}
/**
* Create a command form to delete a single comment
*
* @return DeleteCommentCommandForm
* @return DeleteCommentsCommandForm
*/
private function createDelCommentForm()
{
@ -93,12 +94,6 @@ class Monitoring_CommentController extends Controller
Url::fromPath('monitoring/comment/show')
->setParam('comment_id', $this->comment->id)
);
$delCommentForm->populate(
array(
'redirect' => Url::fromPath('monitoring/list/comments'),
'comment_id' => $this->comment->id
)
);
$delCommentForm->handleRequest();
return $delCommentForm;
}

View File

@ -2,9 +2,8 @@
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentsCommandForm;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\DashboardAction;
use Icinga\Data\Filter\Filter;
/**
@ -71,28 +70,31 @@ class Monitoring_CommentsController extends Controller
$this->view->comments = $this->comments;
$this->view->listAllLink = Url::fromPath('monitoring/list/comments')
->setQueryString($this->filter->toQueryString());
$this->view->removeAllLink = Url::fromPath('monitoring/comments/remove-all')
$this->view->removeAllLink = Url::fromPath('monitoring/comments/delete-all')
->setParams($this->params);
}
/**
* Display the form for removing a comment list
*/
public function removeAllAction()
public function deleteAllAction()
{
$this->assertPermission('monitoring/command/comment/delete');
$this->view->comments = $this->comments;
$this->view->listAllLink = Url::fromPath('monitoring/list/comments')
->setQueryString($this->filter->toQueryString());
$delCommentForm = new DeleteCommentCommandForm();
$listCommentsLink = Url::fromPath('monitoring/list/comments')
->setQueryString('comment_type=(comment|ack)');
$delCommentForm = new DeleteCommentsCommandForm();
$delCommentForm->setTitle($this->view->translate('Remove all Comments'));
$delCommentForm->addDescription(sprintf(
$this->translate('Confirm removal of %d comments.'),
count($this->comments)
));
$delCommentForm->setObjects($this->comments)
->setRedirectUrl(Url::fromPath('monitoring/list/downtimes'))
->handleRequest();
$delCommentForm->setComments($this->comments)
->setRedirectUrl($listCommentsLink)
->handleRequest();
$this->view->delCommentForm = $delCommentForm;
$this->view->comments = $this->comments;
$this->view->listAllLink = Url::fromPath('monitoring/list/comments')
->setQueryString($this->filter->toQueryString());
}
}

View File

@ -5,6 +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\Command\Object\DeleteDowntimeCommand;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\DashboardAction;
@ -81,7 +82,7 @@ class Monitoring_DowntimeController extends Controller
'label' => $this->translate('Downtime'),
'url' =>'monitoring/downtimes/show'
)
)->activate('downtime')->extend(new DashboardAction());
)->activate('downtime')->extend(new DashboardAction());
}
/**
@ -91,7 +92,7 @@ class Monitoring_DowntimeController extends Controller
{
$this->view->downtime = $this->downtime;
$this->view->isService = $this->isService;
$this->view->stateName = isset($this->downtime->service_description) ?
$this->view->stateName = isset($this->downtime->service_description) ?
Service::getStateText($this->downtime->service_state) :
Host::getStateText($this->downtime->host_state);
$this->view->listAllLink = Url::fromPath('monitoring/list/downtimes');
@ -102,6 +103,13 @@ class Monitoring_DowntimeController extends Controller
->setParam('service', $this->downtime->service_description);
if ($this->hasPermission('monitoring/command/downtime/delete')) {
$this->view->delDowntimeForm = $this->createDelDowntimeForm();
$this->view->delDowntimeForm->populate(
array(
'redirect' => Url::fromPath('monitoring/list/downtimes'),
'downtime_id' => $this->downtime->id,
'downtime_is_service' => $this->isService
)
);
}
}
@ -122,18 +130,11 @@ class Monitoring_DowntimeController extends Controller
private function createDelDowntimeForm()
{
$this->assertPermission('monitoring/command/downtime/delete');
$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;
}

View File

@ -104,14 +104,14 @@ class Monitoring_DowntimesController extends Controller
$this->view->downtimes = $this->downtimes;
$this->view->listAllLink = Url::fromPath('monitoring/list/downtimes')
->setQueryString($this->filter->toQueryString());
$this->view->removeAllLink = Url::fromPath('monitoring/downtimes/remove-all')
$this->view->removeAllLink = Url::fromPath('monitoring/downtimes/delete-all')
->setParams($this->params);
}
/**
* Display the form for removing a downtime list
*/
public function removeAllAction()
public function deleteAllAction()
{
$this->assertPermission('monitoring/command/downtime/delete');
$this->view->downtimes = $this->downtimes;
@ -123,9 +123,8 @@ class Monitoring_DowntimesController extends Controller
$this->translate('Confirm removal of %d downtimes.'),
count($this->downtimes)
));
$delDowntimeForm->setDowntimes($this->downtimes)
->setRedirectUrl(Url::fromPath('monitoring/list/downtimes'))
->handleRequest();
$delDowntimeForm->setRedirectUrl(Url::fromPath('monitoring/list/downtimes'));
$delDowntimeForm->setDowntimes($this->downtimes)->handleRequest();
$this->view->delDowntimeForm = $delDowntimeForm;
}
}

View File

@ -295,6 +295,7 @@ class Monitoring_ListController extends Controller
if ($this->Auth()->hasPermission('monitoring/command/downtime/delete')) {
$this->view->delDowntimeForm = new DeleteDowntimeCommandForm();
$this->view->delDowntimeForm->handleRequest();
}
}
@ -502,6 +503,7 @@ class Monitoring_ListController extends Controller
if ($this->Auth()->hasPermission('monitoring/command/comment/delete')) {
$this->view->delCommentForm = new DeleteCommentCommandForm();
$this->view->delCommentForm->handleRequest();
}
}

View File

@ -4,12 +4,13 @@
namespace Icinga\Module\Monitoring\Forms\Command\Object;
use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand;
use Icinga\Module\Monitoring\Forms\Command\CommandForm;
use Icinga\Web\Notification;
/**
* Form for deleting host or service comments
*/
class DeleteCommentCommandForm extends ObjectsCommandForm
class DeleteCommentCommandForm extends CommandForm
{
/**
* (non-PHPDoc)
@ -26,23 +27,34 @@ class DeleteCommentCommandForm extends ObjectsCommandForm
*/
public function createElements(array $formData = array())
{
$this->addElements(array(
$this->addElements(
array(
'hidden',
'comment_id',
array(
'required' => true,
'decorators' => array('ViewHelper')
)
),
array(
'hidden',
'redirect',
'hidden',
'comment_id',
array(
'required' => true,
'validators' => array('NotEmpty'),
'decorators' => array('ViewHelper')
)
),
array(
'decorators' => array('ViewHelper')
'hidden',
'comment_is_service',
array(
'filters' => array('Boolean'),
'decorators' => array('ViewHelper')
)
),
array(
'hidden',
'redirect',
array(
'decorators' => array('ViewHelper')
)
)
)
));
);
return $this;
}
@ -74,14 +86,10 @@ class DeleteCommentCommandForm extends ObjectsCommandForm
*/
public function onSuccess()
{
foreach ($this->objects as $object) {
/** @var \Icinga\Module\Monitoring\Object\MonitoredObject $object */
$delComment = new DeleteCommentCommand();
$delComment
->setObject($object)
->setCommentId($this->getElement('comment_id')->getValue());
$this->getTransport($this->request)->send($delComment);
}
$cmd = new DeleteCommentCommand();
$cmd->setIsService($this->getElement('comment_is_service')->getValue())
->setCommentId($this->getElement('comment_id')->getValue());
$this->getTransport($this->request)->send($cmd);
$redirect = $this->getElement('redirect')->getValue();
if (! empty($redirect)) {
$this->setRedirectUrl($redirect);

View File

@ -0,0 +1,88 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Module\Monitoring\Forms\Command\Object;
use Icinga\Module\Monitoring\Command\Object\DeleteCommentCommand;
use \Icinga\Module\Monitoring\Forms\Command\CommandForm;
use Icinga\Web\Notification;
/**
* Form for deleting host or service comments
*/
class DeleteCommentsCommandForm extends CommandForm
{
/**
* The comments deleted on success
*
* @var array
*/
protected $comments;
/**
* (non-PHPDoc)
* @see \Zend_Form::init() For the method documentation.
*/
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(
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->comments as $comment) {
$cmd = new DeleteCommentCommand();
$cmd->setCommentId($comment->id)
->setIsService(isset($comment->service_description));
$this->getTransport($this->request)->send($cmd);
}
$redirect = $this->getElement('redirect')->getValue();
if (! empty($redirect)) {
$this->setRedirectUrl($redirect);
}
Notification::success($this->translate('Deleting comment..'));
return true;
}
/**
* Set the comments to be deleted upon success
*
* @param array $comments
*
* @return this fluent interface
*/
public function setComments(array $comments)
{
$this->comments = $comments;
return $this;
}
}

View File

@ -21,7 +21,7 @@ class DeleteDowntimeCommandForm extends CommandForm
$this->setAttrib('class', 'inline');
}
/**
/**
* (non-PHPDoc)
* @see \Icinga\Web\Form::createElements() For the method documentation.
*/
@ -33,6 +33,16 @@ class DeleteDowntimeCommandForm extends CommandForm
'hidden',
'downtime_id',
array(
'required' => true,
'validators' => array('NotEmpty'),
'decorators' => array('ViewHelper')
)
),
array(
'hidden',
'downtime_is_service',
array(
'filters' => array('Boolean'),
'decorators' => array('ViewHelper')
)
),
@ -76,20 +86,11 @@ class DeleteDowntimeCommandForm extends CommandForm
*/
public function onSuccess()
{
$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);
$cmd = new DeleteDowntimeCommand();
$cmd->setDowntimeId($this->getElement('downtime_id')->getValue());
$cmd->setIsService($this->getElement('downtime_is_service')->getValue());
$this->getTransport($this->request)->send($cmd);
$redirect = $this->getElement('redirect')->getValue();
if (! empty($redirect)) {
$this->setRedirectUrl($redirect);

View File

@ -38,9 +38,7 @@ class DeleteDowntimesCommandForm extends CommandForm
array(
'hidden',
'redirect',
array(
'decorators' => array('ViewHelper')
)
array('decorators' => array('ViewHelper'))
)
));
return $this;
@ -64,11 +62,7 @@ class DeleteDowntimesCommandForm extends CommandForm
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
);
$delDowntime->setIsService(isset($downtime->service_description));
$this->getTransport($this->request)->send($delDowntime);
}
$redirect = $this->getElement('redirect')->getValue();
@ -86,7 +80,7 @@ class DeleteDowntimesCommandForm extends CommandForm
*
* @return $this
*/
public function setDowntimes($downtimes)
public function setDowntimes(array $downtimes)
{
$this->downtimes = $downtimes;
return $this;

View File

@ -16,13 +16,14 @@
<?php if ($this->comment->objecttype === 'service'): ?>
<th> <?= $this->translate('Service') ?> </th>
<td>
<?= $this->icon('service', $this->translate('Service')); ?>
<?= $this->icon('service', $this->translate('Service')); ?>
<?= $this->link()->service(
$this->comment->service_description,
$this->comment->service_display_name,
$this->comment->host_name,
$this->comment->host_display_names
); ?>
$this->comment->service_description,
$this->comment->service_display_name,
$this->comment->host_name,
$this->comment->host_display_name
);
?>
</td>
<?php else: ?>
<th> <?= $this->translate('Host') ?> </th>
@ -31,7 +32,8 @@
<?= $this->link()->host(
$this->comment->host_name,
$this->comment->host_display_name
); ?>
);
?>
</td>
<?php endif ?>
</tr>
@ -55,10 +57,11 @@
<th><?= $this->translate('Expires') ?></th>
<td>
<?= $this->comment->expiration ? sprintf(
$this->translate('This comment expires on %s at %s.'),
date('d.m.y', $this->comment->expiration),
date('H:i', $this->comment->expiration)
) : $this->translate('This comment does not expire.'); ?>
$this->translate('This comment expires on %s at %s.'),
date('d.m.y', $this->comment->expiration),
date('H:i', $this->comment->expiration)
) : $this->translate('This comment does not expire.');
?>
</td>
</tr>

View File

@ -83,8 +83,12 @@ if (count($comments) === 0) {
<td style="width: 2em" data-base-target="self">
<?php
$delCommentForm = clone $delCommentForm;
$delCommentForm->populate(array('comment_id' => $comment->id, 'redirect' => $this->url));
$delCommentForm->setAction($this->url('monitoring/comment/remove', array('comment_id' => $comment->id)));
$delCommentForm->populate(
array(
'comment_id' => $comment->id,
'comment_is_service' => isset($comment->service_description)
)
);
echo $delCommentForm;
?>
</td>

View File

@ -128,8 +128,12 @@ if (count($downtimes) === 0) {
<td style="width: 2em" data-base-target="self">
<?php
$delDowntimeForm = clone $delDowntimeForm;
$delDowntimeForm->populate(array('downtime_id' => $downtime->id, 'redirect' => $this->url));
$delDowntimeForm->setAction($this->url('monitoring/downtime/remove', array('downtime_id' => $downtime->id)));
$delDowntimeForm->populate(
array(
'downtime_id' => $downtime->id,
'downtime_is_service' => isset($downtime->service_description)
)
);
echo $delDowntimeForm;
?>
</td>

View File

@ -48,7 +48,12 @@ foreach ($object->comments as $comment) {
<td data-base-target="_self">
<?php if (isset($delCommentForm)) { // Form is unset if the current user lacks the respective permission
$delCommentForm = clone $delCommentForm;
$delCommentForm->populate(array('comment_id' => $comment->id));
$delCommentForm->populate(
array(
'comment_id' => $comment->id,
'comment_is_service' => isset($comment->service_description)
)
);
echo $delCommentForm;
} ?>
<span class="sr-only">(<?= $this->translate('Comment'); ?>): </span><?= str_replace(array('\r\n', '\n'), '<br>', $commentText); ?>

View File

@ -65,7 +65,12 @@ foreach ($object->downtimes as $downtime) {
<td data-base-target="_self">
<?php if (isset($delDowntimeForm)) { // Form is unset if the current user lacks the respective permission
$delDowntimeForm = clone $delDowntimeForm;
$delDowntimeForm->populate(array('downtime_id' => $downtime->id));
$delDowntimeForm->populate(
array(
'downtime_id' => $downtime->id,
'downtime_is_service' => $object->getType() === $object::TYPE_SERVICE
)
);
echo $delDowntimeForm;
} ?>
<span class="sr-only"><?= $this->translate('Downtime'); ?></span>

View File

@ -3,20 +3,13 @@
namespace Icinga\Module\Monitoring\Command\Object;
use Icinga\Module\Monitoring\Command\IcingaCommand;
/**
* Delete a host or service comment
*/
class DeleteCommentCommand extends ObjectCommand
class DeleteCommentCommand extends IcingaCommand
{
/**
* (non-PHPDoc)
* @see \Icinga\Module\Monitoring\Command\Object\ObjectCommand::$allowedObjects For the property documentation.
*/
protected $allowedObjects = array(
self::TYPE_HOST,
self::TYPE_SERVICE
);
/**
* ID of the comment that is to be deleted
*
@ -24,6 +17,13 @@ class DeleteCommentCommand extends ObjectCommand
*/
protected $commentId;
/**
* The type of the comment, either 'host' or 'service'
*
* @var boolean
*/
protected $isService = false;
/**
* Set the ID of the comment that is to be deleted
*
@ -46,4 +46,27 @@ class DeleteCommentCommand extends ObjectCommand
{
return $this->commentId;
}
/**
* Whether the command affects a service comment
*
* @return boolean
*/
public function getIsService()
{
return $this->isService;
}
/**
* Set whether the command affects a service
*
* @param boolean $value The value, defaults to true
*
* @return this fluent interface
*/
public function setIsService($value = true)
{
$this->isService = (bool) $value;
return $this;
}
}

View File

@ -10,46 +10,38 @@ use Icinga\Module\Monitoring\Command\IcingaCommand;
*/
class DeleteDowntimeCommand extends IcingaCommand
{
/**
* Downtime for a host
*/
const DOWNTIME_TYPE_HOST = 'host';
/**
* Downtime for a service
*/
const DOWNTIME_TYPE_SERVICE = 'service';
/**
* ID of the downtime that is to be deleted
*
* @var int
*/
protected $downtimeId;
/**
* If the command affects a service downtime
*
* @var type
* @var boolean
*/
protected $downtimeType = self::DOWNTIME_TYPE_HOST;
protected $isService = false;
/**
* Set the downtime type, either host or service
* Set if this command affects a service
*
* @param string $type the downtime type
* @param type $value
*/
public function setDowntimeType($type)
public function setIsService($value = true)
{
$this->downtimeType = $type;
$this->isService = (bool) $value;
}
/**
*
* Return whether the command affects a service
*
* @return type
*/
public function getDowntimeType()
public function getIsService()
{
return $this->downtimeType;
return $this->isService;
}
/**

View File

@ -323,28 +323,18 @@ class IcingaCommandFileCommandRenderer implements IcingaCommandRendererInterface
public function renderDeleteComment(DeleteCommentCommand $command)
{
if ($command->getObject()->getType() === $command::TYPE_HOST) {
$commandString = 'DEL_HOST_COMMENT';
} else {
$commandString = 'DEL_SVC_COMMENT';
}
return sprintf(
'%s;%u',
$commandString,
$command->getIsService() ? 'DEL_SVC_COMMENT' : 'DEL_HOST_COMMENT',
$command->getCommentId()
);
}
public function renderDeleteDowntime(DeleteDowntimeCommand $command)
{
if ($command->getDowntimeType() === 'host') {
$commandString = 'DEL_HOST_DOWNTIME';
} else {
$commandString = 'DEL_SVC_DOWNTIME';
}
return sprintf(
'%s;%u',
$commandString,
$command->getIsService() ? 'DEL_SVC_DOWNTIME' : 'DEL_HOST_DOWNTIME',
$command->getDowntimeId()
);
}

View File

@ -62,7 +62,7 @@ abstract class MonitoredObjectController extends Controller
->handleRequest();
$this->view->checkNowForm = $checkNowForm;
}
if ( ! in_array((int) $this->object->state, array(0, 99))) {
if (! in_array((int) $this->object->state, array(0, 99))) {
if ((bool) $this->object->acknowledged) {
if ($auth->hasPermission('monitoring/command/remove-acknowledgement')) {
$removeAckForm = new RemoveAcknowledgementCommandForm();
@ -82,9 +82,7 @@ abstract class MonitoredObjectController extends Controller
$this->view->toggleFeaturesForm = $toggleFeaturesForm;
if (! empty($this->object->comments) && $auth->hasPermission('monitoring/command/comment/delete')) {
$delCommentForm = new DeleteCommentCommandForm();
$delCommentForm
->setObjects($this->object)
->handleRequest();
$delCommentForm->handleRequest();
$this->view->delCommentForm = $delCommentForm;
}
if (! empty($this->object->downtimes) && $auth->hasPermission('monitoring/command/downtime/delete')) {
@ -135,16 +133,6 @@ abstract class MonitoredObjectController extends Controller
*/
abstract public function scheduleDowntimeAction();
/**
* Delete a comment
*/
public function deleteCommentAction()
{
$this->assertHttpMethod('POST');
$this->assertPermission('monitoring/command/comment/delete');
$this->handleCommandForm(new DeleteCommentCommandForm());
}
/**
* Create tabs
*/