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\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());

View File

@ -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()
));
}
}

View File

@ -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)
*

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'));
}
}
}