Add activateEntry() & getActiveEntry() interface methods

This commit is contained in:
Yonas Habteab 2022-08-05 17:02:46 +02:00
parent ee829fd4dc
commit b619dd2186
25 changed files with 213 additions and 272 deletions

View File

@ -58,7 +58,7 @@ class DashboardsController extends CompatController
// If we don't load all dashboard homes here, the cog icon won't be rendered in the dashboard tabs // If we don't load all dashboard homes here, the cog icon won't be rendered in the dashboard tabs
$this->dashboard->load(DashboardHome::DEFAULT_HOME, $pane, true); $this->dashboard->load(DashboardHome::DEFAULT_HOME, $pane, true);
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
if (! $activeHome || ! $activeHome->hasEntries()) { if (! $activeHome || ! $activeHome->hasEntries()) {
$this->addTitleTab(t('Welcome')); $this->addTitleTab(t('Welcome'));
@ -89,7 +89,7 @@ class DashboardsController extends CompatController
$this->dashboard->load($home, $pane); $this->dashboard->load($home, $pane);
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
if (! $activeHome->getEntries()) { if (! $activeHome->getEntries()) {
$this->addTitleTab($activeHome->getTitle()); $this->addTitleTab($activeHome->getTitle());
} }
@ -133,7 +133,7 @@ class DashboardsController extends CompatController
}) })
->handleRequest($this->getServerRequest()); ->handleRequest($this->getServerRequest());
$homeForm->load($this->dashboard->getActiveHome()); $homeForm->load($this->dashboard->getActiveEntry());
$this->addTitleTab(t('Update Home')); $this->addTitleTab(t('Update Home'));
$this->addContent($homeForm); $this->addContent($homeForm);
@ -201,7 +201,7 @@ class DashboardsController extends CompatController
} }
})->handleRequest($this->getServerRequest()); })->handleRequest($this->getServerRequest());
$paneForm->load($this->dashboard->getActiveHome()->getEntry($pane)); $paneForm->load($this->dashboard->getActiveEntry()->getEntry($pane));
$this->addTitleTab(t('Update Pane')); $this->addTitleTab(t('Update Pane'));
$this->addContent($paneForm); $this->addContent($paneForm);
@ -282,7 +282,7 @@ class DashboardsController extends CompatController
$this->dashboard->load($home, $pane, true); $this->dashboard->load($home, $pane, true);
$pane = $this->dashboard->getActiveHome()->getActivePane(); $pane = $this->dashboard->getActiveEntry()->getActiveEntry();
if (! $pane->hasEntry($dashlet)) { if (! $pane->hasEntry($dashlet)) {
$this->httpNotFound(t('Dashlet "%s" not found'), $dashlet); $this->httpNotFound(t('Dashlet "%s" not found'), $dashlet);
} }
@ -311,7 +311,7 @@ class DashboardsController extends CompatController
$this->dashboard->load($home, $pane); $this->dashboard->load($home, $pane);
$pane = $this->dashboard->getActiveHome()->getActivePane(); $pane = $this->dashboard->getActiveEntry()->getActiveEntry();
if (! $pane->hasEntry($dashlet)) { if (! $pane->hasEntry($dashlet)) {
$this->httpNotFound(t('Dashlet "%s" not found'), $dashlet); $this->httpNotFound(t('Dashlet "%s" not found'), $dashlet);
} }
@ -510,7 +510,7 @@ class DashboardsController extends CompatController
$this->createTabs(); $this->createTabs();
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
// We can't grant access the user to the dashboard manager if there aren't any dashboards to manage // We can't grant access the user to the dashboard manager if there aren't any dashboards to manage
if (! $activeHome || (! $activeHome->hasEntries() && $this->dashboard->countEntries() === 1)) { if (! $activeHome || (! $activeHome->hasEntries() && $this->dashboard->countEntries() === 1)) {
$this->redirectNow(Dashboard::BASE_ROUTE); $this->redirectNow(Dashboard::BASE_ROUTE);
@ -540,7 +540,7 @@ class DashboardsController extends CompatController
private function createTabs() private function createTabs()
{ {
$tabs = $this->dashboard->getTabs(); $tabs = $this->dashboard->getTabs();
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
if ($activeHome if ($activeHome
&& ( && (
! $activeHome->isDefaultHome() ! $activeHome->isDefaultHome()
@ -553,7 +553,7 @@ class DashboardsController extends CompatController
$params['home'] = $activeHome->getName(); $params['home'] = $activeHome->getName();
} }
if (($activePane = $activeHome->getActivePane())) { if (($activePane = $activeHome->getActiveEntry())) {
$params['pane'] = $activePane->getName(); $params['pane'] = $activePane->getName();
} }

View File

@ -94,7 +94,7 @@ abstract class BaseDashboardForm extends CompatForm
*/ */
protected function isUpdating(): bool protected function isUpdating(): bool
{ {
return substr($this->requestUrl->getPath(), 0, 16) === Dashboard::BASE_ROUTE . '/edit-'; return strpos($this->requestUrl->getPath(), Dashboard::BASE_ROUTE . '/edit-') !== false;
} }
/** /**

View File

@ -39,7 +39,7 @@ class DashletForm extends SetupNewDashboardForm
protected function assembleNextPageDashboardPart() protected function assembleNextPageDashboardPart()
{ {
$homes = $this->dashboard->getEntryKeyTitleArr(); $homes = $this->dashboard->getEntryKeyTitleArr();
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
$currentHome = $this->requestUrl->getParam('home', reset($homes)); $currentHome = $this->requestUrl->getParam('home', reset($homes));
$populatedHome = $this->getPopulatedValue('home', $currentHome); $populatedHome = $this->getPopulatedValue('home', $currentHome);
@ -177,16 +177,16 @@ class DashletForm extends SetupNewDashboardForm
if ($dashboard->hasEntry($currentHome->getName())) { if ($dashboard->hasEntry($currentHome->getName())) {
$currentHome = clone $dashboard->getEntry($currentHome->getName()); $currentHome = clone $dashboard->getEntry($currentHome->getName());
$activatePane = $currentHome->hasEntry($selectedPane) $activatePane = $currentHome->hasEntry($selectedPane)
&& $currentHome->getActivePane()->getName() !== $selectedPane && $currentHome->getActiveEntry()->getName() !== $selectedPane
? $selectedPane ? $selectedPane
: null; : null;
if ($currentHome->getName() !== $dashboard->getActiveHome()->getName() || $activatePane) { if ($currentHome->getName() !== $dashboard->getActiveEntry()->getName() || $activatePane) {
$currentHome->loadDashboardEntries($activatePane); $currentHome->loadDashboardEntries($activatePane);
} }
if ($currentHome->hasEntry($currentPane->getName())) { if ($currentHome->hasEntry($currentPane->getName())) {
$currentPane = clone $currentHome->getActivePane(); $currentPane = clone $currentHome->getActiveEntry();
} }
} }
@ -229,8 +229,8 @@ class DashletForm extends SetupNewDashboardForm
} }
$this->dumpArbitaryDashlets(false); $this->dumpArbitaryDashlets(false);
// Avoid the hassle of iterating through the module dashlets each time to check if exits, // Avoid the overhead of traversing the module Dashlets each time to check if they already exist,
// even though the current pane doesn't have any entries // even the current Dashboard Pane doesn't contain any records
if (! $this->getPopulatedValue('new_pane') && $currentPane->hasEntries()) { if (! $this->getPopulatedValue('new_pane') && $currentPane->hasEntries()) {
foreach ($this->moduleDashlets as $_ => $dashlets) { foreach ($this->moduleDashlets as $_ => $dashlets) {
/** @var Dashlet $dashlet */ /** @var Dashlet $dashlet */
@ -278,10 +278,10 @@ class DashletForm extends SetupNewDashboardForm
} else { } else {
$orgHome = $dashboard->getEntry($this->getValue('org_home')); $orgHome = $dashboard->getEntry($this->getValue('org_home'));
$orgPane = $orgHome->getEntry($this->getValue('org_pane')); $orgPane = $orgHome->getEntry($this->getValue('org_pane'));
if ($orgHome->getActivePane()->getName() !== $orgPane->getName()) { if ($orgHome->getActiveEntry()->getName() !== $orgPane->getName()) {
$orgHome->loadDashboardEntries($orgPane->getName()); $orgHome->loadDashboardEntries($orgPane->getName());
$orgPane = $orgHome->getActivePane(); $orgPane = $orgHome->getActiveEntry();
} }
$orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet')); $orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet'));

View File

@ -41,7 +41,7 @@ class HomeForm extends BaseDashboardForm
protected function onSuccess() protected function onSuccess()
{ {
if ($this->isUpdating()) { if ($this->isUpdating()) {
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
if ($home->getTitle() === $this->getPopulatedValue('title')) { if ($home->getTitle() === $this->getPopulatedValue('title')) {
return; return;
} }

View File

@ -38,7 +38,7 @@ class PaneForm extends BaseDashboardForm
]); ]);
$homes = $this->dashboard->getEntryKeyTitleArr(); $homes = $this->dashboard->getEntryKeyTitleArr();
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
$populatedHome = $this->getPopulatedValue('home', $activeHome->getName()); $populatedHome = $this->getPopulatedValue('home', $activeHome->getName());
$this->addElement('select', 'home', [ $this->addElement('select', 'home', [
@ -90,7 +90,7 @@ class PaneForm extends BaseDashboardForm
if ($dashboard->hasEntry($currentHome->getName())) { if ($dashboard->hasEntry($currentHome->getName())) {
/** @var DashboardHome $currentHome */ /** @var DashboardHome $currentHome */
$currentHome = clone $dashboard->getEntry($currentHome->getName()); $currentHome = clone $dashboard->getEntry($currentHome->getName());
if ($currentHome->getName() !== $dashboard->getActiveHome()->getName()) { if ($currentHome->getName() !== $dashboard->getActiveEntry()->getName()) {
$currentHome->loadDashboardEntries(); $currentHome->loadDashboardEntries();
} }
} }

View File

@ -30,8 +30,8 @@ class RemoveDashletForm extends BaseDashboardForm
protected function onSuccess() protected function onSuccess()
{ {
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$dashlet = $pane->getEntry($this->requestUrl->getParam('dashlet')); $dashlet = $pane->getEntry($this->requestUrl->getParam('dashlet'));

View File

@ -20,7 +20,7 @@ class RemoveHomeForm extends BaseDashboardForm
$this->addHtml(HtmlElement::create( $this->addHtml(HtmlElement::create(
'h2', 'h2',
null, null,
sprintf(t('Please confirm removal of Dashboard Home "%s"'), $this->dashboard->getActiveHome()->getTitle()) sprintf(t('Please confirm removal of Dashboard Home "%s"'), $this->dashboard->getActiveEntry()->getTitle())
)); ));
$this->addHtml($this->registerSubmitButton('Remove Home')->setName('btn_remove')); $this->addHtml($this->registerSubmitButton('Remove Home')->setName('btn_remove'));
@ -28,7 +28,7 @@ class RemoveHomeForm extends BaseDashboardForm
protected function onSuccess() protected function onSuccess()
{ {
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
try { try {
$this->dashboard->removeEntry($home); $this->dashboard->removeEntry($home);

View File

@ -29,8 +29,8 @@ class RemovePaneForm extends BaseDashboardForm
protected function onSuccess() protected function onSuccess()
{ {
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
try { try {
$home->removeEntry($pane); $home->removeEntry($pane);

View File

@ -6,7 +6,7 @@ use Icinga\Web\Widget\SearchDashboard;
$searchDashboard = new SearchDashboard(); $searchDashboard = new SearchDashboard();
$searchDashboard->setUser($this->Auth()->getUser()); $searchDashboard->setUser($this->Auth()->getUser());
$searchPane = $searchDashboard->search('dummy')->getActiveHome()->getEntry(SearchDashboard::SEARCH_PANE); $searchPane = $searchDashboard->search('dummy')->getActiveEntry()->getEntry(SearchDashboard::SEARCH_PANE);
if ($searchPane->hasEntries()): ?> if ($searchPane->hasEntries()): ?>
<form action="<?= $this->href('search') ?>" method="get" role="search" class="search-control"> <form action="<?= $this->href('search') ?>" method="get" role="search" class="search-control">
<input type="text" name="q" id="search" class="search search-input" required <input type="text" name="q" id="search" class="search search-input" required

View File

@ -75,7 +75,6 @@ class DashletManager
->setPriority($priority++) ->setPriority($priority++)
->setModule($module->getName()); ->setModule($module->getName());
// As we don't have a setter for labels, this might be ignored by the data extractor
if (isset($configPart['label'])) { if (isset($configPart['label'])) {
$dashlet->setTitle($configPart['label']); $dashlet->setTitle($configPart['label']);
} }

View File

@ -53,6 +53,20 @@ abstract class BaseDashboard
*/ */
protected $owner; protected $owner;
/**
* A flag whether this widget is currently being loaded
*
* @var bool
*/
protected $active = false;
/**
* A flag whether this widget has been disabled (affects only default home)
*
* @var bool
*/
protected $disabled = false;
/** /**
* Create a new widget * Create a new widget
* *
@ -232,6 +246,57 @@ abstract class BaseDashboard
return $this; return $this;
} }
/**
* Set whether this widget should be disabled
*
* @param bool $disabled
*
* @return $this
*/
public function setDisabled(bool $disabled): self
{
$this->disabled = $disabled;
return $this;
}
/**
* Get whether this widget has been disabled
*
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* Set whether this widget is currently being loaded
*
* @param bool $active
*
* @return $this
*/
public function setActive(bool $active = true): self
{
$this->active = $active;
return $this;
}
/**
* Get whether this widget is currently being loaded
*
* Indicates which dashboard tab is currently open if this widget is a Dashboard Pane type
* or whether the Dashboard Home is active/focused in the navigation bar
*
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
/** /**
* Get this class's structure as array * Get this class's structure as array
* *

View File

@ -58,6 +58,36 @@ trait DashboardEntries
return $this; return $this;
} }
public function activateEntry(BaseDashboard $dashboard)
{
if (! $this->hasEntry($dashboard->getName())) {
throw new ProgrammingError(
'Trying to activate Dashboard Entry "%s" that does not exist.',
$dashboard->getTitle()
);
}
$activeEntry = $this->getActiveEntry();
if ($activeEntry && $activeEntry->getName() !== $dashboard->getName()) {
$activeEntry->setActive(false);
}
$dashboard->setActive();
return $this;
}
public function getActiveEntry()
{
foreach ($this->getEntries() as $entry) {
if ($entry->isActive()) {
return $entry;
}
}
return null;
}
public function addEntry(BaseDashboard $dashboard) public function addEntry(BaseDashboard $dashboard)
{ {
if ($this->hasEntry($dashboard->getName())) { if ($this->hasEntry($dashboard->getName())) {

View File

@ -148,4 +148,20 @@ interface DashboardEntry
* @return $this * @return $this
*/ */
public function unsetEntry(BaseDashboard $dashboard); public function unsetEntry(BaseDashboard $dashboard);
/**
* Activates the given dashboard entry and deactivates all other active ones
*
* @param BaseDashboard $dashboard
*
* @return $this
*/
public function activateEntry(BaseDashboard $dashboard);
/**
* Get the active dashboard entry currently being loaded
*
* @return ?DashboardEntry
*/
public function getActiveEntry();
} }

View File

@ -61,7 +61,7 @@ trait DashboardManager
} }
if ($home) { if ($home) {
$this->activateHome($home); $this->activateEntry($home);
$home->loadDashboardEntries($activePane); $home->loadDashboardEntries($activePane);
} }
@ -90,49 +90,6 @@ trait DashboardManager
return $this; return $this;
} }
/**
* Activates the given home and deactivates all other active homes
*
* @param DashboardHome $home
*
* @return $this
*/
public function activateHome(DashboardHome $home): self
{
if (! $this->hasEntry($home->getName())) {
throw new ProgrammingError(
'Trying to activate Dashboard Home "%s" that does not exist.',
$home->getTitle()
);
}
$activeHome = $this->getActiveHome();
if ($activeHome && $activeHome->getName() !== $home->getName()) {
$activeHome->setActive(false);
}
$home->setActive();
return $this;
}
/**
* Get the active home currently being loaded
*
* @return ?DashboardHome
*/
public function getActiveHome(): ?DashboardHome
{
/** @var DashboardHome $home */
foreach ($this->getEntries() as $home) {
if ($home->isActive()) {
return $home;
}
}
return null;
}
public function removeEntry($home) public function removeEntry($home)
{ {
$name = $home instanceof DashboardHome ? $home->getName() : $home; $name = $home instanceof DashboardHome ? $home->getName() : $home;

View File

@ -1,73 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Icinga\Web\Dashboard\Common;
trait WidgetState
{
/**
* A flag whether this widget is currently being loaded
*
* @var bool
*/
protected $active = false;
/**
* A flag whether this widget has been disabled (affects only default home)
*
* @var bool
*/
protected $disabled = false;
/**
* Set whether this widget should be disabled
*
* @param bool $disabled
*
* @return $this
*/
public function setDisabled(bool $disabled): self
{
$this->disabled = $disabled;
return $this;
}
/**
* Get whether this widget has been disabled
*
* @return bool
*/
public function isDisabled(): bool
{
return $this->disabled;
}
/**
* Set whether this widget is currently being loaded
*
* @param bool $active
*
* @return $this
*/
public function setActive(bool $active = true): self
{
$this->active = $active;
return $this;
}
/**
* Get whether this widget is currently being loaded
*
* Indicates which dashboard tab is currently open if this widget is a Dashboard Pane type
* or whether the Dashboard Home is active/focused in the navigation bar
*
* @return bool
*/
public function isActive(): bool
{
return $this->active;
}
}

View File

@ -113,7 +113,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
*/ */
public function getTabs() public function getTabs()
{ {
$activeHome = $this->getActiveHome(); $activeHome = $this->getActiveEntry();
if ($activeHome && ! $activeHome->isDefaultHome()) { if ($activeHome && ! $activeHome->isDefaultHome()) {
$url = Url::fromPath(self::BASE_ROUTE . '/home')->getUrlWithout(['home', $this->tabParam]); $url = Url::fromPath(self::BASE_ROUTE . '/home')->getUrlWithout(['home', $this->tabParam]);
$url->addParams(['home' => $activeHome->getName()]); $url->addParams(['home' => $activeHome->getName()]);
@ -139,7 +139,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
'active' => $pane->isActive(), 'active' => $pane->isActive(),
'title' => sprintf(t('Show %s', 'dashboard.pane.tooltip'), $pane->getTitle()), 'title' => sprintf(t('Show %s', 'dashboard.pane.tooltip'), $pane->getTitle()),
'label' => $pane->getTitle(), 'label' => $pane->getTitle(),
'url' => clone($url), 'url' => clone $url,
'urlParams' => [$this->tabParam => $pane->getName()] 'urlParams' => [$this->tabParam => $pane->getName()]
] ]
); );
@ -151,7 +151,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
protected function assemble() protected function assemble()
{ {
$activeHome = $this->getActiveHome(); $activeHome = $this->getActiveEntry();
if (! $activeHome || (! $activeHome->hasEntries() && $activeHome->isDefaultHome())) { if (! $activeHome || (! $activeHome->hasEntries() && $activeHome->isDefaultHome())) {
$this->setAttribute('class', 'dashboard-introduction'); $this->setAttribute('class', 'dashboard-introduction');
@ -174,7 +174,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
); );
$this->addHtml(HtmlElement::create('p', null, $message)); $this->addHtml(HtmlElement::create('p', null, $message));
} elseif ($activeHome->hasEntries()) { } elseif ($activeHome->hasEntries()) {
$activePane = $activeHome->getActivePane(); $activePane = $activeHome->getActiveEntry();
if (! $activePane->hasEntries()) { if (! $activePane->hasEntries()) {
$this->addHtml(HtmlElement::create('h1', null, t('No dashlet added to this pane.'))); $this->addHtml(HtmlElement::create('h1', null, t('No dashlet added to this pane.')));

View File

@ -13,7 +13,6 @@ use Icinga\Web\Dashboard\Common\DashboardEntries;
use Icinga\Web\Dashboard\Common\DashboardEntry; use Icinga\Web\Dashboard\Common\DashboardEntry;
use Icinga\Web\Dashboard\Common\Sortable; use Icinga\Web\Dashboard\Common\Sortable;
use Icinga\Util\DBUtils; use Icinga\Util\DBUtils;
use Icinga\Web\Dashboard\Common\WidgetState;
use Icinga\Web\Navigation\NavigationItem; use Icinga\Web\Navigation\NavigationItem;
use ipl\Stdlib\Filter; use ipl\Stdlib\Filter;
@ -27,7 +26,6 @@ use function ipl\Stdlib\get_php_type;
class DashboardHome extends BaseDashboard implements DashboardEntry, Sortable class DashboardHome extends BaseDashboard implements DashboardEntry, Sortable
{ {
use DashboardEntries; use DashboardEntries;
use WidgetState;
/** /**
* Name of the default home * Name of the default home
@ -104,49 +102,6 @@ class DashboardHome extends BaseDashboard implements DashboardEntry, Sortable
return $this->type; return $this->type;
} }
/**
* Activate the given pane and deactivate all the others
*
* @param Pane $pane
*
* @return $this
*/
public function activatePane(Pane $pane): self
{
if (! $this->hasEntry($pane->getName())) {
throw new ProgrammingError(
'Trying to activate Dashboard Pane "%s" that does not exist.',
$pane->getTitle()
);
}
$active = $this->getActivePane();
if ($active && $active->getName() !== $pane->getName()) {
$active->setActive(false);
}
$pane->setActive();
return $this;
}
/**
* Determine the active pane currently being loaded
*
* @return ?Pane
*/
public function getActivePane(): ?Pane
{
/** @var Pane $pane */
foreach ($this->getEntries() as $pane) {
if ($pane->isActive()) {
return $pane;
}
}
return null;
}
public function removeEntry($pane) public function removeEntry($pane)
{ {
$name = $pane instanceof Pane ? $pane->getName() : $pane; $name = $pane instanceof Pane ? $pane->getName() : $pane;
@ -193,13 +148,13 @@ class DashboardHome extends BaseDashboard implements DashboardEntry, Sortable
if ($this->hasEntry($name)) { if ($this->hasEntry($name)) {
$pane = $this->getEntry($name); $pane = $this->getEntry($name);
$this->activatePane($pane); $this->activateEntry($pane);
$pane->loadDashboardEntries(); $pane->loadDashboardEntries();
} else { } else {
throw new HttpNotFoundException(t('Pane "%s" not found'), $name); throw new HttpNotFoundException(t('Pane "%s" not found'), $name);
} }
} elseif (($firstPane = $this->rewindEntries())) { } elseif (($firstPane = $this->rewindEntries())) {
$this->activatePane($firstPane); $this->activateEntry($firstPane);
$firstPane->loadDashboardEntries(); $firstPane->loadDashboardEntries();
} }

View File

@ -6,7 +6,6 @@ namespace Icinga\Web\Dashboard;
use Icinga\Application\Icinga; use Icinga\Application\Icinga;
use Icinga\Web\Dashboard\Common\BaseDashboard; use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Web\Dashboard\Common\WidgetState;
use Icinga\Web\Url; use Icinga\Web\Url;
use ipl\Html\BaseHtmlElement; use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement; use ipl\Html\HtmlElement;
@ -18,8 +17,6 @@ use ipl\Web\Widget\Link;
*/ */
class Dashlet extends BaseDashboard class Dashlet extends BaseDashboard
{ {
use WidgetState;
/** @var string Database table name */ /** @var string Database table name */
public const TABLE = 'icingaweb_dashlet'; public const TABLE = 'icingaweb_dashlet';

View File

@ -14,7 +14,6 @@ use Icinga\Web\Dashboard\Common\DashboardEntries;
use Icinga\Util\DBUtils; use Icinga\Util\DBUtils;
use Icinga\Web\Dashboard\Common\DashboardEntry; use Icinga\Web\Dashboard\Common\DashboardEntry;
use Icinga\Web\Dashboard\Common\Sortable; use Icinga\Web\Dashboard\Common\Sortable;
use Icinga\Web\Dashboard\Common\WidgetState;
use InvalidArgumentException; use InvalidArgumentException;
use ipl\Stdlib\Filter; use ipl\Stdlib\Filter;
use LogicException; use LogicException;
@ -28,7 +27,6 @@ use function ipl\Stdlib\get_php_type;
class Pane extends BaseDashboard implements DashboardEntry, Sortable class Pane extends BaseDashboard implements DashboardEntry, Sortable
{ {
use DashboardEntries; use DashboardEntries;
use WidgetState;
public const TABLE = 'icingaweb_dashboard'; public const TABLE = 'icingaweb_dashboard';
@ -132,11 +130,8 @@ class Pane extends BaseDashboard implements DashboardEntry, Sortable
$newDashlet->isModuleDashlet() $newDashlet->isModuleDashlet()
&& $dashlet->system_module_dashlet_id === null && $dashlet->system_module_dashlet_id === null
) )
) { ) { // The module from which this dashlet originates doesn't exist anymore
// The module from which this dashlet originates doesn't exist anymore
$this->removeEntry($newDashlet); $this->removeEntry($newDashlet);
$this->unsetEntry($newDashlet);
} elseif (! $newDashlet->isDisabled() && ! Modules\DashletManager::isUsable($newDashlet)) { } elseif (! $newDashlet->isDisabled() && ! Modules\DashletManager::isUsable($newDashlet)) {
// The module from which this dashlet originates is probably disabled, // The module from which this dashlet originates is probably disabled,
// so don't load this dashlet anymore and disable it // so don't load this dashlet anymore and disable it
@ -250,7 +245,7 @@ class Pane extends BaseDashboard implements DashboardEntry, Sortable
'description' => $dashlet->getDescription() 'description' => $dashlet->getDescription()
], $filterCondition); ], $filterCondition);
} else { } else {
// Failed to move the pane! Should have already been handled by the caller, // Failed to move the dashlet! Should have already been handled by the caller,
// though I think it's better that we raise an exception here!! // though I think it's better that we raise an exception here!!
throw new AlreadyExistsException( throw new AlreadyExistsException(
'Failed to successfully manage the Dashlet. Pane "%s" has already a Dashlet called "%s"!', 'Failed to successfully manage the Dashlet. Pane "%s" has already a Dashlet called "%s"!',

View File

@ -23,7 +23,7 @@ class Settings extends BaseHtmlElement
protected function assemble() protected function assemble()
{ {
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveEntry();
if ($this->dashboard->countEntries() === 1 && $activeHome->isDefaultHome()) { if ($this->dashboard->countEntries() === 1 && $activeHome->isDefaultHome()) {
$this->setAttribute('data-icinga-home', DashboardHome::DEFAULT_HOME); $this->setAttribute('data-icinga-home', DashboardHome::DEFAULT_HOME);
$this->addFrom((new DashboardHomeList($activeHome))->setHeaderDisabled()); $this->addFrom((new DashboardHomeList($activeHome))->setHeaderDisabled());

View File

@ -59,7 +59,7 @@ class SearchDashboard extends Dashboard
return $this->tabs; return $this->tabs;
} }
public function getActiveHome(): ?DashboardHome public function getActiveEntry()
{ {
return $this->searchHome; return $this->searchHome;
} }

View File

@ -30,8 +30,8 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$this->assertCount( $this->assertCount(
2, 2,
@ -60,8 +60,8 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$this->assertCount( $this->assertCount(
2, 2,
@ -85,16 +85,16 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$pane->getEntry(self::TEST_DASHLET)->setTitle('Hello'); $pane->getEntry(self::TEST_DASHLET)->setTitle('Hello');
$pane->manageEntry($pane->getEntries()); $pane->manageEntry($pane->getEntries());
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$this->assertEquals( $this->assertEquals(
'Hello', 'Hello',
@ -120,8 +120,8 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$default = $this->dashboard->getActiveHome(); $default = $this->dashboard->getActiveEntry();
$pane1 = $default->getActivePane(); $pane1 = $default->getActiveEntry();
$pane2 = $default->getEntry('Test2'); $pane2 = $default->getEntry('Test2');
// Move the dashlet from pane1 -> pane2 // Move the dashlet from pane1 -> pane2
@ -129,8 +129,8 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$default = $this->dashboard->getActiveHome(); $default = $this->dashboard->getActiveEntry();
$pane1 = $default->getActivePane(); $pane1 = $default->getActiveEntry();
$pane2 = $default->getEntry('Test2'); $pane2 = $default->getEntry('Test2');
$pane2->loadDashboardEntries(); $pane2->loadDashboardEntries();
@ -166,23 +166,23 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME, $pane1->getName(), true); $this->dashboard->load(self::TEST_HOME, $pane1->getName(), true);
$default = $this->dashboard->getActiveHome(); $default = $this->dashboard->getActiveEntry();
$home = $this->dashboard->getEntry($home->getName()); $home = $this->dashboard->getEntry($home->getName());
$home->loadDashboardEntries(); $home->loadDashboardEntries();
$pane1 = $default->getActivePane(); $pane1 = $default->getActiveEntry();
$pane2 = $home->getActivePane(); $pane2 = $home->getActiveEntry();
// Move the dashlet from pane1 -> pane2 // Move the dashlet from pane1 -> pane2
$pane2->manageEntry($pane1->getEntries(), $pane1); $pane2->manageEntry($pane1->getEntries(), $pane1);
$this->dashboard->load(self::TEST_HOME, $pane1->getName(), true); $this->dashboard->load(self::TEST_HOME, $pane1->getName(), true);
//$default = $this->dashboard->getActiveHome(); //$default = $this->dashboard->getActiveEntry();
$home = $this->dashboard->getEntry($home->getName()); $home = $this->dashboard->getEntry($home->getName());
$home->loadDashboardEntries(); $home->loadDashboardEntries();
$pane2 = $home->getActivePane(); $pane2 = $home->getActiveEntry();
$this->assertCount( $this->assertCount(
1, 1,
@ -218,8 +218,8 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load($default->getName()); $this->dashboard->load($default->getName());
$default = $this->dashboard->getActiveHome(); $default = $this->dashboard->getActiveEntry();
$pane1 = $default->getActivePane(); $pane1 = $default->getActiveEntry();
$pane2 = $default->getEntry('Test2'); $pane2 = $default->getEntry('Test2');
$pane2->loadDashboardEntries(); $pane2->loadDashboardEntries();
@ -246,17 +246,17 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME, $pane->getName()); $this->dashboard->load(self::TEST_HOME, $pane->getName());
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$pane->removeEntry(self::TEST_DASHLET); $pane->removeEntry(self::TEST_DASHLET);
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertFalse( $this->assertFalse(
$home->getActivePane()->hasEntry(self::TEST_DASHLET), $home->getActiveEntry()->hasEntry(self::TEST_DASHLET),
'Pane::removeEntry() could not remove expected Dashlet' 'Pane::removeEntry() could not remove expected Dashlet'
); );
} }
@ -276,17 +276,17 @@ class DashletTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME, $pane->getName()); $this->dashboard->load(self::TEST_HOME, $pane->getName());
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$pane = $home->getActivePane(); $pane = $home->getActiveEntry();
$pane->removeEntries(); $pane->removeEntries();
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertFalse( $this->assertFalse(
$home->getActivePane()->hasEntries(), $home->getActiveEntry()->hasEntries(),
'Pane::removeEntries() could not remove all Dashlet Entries' 'Pane::removeEntries() could not remove all Dashlet Entries'
); );
} }

View File

@ -30,13 +30,13 @@ class HomeTest extends BaseDashboardTestCase
$this->dashboard->manageEntry($this->getTestHome()); $this->dashboard->manageEntry($this->getTestHome());
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$home->setTitle('Hello'); $home->setTitle('Hello');
$this->dashboard->manageEntry($home); $this->dashboard->manageEntry($home);
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertEquals( $this->assertEquals(
'Hello', 'Hello',
@ -125,7 +125,7 @@ class HomeTest extends BaseDashboardTestCase
$this->assertEquals( $this->assertEquals(
'Second Home', 'Second Home',
$this->dashboard->getActiveHome()->getName(), $this->dashboard->getActiveEntry()->getName(),
'Dashboard::load() could not load all expected Dashboard Homes and activate expected Dashboard Home' 'Dashboard::load() could not load all expected Dashboard Homes and activate expected Dashboard Home'
); );
} }
@ -134,7 +134,7 @@ class HomeTest extends BaseDashboardTestCase
{ {
$this->expectException(ProgrammingError::class); $this->expectException(ProgrammingError::class);
$this->dashboard->activateHome($this->getTestHome('Activate Home')); $this->dashboard->activateEntry($this->getTestHome('Activate Home'));
} }
public function testWhetherLoadHomesActivatesFirstHome() public function testWhetherLoadHomesActivatesFirstHome()
@ -145,7 +145,7 @@ class HomeTest extends BaseDashboardTestCase
$this->assertEquals( $this->assertEquals(
self::TEST_HOME, self::TEST_HOME,
$this->dashboard->getActiveHome()->getName(), $this->dashboard->getActiveEntry()->getName(),
'Dashboard::load() could not activate expected Dashboard Home' 'Dashboard::load() could not activate expected Dashboard Home'
); );
} }
@ -159,9 +159,9 @@ class HomeTest extends BaseDashboardTestCase
$this->dashboard->load(); $this->dashboard->load();
$active = $this->dashboard->getEntry('Second Home'); $active = $this->dashboard->getEntry('Second Home');
$this->dashboard->activateHome($active); $this->dashboard->activateEntry($active);
$this->assertTrue($active->isActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home'); $this->assertTrue($active->isActive(), 'Dashboard::activateEntry() could not activate expected Dashboard Home');
} }
/** /**
@ -173,12 +173,12 @@ class HomeTest extends BaseDashboardTestCase
$this->dashboard->addEntry($this->getTestHome('Second Home')); $this->dashboard->addEntry($this->getTestHome('Second Home'));
$active = $this->dashboard->getEntry(self::TEST_HOME); $active = $this->dashboard->getEntry(self::TEST_HOME);
$this->dashboard->activateHome($active); $this->dashboard->activateEntry($active);
$this->assertEquals( $this->assertEquals(
self::TEST_HOME, self::TEST_HOME,
$this->dashboard->getActiveHome()->getName(), $this->dashboard->getActiveEntry()->getName(),
'Dashboard::getActiveHome() could not return expected Dashboard Home' 'Dashboard::getActiveEntry() could not return expected Dashboard Home'
); );
} }
} }

View File

@ -24,7 +24,7 @@ class PaneTest extends BaseDashboardTestCase
$this->expectException(ProgrammingError::class); $this->expectException(ProgrammingError::class);
$home = $this->getTestHome(); $home = $this->getTestHome();
$home->activatePane(new Pane(self::TEST_PANE)); $home->activateEntry(new Pane(self::TEST_PANE));
} }
public function testWhetherActivatePaneActivatesExpectedPane() public function testWhetherActivatePaneActivatesExpectedPane()
@ -32,11 +32,11 @@ class PaneTest extends BaseDashboardTestCase
$home = $this->getTestHome(); $home = $this->getTestHome();
$home->addEntry($this->getTestPane()); $home->addEntry($this->getTestPane());
$home->activatePane($home->getEntry(self::TEST_PANE)); $home->activateEntry($home->getEntry(self::TEST_PANE));
$this->assertEquals( $this->assertEquals(
self::TEST_PANE, self::TEST_PANE,
$home->getActivePane()->getName(), $home->getActiveEntry()->getName(),
'DashboardHome::activatePane() could not activate expected Pane' 'DashboardHome::activatePane() could not activate expected Pane'
); );
} }
@ -53,11 +53,11 @@ class PaneTest extends BaseDashboardTestCase
$home->manageEntry($this->getTestPane('Test Me')); $home->manageEntry($this->getTestPane('Test Me'));
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertEquals( $this->assertEquals(
self::TEST_PANE, self::TEST_PANE,
$home->getActivePane()->getName(), $home->getActiveEntry()->getName(),
'DashboardHome::loadDashboardEntries() could not activate expected Pane' 'DashboardHome::loadDashboardEntries() could not activate expected Pane'
); );
} }
@ -73,11 +73,11 @@ class PaneTest extends BaseDashboardTestCase
$home->manageEntry(new Pane(self::TEST_PANE)); $home->manageEntry(new Pane(self::TEST_PANE));
$this->dashboard->load(self::TEST_HOME, self::TEST_PANE); $this->dashboard->load(self::TEST_HOME, self::TEST_PANE);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertEquals( $this->assertEquals(
self::TEST_PANE, self::TEST_PANE,
$home->getActivePane()->getName(), $home->getActiveEntry()->getName(),
'DashboardHome::loadDashboardEntries() could not load and activate expected Pane' 'DashboardHome::loadDashboardEntries() could not load and activate expected Pane'
); );
} }
@ -85,18 +85,18 @@ class PaneTest extends BaseDashboardTestCase
/** /**
* @depends testWhetherActivatePaneActivatesAPaneEntry * @depends testWhetherActivatePaneActivatesAPaneEntry
*/ */
public function testWhetherGetActivePaneGetsExpectedPane() public function testWhethergetActiveEntryGetsExpectedPane()
{ {
$home = $this->getTestHome(); $home = $this->getTestHome();
$home->addEntry($this->getTestPane()); $home->addEntry($this->getTestPane());
$home->addEntry($this->getTestPane('Test Me')); $home->addEntry($this->getTestPane('Test Me'));
$home->activatePane($home->getEntry('Test Me')); $home->activateEntry($home->getEntry('Test Me'));
$this->assertEquals( $this->assertEquals(
'Test Me', 'Test Me',
$home->getActivePane()->getName(), $home->getActiveEntry()->getName(),
'DashboardHome::getActivePane() could not determine valid active pane' 'DashboardHome::getActiveEntry() could not determine valid active pane'
); );
} }
@ -111,7 +111,7 @@ class PaneTest extends BaseDashboardTestCase
$home->manageEntry($this->getTestPane()); $home->manageEntry($this->getTestPane());
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertCount( $this->assertCount(
1, 1,
@ -132,17 +132,17 @@ class PaneTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$home->getActivePane()->setTitle('Hello'); $home->getActiveEntry()->setTitle('Hello');
$home->manageEntry($home->getEntries()); $home->manageEntry($home->getEntries());
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertEquals( $this->assertEquals(
'Hello', 'Hello',
$home->getActivePane()->getTitle(), $home->getActiveEntry()->getTitle(),
'DashboardHome::manageEntry() could not update existing Pane' 'DashboardHome::manageEntry() could not update existing Pane'
); );
} }
@ -159,14 +159,14 @@ class PaneTest extends BaseDashboardTestCase
$this->dashboard->load('Second Home', null, true); $this->dashboard->load('Second Home', null, true);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
/** @var DashboardHome $default */ /** @var DashboardHome $default */
$default = $this->dashboard->getEntry(self::TEST_HOME); $default = $this->dashboard->getEntry(self::TEST_HOME);
$default->manageEntry($home->getEntry(self::TEST_PANE), $home); $default->manageEntry($home->getEntry(self::TEST_PANE), $home);
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$default = $this->dashboard->getActiveHome(); $default = $this->dashboard->getActiveEntry();
$this->assertCount( $this->assertCount(
1, 1,
@ -191,7 +191,7 @@ class PaneTest extends BaseDashboardTestCase
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$default = $this->dashboard->getEntry(self::TEST_HOME); $default = $this->dashboard->getEntry(self::TEST_HOME);
$default->loadDashboardEntries(); $default->loadDashboardEntries();
@ -215,12 +215,12 @@ class PaneTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME, self::TEST_PANE); $this->dashboard->load(self::TEST_HOME, self::TEST_PANE);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$home->removeEntry(self::TEST_PANE); $home->removeEntry(self::TEST_PANE);
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertFalse( $this->assertFalse(
$home->hasEntry(self::TEST_PANE), $home->hasEntry(self::TEST_PANE),
@ -240,12 +240,12 @@ class PaneTest extends BaseDashboardTestCase
$this->dashboard->load(self::TEST_HOME); $this->dashboard->load(self::TEST_HOME);
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$home->removeEntries(); $home->removeEntries();
$this->dashboard->load(); $this->dashboard->load();
$home = $this->dashboard->getActiveHome(); $home = $this->dashboard->getActiveEntry();
$this->assertFalse( $this->assertFalse(
$home->hasEntries(), $home->hasEntries(),

View File

@ -28,7 +28,7 @@ class SearchDashboardTest extends BaseDashboardTestCase
$dashboard->setUser($this->getUser()); $dashboard->setUser($this->getUser());
$dashboard->search('pending'); $dashboard->search('pending');
$searchHome = $dashboard->getActiveHome(); $searchHome = $dashboard->getActiveEntry();
$searchHome->getEntry(SearchDashboard::SEARCH_PANE)->setEntries([]); $searchHome->getEntry(SearchDashboard::SEARCH_PANE)->setEntries([]);
$dashboard->render(); $dashboard->render();
} }
@ -39,7 +39,7 @@ class SearchDashboardTest extends BaseDashboardTestCase
$dashboard->setUser($this->getUser()); $dashboard->setUser($this->getUser());
$dashboard->search('pending'); $dashboard->search('pending');
$searchHome = $dashboard->getActiveHome(); $searchHome = $dashboard->getActiveEntry();
$result = $searchHome->getEntry(SearchDashboard::SEARCH_PANE)->hasEntry('Hosts: pending'); $result = $searchHome->getEntry(SearchDashboard::SEARCH_PANE)->hasEntry('Hosts: pending');
$this->assertTrue($result, 'SearchDashboard::search() could not load search dashlets from modules'); $this->assertTrue($result, 'SearchDashboard::search() could not load search dashlets from modules');
@ -51,7 +51,7 @@ class SearchDashboardTest extends BaseDashboardTestCase
$dashboard->setUser($this->getUser()); $dashboard->setUser($this->getUser());
$dashboard->search(); $dashboard->search();
$searchHome = $dashboard->getActiveHome(); $searchHome = $dashboard->getActiveEntry();
$result = $searchHome->getEntry(SearchDashboard::SEARCH_PANE)->hasEntry('Ready to search'); $result = $searchHome->getEntry(SearchDashboard::SEARCH_PANE)->hasEntry('Ready to search');
$this->assertTrue($result, 'SearchDashboard::search() could not get hint for search'); $this->assertTrue($result, 'SearchDashboard::search() could not get hint for search');