From b619dd21860f2fa170e54a53978149bf8bc41f2e Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 5 Aug 2022 17:02:46 +0200 Subject: [PATCH] Add `activateEntry()` & `getActiveEntry()` interface methods --- .../controllers/DashboardsController.php | 18 ++--- .../forms/Dashboard/BaseDashboardForm.php | 2 +- application/forms/Dashboard/DashletForm.php | 16 ++-- application/forms/Dashboard/HomeForm.php | 2 +- application/forms/Dashboard/PaneForm.php | 4 +- .../forms/Dashboard/RemoveDashletForm.php | 4 +- .../forms/Dashboard/RemoveHomeForm.php | 4 +- .../forms/Dashboard/RemovePaneForm.php | 4 +- application/views/scripts/layout/menu.phtml | 2 +- .../Application/Modules/DashletManager.php | 1 - .../Web/Dashboard/Common/BaseDashboard.php | 65 +++++++++++++++++ .../Web/Dashboard/Common/DashboardEntries.php | 30 ++++++++ .../Web/Dashboard/Common/DashboardEntry.php | 16 ++++ .../Web/Dashboard/Common/DashboardManager.php | 45 +----------- .../Web/Dashboard/Common/WidgetState.php | 73 ------------------- library/Icinga/Web/Dashboard/Dashboard.php | 8 +- .../Icinga/Web/Dashboard/DashboardHome.php | 49 +------------ library/Icinga/Web/Dashboard/Dashlet.php | 3 - library/Icinga/Web/Dashboard/Pane.php | 9 +-- library/Icinga/Web/Dashboard/Settings.php | 2 +- library/Icinga/Web/Widget/SearchDashboard.php | 2 +- .../Icinga/Web/Dashboard/DashletTest.php | 54 +++++++------- .../library/Icinga/Web/Dashboard/HomeTest.php | 20 ++--- .../library/Icinga/Web/Dashboard/PaneTest.php | 46 ++++++------ .../Icinga/Web/Widget/SearchDashboardTest.php | 6 +- 25 files changed, 213 insertions(+), 272 deletions(-) delete mode 100644 library/Icinga/Web/Dashboard/Common/WidgetState.php diff --git a/application/controllers/DashboardsController.php b/application/controllers/DashboardsController.php index 1d91e6274..7c9dd0157 100644 --- a/application/controllers/DashboardsController.php +++ b/application/controllers/DashboardsController.php @@ -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 $this->dashboard->load(DashboardHome::DEFAULT_HOME, $pane, true); - $activeHome = $this->dashboard->getActiveHome(); + $activeHome = $this->dashboard->getActiveEntry(); if (! $activeHome || ! $activeHome->hasEntries()) { $this->addTitleTab(t('Welcome')); @@ -89,7 +89,7 @@ class DashboardsController extends CompatController $this->dashboard->load($home, $pane); - $activeHome = $this->dashboard->getActiveHome(); + $activeHome = $this->dashboard->getActiveEntry(); if (! $activeHome->getEntries()) { $this->addTitleTab($activeHome->getTitle()); } @@ -133,7 +133,7 @@ class DashboardsController extends CompatController }) ->handleRequest($this->getServerRequest()); - $homeForm->load($this->dashboard->getActiveHome()); + $homeForm->load($this->dashboard->getActiveEntry()); $this->addTitleTab(t('Update Home')); $this->addContent($homeForm); @@ -201,7 +201,7 @@ class DashboardsController extends CompatController } })->handleRequest($this->getServerRequest()); - $paneForm->load($this->dashboard->getActiveHome()->getEntry($pane)); + $paneForm->load($this->dashboard->getActiveEntry()->getEntry($pane)); $this->addTitleTab(t('Update Pane')); $this->addContent($paneForm); @@ -282,7 +282,7 @@ class DashboardsController extends CompatController $this->dashboard->load($home, $pane, true); - $pane = $this->dashboard->getActiveHome()->getActivePane(); + $pane = $this->dashboard->getActiveEntry()->getActiveEntry(); if (! $pane->hasEntry($dashlet)) { $this->httpNotFound(t('Dashlet "%s" not found'), $dashlet); } @@ -311,7 +311,7 @@ class DashboardsController extends CompatController $this->dashboard->load($home, $pane); - $pane = $this->dashboard->getActiveHome()->getActivePane(); + $pane = $this->dashboard->getActiveEntry()->getActiveEntry(); if (! $pane->hasEntry($dashlet)) { $this->httpNotFound(t('Dashlet "%s" not found'), $dashlet); } @@ -510,7 +510,7 @@ class DashboardsController extends CompatController $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 if (! $activeHome || (! $activeHome->hasEntries() && $this->dashboard->countEntries() === 1)) { $this->redirectNow(Dashboard::BASE_ROUTE); @@ -540,7 +540,7 @@ class DashboardsController extends CompatController private function createTabs() { $tabs = $this->dashboard->getTabs(); - $activeHome = $this->dashboard->getActiveHome(); + $activeHome = $this->dashboard->getActiveEntry(); if ($activeHome && ( ! $activeHome->isDefaultHome() @@ -553,7 +553,7 @@ class DashboardsController extends CompatController $params['home'] = $activeHome->getName(); } - if (($activePane = $activeHome->getActivePane())) { + if (($activePane = $activeHome->getActiveEntry())) { $params['pane'] = $activePane->getName(); } diff --git a/application/forms/Dashboard/BaseDashboardForm.php b/application/forms/Dashboard/BaseDashboardForm.php index 5222c9d1e..b2470ffb4 100644 --- a/application/forms/Dashboard/BaseDashboardForm.php +++ b/application/forms/Dashboard/BaseDashboardForm.php @@ -94,7 +94,7 @@ abstract class BaseDashboardForm extends CompatForm */ 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; } /** diff --git a/application/forms/Dashboard/DashletForm.php b/application/forms/Dashboard/DashletForm.php index a403e2f61..9b8e5c597 100644 --- a/application/forms/Dashboard/DashletForm.php +++ b/application/forms/Dashboard/DashletForm.php @@ -39,7 +39,7 @@ class DashletForm extends SetupNewDashboardForm protected function assembleNextPageDashboardPart() { $homes = $this->dashboard->getEntryKeyTitleArr(); - $activeHome = $this->dashboard->getActiveHome(); + $activeHome = $this->dashboard->getActiveEntry(); $currentHome = $this->requestUrl->getParam('home', reset($homes)); $populatedHome = $this->getPopulatedValue('home', $currentHome); @@ -177,16 +177,16 @@ class DashletForm extends SetupNewDashboardForm if ($dashboard->hasEntry($currentHome->getName())) { $currentHome = clone $dashboard->getEntry($currentHome->getName()); $activatePane = $currentHome->hasEntry($selectedPane) - && $currentHome->getActivePane()->getName() !== $selectedPane + && $currentHome->getActiveEntry()->getName() !== $selectedPane ? $selectedPane : null; - if ($currentHome->getName() !== $dashboard->getActiveHome()->getName() || $activatePane) { + if ($currentHome->getName() !== $dashboard->getActiveEntry()->getName() || $activatePane) { $currentHome->loadDashboardEntries($activatePane); } if ($currentHome->hasEntry($currentPane->getName())) { - $currentPane = clone $currentHome->getActivePane(); + $currentPane = clone $currentHome->getActiveEntry(); } } @@ -229,8 +229,8 @@ class DashletForm extends SetupNewDashboardForm } $this->dumpArbitaryDashlets(false); - // Avoid the hassle of iterating through the module dashlets each time to check if exits, - // even though the current pane doesn't have any entries + // Avoid the overhead of traversing the module Dashlets each time to check if they already exist, + // even the current Dashboard Pane doesn't contain any records if (! $this->getPopulatedValue('new_pane') && $currentPane->hasEntries()) { foreach ($this->moduleDashlets as $_ => $dashlets) { /** @var Dashlet $dashlet */ @@ -278,10 +278,10 @@ class DashletForm extends SetupNewDashboardForm } else { $orgHome = $dashboard->getEntry($this->getValue('org_home')); $orgPane = $orgHome->getEntry($this->getValue('org_pane')); - if ($orgHome->getActivePane()->getName() !== $orgPane->getName()) { + if ($orgHome->getActiveEntry()->getName() !== $orgPane->getName()) { $orgHome->loadDashboardEntries($orgPane->getName()); - $orgPane = $orgHome->getActivePane(); + $orgPane = $orgHome->getActiveEntry(); } $orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet')); diff --git a/application/forms/Dashboard/HomeForm.php b/application/forms/Dashboard/HomeForm.php index 1fe3c460c..413b11454 100644 --- a/application/forms/Dashboard/HomeForm.php +++ b/application/forms/Dashboard/HomeForm.php @@ -41,7 +41,7 @@ class HomeForm extends BaseDashboardForm protected function onSuccess() { if ($this->isUpdating()) { - $home = $this->dashboard->getActiveHome(); + $home = $this->dashboard->getActiveEntry(); if ($home->getTitle() === $this->getPopulatedValue('title')) { return; } diff --git a/application/forms/Dashboard/PaneForm.php b/application/forms/Dashboard/PaneForm.php index 4ba5c72af..310f5eedc 100644 --- a/application/forms/Dashboard/PaneForm.php +++ b/application/forms/Dashboard/PaneForm.php @@ -38,7 +38,7 @@ class PaneForm extends BaseDashboardForm ]); $homes = $this->dashboard->getEntryKeyTitleArr(); - $activeHome = $this->dashboard->getActiveHome(); + $activeHome = $this->dashboard->getActiveEntry(); $populatedHome = $this->getPopulatedValue('home', $activeHome->getName()); $this->addElement('select', 'home', [ @@ -90,7 +90,7 @@ class PaneForm extends BaseDashboardForm if ($dashboard->hasEntry($currentHome->getName())) { /** @var DashboardHome $currentHome */ $currentHome = clone $dashboard->getEntry($currentHome->getName()); - if ($currentHome->getName() !== $dashboard->getActiveHome()->getName()) { + if ($currentHome->getName() !== $dashboard->getActiveEntry()->getName()) { $currentHome->loadDashboardEntries(); } } diff --git a/application/forms/Dashboard/RemoveDashletForm.php b/application/forms/Dashboard/RemoveDashletForm.php index 38b1291f9..78de77963 100644 --- a/application/forms/Dashboard/RemoveDashletForm.php +++ b/application/forms/Dashboard/RemoveDashletForm.php @@ -30,8 +30,8 @@ class RemoveDashletForm extends BaseDashboardForm protected function onSuccess() { - $home = $this->dashboard->getActiveHome(); - $pane = $home->getActivePane(); + $home = $this->dashboard->getActiveEntry(); + $pane = $home->getActiveEntry(); $dashlet = $pane->getEntry($this->requestUrl->getParam('dashlet')); diff --git a/application/forms/Dashboard/RemoveHomeForm.php b/application/forms/Dashboard/RemoveHomeForm.php index 13dc2c717..e6219ec59 100644 --- a/application/forms/Dashboard/RemoveHomeForm.php +++ b/application/forms/Dashboard/RemoveHomeForm.php @@ -20,7 +20,7 @@ class RemoveHomeForm extends BaseDashboardForm $this->addHtml(HtmlElement::create( 'h2', 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')); @@ -28,7 +28,7 @@ class RemoveHomeForm extends BaseDashboardForm protected function onSuccess() { - $home = $this->dashboard->getActiveHome(); + $home = $this->dashboard->getActiveEntry(); try { $this->dashboard->removeEntry($home); diff --git a/application/forms/Dashboard/RemovePaneForm.php b/application/forms/Dashboard/RemovePaneForm.php index 9d7d61159..802d382ef 100644 --- a/application/forms/Dashboard/RemovePaneForm.php +++ b/application/forms/Dashboard/RemovePaneForm.php @@ -29,8 +29,8 @@ class RemovePaneForm extends BaseDashboardForm protected function onSuccess() { - $home = $this->dashboard->getActiveHome(); - $pane = $home->getActivePane(); + $home = $this->dashboard->getActiveEntry(); + $pane = $home->getActiveEntry(); try { $home->removeEntry($pane); diff --git a/application/views/scripts/layout/menu.phtml b/application/views/scripts/layout/menu.phtml index 1898b77c4..15249b4e3 100644 --- a/application/views/scripts/layout/menu.phtml +++ b/application/views/scripts/layout/menu.phtml @@ -6,7 +6,7 @@ use Icinga\Web\Widget\SearchDashboard; $searchDashboard = new SearchDashboard(); $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()): ?>