diff --git a/application/controllers/CommentsController.php b/application/controllers/CommentsController.php index 466f2d99..d0b75ca4 100644 --- a/application/controllers/CommentsController.php +++ b/application/controllers/CommentsController.php @@ -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() diff --git a/application/forms/Command/Object/DeleteCommentForm.php b/application/forms/Command/Object/DeleteCommentForm.php new file mode 100644 index 00000000..036ce413 --- /dev/null +++ b/application/forms/Command/Object/DeleteCommentForm.php @@ -0,0 +1,47 @@ +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; + } +} diff --git a/library/Icingadb/Common/CommandActions.php b/library/Icingadb/Common/CommandActions.php index 0ac67f71..3e1dc385 100644 --- a/library/Icingadb/Common/CommandActions.php +++ b/library/Icingadb/Common/CommandActions.php @@ -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'); diff --git a/library/Icingadb/Common/HostLinks.php b/library/Icingadb/Common/HostLinks.php index 15f0a8d4..5adc6c5f 100644 --- a/library/Icingadb/Common/HostLinks.php +++ b/library/Icingadb/Common/HostLinks.php @@ -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]); diff --git a/library/Icingadb/Common/ServiceLinks.php b/library/Icingadb/Common/ServiceLinks.php index 64dc35b0..cc54eeac 100644 --- a/library/Icingadb/Common/ServiceLinks.php +++ b/library/Icingadb/Common/ServiceLinks.php @@ -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( diff --git a/library/Icingadb/Widget/Detail/CommentDetail.php b/library/Icingadb/Widget/Detail/CommentDetail.php index 66ab294a..3fd6d029 100644 --- a/library/Icingadb/Widget/Detail/CommentDetail.php +++ b/library/Icingadb/Widget/Detail/CommentDetail.php @@ -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()