icingadb-web/application/controllers/NotificationsController.php

135 lines
4.3 KiB
PHP
Raw Permalink 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\LoadMoreObjectList;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
2021-08-19 09:14:06 -04:00
use ipl\Stdlib\Filter;
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->addTitleTab(t('Notifications'));
$compact = $this->view->compact;
2019-10-23 06:30:00 -04:00
$preserveParams = [
LimitControl::DEFAULT_LIMIT_PARAM,
SortControl::DEFAULT_SORT_PARAM,
ViewModeSwitcher::DEFAULT_VIEW_MODE_PARAM
];
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);
2021-08-19 09:14:06 -04:00
$before = $this->params->shift('before', time());
2019-10-23 06:30:00 -04:00
$limitControl = $this->createLimitControl();
2021-08-23 09:23:34 -04:00
$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
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl, true);
$searchBar = $this->createSearchBar($notifications, $preserveParams);
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();
}
2021-08-19 09:14:06 -04:00
$notifications->peekAhead();
2021-08-23 09:23:34 -04:00
$page = $paginationControl->getCurrentPageNumber();
if ($page > 1 && ! $compact) {
$notifications->resetOffset();
2021-08-23 09:23:34 -04:00
$notifications->limit($page * $limitControl->getLimit());
2021-08-19 09:14:06 -04:00
}
$notifications->filter(Filter::lessThanOrEqual('send_time', $before));
$this->filter($notifications, $filter);
$notifications->filter(Filter::any(
2020-03-10 04:26:28 -04:00
// Make sure we'll fetch service history entries only for services which still exist
Filter::unlike('service_id', '*'),
Filter::like('history.service.id', '*')
));
2019-10-23 06:30:00 -04:00
yield $this->export($notifications);
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
$url = Url::fromRequest()
->onlyWith($preserveParams)
->setFilter($filter);
$notificationList = (new LoadMoreObjectList($notifications->execute()))
2021-08-19 09:14:06 -04:00
->setPageSize($limitControl->getLimit())
->setViewMode($viewModeSwitcher->getViewMode())
->setLoadMoreUrl($url->setParam('before', $before));
if ($compact) {
2021-08-19 09:14:06 -04:00
$notificationList->setPageNumber($page);
}
if ($compact && $page > 1) {
$this->document->addFrom($notificationList);
} else {
$this->addContent($notificationList);
}
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
$this->sendMultipartUpdate();
}
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
}