From 37a972cf7599b4600f4f5743e9d327bc178956cf Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 26 Jun 2019 15:46:06 +0200 Subject: [PATCH] dashboard: Differentiate between `name` and `title` The former is the original name while title may be changed by the user. This change is strictly necessary for the references bug and should also be introduced when rewriting this mess. refs #3542 --- application/controllers/DashboardController.php | 17 +++++------------ application/forms/Dashboard/DashletForm.php | 4 ++-- .../views/scripts/dashboard/settings.phtml | 6 +++--- library/Icinga/Web/Widget/Dashboard.php | 5 +++-- library/Icinga/Web/Widget/Dashboard/Dashlet.php | 14 ++++++++++++++ library/Icinga/Web/Widget/Dashboard/Pane.php | 13 ++++++------- 6 files changed, 33 insertions(+), 26 deletions(-) diff --git a/application/controllers/DashboardController.php b/application/controllers/DashboardController.php index e9aac4a95..ff2580c1b 100644 --- a/application/controllers/DashboardController.php +++ b/application/controllers/DashboardController.php @@ -106,29 +106,22 @@ class DashboardController extends ActionController $action = $this; $form->setOnSuccess(function (Form $form) use ($dashboard, $action) { try { - $pane = $dashboard->getPane($form->getValue('pane')); + $pane = $dashboard->getPane($form->getValue('org_pane')); + $pane->setTitle($form->getValue('pane')); } catch (ProgrammingError $e) { $pane = new Dashboard\Pane($form->getValue('pane')); $pane->setUserWidget(); $dashboard->addPane($pane); } try { - $dashlet = $pane->getDashlet($form->getValue('dashlet')); + $dashlet = $pane->getDashlet($form->getValue('org_dashlet')); + $dashlet->setTitle($form->getValue('dashlet')); $dashlet->setUrl($form->getValue('url')); } catch (ProgrammingError $e) { $dashlet = new Dashboard\Dashlet($form->getValue('dashlet'), $form->getValue('url'), $pane); $pane->addDashlet($dashlet); } $dashlet->setUserWidget(); - // Rename dashlet - if ($form->getValue('org_dashlet') && $form->getValue('org_dashlet') !== $dashlet->getTitle()) { - $pane->removeDashlet($form->getValue('org_dashlet')); - } - // Move - if ($form->getValue('org_pane') && $form->getValue('org_pane') !== $pane->getTitle()) { - $oldPane = $dashboard->getPane($form->getValue('org_pane')); - $oldPane->removeDashlet($dashlet->getTitle()); - } $dashboardConfig = $dashboard->getConfig(); try { $dashboardConfig->saveIni(); @@ -269,7 +262,7 @@ class DashboardController extends ActionController $action = $this; $form->setOnSuccess(function (Form $form) use ($dashboard, $pane, $action) { $pane = $dashboard->getPane($pane); - $dashboard->removePane($pane->getTitle()); + $dashboard->removePane($pane->getName()); $dashboardConfig = $dashboard->getConfig(); try { $dashboardConfig->saveIni(); diff --git a/application/forms/Dashboard/DashletForm.php b/application/forms/Dashboard/DashletForm.php index 22f1f6c5a..1af65a954 100644 --- a/application/forms/Dashboard/DashletForm.php +++ b/application/forms/Dashboard/DashletForm.php @@ -161,10 +161,10 @@ class DashletForm extends Form public function load(Dashlet $dashlet) { $this->populate(array( - 'pane' => $dashlet->getPane()->getName(), + 'pane' => $dashlet->getPane()->getTitle(), 'org_pane' => $dashlet->getPane()->getName(), 'dashlet' => $dashlet->getTitle(), - 'org_dashlet' => $dashlet->getTitle(), + 'org_dashlet' => $dashlet->getName(), 'url' => $dashlet->getUrl()->getRelativeUrl() )); } diff --git a/application/views/scripts/dashboard/settings.phtml b/application/views/scripts/dashboard/settings.phtml index ffa48b78a..ba672988e 100644 --- a/application/views/scripts/dashboard/settings.phtml +++ b/application/views/scripts/dashboard/settings.phtml @@ -59,7 +59,7 @@ qlink( $dashlet->getTitle(), 'dashboard/update-dashlet', - array('pane' => $pane->getName(), 'dashlet' => $dashlet->getTitle()), + array('pane' => $pane->getName(), 'dashlet' => $dashlet->getName()), array('title' => sprintf($this->translate('Edit dashlet %s'), $dashlet->getTitle())) ); ?> @@ -75,10 +75,10 @@ qlink( '', 'dashboard/remove-dashlet', - array('pane' => $pane->getName(), 'dashlet' => $dashlet->getTitle()), + array('pane' => $pane->getName(), 'dashlet' => $dashlet->getName()), array( 'icon' => 'trash', - 'title' => sprintf($this->translate('Remove dashlet %s from pane %s'), $dashlet->getTitle(), $pane->getName()) + 'title' => sprintf($this->translate('Remove dashlet %s from pane %s'), $dashlet->getTitle(), $pane->getTitle()) ) ); ?> diff --git a/library/Icinga/Web/Widget/Dashboard.php b/library/Icinga/Web/Widget/Dashboard.php index 83b0be654..d6d8fc92e 100644 --- a/library/Icinga/Web/Widget/Dashboard.php +++ b/library/Icinga/Web/Widget/Dashboard.php @@ -103,7 +103,7 @@ class Dashboard extends AbstractWidget } foreach ($pane->getDashlets() as $dashlet) { if ($dashlet->isUserWidget()) { - $output[$pane->getName() . '.' . $dashlet->getTitle()] = $dashlet->toArray(); + $output[$pane->getName() . '.' . $dashlet->getName()] = $dashlet->toArray(); } } } @@ -175,6 +175,7 @@ class Dashboard extends AbstractWidget $dashletData->url, $pane ); + $dashlet->setName($dashletData->dashlet); if ((bool) $dashletData->get('disabled', false) === true) { $dashlet->setDisabled(true); @@ -200,7 +201,7 @@ class Dashboard extends AbstractWidget { /** @var $pane Pane */ foreach ($panes as $pane) { - if ($this->hasPane($pane->getTitle()) === true) { + if ($this->hasPane($pane->getName()) === true) { /** @var $current Pane */ $current = $this->panes[$pane->getName()]; $current->addDashlets($pane->getDashlets()); diff --git a/library/Icinga/Web/Widget/Dashboard/Dashlet.php b/library/Icinga/Web/Widget/Dashboard/Dashlet.php index 17f971f0a..b9b2eb0d1 100644 --- a/library/Icinga/Web/Widget/Dashboard/Dashlet.php +++ b/library/Icinga/Web/Widget/Dashboard/Dashlet.php @@ -22,6 +22,8 @@ class Dashlet extends UserWidget */ private $url; + private $name; + /** * The title being displayed on top of the dashlet * @var @@ -91,11 +93,23 @@ EOD; */ public function __construct($title, $url, Pane $pane) { + $this->name = $title; $this->title = $title; $this->pane = $pane; $this->url = $url; } + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + /** * Retrieve the dashlets title * diff --git a/library/Icinga/Web/Widget/Dashboard/Pane.php b/library/Icinga/Web/Widget/Dashboard/Pane.php index 967ba48e9..c8b14c5a1 100644 --- a/library/Icinga/Web/Widget/Dashboard/Pane.php +++ b/library/Icinga/Web/Widget/Dashboard/Pane.php @@ -22,7 +22,6 @@ class Pane extends UserWidget /** * The title of this pane, as displayed in the dashboard tabs - * @TODO: Currently the same as $name, evaluate if distinguishing is needed * * @var string */ @@ -229,7 +228,7 @@ class Pane extends UserWidget public function addDashlet($dashlet, $url = null) { if ($dashlet instanceof Dashlet) { - $this->dashlets[$dashlet->getTitle()] = $dashlet; + $this->dashlets[$dashlet->getName()] = $dashlet; } elseif (is_string($dashlet) && $url !== null) { $this->createDashlet($dashlet, $url); } else { @@ -248,15 +247,15 @@ class Pane extends UserWidget { /* @var $dashlet Dashlet */ foreach ($dashlets as $dashlet) { - if (array_key_exists($dashlet->getTitle(), $this->dashlets)) { - if (preg_match('/_(\d+)$/', $dashlet->getTitle(), $m)) { - $name = preg_replace('/_\d+$/', $m[1]++, $dashlet->getTitle()); + if (array_key_exists($dashlet->getName(), $this->dashlets)) { + if (preg_match('/_(\d+)$/', $dashlet->getName(), $m)) { + $name = preg_replace('/_\d+$/', $m[1]++, $dashlet->getName()); } else { - $name = $dashlet->getTitle() . '_2'; + $name = $dashlet->getName() . '_2'; } $this->dashlets[$name] = $dashlet; } else { - $this->dashlets[$dashlet->getTitle()] = $dashlet; + $this->dashlets[$dashlet->getName()] = $dashlet; } }