icingadb-web/application/controllers/HistoryController.php

94 lines
3 KiB
PHP
Raw Normal View History

2019-11-05 05:52:57 -05:00
<?php
namespace Icinga\Module\Icingadb\Controllers;
use Icinga\Module\Icingadb\Model\History;
use Icinga\Module\Icingadb\Web\Controller;
use Icinga\Module\Icingadb\Widget\ItemList\HistoryList;
use Icinga\Module\Icingadb\Widget\ShowMore;
2020-03-10 04:26:28 -04:00
use ipl\Sql\Sql;
use ipl\Web\Url;
2019-11-05 05:52:57 -05:00
class HistoryController extends Controller
{
public function indexAction()
{
$this->setTitle($this->translate('History'));
$compact = $this->view->compact; // TODO: Find a less-legacy way..
2019-11-05 05:52:57 -05:00
$db = $this->getDb();
$history = History::on($db)->with([
'host',
'host.state',
'service',
'service.state',
'comment',
'downtime',
'notification',
'acknowledgement',
2019-11-05 05:52:57 -05:00
'state'
]);
$this->params->shift('view'); // TODO: Don't shift here, damn it (Nothing else does it)
$url = Url::fromPath('icingadb/history')->setParams(clone $this->params);
if (! $this->params->has('page') || ($page = (int) $this->params->shift('page')) < 1) {
$page = 1;
}
2019-11-05 05:52:57 -05:00
$limitControl = $this->createLimitControl();
2019-12-11 07:38:29 -05:00
$sortControl = $this->createSortControl(
$history,
[
'history.event_time desc' => $this->translate('Event Time')
]
);
2019-11-05 05:52:57 -05:00
$filterControl = $this->createFilterControl($history);
$history->peekAhead();
$history->limit($limitControl->getLimit());
if ($page > 1) {
if ($compact) {
$history->offset(($page - 1) * $limitControl->getLimit());
} else {
$history->limit($page * $limitControl->getLimit());
}
}
2019-11-05 05:52:57 -05:00
$this->filter($history);
2020-03-10 04:26:28 -04:00
$history->getSelectBase()
// Make sure we'll fetch service history entries only for services which still exist
->where(['history.service_id IS NULL', 'history_service.id IS NOT NULL'], Sql::ANY);
2019-11-05 05:52:57 -05:00
yield $this->export($history);
$results = $history->execute();
$showMore = (new ShowMore(
$results,
$url->setParam('page', $page + 1)
->setAnchor('page-' . ($page + 1))
))
->setLabel('Load More')
->setAttribute('data-no-icinga-ajax', true);
2019-12-11 07:38:29 -05:00
$this->addControl($sortControl);
2019-11-05 05:52:57 -05:00
$this->addControl($limitControl);
$this->addControl($filterControl);
$historyList = (new HistoryList($results))
->setPageSize($limitControl->getLimit());
if ($compact) {
$historyList->setPageNumber($page);
}
// TODO: Dirty, really dirty, find a better solution (And I don't just mean `getContent()` !)
$historyList->add($showMore->setTag('li')->addAttributes(['class' => 'list-item']));
if ($compact && $page > 1) {
$this->document->add($historyList->getContent());
} else {
$this->addContent($historyList);
}
2019-11-05 05:52:57 -05:00
}
}