icingadb-web/application/controllers/DowntimesController.php

229 lines
7.2 KiB
PHP
Raw Permalink Normal View History

2019-10-17 04:16:08 -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-17 04:16:08 -04:00
2020-11-02 08:29:46 -05:00
use GuzzleHttp\Psr7\ServerRequest;
2024-09-18 03:34:02 -04:00
use Icinga\Module\Icingadb\Common\CommandActions;
2019-12-16 09:41:08 -05:00
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Forms\Command\Object\DeleteDowntimeForm;
2019-11-04 19:07:30 -05:00
use Icinga\Module\Icingadb\Model\Downtime;
2020-11-02 08:29:46 -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:34:02 -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-17 04:16:08 -04:00
class DowntimesController extends Controller
{
2024-09-18 03:34:02 -04:00
use CommandActions;
2019-10-17 04:16:08 -04:00
public function indexAction()
{
$this->addTitleTab(t('Downtimes'));
$compact = $this->view->compact;
2019-10-17 04:16:08 -04:00
$db = $this->getDb();
$downtimes = Downtime::on($db)->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($downtimes);
2019-12-11 07:37:39 -05:00
$sortControl = $this->createSortControl(
$downtimes,
[
'downtime.is_in_effect desc, downtime.start_time desc' => t('Is In Effect'),
'downtime.entry_time' => t('Entry Time'),
'host.display_name' => t('Host'),
'service.display_name' => t('Service'),
'downtime.author' => t('Author'),
'downtime.start_time desc' => t('Start Time'),
'downtime.end_time desc' => t('End Time'),
'downtime.scheduled_start_time desc' => t('Scheduled Start Time'),
'downtime.scheduled_end_time desc' => t('Scheduled End Time'),
'downtime.duration desc' => t('Duration')
2019-12-11 07:37:39 -05:00
]
);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);
2020-11-02 08:29:46 -05:00
$searchBar = $this->createSearchBar($downtimes, [
$limitControl->getLimitParam(),
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);
2019-10-17 04:16:08 -04:00
if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
if ($searchBar->hasBeenSubmitted()) {
$filter = $this->getFilter();
} else {
$this->addControl($searchBar);
$this->sendMultipartUpdate();
2021-11-12 05:45:27 -05:00
return;
}
} else {
$filter = $searchBar->getFilter();
}
$this->filter($downtimes, $filter);
2019-10-17 04:16:08 -04:00
$downtimes->peekAhead($compact);
2019-10-17 04:16:08 -04:00
yield $this->export($downtimes);
$this->addControl($paginationControl);
2019-12-11 07:37:39 -05:00
$this->addControl($sortControl);
2019-10-17 04:16:08 -04:00
$this->addControl($limitControl);
2019-12-16 08:39:28 -05:00
$this->addControl($viewModeSwitcher);
2020-11-02 08:29:46 -05:00
$this->addControl($searchBar);
$continueWith = $this->createContinueWith(Links::downtimesDetails(), $searchBar);
2019-10-17 04:16:08 -04:00
$results = $downtimes->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 downtimes'),
$downtimes->count()
))
);
}
if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
2021-05-26 05:44:50 -04:00
$this->sendMultipartUpdate($continueWith);
2020-11-02 08:29:46 -05:00
}
$this->setAutorefreshInterval(10);
2019-10-17 04:16:08 -04:00
}
2019-12-16 09:41:08 -05:00
public function deleteAction()
{
2024-09-18 03:34:02 -04:00
$this->assertIsGrantedOnCommandTargets('icingadb/command/downtime/delete');
$this->setTitle(t('Cancel Downtimes'));
2024-09-18 03:34:02 -04:00
$this->handleCommandForm(DeleteDowntimeForm::class);
}
2019-12-16 09:41:08 -05:00
public function detailsAction()
{
$this->addTitleTab(t('Downtimes'));
2019-12-16 09:41:08 -05:00
$db = $this->getDb();
$downtimes = Downtime::on($db)->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$downtimes->limit(3)->peekAhead();
$this->filter($downtimes);
yield $this->export($downtimes);
$rs = $downtimes->execute();
$this->addControl(
(new ObjectList($rs))
->setViewMode('minimal')
->setDetailActionsDisabled()
);
2019-12-16 09:41:08 -05:00
$this->addControl(new ShowMore(
$rs,
Links::downtimes()->setFilter($this->getFilter()),
sprintf(t('Show all %d downtimes'), $downtimes->count())
));
$this->addContent(
(new DeleteDowntimeForm())
->setObjects($downtimes)
->setAction(
Links::downtimesDelete()
->setFilter($this->getFilter())
->getAbsoluteUrl()
)
);
2019-12-16 09:41:08 -05:00
}
2020-11-02 08:29:46 -05:00
public function completeAction()
{
$suggestions = new ObjectSuggestions();
$suggestions->setModel(Downtime::class);
$suggestions->forRequest(ServerRequest::fromGlobals());
$this->getDocument()->add($suggestions);
}
public function searchEditorAction()
{
$editor = $this->createSearchEditor(Downtime::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:34:02 -04:00
protected function getCommandTargetsUrl(): Url
{
return Url::fromPath('__CLOSE__');
}
protected function fetchCommandTargets(): Query
{
$downtimes = Downtime::on($this->getDb())->with([
'host',
'host.state',
'service',
'service.host',
'service.host.state',
'service.state'
]);
$this->filter($downtimes);
return $downtimes;
}
public function isGrantedOn(string $permission, Model $object): bool
{
if ($object->scheduled_by !== null) {
return false;
}
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-17 04:16:08 -04:00
}