From 49b37ea522f696eea5ba0160e81230555257fb67 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Thu, 14 Apr 2022 09:44:00 +0200 Subject: [PATCH] Merge some related classes --- .../controllers/DashboardsController.php | 6 +- application/forms/Dashboard/DashletForm.php | 208 +++++++++--------- ...ashboard.php => SetupNewDashboardForm.php} | 51 ++++- .../Web/Dashboard/Setup/SetupNewDashboard.php | 70 ------ 4 files changed, 158 insertions(+), 177 deletions(-) rename application/forms/Dashboard/{BaseSetupDashboard.php => SetupNewDashboardForm.php} (83%) delete mode 100644 library/Icinga/Web/Dashboard/Setup/SetupNewDashboard.php diff --git a/application/controllers/DashboardsController.php b/application/controllers/DashboardsController.php index 70f97ecce..df6f27630 100644 --- a/application/controllers/DashboardsController.php +++ b/application/controllers/DashboardsController.php @@ -10,13 +10,13 @@ use Icinga\Forms\Dashboard\HomePaneForm; use Icinga\Forms\Dashboard\NewHomePaneForm; use Icinga\Forms\Dashboard\RemoveDashletForm; use Icinga\Forms\Dashboard\RemoveHomePaneForm; +use Icinga\Forms\Dashboard\SetupNewDashboardForm; use Icinga\Forms\Dashboard\WelcomeForm; use Icinga\Util\Json; use Icinga\Web\Dashboard\Dashboard; use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Dashboard\Pane; use Icinga\Web\Dashboard\Settings; -use Icinga\Web\Dashboard\Setup\SetupNewDashboard; use Icinga\Web\Notification; use Icinga\Web\Widget\Tabextension\DashboardSettings; use ipl\Web\Compat\CompatController; @@ -395,8 +395,8 @@ class DashboardsController extends CompatController $this->setTitle(t('Add Dashlet')); } - $setupForm = new SetupNewDashboard($this->dashboard); - $setupForm->on(SetupNewDashboard::ON_SUCCESS, function () use ($setupForm) { + $setupForm = new SetupNewDashboardForm($this->dashboard); + $setupForm->on(SetupNewDashboardForm::ON_SUCCESS, function () use ($setupForm) { $this->redirectNow($setupForm->getRedirectUrl()); })->handleRequest(ServerRequest::fromGlobals()); diff --git a/application/forms/Dashboard/DashletForm.php b/application/forms/Dashboard/DashletForm.php index caf1ed36b..8debbcb8a 100644 --- a/application/forms/Dashboard/DashletForm.php +++ b/application/forms/Dashboard/DashletForm.php @@ -14,105 +14,124 @@ use Icinga\Web\Dashboard\Pane; use ipl\Html\HtmlElement; use ipl\Web\Url; -class DashletForm extends BaseSetupDashboard +class DashletForm extends SetupNewDashboardForm { - protected function assembleNextPage() + protected function init() { - if (! $this->getPopulatedValue('btn_next')) { + parent::init(); + + $this->setAction((string) Url::fromRequest()); + } + + public function load(BaseDashboard $dashboard) + { + $home = Url::fromRequest()->getParam('home'); + /** @var Dashlet $dashboard */ + $this->populate(array( + 'org_home' => $home, + 'org_pane' => $dashboard->getPane()->getName(), + 'org_dashlet' => $dashboard->getName(), + 'dashlet' => $dashboard->getTitle(), + 'url' => $dashboard->getUrl()->getRelativeUrl() + )); + } + + protected function assembleNextPageDashboardPart() + { + if (! $this->isUpdatingADashlet() && ! $this->getPopulatedValue('btn_next')) { return; } - $this->dumpArbitaryDashlets(); - $this->assembleNexPageDashletPart(); + $requestUrl = Url::fromRequest(); + + $homes = $this->dashboard->getEntryKeyTitleArr(); + $activeHome = $this->dashboard->getActiveHome(); + $currentHome = $requestUrl->getParam('home', reset($homes)); + $populatedHome = $this->getPopulatedValue('home', $currentHome); + + $panes = []; + if ($currentHome === $populatedHome && $populatedHome !== self::CREATE_NEW_HOME) { + if (! $currentHome || ! $activeHome) { + // Home param isn't passed through, so let's try to load based on the first home + $firstHome = $this->dashboard->rewindEntries(); + if ($firstHome) { + $this->dashboard->loadDashboardEntries($firstHome->getName()); + + $panes = $firstHome->getEntryKeyTitleArr(); + } + } else { + $panes = $activeHome->getEntryKeyTitleArr(); + } + } elseif ($this->dashboard->hasEntry($populatedHome)) { + $this->dashboard->loadDashboardEntries($populatedHome); + + $panes = $this->dashboard->getActiveHome()->getEntryKeyTitleArr(); + } + + $this->addElement('hidden', 'org_pane', ['required' => false]); + $this->addElement('hidden', 'org_home', ['required' => false]); + $this->addElement('hidden', 'org_dashlet', ['required' => false]); + + if ($this->isUpdatingADashlet()) { + $this->assembleDashletElements(); + + $this->addHtml(new HtmlElement('hr')); + } + + $this->addElement('select', 'home', [ + 'class' => 'autosubmit', + 'required' => true, + 'disabled' => empty($homes) ?: null, + 'value' => $populatedHome, + 'multiOptions' => array_merge([self::CREATE_NEW_HOME => self::CREATE_NEW_HOME], $homes), + 'label' => t('Select Home'), + 'description' => t('Select a dashboard home you want to add the dashboard pane to.') + ]); + + if (empty($homes) || $populatedHome === self::CREATE_NEW_HOME) { + $this->addElement('text', 'new_home', [ + 'required' => true, + 'label' => t('Home Title'), + 'placeholder' => t('Enter dashboard home title'), + 'description' => t('Enter a title for the new dashboard home.') + ]); + } + + $populatedPane = $this->getPopulatedValue('pane'); + // Pane element's values are depending on the home element's value + if ($populatedPane !== self::CREATE_NEW_PANE && ! in_array($populatedPane, $panes)) { + $this->clearPopulatedValue('pane'); + } + + $populatedPane = $this->getPopulatedValue('pane', $requestUrl->getParam('pane', reset($panes))); + $disable = empty($panes) || $populatedHome === self::CREATE_NEW_HOME; + $this->addElement('select', 'pane', [ + 'class' => 'autosubmit', + 'required' => true, + 'disabled' => $disable ?: null, + 'value' => ! $disable ? $populatedPane : self::CREATE_NEW_PANE, + 'multiOptions' => array_merge([self::CREATE_NEW_PANE => self::CREATE_NEW_PANE], $panes), + 'label' => t('Select Dashboard'), + 'description' => t('Select a dashboard you want to add the dashlet to.'), + ]); + + if ($disable || $this->getPopulatedValue('pane') === self::CREATE_NEW_PANE) { + $this->addElement('text', 'new_pane', [ + 'required' => true, + 'label' => t('Dashboard Title'), + 'placeholder' => t('Enter dashboard title'), + 'description' => t('Enter a title for the new dashboard.'), + ]); + } } protected function assemble() { if ($this->isUpdatingADashlet() || $this->getPopulatedValue('btn_next')) { - $requestUrl = Url::fromRequest(); - - $homes = $this->dashboard->getEntryKeyTitleArr(); - $activeHome = $this->dashboard->getActiveHome(); - $currentHome = $requestUrl->getParam('home', reset($homes)); - $populatedHome = $this->getPopulatedValue('home', $currentHome); - - $panes = []; - if ($currentHome === $populatedHome && $populatedHome !== self::CREATE_NEW_HOME) { - if (! $currentHome || ! $activeHome) { - // Home param isn't passed through, so let's try to load based on the first home - $firstHome = $this->dashboard->rewindEntries(); - if ($firstHome) { - $this->dashboard->loadDashboardEntries($firstHome->getName()); - - $panes = $firstHome->getEntryKeyTitleArr(); - } - } else { - $panes = $activeHome->getEntryKeyTitleArr(); - } - } elseif ($this->dashboard->hasEntry($populatedHome)) { - $this->dashboard->loadDashboardEntries($populatedHome); - - $panes = $this->dashboard->getActiveHome()->getEntryKeyTitleArr(); - } - - $this->addElement('hidden', 'org_pane', ['required' => false]); - $this->addElement('hidden', 'org_home', ['required' => false]); - $this->addElement('hidden', 'org_dashlet', ['required' => false]); + $this->assembleNextPage(); if ($this->isUpdatingADashlet()) { - $this->assembleDashletElements(); - - $this->addHtml(new HtmlElement('hr')); - } - - $this->addElement('select', 'home', [ - 'class' => 'autosubmit', - 'required' => true, - 'disabled' => empty($homes) ?: null, - 'value' => $populatedHome, - 'multiOptions' => array_merge([self::CREATE_NEW_HOME => self::CREATE_NEW_HOME], $homes), - 'label' => t('Select Home'), - 'description' => t('Select a dashboard home you want to add the dashboard pane to.') - ]); - - if (empty($homes) || $populatedHome === self::CREATE_NEW_HOME) { - $this->addElement('text', 'new_home', [ - 'required' => true, - 'label' => t('Home Title'), - 'placeholder' => t('Enter dashboard home title'), - 'description' => t('Enter a title for the new dashboard home.') - ]); - } - - $populatedPane = $this->getPopulatedValue('pane'); - // Pane element's values are depending on the home element's value - if ($populatedPane !== self::CREATE_NEW_PANE && ! in_array($populatedPane, $panes)) { - $this->clearPopulatedValue('pane'); - } - - $populatedPane = $this->getPopulatedValue('pane', $requestUrl->getParam('pane', reset($panes))); - $disable = empty($panes) || $populatedHome === self::CREATE_NEW_HOME; - $this->addElement('select', 'pane', [ - 'class' => 'autosubmit', - 'required' => true, - 'disabled' => $disable ?: null, - 'value' => ! $disable ? $populatedPane : self::CREATE_NEW_PANE, - 'multiOptions' => array_merge([self::CREATE_NEW_PANE => self::CREATE_NEW_PANE], $panes), - 'label' => t('Select Dashboard'), - 'description' => t('Select a dashboard you want to add the dashlet to.'), - ]); - - if ($disable || $this->getPopulatedValue('pane') === self::CREATE_NEW_PANE) { - $this->addElement('text', 'new_pane', [ - 'required' => true, - 'label' => t('Dashboard Title'), - 'placeholder' => t('Enter dashboard title'), - 'description' => t('Enter a title for the new dashboard.'), - ]); - } - - if ($this->isUpdatingADashlet()) { - $targetUrl = (clone $requestUrl)->setPath(Dashboard::BASE_ROUTE . '/remove-dashlet'); + $targetUrl = (clone Url::fromRequest())->setPath(Dashboard::BASE_ROUTE . '/remove-dashlet'); $removeButton = $this->createRemoveButton($targetUrl, t('Remove Dashlet')); $formControls = $this->createFormControls(); @@ -124,8 +143,6 @@ class DashletForm extends BaseSetupDashboard $this->addHtml($formControls); } else { - $this->assembleNextPage(); - $formControls = $this->createFormControls(); $formControls->add([ $this->registerSubmitButton(t('Add to Dashboard')), @@ -315,17 +332,4 @@ class DashletForm extends BaseSetupDashboard Notification::success(sprintf(t('Updated dashlet "%s" successfully'), $currentDashlet->getTitle())); } } - - public function load(BaseDashboard $dashboard) - { - $home = Url::fromRequest()->getParam('home'); - /** @var Dashlet $dashboard */ - $this->populate(array( - 'org_home' => $home, - 'org_pane' => $dashboard->getPane()->getName(), - 'org_dashlet' => $dashboard->getName(), - 'dashlet' => $dashboard->getTitle(), - 'url' => $dashboard->getUrl()->getRelativeUrl() - )); - } } diff --git a/application/forms/Dashboard/BaseSetupDashboard.php b/application/forms/Dashboard/SetupNewDashboardForm.php similarity index 83% rename from application/forms/Dashboard/BaseSetupDashboard.php rename to application/forms/Dashboard/SetupNewDashboardForm.php index 7ef78584a..10315d401 100644 --- a/application/forms/Dashboard/BaseSetupDashboard.php +++ b/application/forms/Dashboard/SetupNewDashboardForm.php @@ -3,15 +3,18 @@ namespace Icinga\Forms\Dashboard; use Icinga\Web\Dashboard\Dashboard; +use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Dashboard\Dashlet; use Icinga\Web\Dashboard\ItemList\DashletListMultiSelect; use Icinga\Web\Dashboard\ItemList\EmptyDashlet; +use Icinga\Web\Dashboard\Pane; +use Icinga\Web\Notification; use ipl\Html\HtmlElement; use ipl\Html\ValidHtml; use ipl\Web\Url; use ipl\Web\Widget\Icon; -abstract class BaseSetupDashboard extends BaseDashboardForm +class SetupNewDashboardForm extends BaseDashboardForm { const DATA_TOGGLE_ELEMENT = 'dashlets-list-info'; @@ -31,6 +34,9 @@ abstract class BaseSetupDashboard extends BaseDashboardForm if (empty(self::$moduleDashlets)) { self::$moduleDashlets = Dashboard::getModuleDashlets(); } + + $this->setRedirectUrl((string) Url::fromPath(Dashboard::BASE_ROUTE)); + $this->setAction($this->getRedirectUrl() . '/setup-dashboard'); } /** @@ -91,7 +97,7 @@ abstract class BaseSetupDashboard extends BaseDashboardForm */ protected function assembleNextPage() { - if (! $this->getPopulatedValue('btn_next')) { + if (! $this->isUpdatingADashlet() && ! $this->getPopulatedValue('btn_next')) { return; } @@ -245,6 +251,47 @@ abstract class BaseSetupDashboard extends BaseDashboardForm $this->addHtml($formControls); } + protected function onSuccess() + { + if ($this->getPopulatedValue('submit')) { + $conn = Dashboard::getConn(); + $pane = new Pane($this->getPopulatedValue('pane')); + $home = $this->dashboard->getEntry(DashboardHome::DEFAULT_HOME); + + $conn->beginTransaction(); + + try { + $this->dashboard->manageEntry($home); + $home->manageEntry($pane); + + $this->dumpArbitaryDashlets(false); + + if (($name = $this->getPopulatedValue('dashlet')) && ($url = $this->getPopulatedValue('url'))) { + if ($this->duplicateCustomDashlet) { + Notification::error(sprintf( + t('Failed to create new dahlets. Dashlet "%s" exists within the selected one'), + $name + )); + + return; + } + + $dashlet = new Dashlet($name, $url, $pane); + $pane->manageEntry($dashlet); + } + + $pane->manageEntry(self::$moduleDashlets); + + $conn->commitTransaction(); + } catch (\Exception $err) { + $conn->rollBackTransaction(); + throw $err; + } + + Notification::success(t('Added new dashlet(s) successfully')); + } + } + /** * Create form list controls (can be collapsible if you want) * diff --git a/library/Icinga/Web/Dashboard/Setup/SetupNewDashboard.php b/library/Icinga/Web/Dashboard/Setup/SetupNewDashboard.php deleted file mode 100644 index 3b47af36d..000000000 --- a/library/Icinga/Web/Dashboard/Setup/SetupNewDashboard.php +++ /dev/null @@ -1,70 +0,0 @@ -setRedirectUrl((string) Url::fromPath(Dashboard::BASE_ROUTE)); - $this->setAction($this->getRedirectUrl() . '/setup-dashboard'); - } - - protected function onSuccess() - { - if ($this->getPopulatedValue('submit')) { - $conn = Dashboard::getConn(); - $pane = new Pane($this->getPopulatedValue('pane')); - $home = $this->dashboard->getEntry(DashboardHome::DEFAULT_HOME); - - $conn->beginTransaction(); - - try { - $this->dashboard->manageEntry($home); - $home->manageEntry($pane); - - $this->dumpArbitaryDashlets(false); - - if (($name = $this->getPopulatedValue('dashlet')) && ($url = $this->getPopulatedValue('url'))) { - if ($this->duplicateCustomDashlet) { - Notification::error(sprintf( - t('Failed to create new dahlets. Dashlet "%s" exists within the selected one'), - $name - )); - - return; - } - - $dashlet = new Dashlet($name, $url, $pane); - $pane->manageEntry($dashlet); - } - - $pane->manageEntry(self::$moduleDashlets); - - $conn->commitTransaction(); - } catch (\Exception $err) { - $conn->rollBackTransaction(); - throw $err; - } - - Notification::success(t('Added new dashlet(s) successfully')); - } - } -}