Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Host-/Servicegroup grid views #355

Merged
merged 11 commits into from
Jun 20, 2023
39 changes: 32 additions & 7 deletions application/controllers/HostgroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Icinga\Module\Icingadb\Widget\ItemTable\HostgroupTable;
use Icinga\Module\Icingadb\Widget\ShowMore;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use ipl\Orm\Query;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;
use ipl\Web\Url;
Expand All @@ -38,18 +39,39 @@ public function indexAction()

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($hostgroups);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);

$defaultSort = null;
if ($viewModeSwitcher->getViewMode() === 'grid') {
$hostgroups->without([
'services_critical_handled',
'services_critical_unhandled',
'services_ok',
'services_pending',
'services_total',
'services_unknown_handled',
'services_unknown_unhandled',
'services_warning_handled',
'services_warning_unhandled',
]);

$defaultSort = ['hosts_severity DESC', 'display_name'];
}

$sortControl = $this->createSortControl(
$hostgroups,
[
'display_name' => t('Name'),
'hosts_severity desc' => t('Severity'),
'hosts_total desc' => t('Total Hosts'),
'services_total desc' => t('Total Services')
]
'display_name' => t('Name'),
'hosts_severity desc, display_name' => t('Severity'),
'hosts_total desc' => t('Total Hosts'),
],
$defaultSort
);

$searchBar = $this->createSearchBar($hostgroups, [
$limitControl->getLimitParam(),
$sortControl->getSortParam()
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
Expand All @@ -73,12 +95,15 @@ public function indexAction()
$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
$this->addControl($searchBar);

$results = $hostgroups->execute();

$this->addContent(
(new HostgroupTable($results))->setBaseFilter($filter)
(new HostgroupTable($results))
->setBaseFilter($filter)
->setViewMode($viewModeSwitcher->getViewMode())
);

if ($compact) {
Expand Down
25 changes: 19 additions & 6 deletions application/controllers/ServicegroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,27 @@ public function indexAction()

$limitControl = $this->createLimitControl();
$paginationControl = $this->createPaginationControl($servicegroups);
$viewModeSwitcher = $this->createViewModeSwitcher($paginationControl, $limitControl);

$defaultSort = null;
if ($viewModeSwitcher->getViewMode() === 'grid') {
$defaultSort = ['services_severity DESC', 'display_name'];
}

$sortControl = $this->createSortControl(
$servicegroups,
[
'display_name' => t('Name'),
'services_severity desc' => t('Severity'),
'services_total desc' => t('Total Services')
]
'display_name' => t('Name'),
'services_severity desc, display_name' => t('Severity'),
'services_total desc' => t('Total Services')
],
$defaultSort
);

$searchBar = $this->createSearchBar($servicegroups, [
$limitControl->getLimitParam(),
$sortControl->getSortParam()
$sortControl->getSortParam(),
$viewModeSwitcher->getViewModeParam()
]);

if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
Expand All @@ -72,12 +82,15 @@ public function indexAction()
$this->addControl($paginationControl);
$this->addControl($sortControl);
$this->addControl($limitControl);
$this->addControl($viewModeSwitcher);
$this->addControl($searchBar);

$results = $servicegroups->execute();

$this->addContent(
(new ServicegroupTable($results))->setBaseFilter($filter)
(new ServicegroupTable($results))
->setBaseFilter($filter)
->setViewMode($viewModeSwitcher->getViewMode())
);

if ($compact) {
Expand Down
6 changes: 4 additions & 2 deletions library/Icingadb/Common/BaseTableRowItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public function __construct($item, BaseItemTable $table = null)
$this->init();
}

abstract protected function assembleColumns(HtmlDocument $columns);

abstract protected function assembleTitle(BaseHtmlElement $title);

protected function assembleColumns(HtmlDocument $columns)
{
}

protected function assembleVisual(BaseHtmlElement $visual)
{
}
Expand Down
36 changes: 36 additions & 0 deletions library/Icingadb/Web/Control/GridViewModeSwitcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Web\Control;

/**
* View mode switcher to toggle between grid and list view
*/
class GridViewModeSwitcher extends ViewModeSwitcher
{
/** @var string Default view mode */
public const DEFAULT_VIEW_MODE = 'list';

/** @var array View mode-icon pairs */
public static $viewModes = [
'list' => 'default',
'grid' => 'grid'
];

protected function getTitle(string $viewMode): string
{
switch ($viewMode) {
case 'list':
$active = t('List view active');
$inactive = t('Switch to list view');
break;
case 'grid':
$active = t('Grid view active');
$inactive = t('Switch to grid view');
break;
}

return $viewMode === $this->getViewMode() ? $active : $inactive;
}
}
44 changes: 29 additions & 15 deletions library/Icingadb/Web/Control/ViewModeSwitcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,24 +180,38 @@ protected function assemble()
new IcingaIcon($icon)
);
$label->getAttributes()->registerAttributeCallback('title', function () use ($viewMode) {
switch ($viewMode) {
case 'minimal':
$active = t('Minimal view active');
$inactive = t('Switch to minimal view');
break;
case 'common':
$active = t('Common view active');
$inactive = t('Switch to common view');
break;
case 'detailed':
$active = t('Detailed view active');
$inactive = t('Switch to detailed view');
}

return $viewMode === $this->getViewMode() ? $active : $inactive;

return $this->getTitle($viewMode);
});

$this->addHtml($input, $label);
}
}

/**
* Return the title for the view mode when it is active and inactive
*
* @param string $viewMode
*
* @return string Title for the view mode when it is active and inactive
*/
protected function getTitle(string $viewMode): string
{
switch ($viewMode) {
case 'minimal':
$active = t('Minimal view active');
$inactive = t('Switch to minimal view');
break;
case 'common':
$active = t('Common view active');
$inactive = t('Switch to common view');
break;
case 'detailed':
$active = t('Detailed view active');
$inactive = t('Switch to detailed view');
break;
}

return $viewMode === $this->getViewMode() ? $active : $inactive;
}
}
23 changes: 18 additions & 5 deletions library/Icingadb/Web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Icinga\Module\Icingadb\Common\SearchControls;
use Icinga\Module\Icingadb\Data\CsvResultSet;
use Icinga\Module\Icingadb\Data\JsonResultSet;
use Icinga\Module\Icingadb\Web\Control\GridViewModeSwitcher;
use Icinga\Module\Icingadb\Web\Control\ViewModeSwitcher;
use Icinga\Module\Icingadb\Widget\ItemTable\StateItemTable;
use Icinga\Module\Pdfexport\PrintableHtmlDocument;
Expand Down Expand Up @@ -106,14 +107,22 @@ public function createColumnControl(Query $query, ViewModeSwitcher $viewModeSwit
* @param LimitControl $limitControl
* @param bool $verticalPagination
*
* @return ViewModeSwitcher
* @return ViewModeSwitcher|GridViewModeSwitcher
*/
public function createViewModeSwitcher(
PaginationControl $paginationControl,
LimitControl $limitControl,
bool $verticalPagination = false
): ViewModeSwitcher {
$viewModeSwitcher = new ViewModeSwitcher();
$controllerName = $this->getRequest()->getControllerName();

// TODO: Make this configurable somehow. The route shouldn't be checked to choose the view modes!
if ($controllerName === 'hostgroups' || $controllerName === 'servicegroups') {
raviks789 marked this conversation as resolved.
Show resolved Hide resolved
$viewModeSwitcher = new GridViewModeSwitcher();
} else {
$viewModeSwitcher = new ViewModeSwitcher();
}

$viewModeSwitcher->setIdProtector([$this->getRequest(), 'protectId']);

$user = $this->Auth()->getUser();
Expand Down Expand Up @@ -181,7 +190,7 @@ function (ViewModeSwitcher $viewModeSwitcher) use (

$requestUrl->setParam($viewModeSwitcher->getViewModeParam(), $viewMode);
if (! $requestUrl->hasParam($limitParam)) {
if ($viewMode === 'minimal') {
if ($viewMode === 'minimal' || $viewMode === 'grid') {
$session->set('previous_page', $currentPage);
$session->set('request_path', $requestUrl->getPath());

Expand All @@ -194,7 +203,10 @@ function (ViewModeSwitcher $viewModeSwitcher) use (
}

$session->set('current_page', $currentPage);
} elseif ($viewModeSwitcher->getDefaultViewMode() === 'minimal') {
} elseif (
$viewModeSwitcher->getDefaultViewMode() === 'minimal'
|| $viewModeSwitcher->getDefaultViewMode() === 'grid'
) {
$limit = $paginationControl->getLimit();
if ($currentPage === $session->get('current_page')) {
// No other page numbers have been selected, i.e the user only
Expand All @@ -220,7 +232,8 @@ function (ViewModeSwitcher $viewModeSwitcher) use (
}
)->handleRequest(ServerRequest::fromGlobals());

if ($viewModeSwitcher->getViewMode() === 'minimal') {
$viewMode = $viewModeSwitcher->getViewMode();
if ($viewMode === 'minimal' || $viewMode === 'grid') {
$hasLimitParam = Url::fromRequest()->hasParam($limitControl->getLimitParam());

if ($paginationControl->getDefaultPageSize() <= LimitControl::DEFAULT_LIMIT && ! $hasLimitParam) {
Expand Down
60 changes: 60 additions & 0 deletions library/Icingadb/Widget/ItemTable/BaseHostGroupItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/* Icinga DB Web | (c) 2023 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Widget\ItemTable;

use Icinga\Module\Icingadb\Common\BaseTableRowItem;
use Icinga\Module\Icingadb\Common\Links;
use Icinga\Module\Icingadb\Model\Hostgroup;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\I18n\Translation;
use ipl\Stdlib\Filter;
use ipl\Web\Widget\Link;

/**
* Hostgroup item of a hostgroup list. Represents one database row.
*
* @property Hostgroup $item
* @property HostgroupTable $table
*/
abstract class BaseHostGroupItem extends BaseTableRowItem
{
use Translation;

protected function init()
{
if (isset($this->table)) {
$this->table->addDetailFilterAttribute($this, Filter::equal('name', $this->item->name));
}
}

protected function createSubject(): BaseHtmlElement
{
return isset($this->table)
? new Link(
$this->item->display_name,
Links::hostgroup($this->item),
[
'class' => 'subject',
'title' => sprintf(
$this->translate('List all hosts in the group "%s"'),
$this->item->display_name
)
]
)
: new HtmlElement(
'span',
Attributes::create(['class' => 'subject']),
Text::create($this->item->display_name)
);
}

protected function createCaption(): BaseHtmlElement
{
return new HtmlElement('span', null, Text::create($this->item->name));
}
}
Loading