Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't translate pane and dashlet names in configs #3837

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions application/controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,29 +106,22 @@ public function updateDashletAction()
$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();
Expand Down Expand Up @@ -269,7 +262,7 @@ public function removePaneAction()
$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();
Expand Down
4 changes: 2 additions & 2 deletions application/forms/Dashboard/DashletForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function getDashboard()
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()
));
}
Expand Down
6 changes: 3 additions & 3 deletions application/views/scripts/dashboard/settings.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<?= $this->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()))
); ?>
</td>
Expand All @@ -75,10 +75,10 @@
<?= $this->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())
)
); ?>
</td>
Expand Down
3 changes: 3 additions & 0 deletions doc/80-Upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ v2.3 to v2.5 requires to follow the instructions for v2.4 too.
for log messages emitted by jquery-migrate. (https://github.com/jquery/jquery-migrate) Your javascript code will still
work, though jquery-migrate will notify you if you're utilizing deprecated/removed functions. jquery-migrate will be
removed with Icinga Web v2.8 and code not adjusted accordingly will stop working.
* If you're using a language other than english and you've adjusted or disabled module dashboards, you'll need to
update all of your `dashboard.ini` files. A CLI command exists to assist you with this task. Enable the `migrate`
module and run the following on the host where these files exist: `icingacli migrate dashboard sections --verbose`

## Upgrading to Icinga Web 2 2.6.x <a id="upgrading-to-2.6.x"></a>

Expand Down
9 changes: 6 additions & 3 deletions library/Icinga/Application/Modules/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,12 @@ public function createDashboard(array $panes)
$navigation = new Navigation();
foreach ($panes as $pane) {
/** @var DashboardContainer $pane */
$dashlets = array();
$dashlets = [];
foreach ($pane->getDashlets() as $dashletName => $dashletUrl) {
$dashlets[$this->translate($dashletName)] = $dashletUrl;
$dashlets[$dashletName] = [
'label' => $this->translate($dashletName),
'url' => $dashletUrl
];
}

$navigation->addItem(
Expand All @@ -326,7 +329,7 @@ public function createDashboard(array $panes)
array(
'label' => $this->translate($pane->getName()),
'type' => 'dashboard-pane',
'dashlets' => $dashlets
'children' => $dashlets
)
)
);
Expand Down
48 changes: 48 additions & 0 deletions library/Icinga/Legacy/DashboardConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

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
Expand Down Expand Up @@ -76,6 +79,51 @@ public static function listConfigFilesForUser(User $user)
*/
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;
Expand Down
13 changes: 0 additions & 13 deletions library/Icinga/Web/Navigation/DashboardPane.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ public function init()
$this->setUrl(Url::fromPath('dashboard', array('pane' => $this->getName())));
}

/**
* {@inheritdoc}
*/
public function merge(NavigationItem $item)
{
parent::merge($item);

$this->setDashlets(array_merge(
$this->getDashlets(false),
$item->getDashlets(false)
));
}

/**
* Set disabled state for pane
*
Expand Down
33 changes: 23 additions & 10 deletions library/Icinga/Web/Widget/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ public function load()
foreach ($navigation as $dashboardPane) {
/** @var DashboardPane $dashboardPane */
$pane = new Pane($dashboardPane->getLabel());
nilmerg marked this conversation as resolved.
Show resolved Hide resolved
foreach ($dashboardPane->getDashlets(false) as $title => $url) {
$pane->addDashlet($title, $url);
foreach ($dashboardPane->getChildren() as $dashlet) {
$pane->addDashlet($dashlet->getLabel(), $dashlet->getUrl());
}

$panes[] = $pane;
}

$this->mergePanes($panes);
$this->loadUserDashboards();
$this->loadUserDashboards($navigation);
return $this;
}

Expand All @@ -103,7 +103,7 @@ public function getConfig()
}
foreach ($pane->getDashlets() as $dashlet) {
if ($dashlet->isUserWidget()) {
$output[$pane->getName() . '.' . $dashlet->getTitle()] = $dashlet->toArray();
$output[$pane->getName() . '.' . $dashlet->getName()] = $dashlet->toArray();
}
}
}
Expand All @@ -114,10 +114,10 @@ public function getConfig()
/**
* 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);
}
}

Expand All @@ -128,7 +128,7 @@ protected function loadUserDashboards()
*
* @return bool
*/
protected function loadUserDashboardsFromFile($file)
protected function loadUserDashboardsFromFile($file, Navigation $dashboardNavigation)
{
try {
$config = Config::fromIni($file);
Expand All @@ -143,8 +143,12 @@ protected function loadUserDashboardsFromFile($file)
$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);
Expand All @@ -155,6 +159,14 @@ protected function loadUserDashboardsFromFile($file)
}
} 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;
Expand All @@ -175,6 +187,7 @@ protected function loadUserDashboardsFromFile($file)
$dashletData->url,
$pane
);
$dashlet->setName($dashletData->dashlet);

if ((bool) $dashletData->get('disabled', false) === true) {
$dashlet->setDisabled(true);
Expand All @@ -200,7 +213,7 @@ public function mergePanes(array $panes)
{
/** @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());
Expand Down
14 changes: 14 additions & 0 deletions library/Icinga/Web/Widget/Dashboard/Dashlet.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Dashlet extends UserWidget
*/
private $url;

private $name;

/**
* The title being displayed on top of the dashlet
* @var
Expand Down Expand Up @@ -91,11 +93,23 @@ class Dashlet extends UserWidget
*/
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
*
Expand Down
13 changes: 6 additions & 7 deletions library/Icinga/Web/Widget/Dashboard/Pane.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -229,7 +228,7 @@ public function createDashlet($title, $url = null)
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 {
Expand All @@ -248,15 +247,15 @@ public function addDashlets(array $dashlets)
{
/* @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;
}
}

Expand Down
Loading