Merge some related classes

This commit is contained in:
Yonas Habteab 2022-04-14 09:44:00 +02:00
parent 1429d4aa65
commit 49b37ea522
4 changed files with 158 additions and 177 deletions

View File

@ -10,13 +10,13 @@ use Icinga\Forms\Dashboard\HomePaneForm;
use Icinga\Forms\Dashboard\NewHomePaneForm; use Icinga\Forms\Dashboard\NewHomePaneForm;
use Icinga\Forms\Dashboard\RemoveDashletForm; use Icinga\Forms\Dashboard\RemoveDashletForm;
use Icinga\Forms\Dashboard\RemoveHomePaneForm; use Icinga\Forms\Dashboard\RemoveHomePaneForm;
use Icinga\Forms\Dashboard\SetupNewDashboardForm;
use Icinga\Forms\Dashboard\WelcomeForm; use Icinga\Forms\Dashboard\WelcomeForm;
use Icinga\Util\Json; use Icinga\Util\Json;
use Icinga\Web\Dashboard\Dashboard; use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Dashboard\DashboardHome;
use Icinga\Web\Dashboard\Pane; use Icinga\Web\Dashboard\Pane;
use Icinga\Web\Dashboard\Settings; use Icinga\Web\Dashboard\Settings;
use Icinga\Web\Dashboard\Setup\SetupNewDashboard;
use Icinga\Web\Notification; use Icinga\Web\Notification;
use Icinga\Web\Widget\Tabextension\DashboardSettings; use Icinga\Web\Widget\Tabextension\DashboardSettings;
use ipl\Web\Compat\CompatController; use ipl\Web\Compat\CompatController;
@ -395,8 +395,8 @@ class DashboardsController extends CompatController
$this->setTitle(t('Add Dashlet')); $this->setTitle(t('Add Dashlet'));
} }
$setupForm = new SetupNewDashboard($this->dashboard); $setupForm = new SetupNewDashboardForm($this->dashboard);
$setupForm->on(SetupNewDashboard::ON_SUCCESS, function () use ($setupForm) { $setupForm->on(SetupNewDashboardForm::ON_SUCCESS, function () use ($setupForm) {
$this->redirectNow($setupForm->getRedirectUrl()); $this->redirectNow($setupForm->getRedirectUrl());
})->handleRequest(ServerRequest::fromGlobals()); })->handleRequest(ServerRequest::fromGlobals());

View File

@ -14,21 +14,34 @@ use Icinga\Web\Dashboard\Pane;
use ipl\Html\HtmlElement; use ipl\Html\HtmlElement;
use ipl\Web\Url; 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; return;
} }
$this->dumpArbitaryDashlets();
$this->assembleNexPageDashletPart();
}
protected function assemble()
{
if ($this->isUpdatingADashlet() || $this->getPopulatedValue('btn_next')) {
$requestUrl = Url::fromRequest(); $requestUrl = Url::fromRequest();
$homes = $this->dashboard->getEntryKeyTitleArr(); $homes = $this->dashboard->getEntryKeyTitleArr();
@ -110,9 +123,15 @@ class DashletForm extends BaseSetupDashboard
'description' => t('Enter a title for the new dashboard.'), 'description' => t('Enter a title for the new dashboard.'),
]); ]);
} }
}
protected function assemble()
{
if ($this->isUpdatingADashlet() || $this->getPopulatedValue('btn_next')) {
$this->assembleNextPage();
if ($this->isUpdatingADashlet()) { 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')); $removeButton = $this->createRemoveButton($targetUrl, t('Remove Dashlet'));
$formControls = $this->createFormControls(); $formControls = $this->createFormControls();
@ -124,8 +143,6 @@ class DashletForm extends BaseSetupDashboard
$this->addHtml($formControls); $this->addHtml($formControls);
} else { } else {
$this->assembleNextPage();
$formControls = $this->createFormControls(); $formControls = $this->createFormControls();
$formControls->add([ $formControls->add([
$this->registerSubmitButton(t('Add to Dashboard')), $this->registerSubmitButton(t('Add to Dashboard')),
@ -315,17 +332,4 @@ class DashletForm extends BaseSetupDashboard
Notification::success(sprintf(t('Updated dashlet "%s" successfully'), $currentDashlet->getTitle())); 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()
));
}
} }

View File

@ -3,15 +3,18 @@
namespace Icinga\Forms\Dashboard; namespace Icinga\Forms\Dashboard;
use Icinga\Web\Dashboard\Dashboard; use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
use Icinga\Web\Dashboard\Dashlet; use Icinga\Web\Dashboard\Dashlet;
use Icinga\Web\Dashboard\ItemList\DashletListMultiSelect; use Icinga\Web\Dashboard\ItemList\DashletListMultiSelect;
use Icinga\Web\Dashboard\ItemList\EmptyDashlet; use Icinga\Web\Dashboard\ItemList\EmptyDashlet;
use Icinga\Web\Dashboard\Pane;
use Icinga\Web\Notification;
use ipl\Html\HtmlElement; use ipl\Html\HtmlElement;
use ipl\Html\ValidHtml; use ipl\Html\ValidHtml;
use ipl\Web\Url; use ipl\Web\Url;
use ipl\Web\Widget\Icon; use ipl\Web\Widget\Icon;
abstract class BaseSetupDashboard extends BaseDashboardForm class SetupNewDashboardForm extends BaseDashboardForm
{ {
const DATA_TOGGLE_ELEMENT = 'dashlets-list-info'; const DATA_TOGGLE_ELEMENT = 'dashlets-list-info';
@ -31,6 +34,9 @@ abstract class BaseSetupDashboard extends BaseDashboardForm
if (empty(self::$moduleDashlets)) { if (empty(self::$moduleDashlets)) {
self::$moduleDashlets = Dashboard::getModuleDashlets(); 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() protected function assembleNextPage()
{ {
if (! $this->getPopulatedValue('btn_next')) { if (! $this->isUpdatingADashlet() && ! $this->getPopulatedValue('btn_next')) {
return; return;
} }
@ -245,6 +251,47 @@ abstract class BaseSetupDashboard extends BaseDashboardForm
$this->addHtml($formControls); $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) * Create form list controls (can be collapsible if you want)
* *

View File

@ -1,70 +0,0 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Icinga\Web\Dashboard\Setup;
use Icinga\Forms\Dashboard\BaseDashboardForm;
use Icinga\Forms\Dashboard\BaseSetupDashboard;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
use Icinga\Web\Dashboard\Dashlet;
use Icinga\Web\Dashboard\Pane;
use Icinga\Web\Notification;
use Icinga\Web\Dashboard\ItemList\DashletListMultiSelect;
use ipl\Html\HtmlElement;
use ipl\Html\ValidHtml;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;
class SetupNewDashboard extends BaseSetupDashboard
{
protected function init()
{
parent::init();
$this->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'));
}
}
}