DashboardsController: Remove some superfluous control structures & keep all dashboard actions consistent

This commit is contained in:
Yonas Habteab 2022-06-14 09:05:50 +02:00
parent 67b9d58557
commit 73853c327c

View File

@ -88,7 +88,10 @@ class DashboardsController extends CompatController
*/ */
public function homeAction() public function homeAction()
{ {
$this->dashboard->load($this->params->getRequired('home'), $this->params->get('pane')); $home = $this->params->getRequired('home');
$pane = $this->params->get('pane');
$this->dashboard->load($home, $pane);
$activeHome = $this->dashboard->getActiveHome(); $activeHome = $this->dashboard->getActiveHome();
if (! $activeHome->getEntries()) { if (! $activeHome->getEntries()) {
@ -190,10 +193,6 @@ class DashboardsController extends CompatController
$this->dashboard->load($home, $pane, true); $this->dashboard->load($home, $pane, true);
if (! $this->dashboard->getActiveHome()->hasEntry($pane)) {
$this->httpNotFound(t('Pane "%s" not found'), $pane);
}
$paneForm = new PaneForm($this->dashboard); $paneForm = new PaneForm($this->dashboard);
$paneForm->on(PaneForm::ON_SUCCESS, function () use ($paneForm, $home) { $paneForm->on(PaneForm::ON_SUCCESS, function () use ($paneForm, $home) {
if ($this->prevActive && $home !== $paneForm->getPopulatedValue('home')) { if ($this->prevActive && $home !== $paneForm->getPopulatedValue('home')) {
@ -219,10 +218,6 @@ class DashboardsController extends CompatController
$this->dashboard->load($home, $paneParam); $this->dashboard->load($home, $paneParam);
if (! $this->dashboard->getActiveHome()->hasEntry($paneParam)) {
$this->httpNotFound(t('Pane "%s" not found'), $paneParam);
}
$paneForm = new RemovePaneForm($this->dashboard); $paneForm = new RemovePaneForm($this->dashboard);
$paneForm->populate(['org_name' => $paneParam]); $paneForm->populate(['org_name' => $paneParam]);
$paneForm->on(RemovePaneForm::ON_SUCCESS, function () { $paneForm->on(RemovePaneForm::ON_SUCCESS, function () {
@ -242,8 +237,9 @@ class DashboardsController extends CompatController
public function newDashletAction() public function newDashletAction()
{ {
$home = $this->params->getRequired('home'); $home = $this->params->getRequired('home');
$pane = $this->params->get('pane');
$this->dashboard->load($home, $this->params->get('pane'), true); $this->dashboard->load($home, $pane, true);
$dashletForm = new DashletForm($this->dashboard); $dashletForm = new DashletForm($this->dashboard);
$dashletForm->populate($this->getRequest()->getPost()); $dashletForm->populate($this->getRequest()->getPost());
@ -284,8 +280,18 @@ class DashboardsController extends CompatController
public function editDashletAction() public function editDashletAction()
{ {
$pane = $this->validateDashletParams(); $home = $this->params->getRequired('home');
$dashlet = $pane->getEntry($this->params->get('dashlet')); $pane = $this->params->getRequired('pane');
$dashlet = $this->params->getRequired('dashlet');
$this->dashboard->load($home, $pane, true);
$pane = $this->dashboard->getActiveHome()->getActivePane();
if (! $pane->hasEntry($dashlet)) {
$this->httpNotFound(t('Dashlet "%s" not found'), $dashlet);
}
$dashlet = $pane->getEntry($dashlet);
$dashletForm = (new DashletForm($this->dashboard)) $dashletForm = (new DashletForm($this->dashboard))
->on(DashletForm::ON_SUCCESS, function () { ->on(DashletForm::ON_SUCCESS, function () {
@ -295,8 +301,6 @@ class DashboardsController extends CompatController
}) })
->handleRequest($this->getServerRequest()); ->handleRequest($this->getServerRequest());
$dashletForm->getElement('submit')->setLabel(t('Update Dashlet'));
$dashletForm->load($dashlet); $dashletForm->load($dashlet);
$this->setTitle(t('Edit Dashlet')); $this->setTitle(t('Edit Dashlet'));
@ -305,7 +309,16 @@ class DashboardsController extends CompatController
public function removeDashletAction() public function removeDashletAction()
{ {
$this->validateDashletParams(); $home = $this->params->getRequired('home');
$pane = $this->params->getRequired('pane');
$dashlet = $this->params->getRequired('dashlet');
$this->dashboard->load($home, $pane);
$pane = $this->dashboard->getActiveHome()->getActivePane();
if (! $pane->hasEntry($dashlet)) {
$this->httpNotFound(t('Dashlet "%s" not found'), $dashlet);
}
$removeForm = (new RemoveDashletForm($this->dashboard)) $removeForm = (new RemoveDashletForm($this->dashboard))
->on(RemoveDashletForm::ON_SUCCESS, function () { ->on(RemoveDashletForm::ON_SUCCESS, function () {
@ -469,6 +482,13 @@ class DashboardsController extends CompatController
*/ */
public function setupDashboardAction() public function setupDashboardAction()
{ {
$this->dashboard->load(DashboardHome::DEFAULT_HOME);
$setupForm = new SetupNewDashboardForm($this->dashboard);
$setupForm->on(SetupNewDashboardForm::ON_SUCCESS, function () use ($setupForm) {
$this->redirectNow($setupForm->getRedirectUrl());
})->handleRequest($this->getServerRequest());
if (isset($this->getRequest()->getPost()['btn_next'])) { if (isset($this->getRequest()->getPost()['btn_next'])) {
// Set compact view to prevent the controls from being // Set compact view to prevent the controls from being
// rendered in the modal view when redirecting // rendered in the modal view when redirecting
@ -479,13 +499,6 @@ class DashboardsController extends CompatController
$this->setTitle(t('Add Dashlet')); $this->setTitle(t('Add Dashlet'));
} }
$this->dashboard->load();
$setupForm = new SetupNewDashboardForm($this->dashboard);
$setupForm->on(SetupNewDashboardForm::ON_SUCCESS, function () use ($setupForm) {
$this->redirectNow($setupForm->getRedirectUrl());
})->handleRequest($this->getServerRequest());
$this->addContent($setupForm); $this->addContent($setupForm);
} }
@ -555,24 +568,4 @@ class DashboardsController extends CompatController
return $tabs; return $tabs;
} }
private function validateDashletParams()
{
$home = $this->params->getRequired('home');
$pane = $this->params->getRequired('pane');
$dashlet = $this->params->getRequired('dashlet');
$this->dashboard->load($home, $pane, true);
if (! $this->dashboard->getActiveHome()->hasEntry($pane)) {
$this->httpNotFound(t('Pane "%s" not found'), $pane);
}
$pane = $this->dashboard->getActiveHome()->getEntry($pane);
if (! $pane->hasEntry($dashlet)) {
$this->httpNotFound(t('Dashlet "%s" not found'), $dashlet);
}
return $pane;
}
} }