Add native implementation to delete comments

This commit is contained in:
Johannes Meyer 2021-02-10 09:48:23 +01:00
parent d772b1324b
commit f58d77ebc5
6 changed files with 80 additions and 101 deletions

View file

@ -6,19 +6,16 @@ namespace Icinga\Module\Icingadb\Controllers;
use GuzzleHttp\Psr7\ServerRequest;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
use Icinga\Module\Icingadb\Model\Comment;
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\ContinueWith;
use Icinga\Module\Icingadb\Widget\ItemList\CommentList;
use Icinga\Module\Icingadb\Widget\ShowMore;
use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentsCommandForm;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use ipl\Web\Widget\ActionLink;
use ipl\Web\Widget\Icon;
class CommentsController extends Controller
{
@ -109,6 +106,8 @@ class CommentsController extends Controller
public function deleteAction()
{
// TODO: Check permission
$this->setTitle(t('Remove Comments'));
$db = $this->getDb();
@ -124,40 +123,19 @@ class CommentsController extends Controller
$this->filter($comments);
$deleteCommentsForm = (new DeleteCommentsCommandForm())
->addDescription(sprintf(
t('Confirm removal of %d comments.'),
$comments->count()
))
->setComments($comments)
->setRedirectUrl(Links::comments())
->create();
$form = (new DeleteCommentForm())
->setObjects($comments)
->setRedirectUrl(Links::comments()->getAbsoluteUrl())
->on(DeleteCommentForm::ON_SUCCESS, function ($form) {
// This forces the column to reload nearly instantly after the redirect
// and ensures the effect of the command is visible to the user asap
$this->getResponse()->setAutoRefreshInterval(1);
$deleteCommentsForm->removeElement('btn_submit');
$this->redirectNow($form->getRedirectUrl());
})
->handleRequest(ServerRequest::fromGlobals());
$deleteCommentsForm->addElement(
'button',
'btn_submit',
[
'class' => 'cancel-button spinner',
'decorators' => [
'ViewHelper',
['HtmlTag', ['tag' => 'div', 'class' => 'control-group form-controls']]
],
'escape' => false,
'ignore' => true,
'label' => (new HtmlDocument())
->add([new Icon('trash'), t('Remove Comments')])
->setSeparator(' ')
->render(),
'title' => t('Remove comments'),
'type' => 'submit'
]
);
$deleteCommentsForm->handleRequest();
$this->addContent(HtmlString::create($deleteCommentsForm->render()));
$this->addContent($form);
}
public function detailsAction()
@ -191,15 +169,16 @@ class CommentsController extends Controller
sprintf(t('Show all %d comments'), $comments->count())
));
$this->addContent((new ActionLink(
sprintf(t('Remove %d comments'), $comments->count()),
Links::commentsDelete()->setQueryString(QueryString::render($this->getFilter())),
'trash',
[
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true
]
))->setAttribute('class', 'cancel-button'));
// TODO: Check permission
$this->addContent(
(new DeleteCommentForm())
->setObjects($comments)
->setAction(
Links::commentsDelete()
->setQueryString(QueryString::render($this->getFilter()))
->getAbsoluteUrl()
)
);
}
public function completeAction()

View file

@ -0,0 +1,47 @@
<?php
/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */
namespace Icinga\Module\Icingadb\Forms\Command\Object;
use Icinga\Module\Icingadb\Command\Object\DeleteCommentCommand;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use ipl\Orm\Model;
use ipl\Web\Common\RedirectOption;
use ipl\Web\Widget\Icon;
class DeleteCommentForm extends CommandForm
{
use Auth;
use RedirectOption;
protected function assembleElements()
{
$this->addElement($this->createRedirectOption());
}
protected function assembleSubmitButton()
{
$this->addElement(
'submitButton',
'btn_submit',
[
'class' => ['cancel-button', 'spinner'],
'label' => [
new Icon('trash'),
tp('Remove Comment', 'Remove Comments', count($this->getObjects()))
]
]
);
}
protected function getCommand(Model $object)
{
$command = new DeleteCommentCommand();
$command->setCommentName($object->name);
$command->setAuthor($this->getAuth()->getUser()->getUsername());
return $command;
}
}

View file

@ -143,18 +143,6 @@ trait CommandActions
$this->handleCommandForm(CheckNowCommandForm::class);
}
public function deleteCommentAction()
{
$this->assertPermission('monitoring/command/comment/delete');
$this->handleCommandForm(DeleteCommentCommandForm::class);
}
public function deleteCommentsAction()
{
$this->assertPermission('monitoring/command/comment/delete');
$this->handleCommandForm(DeleteCommentsCommandForm::class);
}
public function deleteDowntimeAction()
{
$this->assertPermission('monitoring/command/downtime/delete');

View file

@ -54,11 +54,6 @@ abstract class HostLinks
return Url::fromPath('icingadb/host/remove-acknowledgement', ['name' => $host->name]);
}
public static function removeComment(Host $host)
{
return Url::fromPath('icingadb/host/delete-comment', ['name' => $host->name]);
}
public static function scheduleDowntime(Host $host)
{
return Url::fromPath('icingadb/host/schedule-downtime', ['name' => $host->name]);

View file

@ -82,14 +82,6 @@ abstract class ServiceLinks
);
}
public static function removeComment(Service $service, Host $host)
{
return Url::fromPath(
'icingadb/service/delete-comment',
['name' => $service->name, 'host.name' => $host->name]
);
}
public static function scheduleDowntime(Service $service, Host $host)
{
return Url::fromPath(

View file

@ -6,17 +6,13 @@ namespace Icinga\Module\Icingadb\Widget\Detail;
use Icinga\Module\Icingadb\Common\Auth;
use Icinga\Module\Icingadb\Common\HostLink;
use Icinga\Module\Icingadb\Common\HostLinks;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Common\MarkdownText;
use Icinga\Module\Icingadb\Common\ServiceLink;
use Icinga\Module\Icingadb\Common\ServiceLinks;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
use Icinga\Module\Icingadb\Widget\TimeUntil;
use Icinga\Module\Monitoring\Forms\Command\Object\DeleteCommentCommandForm;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Html\HtmlDocument;
use ipl\Html\HtmlString;
use ipl\Web\Widget\Icon;
class CommentDetail extends BaseHtmlElement
{
@ -70,32 +66,14 @@ class CommentDetail extends BaseHtmlElement
protected function createRemoveCommentForm()
{
$formData = [
'comment_id' => $this->comment->name,
'comment_name' => $this->comment->name,
'redirect' => '__BACK__'
];
// TODO: Check permission
$action = Links::commentsDelete();
$action->setParam('name', $this->comment->name);
if ($this->comment->object_type === 'host') {
$action = HostLinks::removeComment($this->comment->host);
} else {
$action = ServiceLinks::removeComment($this->comment->service, $this->comment->service->host);
$formData['comment_is_service'] = true;
}
$removeCommentForm = (new DeleteCommentCommandForm())
->create()
->populate($formData)
->setAction($action);
$submitButton = $removeCommentForm->getElement('btn_submit');
$submitButton->content = (new HtmlDocument())
->add([new Icon('trash'), t('Remove Comment')])
->setSeparator(' ')
->render();
return new HtmlString($removeCommentForm->render());
return (new DeleteCommentForm())
->setObjects([$this->comment])
->populate(['redirect' => '__BACK__'])
->setAction($action->getAbsoluteUrl());
}
protected function assemble()