Ensure to load dashlets of the current pane before creating/updating a dashlet

This commit is contained in:
Yonas Habteab 2022-06-13 09:08:47 +02:00
parent 51370a9e37
commit d0d33d990c
2 changed files with 32 additions and 27 deletions

View File

@ -82,7 +82,7 @@ abstract class BaseDashboardForm extends CompatForm
*/ */
protected function isUpdating(): bool protected function isUpdating(): bool
{ {
return substr($this->requestUrl->getPath(), 0, 16) === Dashboard::BASE_ROUTE . DIRECTORY_SEPARATOR . 'edit-'; return substr($this->requestUrl->getPath(), 0, 16) === Dashboard::BASE_ROUTE . '/edit-';
} }
/** /**

View File

@ -133,19 +133,19 @@ class DashletForm extends SetupNewDashboardForm
$removeButton = $this->createRemoveButton($targetUrl, t('Remove Dashlet')); $removeButton = $this->createRemoveButton($targetUrl, t('Remove Dashlet'));
$formControls = $this->createFormControls(); $formControls = $this->createFormControls();
$formControls->add([ $formControls->addHtml(
$this->registerSubmitButton(t('Add to Dashboard')), $this->registerSubmitButton(t('Add to Dashboard')),
$removeButton, $removeButton,
$this->createCancelButton() $this->createCancelButton()
]); );
$this->addHtml($formControls); $this->addHtml($formControls);
} else { } else {
$formControls = $this->createFormControls(); $formControls = $this->createFormControls();
$formControls->add([ $formControls->addHtml(
$this->registerSubmitButton(t('Add to Dashboard')), $this->registerSubmitButton(t('Add to Dashboard')),
$this->createCancelButton() $this->createCancelButton()
]); );
$this->addHtml($formControls); $this->addHtml($formControls);
} }
@ -172,16 +172,22 @@ class DashletForm extends SetupNewDashboardForm
} }
$currentHome = new DashboardHome($selectedHome); $currentHome = new DashboardHome($selectedHome);
$currentPane = new Pane($selectedPane);
if ($dashboard->hasEntry($currentHome->getName())) { if ($dashboard->hasEntry($currentHome->getName())) {
$currentHome = clone $dashboard->getEntry($currentHome->getName()); $currentHome = clone $dashboard->getEntry($currentHome->getName());
if ($currentHome->getName() !== $dashboard->getActiveHome()->getName()) { $activatePane = $currentHome->hasEntry($selectedPane)
$currentHome->loadDashboardEntries(); && $currentHome->getActivePane()->getName() !== $selectedPane
} ? $selectedPane
} : null;
$currentPane = new Pane($selectedPane); if ($currentHome->getName() !== $dashboard->getActiveHome()->getName() || $activatePane) {
if ($currentHome->hasEntry($currentPane->getName())) { $currentHome->loadDashboardEntries($activatePane);
$currentPane = clone $currentHome->getEntry($currentPane->getName()); }
if ($currentHome->hasEntry($currentPane->getName())) {
$currentPane = clone $currentHome->getActivePane();
}
} }
if (! $this->isUpdating()) { if (! $this->isUpdating()) {
@ -257,16 +263,22 @@ class DashletForm extends SetupNewDashboardForm
} else { } else {
$orgHome = $dashboard->getEntry($this->getValue('org_home')); $orgHome = $dashboard->getEntry($this->getValue('org_home'));
$orgPane = $orgHome->getEntry($this->getValue('org_pane')); $orgPane = $orgHome->getEntry($this->getValue('org_pane'));
$orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet')); if ($orgHome->getActivePane()->getName() !== $orgPane->getName()) {
$orgHome->loadDashboardEntries($orgPane->getName());
$orgPane = $orgHome->getActivePane();
}
$orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet'));
$currentPane->setHome($currentHome); $currentPane->setHome($currentHome);
/**
* When the user wishes to create a new dashboard pane, we have to explicitly reset the dashboard panes if (! $currentHome->hasEntry($currentPane->getName())) {
* of the original home, so that it isn't considered as we want to move the pane even though it isn't /**
* supposed to when the original home contains a dashboard with the same name * When the user is going to move the Dashlet into a new pane in a different home, it might be possible
* {@see DashboardHome::manageEntry()} for details * that the original Home contains a Pane with the same name and in {@see DashboardHome::manageEntry()}
*/ * this would be interpreted as if we wanted to move the Pane from the original Home. Therefore, we need
if (! $currentHome->hasEntry($currentPane->getName()) || $currentHome->getName() === $orgHome->getName()) { * to explicitly reset all dashboard entries of the org Home here.
*/
$orgHome->setEntries([]); $orgHome->setEntries([]);
} }
@ -278,7 +290,6 @@ class DashletForm extends SetupNewDashboardForm
->setTitle($this->getValue('dashlet')) ->setTitle($this->getValue('dashlet'))
->setDescription($this->getValue('description')); ->setDescription($this->getValue('description'));
if ($orgPane->getName() !== $currentPane->getName() if ($orgPane->getName() !== $currentPane->getName()
&& $currentPane->hasEntry($currentDashlet->getName())) { && $currentPane->hasEntry($currentDashlet->getName())) {
Notification::error(sprintf( Notification::error(sprintf(
@ -304,12 +315,6 @@ class DashletForm extends SetupNewDashboardForm
return; return;
} }
if (empty($paneDiff)) {
// No dashboard diff means the dashlet is still in the same pane, so just
// reset the dashlets of the original pane
$orgPane->setEntries([]);
}
$conn->beginTransaction(); $conn->beginTransaction();
try { try {