diff --git a/application/controllers/DashboardsController.php b/application/controllers/DashboardsController.php index 0fcaef920..c38fbee91 100644 --- a/application/controllers/DashboardsController.php +++ b/application/controllers/DashboardsController.php @@ -45,7 +45,8 @@ class DashboardsController extends CompatController public function indexAction() { - $this->dashboard->load(DashboardHome::DEFAULT_HOME); + $pane = $this->getParam('pane'); + $this->dashboard->load(DashboardHome::DEFAULT_HOME, $pane); $this->createTabs(); @@ -62,12 +63,8 @@ class DashboardsController extends CompatController $this->content->getAttributes()->add('class', 'welcome-view'); $this->dashboard->addHtml($welcomeForm); } else { - $pane = $this->getParam('pane'); - if (! $pane) { - $pane = $activeHome->getActivePane($this->dashboard->getTabs())->getName(); - } - - $this->dashboard->activate($pane); + $pane = $activeHome->getActivePane(); + $this->dashboard->activate($pane->getName()); } $this->addContent($this->dashboard); @@ -91,12 +88,8 @@ class DashboardsController extends CompatController $this->createTabs(); if ($activeHome->hasEntries()) { - $pane = $this->getParam('pane'); - if (! $pane) { - $pane = $activeHome->getActivePane($this->dashboard->getTabs())->getName(); - } - - $this->dashboard->activate($pane); + $pane = $activeHome->getActivePane(); + $this->dashboard->activate($pane->getName()); } $this->addContent($this->dashboard); @@ -154,7 +147,7 @@ class DashboardsController extends CompatController { $home = $this->params->getRequired('home'); - $this->dashboard->load(); + $this->dashboard->load($home, null, true); $paneForm = (new PaneForm($this->dashboard)) ->on(PaneForm::ON_SUCCESS, function () { @@ -171,7 +164,7 @@ class DashboardsController extends CompatController $home = $this->params->getRequired('home'); $pane = $this->params->getRequired('pane'); - $this->dashboard->load(); + $this->dashboard->load($home, $pane, true); if (! $this->dashboard->getActiveHome()->hasEntry($pane)) { $this->httpNotFound(t('Pane "%s" not found'), $pane); @@ -194,7 +187,7 @@ class DashboardsController extends CompatController $home = $this->params->getRequired('home'); $paneParam = $this->params->getRequired('pane'); - $this->dashboard->load($home); + $this->dashboard->load($home, $paneParam); if (! $this->dashboard->getActiveHome()->hasEntry($paneParam)) { $this->httpNotFound(t('Pane "%s" not found'), $paneParam); @@ -214,7 +207,7 @@ class DashboardsController extends CompatController { $home = $this->params->getRequired('home'); - $this->dashboard->load(); + $this->dashboard->load($home, null, true); $dashletForm = new DashletForm($this->dashboard); $dashletForm->populate($this->getRequest()->getPost()); @@ -479,7 +472,7 @@ class DashboardsController extends CompatController $pane = $this->params->getRequired('pane'); $dashlet = $this->params->getRequired('dashlet'); - $this->dashboard->load(); + $this->dashboard->load($home, $pane, true); if (! $this->dashboard->getActiveHome()->hasEntry($pane)) { $this->httpNotFound(t('Pane "%s" not found'), $pane); diff --git a/library/Icinga/Web/Dashboard/Common/DashboardManager.php b/library/Icinga/Web/Dashboard/Common/DashboardManager.php index 90ccd4457..bb5e6c8bd 100644 --- a/library/Icinga/Web/Dashboard/Common/DashboardManager.php +++ b/library/Icinga/Web/Dashboard/Common/DashboardManager.php @@ -12,7 +12,6 @@ use Icinga\Web\Dashboard\Dashboard; use Icinga\Web\Dashboard\DashboardHome; use Icinga\Util\DBUtils; use ipl\Stdlib\Filter; -use ipl\Web\Url; trait DashboardManager { @@ -25,12 +24,13 @@ trait DashboardManager * * @return void */ - public function load(string $name = null) + public function load(string $name = null, string $activePane = null, bool $loadAll = false) { $query = Model\Home::on(DBUtils::getConn()); $query->filter(Filter::equal('icingaweb_dashboard_owner.id', $this::getUser()->getAdditional('id'))); - if ($name !== null) { + $this->setEntries([]); + if ($name !== null && ! $loadAll) { $query->filter(Filter::equal('name', $name)); /** @var Model\Home $row */ @@ -46,22 +46,25 @@ trait DashboardManager } $this->activateHome($home); - $home->loadDashboardEntries(); + $home->loadDashboardEntries($activePane); } else { foreach ($query as $row) { $this->addEntry(DashboardHome::create($row)); } - $homeParam = Url::fromRequest()->getParam('home'); - if ($homeParam && $this->hasEntry($homeParam)) { - $firstHome = $this->getEntry($homeParam); + if ($name !== null && $loadAll) { + if (! $this->hasEntry($name)) { + throw new HttpNotFoundException(t('Home "%s" not found'), $name); + } + + $firstHome = $this->getEntry($name); } else { $firstHome = $this->rewindEntries(); } if ($firstHome) { $this->activateHome($firstHome); - $firstHome->loadDashboardEntries(); + $firstHome->loadDashboardEntries($activePane); } } @@ -97,6 +100,10 @@ trait DashboardManager */ 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->getName()); + } + $activeHome = $this->getActiveHome(); if ($activeHome && $activeHome->getName() !== $home->getName()) { $activeHome->setActive(false); @@ -116,7 +123,7 @@ trait DashboardManager { /** @var DashboardHome $home */ foreach ($this->getEntries() as $home) { - if ($home->getActive()) { + if ($home->isActive()) { return $home; } } diff --git a/library/Icinga/Web/Dashboard/Dashboard.php b/library/Icinga/Web/Dashboard/Dashboard.php index c32693e5d..ba9229f3a 100644 --- a/library/Icinga/Web/Dashboard/Dashboard.php +++ b/library/Icinga/Web/Dashboard/Dashboard.php @@ -4,8 +4,6 @@ namespace Icinga\Web\Dashboard; -use Icinga\Exception\ConfigurationError; -use Icinga\Exception\ProgrammingError; use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\DashboardEntry; use Icinga\Web\Dashboard\Common\DashboardManager; @@ -180,7 +178,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry } elseif (! $activeHome->hasEntries()) { $this->addHtml(HtmlElement::create('h1', null, t('No dashboard added to this dashboard home.'))); } else { - $activePane = $activeHome->getActivePane($this->getTabs()); + $activePane = $activeHome->getActivePane(); if (! $activePane->hasEntries()) { $this->addHtml(HtmlElement::create('h1', null, t('No dashlet added to this pane.'))); diff --git a/library/Icinga/Web/Dashboard/DashboardHome.php b/library/Icinga/Web/Dashboard/DashboardHome.php index 64ca07ea9..8a1cb06e8 100644 --- a/library/Icinga/Web/Dashboard/DashboardHome.php +++ b/library/Icinga/Web/Dashboard/DashboardHome.php @@ -5,6 +5,7 @@ namespace Icinga\Web\Dashboard; use Icinga\Exception\ConfigurationError; +use Icinga\Exception\Http\HttpNotFoundException; use Icinga\Exception\ProgrammingError; use Icinga\Model\Home; use Icinga\Web\Dashboard\Common\BaseDashboard; @@ -13,9 +14,8 @@ use Icinga\Web\Dashboard\Common\Sortable; use Icinga\Util\DBUtils; use Icinga\Web\Dashboard\Common\WidgetState; use ipl\Stdlib\Filter; - use ipl\Web\Url; -use ipl\Web\Widget\Tabs; + use function ipl\Stdlib\get_php_type; class DashboardHome extends BaseDashboard implements Sortable @@ -44,13 +44,6 @@ class DashboardHome extends BaseDashboard implements Sortable */ protected $type = Dashboard::SYSTEM; - /** - * A flag whether this home has been activated - * - * @var bool - */ - protected $active = false; - /** * Create a new dashboard home from the given model * @@ -71,32 +64,6 @@ class DashboardHome extends BaseDashboard implements Sortable return $self; } - /** - * Set whether this home is active - * - * DB dashboards will load only when this home has been activated - * - * @param bool $active - * - * @return $this - */ - public function setActive(bool $active = true): self - { - $this->active = $active; - - return $this; - } - - /** - * Get whether this home has been activated - * - * @return bool - */ - public function getActive(): bool - { - return $this->active; - } - /** * Set the type of this dashboard home * @@ -122,46 +89,43 @@ class DashboardHome extends BaseDashboard implements Sortable } /** - * @see determineActivePane() + * Activate the given pane and deactivate all the others + * + * @param Pane $pane + * + * @return $this */ - public function getActivePane(Tabs $tabs): Pane + public function activatePane(Pane $pane): self { - return $this->determineActivePane($tabs); + if (! $this->hasEntry($pane->getName())) { + throw new ProgrammingError('Trying to activate Dashboard Pane "%s" that does not exist.', $pane->getName()); + } + + $active = $this->getActivePane(); + if ($active && $active->getName() !== $pane->getName()) { + $active->setActive(false); + } + + $pane->setActive(true); + + return $this; } /** - * Determine the active pane either by the selected tab or the current request + * Determine the active pane currently being loaded * - * @param Tabs $tabs - * - * @return Pane + * @return ?Pane */ - public function determineActivePane(Tabs $tabs): Pane + public function getActivePane() { - $activeTab = $tabs->getActiveTab(); - if ($activeTab) { - $pane = $activeTab->getName(); - } else { - if (! ($pane = Url::fromRequest()->getParam('pane'))) { - if (($firstPane = $this->rewindEntries())) { - $tabs->activate($firstPane->getName()); - - $pane = $firstPane->getName(); - } - } else { - if ($this->hasEntry($pane)) { - $tabs->activate($pane); - } else { - throw new ProgrammingError('Try to get an inexistent pane.'); - } + /** @var Pane $pane */ + foreach ($this->getEntries() as $pane) { + if ($pane->isActive()) { + return $pane; } } - if ($pane && $this->hasEntry($pane)) { - return $this->getEntry($pane); - } - - throw new ConfigurationError('Could not determine active pane'); + return null; } public function removeEntry($pane) @@ -201,10 +165,23 @@ class DashboardHome extends BaseDashboard implements Sortable ->setTitle($pane->label) ->setPriority($pane->priority); - $newPane->loadDashboardEntries($name); $this->addEntry($newPane); } + if ($name !== null) { + if ($this->hasEntry($name)) { + $pane = $this->getEntry($name); + + $this->activatePane($pane); + $pane->loadDashboardEntries(); + } else { + throw new HttpNotFoundException(t('Pane "%s" not found'), $name); + } + } elseif (($firstPane = $this->rewindEntries())) { + $this->activatePane($firstPane); + $firstPane->loadDashboardEntries(); + } + return $this; } diff --git a/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php b/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php index 5f9eac3ec..06ae3a321 100644 --- a/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php +++ b/library/Icinga/Web/Dashboard/ItemList/DashboardHomeList.php @@ -55,7 +55,7 @@ class DashboardHomeList extends ItemListControl protected function shouldExpandByDefault(): bool { - return $this->home->getActive(); + return $this->home->isActive(); } protected function getCollapsibleControlClass(): string diff --git a/library/Icinga/Web/Dashboard/ItemList/DashboardList.php b/library/Icinga/Web/Dashboard/ItemList/DashboardList.php index a4deba46b..1c7b245d9 100644 --- a/library/Icinga/Web/Dashboard/ItemList/DashboardList.php +++ b/library/Icinga/Web/Dashboard/ItemList/DashboardList.php @@ -37,7 +37,7 @@ class DashboardList extends ItemListControl protected function shouldExpandByDefault(): bool { - return $this->pane->getHome()->getActive(); + return $this->pane->isActive(); } protected function getCollapsibleControlClass(): string diff --git a/library/Icinga/Web/Dashboard/Pane.php b/library/Icinga/Web/Dashboard/Pane.php index ff68a83f4..bbe997061 100644 --- a/library/Icinga/Web/Dashboard/Pane.php +++ b/library/Icinga/Web/Dashboard/Pane.php @@ -12,6 +12,7 @@ use Icinga\Model; use Icinga\Web\Dashboard\Common\DashboardEntries; use Icinga\Web\Dashboard\Common\Sortable; use Icinga\Util\DBUtils; +use Icinga\Web\Dashboard\Common\WidgetState; use ipl\Stdlib\Filter; use function ipl\Stdlib\get_php_type; @@ -22,6 +23,7 @@ use function ipl\Stdlib\get_php_type; class Pane extends BaseDashboard implements Sortable { use DashboardEntries; + use WidgetState; const TABLE = 'icingaweb_dashboard';