DashboardsCommand: Enhance how duplcate errors are handled

This commit is contained in:
Yonas Habteab 2022-06-07 16:05:17 +02:00
parent 77db6c3168
commit a02c36e231

View File

@ -38,6 +38,8 @@ class DashboardsCommand extends Command
* *
* --delete Remove all INI files after successfully migrated * --delete Remove all INI files after successfully migrated
* the dashboards to the database. * the dashboards to the database.
*
* --silent Suppress all kind of DB errors and have them handled automatically.
*/ */
public function indexAction() public function indexAction()
{ {
@ -49,6 +51,7 @@ class DashboardsCommand extends Command
$rc = 0; $rc = 0;
$deleteLegacyFiles = $this->params->get('delete'); $deleteLegacyFiles = $this->params->get('delete');
$silent = $this->params->get('silent');
$user = $this->params->get('user'); $user = $this->params->get('user');
$home = $this->params->get('home'); $home = $this->params->get('home');
$dashboardDirs = new DirectoryIterator($dashboardsPath); $dashboardDirs = new DirectoryIterator($dashboardsPath);
@ -76,43 +79,72 @@ class DashboardsCommand extends Command
$dashboard->load(); $dashboard->load();
if ($dashboardHome) { if ($dashboardHome) {
$dashboard->manageEntry($dashboardHome); if ($dashboard->hasEntry($dashboardHome->getName())) {
$dashboardHome->loadDashboardEntries(); $dashboardHome = $dashboard->getEntry($dashboardHome->getName());
} else {
$dashboard->manageEntry($dashboardHome);
}
} else { } else {
$dashboardHome = $dashboard->initGetDefaultHome(); $dashboardHome = $dashboard->initGetDefaultHome();
} }
$dashboardHome->loadDashboardEntries();
$panes = [];
foreach ($config as $key => $part) { foreach ($config as $key => $part) {
if (strpos($key, '.') === false) { // Panes if (strpos($key, '.') === false) { // Panes
$counter = 1;
$pane = $key; $pane = $key;
while ($dashboardHome->hasEntry($pane)) { if ($silent && $dashboardHome->hasEntry($pane)) {
$pane = $key . $counter++; $counter = 1;
while ($dashboardHome->hasEntry($pane)) {
$pane = $key . $counter++;
}
} elseif ($dashboardHome->hasEntry($pane)) {
do {
$pane = readline(sprintf(
'Dashboard Pane "%s" already exists within the "%s" Dashboard Home.' . "\n" .
'Please enter another name for this pane or rerun the command with the "silent"' .
' param to suppress such errors!: ',
$pane,
$dashboardHome->getTitle()
));
} while (empty($pane) || $dashboardHome->hasEntry($pane));
} }
$dashboardHome->createEntry($pane); $panes[$pane] = (new Pane($pane))
$dashboardHome->getEntry($pane)->setTitle($part->get('title', $pane)); ->setHome($dashboardHome)
->setTitle($part->get('title', $pane));
} else { // Dashlets } else { // Dashlets
list($pane, $dashlet) = explode('.', $key, 2); list($pane, $dashletName) = explode('.', $key, 2);
if (! $dashboardHome->hasEntry($pane)) { if (! isset($panes[$pane])) {
continue; continue;
} }
/** @var Pane $dashboardPane */ $pane = $panes[$pane];
$dashboardPane = $dashboardHome->getEntry($pane); $dashlet = $dashletName;
if ($silent && $pane->hasEntry($dashlet)) {
$counter = 1; $counter = 1;
$newDashelt = $dashlet; while ($pane->hasEntry($dashlet)) {
while ($dashboardPane->hasEntry($newDashelt)) { $dashlet = $dashletName . $counter++;
$newDashelt = $dashlet . $counter++; }
} elseif ($pane->hasEntry($dashlet)) {
do {
$dashlet = readline(sprintf(
'Dashlet "%s" already exists within the "%s" Dashboard Pane.' . "\n" .
'Please enter another name for this Dashlet or rerun the command with the' .
' "silent" param to suppress such errors!: ',
$dashlet,
$pane->getTitle()
));
} while (empty($dashlet) || $pane->hasEntry($dashlet));
} }
$dashboardPane->createEntry($newDashelt, $part->get('url')); $dashletName = $dashlet;
$dashboardPane->getEntry($newDashelt)->setTitle($part->get('title', $newDashelt)); $dashlet = $pane->createEntry($dashletName, $part->get('url'))->getEntry($dashletName);
$dashlet->setTitle($part->get('title', $dashletName));
} }
} }
$panes = $dashboardHome->getEntries();
$dashboardHome->setEntries([]); $dashboardHome->setEntries([]);
$dashboardHome->manageEntry($panes, null, true); $dashboardHome->manageEntry($panes, null, true);