icingadb-web/application/controllers/NotificationsController.php

128 lines
4 KiB
PHP
Raw Normal View History

2019-10-23 06:30:00 -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-23 06:30:00 -04:00
use GuzzleHttp\Psr7\ServerRequest;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\NotificationHistory;
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\Widget\ItemList\NotificationList;
use Icinga\Module\Icingadb\Widget\ShowMore;
use Icinga\Module\Icingadb\Widget\ViewModeSwitcher;
2020-03-10 04:26:28 -04:00
use ipl\Sql\Sql;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Url;
2019-10-23 06:30:00 -04:00
class NotificationsController extends Controller
{
public function indexAction()
{
$this->setTitle(t('Notifications'));
$compact = $this->view->compact;
2019-10-23 06:30:00 -04:00
$db = $this->getDb();
$notifications = NotificationHistory::on($db)->with([
2021-06-08 07:40:17 -04:00
'history',
2019-10-23 06:30:00 -04:00
'host',
'host.state',
'service',
'service.state'
]);
$this->handleSearchRequest($notifications);
2019-10-23 06:30:00 -04:00
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($notifications);
2019-12-11 07:56:14 -05:00
$sortControl = $this->createSortControl(
$notifications,
[
'notification_history.send_time desc' => t('Send Time')
2019-12-11 07:56:14 -05:00
]
);
2021-08-04 04:11:49 -04:00
$viewModeSwitcher = $this->createViewModeSwitcher();
$searchBar = $this->createSearchBar($notifications, [
$limitControl->getLimitParam(),
2021-08-04 04:11:49 -04:00
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);
2019-10-23 06:30:00 -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($notifications, $filter);
2020-03-10 04:26:28 -04:00
$notifications->getSelectBase()
// Make sure we'll fetch service history entries only for services which still exist
->where([
'notification_history.service_id IS NULL',
'notification_history_service.id IS NOT NULL'
], Sql::ANY);
2019-10-23 06:30:00 -04:00
$notifications->peekAhead($compact);
2019-10-23 06:30:00 -04:00
yield $this->export($notifications);
$this->addControl($paginationControl);
2019-12-11 07:56:14 -05:00
$this->addControl($sortControl);
2019-10-23 06:30:00 -04:00
$this->addControl($limitControl);
2021-08-04 04:11:49 -04:00
$this->addControl($viewModeSwitcher);
$this->addControl($searchBar);
2019-10-23 06:30:00 -04:00
$results = $notifications->execute();
2021-08-04 04:11:49 -04:00
$this->addContent(
(new NotificationList($results))
->setViewMode($viewModeSwitcher->getViewMode())
);
if ($compact) {
$this->addContent(
(new ShowMore($results, Url::fromRequest()->without(['showCompact', 'limit'])))
->setBaseTarget('_next')
->setAttribute('title', sprintf(
t('Show all %d notifications'),
$notifications->count()
))
);
}
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}
$this->setAutorefreshInterval(10);
2019-10-23 06:30:00 -04:00
}
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(NotificationHistory::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction()
{
$editor = $this->createSearchEditor(NotificationHistory::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'));
}
2019-10-23 06:30:00 -04:00
}