icingadb-web/application/controllers/CommentsController.php

219 lines
6.5 KiB
PHP
Raw Permalink Normal View History

2019-10-16 10:53:11 -04:00
<?php
2020-03-13 03:38:01 -04:00
/* Icinga DB Web | (c) 2020 Icinga GmbH | GPLv2 */
2019-11-04 19:07:30 -05:00
namespace Icinga\Module\Icingadb\Controllers;
2019-10-16 10:53:11 -04:00
2020-11-02 08:28:41 -05:00
use GuzzleHttp\Psr7\ServerRequest;
2024-09-18 03:33:18 -04:00
use Icinga\Module\Icingadb\Common\CommandActions;
2019-12-16 09:56:06 -05:00
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteCommentForm;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\Comment;
2020-11-02 08:28:41 -05:00
use Icinga\Module\Icingadb\Web\Control\SearchBar\ObjectSuggestions;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Widget\ItemList\ObjectList;
2023-08-09 08:25:44 -04:00
use Icinga\Module\Icingadb\Widget\ShowMore;
2024-09-18 03:33:18 -04:00
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Stdlib\Filter;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Url;
2019-10-16 10:53:11 -04:00
class CommentsController extends Controller
{
2024-09-18 03:33:18 -04:00
use CommandActions;
2019-10-16 10:53:11 -04:00
public function indexAction()
{
$this->addTitleTab(t('Comments'));
$compact = $this->view->compact;
2019-10-16 10:53:11 -04:00
$db = $this->getDb();
$comments = Comment::on($db)->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($comments);
2019-12-11 07:13:14 -05:00
$sortControl = $this->createSortControl(
$comments,
[
'comment.entry_time desc' => t('Entry Time'),
'host.display_name' => t('Host'),
'service.display_name' => t('Service'),
'comment.author' => t('Author'),
'comment.expire_time desc' => t('Expire Time')
2019-12-11 07:13:14 -05:00
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
2020-11-02 08:28:41 -05:00
$searchBar = $this->createSearchBar($comments, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);
2019-10-16 10:53:11 -04:00
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
return;
}
} else {
$filter = $searchBar->getFilter();
}
$this->filter($comments, $filter);
2019-10-16 10:53:11 -04:00
$comments->peekAhead($compact);
2019-10-16 10:53:11 -04:00
yield $this->export($comments);
$this->addControl($paginationControl);
2019-12-11 07:13:14 -05:00
$this->addControl($sortControl);
2019-10-16 10:53:11 -04:00
$this->addControl($limitControl);
2019-12-16 09:55:53 -05:00
$this->addControl($viewModeSwitcher);
2020-11-02 08:28:41 -05:00
$this->addControl($searchBar);
$continueWith = $this->createContinueWith(Links::commentsDetails(), $searchBar);
2019-10-16 10:53:11 -04:00
$results = $comments->execute();
$this->addContent(
(new ObjectList($results))
->setViewMode($viewModeSwitcher->getViewMode())
->setEmptyStateMessage($paginationControl->getEmptyStateMessage())
);
if ($compact) {
$this->addContent(
(new ShowMore($results, Url::fromRequest()->without(['showCompact', 'limit', 'view'])))
->setBaseTarget('_next')
->setAttribute('title', sprintf(
t('Show all %d comments'),
$comments->count()
))
);
}
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
2021-05-26 05:44:50 -04:00
$this->sendMultipartUpdate($continueWith);
2020-11-02 08:28:41 -05:00
}
$this->setAutorefreshInterval(10);
2019-10-16 10:53:11 -04:00
}
2019-12-16 09:56:06 -05:00
public function deleteAction()
{
2024-09-18 03:33:18 -04:00
$this->assertIsGrantedOnCommandTargets('icingadb/command/comment/delete');
$this->setTitle(t('Remove Comments'));
2024-09-18 03:33:18 -04:00
$this->handleCommandForm(DeleteCommentForm::class);
}
2019-12-16 09:56:06 -05:00
public function detailsAction()
{
$this->addTitleTab(t('Comments'));
2019-12-16 09:56:06 -05:00
$db = $this->getDb();
$comments = Comment::on($db)->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$comments->limit(3)->peekAhead();
$this->filter($comments);
yield $this->export($comments);
$rs = $comments->execute();
$this->addControl(
(new ObjectList($rs))
->setViewMode('minimal')
->setDetailActionsDisabled()
);
2019-12-16 09:56:06 -05:00
$this->addControl(new ShowMore(
$rs,
Links::comments()->setFilter($this->getFilter()),
sprintf(t('Show all %d comments'), $comments->count())
));
$this->addContent(
(new DeleteCommentForm())
->setObjects($comments)
->setAction(
Links::commentsDelete()
->setFilter($this->getFilter())
->getAbsoluteUrl()
)
);
2019-12-16 09:56:06 -05:00
}
2020-11-02 08:28:41 -05:00
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Comment::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction()
{
$editor = $this->createSearchEditor(Comment::on($this->getDb()), [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM
]);
$this->getDocument()->add($editor);
$this->setTitle(t('Adjust Filter'));
}
2024-09-18 03:33:18 -04:00
protected function getCommandTargetsUrl(): Url
{
return Url::fromPath('__CLOSE__');
}
protected function fetchCommandTargets(): Query
{
$comments = Comment::on($this->getDb())->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$this->filter($comments);
return $comments;
}
public function isGrantedOn(string $permission, Model $object): bool
{
return parent::isGrantedOn($permission, $object->{$object->object_type});
}
public function isGrantedOnType(string $permission, string $type, Filter\Rule $filter, bool $cache = true): bool
{
return parent::isGrantedOnType($permission, 'host', $filter, $cache)
|| parent::isGrantedOnType($permission, 'service', $filter, $cache);
}
2019-10-16 10:53:11 -04:00
}