From fcde2a9eead1947b927f0db98b7f0e0399930502 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 26 Jun 2019 15:50:35 +0200 Subject: [PATCH] dashboard: Use the non-translated versions of pane and dashlet names for storage This is a dirty quick fix. And calling it *quick* is already an overstatement. Of course, it's also a breaking change, as **all** non-english users need to update their dashboard.ini after this change. Oh, and don't dare to move this over to the new dashboards! refs #3542 --- library/Icinga/Legacy/DashboardConfig.php | 48 +++++++++++++++++++++++ library/Icinga/Web/Widget/Dashboard.php | 24 +++++++++--- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Legacy/DashboardConfig.php b/library/Icinga/Legacy/DashboardConfig.php index 54cd3b78f..3fb5c2fc2 100644 --- a/library/Icinga/Legacy/DashboardConfig.php +++ b/library/Icinga/Legacy/DashboardConfig.php @@ -5,6 +5,9 @@ namespace Icinga\Legacy; use Icinga\Application\Config; use Icinga\User; +use Icinga\Web\Navigation\DashboardPane; +use Icinga\Web\Navigation\Navigation; +use Icinga\Web\Navigation\NavigationItem; /** * Legacy dashboard config class for case insensitive interpretation of dashboard config files @@ -76,6 +79,51 @@ class DashboardConfig extends Config */ public function saveIni($filePath = null, $fileMode = 0660) { + // Preprocessing start, ensures that the non-translated names are used to save module dashboard changes + // TODO: This MUST NOT survive the new dashboard implementation (yes, it's still a thing..) + $dashboardNavigation = new Navigation(); + $dashboardNavigation->load('dashboard-pane'); + $getDashboardPane = function ($label) use ($dashboardNavigation) { + foreach ($dashboardNavigation as $dashboardPane) { + /** @var DashboardPane $dashboardPane */ + if ($dashboardPane->getLabel() === $label) { + return $dashboardPane; + } + + foreach ($dashboardPane->getChildren() as $dashlet) { + /** @var NavigationItem $dashlet */ + if ($dashlet->getLabel() === $label) { + return $dashlet; + } + } + } + }; + + foreach (clone $this->config as $name => $options) { + if (strpos($name, '.') !== false) { + list($dashboardLabel, $dashletLabel) = explode('.', $name, 2); + } else { + $dashboardLabel = $name; + $dashletLabel = null; + } + + $dashboardPane = $getDashboardPane($dashboardLabel); + if ($dashboardPane !== null) { + $dashboardLabel = $dashboardPane->getName(); + } + + if ($dashletLabel !== null) { + $dashletItem = $getDashboardPane($dashletLabel); + if ($dashletItem !== null) { + $dashletLabel = $dashletItem->getName(); + } + } + + unset($this->config[$name]); + $this->config[$dashboardLabel . ($dashletLabel ? '.' . $dashletLabel : '')] = $options; + } + // Preprocessing end + parent::saveIni($filePath, $fileMode); if ($filePath === null) { $filePath = $this->configFile; diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index cf3b0c59d..5a8796d27 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -85,7 +85,7 @@ class Dashboard extends AbstractWidget } $this->mergePanes($panes); - $this->loadUserDashboards(); + $this->loadUserDashboards($navigation); return $this; } @@ -114,10 +114,10 @@ class Dashboard extends AbstractWidget /** * Load user dashboards from all config files that match the username */ - protected function loadUserDashboards() + protected function loadUserDashboards(Navigation $navigation) { foreach (DashboardConfig::listConfigFilesForUser($this->user) as $file) { - $this->loadUserDashboardsFromFile($file); + $this->loadUserDashboardsFromFile($file, $navigation); } } @@ -128,7 +128,7 @@ class Dashboard extends AbstractWidget * * @return bool */ - protected function loadUserDashboardsFromFile($file) + protected function loadUserDashboardsFromFile($file, Navigation $dashboardNavigation) { try { $config = Config::fromIni($file); @@ -143,8 +143,12 @@ class Dashboard extends AbstractWidget $dashlets = array(); foreach ($config as $key => $part) { if (strpos($key, '.') === false) { - if ($this->hasPane($part->title)) { - $panes[$key] = $this->getPane($part->title); + $dashboardPane = $dashboardNavigation->getItem($key); + if ($dashboardPane !== null) { + $key = $dashboardPane->getLabel(); + } + if ($this->hasPane($key)) { + $panes[$key] = $this->getPane($key); } else { $panes[$key] = new Pane($key); $panes[$key]->setTitle($part->title); @@ -155,6 +159,14 @@ class Dashboard extends AbstractWidget } } else { list($paneName, $dashletName) = explode('.', $key, 2); + $dashboardPane = $dashboardNavigation->getItem($paneName); + if ($dashboardPane !== null) { + $paneName = $dashboardPane->getLabel(); + $dashletItem = $dashboardPane->getChildren()->getItem($dashletName); + if ($dashletItem !== null) { + $dashletName = $dashletItem->getLabel(); + } + } $part->pane = $paneName; $part->dashlet = $dashletName; $dashlets[] = $part;