Forms: Unify notification messages & add some return type declarations

This commit is contained in:
Yonas Habteab 2022-06-14 09:02:53 +02:00
parent 303084e34a
commit 67b9d58557
9 changed files with 239 additions and 85 deletions

View File

@ -18,9 +18,9 @@ use ipl\Web\Widget\Icon;
*/
abstract class BaseDashboardForm extends CompatForm
{
const CREATE_NEW_HOME = 'Create new Home';
public const CREATE_NEW_HOME = 'Create new Home';
const CREATE_NEW_PANE = 'Create new Dashboard';
public const CREATE_NEW_PANE = 'Create new Dashboard';
/**
* Dashboard instance for which this form is being rendered
@ -32,6 +32,8 @@ abstract class BaseDashboardForm extends CompatForm
/** @var Url */
protected $requestUrl;
protected $requestSucceeded = false;
/**
* Create a new Dashboard Form
*
@ -50,7 +52,7 @@ abstract class BaseDashboardForm extends CompatForm
*
* @return void
*/
protected function init()
protected function init(): void
{
// This is needed for the modal views
$this->setAction((string) $this->requestUrl);
@ -71,10 +73,20 @@ abstract class BaseDashboardForm extends CompatForm
*
* @return void
*/
public function load(BaseDashboard $dashboard)
public function load(BaseDashboard $dashboard): void
{
}
/**
* Get whether the current request was successfully processed
*
* @return bool
*/
public function requestSucceeded(): bool
{
return $this->requestSucceeded;
}
/**
* Get whether we are updating an existing widget
*

View File

@ -16,14 +16,14 @@ use ipl\Html\HtmlElement;
class DashletForm extends SetupNewDashboardForm
{
protected function init()
protected function init(): void
{
parent::init();
$this->setAction((string) $this->requestUrl);
}
public function load(BaseDashboard $dashboard)
public function load(BaseDashboard $dashboard): void
{
/** @var Dashlet $dashboard */
$this->populate([
@ -79,7 +79,7 @@ class DashletForm extends SetupNewDashboardForm
'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.')
'description' => t('Select a dashboard home you want to add the pane to.')
]);
if (empty($homes) || $populatedHome === self::CREATE_NEW_HOME) {
@ -134,7 +134,7 @@ class DashletForm extends SetupNewDashboardForm
$formControls = $this->createFormControls();
$formControls->addHtml(
$this->registerSubmitButton(t('Add to Dashboard')),
$this->registerSubmitButton(t('Update Dashlet')),
$removeButton,
$this->createCancelButton()
);
@ -192,6 +192,8 @@ class DashletForm extends SetupNewDashboardForm
if (! $this->isUpdating()) {
$customDashlet = null;
$countDashlets = $currentPane->countEntries();
if (($dashlet = $this->getPopulatedValue('dashlet')) && ($url = $this->getPopulatedValue('url'))) {
$customDashlet = new Dashlet($dashlet, $url, $currentPane);
$customDashlet->setDescription($this->getPopulatedValue('description'));
@ -199,15 +201,14 @@ class DashletForm extends SetupNewDashboardForm
if ($currentPane->hasEntry($customDashlet->getName()) || $this->customDashletAlreadyExists) {
if ($this->customDashletAlreadyExists) {
$message = sprintf(
t('The specified custom Dashlet name "%s" is the same as one of the selected' .
' module Dashlets.'),
$customDashlet->getName()
t('Failed to create custom Dashlet! The selected module Dashlet(s) contains Dashlet "%s"'),
$customDashlet->getTitle()
);
} else {
$message = sprintf(
t('Dashlet "%s" already exists within the "%s" dashboard pane'),
$customDashlet->getTitle(),
$currentPane->getTitle()
t('Dashboard pane "%s" has already a Dashlet called "%s"'),
$currentPane->getTitle(),
$customDashlet->getTitle()
);
}
@ -236,9 +237,9 @@ class DashletForm extends SetupNewDashboardForm
foreach ($dashlets as $dashlet) {
if ($currentPane->hasEntry($dashlet->getName())) {
Notification::error(sprintf(
t('Dashlet "%s" already exists within the "%s" dashboard pane'),
$dashlet->getTitle(),
$currentPane->getTitle()
t('Pane "%s" has already a Dashlet called "%s"'),
$currentPane->getTitle(),
$dashlet->getTitle()
));
return;
@ -253,13 +254,27 @@ class DashletForm extends SetupNewDashboardForm
$conn->commitTransaction();
} catch (Exception $err) {
Logger::error($err);
$conn->rollBackTransaction();
throw $err;
Logger::error('Unable to add new Dashlet(s). An unexpected error occurred: %s', $err);
Notification::error(
t('Failed to successfully add new Dashlet(s). Please check the logs for details!')
);
return;
}
Notification::success(t('Created dashlet(s) successfully'));
$countDashlets = $currentPane->countEntries() - $countDashlets;
$dashlet = $currentPane->getEntries();
$dashlet = end($dashlet);
$this->requestSucceeded = true;
Notification::success(sprintf(
tp('Added Dashlet "%s" successfully', 'Added %d Dashlets successfully', $countDashlets),
$countDashlets === 1 ? $dashlet->getTitle() : $countDashlets
));
} else {
$orgHome = $dashboard->getEntry($this->getValue('org_home'));
$orgPane = $orgHome->getEntry($this->getValue('org_pane'));
@ -290,12 +305,16 @@ class DashletForm extends SetupNewDashboardForm
->setTitle($this->getValue('dashlet'))
->setDescription($this->getValue('description'));
if ($orgPane->getName() !== $currentPane->getName()
&& $currentPane->hasEntry($currentDashlet->getName())) {
if ($currentPane->hasEntry($currentDashlet->getName())
&& (
$currentHome->getName() !== $orgHome->getName()
|| $orgPane->getName() !== $currentPane->getName()
)
) {
Notification::error(sprintf(
t('Failed to move dashlet "%s": Dashlet already exists within the "%s" dashboard pane'),
$currentDashlet->getTitle(),
$currentPane->getTitle()
t('Failed to move a Dashlet: Pane "%s" has already a Dashlet called "%s"'),
$currentPane->getTitle(),
$currentDashlet->getTitle()
));
return;
@ -324,13 +343,22 @@ class DashletForm extends SetupNewDashboardForm
$conn->commitTransaction();
} catch (Exception $err) {
Logger::error($err);
$conn->rollBackTransaction();
throw $err;
Logger::error(
'Unable to update Dashlet "%s". An unexpected error occurred: %s',
$currentDashlet->getTitle(),
$err
);
Notification::error(t('Failed to update the Dashlet. Please check the logs for details!'));
return;
}
Notification::success(sprintf(t('Updated dashlet "%s" successfully'), $currentDashlet->getTitle()));
$this->requestSucceeded = true;
Notification::success(sprintf(t('Updated Dashlet "%s" successfully'), $currentDashlet->getTitle()));
}
}
}

View File

@ -4,6 +4,7 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Logger;
use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
@ -11,7 +12,7 @@ use Icinga\Web\Notification;
class HomeForm extends BaseDashboardForm
{
public function load(BaseDashboard $dashboard)
public function load(BaseDashboard $dashboard): void
{
$this->populate(['title' => $dashboard->getTitle()]);
}
@ -47,19 +48,49 @@ class HomeForm extends BaseDashboardForm
$home->setTitle($this->getPopulatedValue('title'));
$this->dashboard->manageEntry($home);
try {
$this->dashboard->manageEntry($home);
} catch (\Exception $err) {
Logger::error(
'Unable to update Dashboard Home "%s". An unexpected error occurred: %s',
$home->getTitle(),
$err
);
Notification::error(
t('Failed to successfully update the Dashboard Home. Please check the logs for details!')
);
Notification::success(sprintf(t('Updated dashboard home "%s" successfully'), $home->getTitle()));
} else {
$home = new DashboardHome($this->getPopulatedValue('title'));
if ($this->dashboard->hasEntry($home->getName())) {
Notification::error(sprintf(t('Dashboard home "%s" already exists'), $home->getName()));
return;
}
$this->dashboard->manageEntry($home);
Notification::success(sprintf(t('Updated Dashboard Home "%s" successfully'), $home->getTitle()));
} else {
$home = new DashboardHome($this->getPopulatedValue('title'));
if ($this->dashboard->hasEntry($home->getName())) {
Notification::error(sprintf(t('Dashboard Home "%s" already exists'), $home->getTitle()));
return;
}
Notification::success(sprintf(t('Added dashboard home "%s" successfully'), $home->getName()));
try {
$this->dashboard->manageEntry($home);
$this->requestSucceeded = true;
Notification::success(sprintf(t('Added Dashboard Home "%s" successfully'), $home->getTitle()));
} catch (\Exception $err) {
Logger::error(
'Unable to add Dashboard Home "%s". An unexpected error occurred: %s',
$home->getTitle(),
$err
);
Notification::error(
t('Failed to successfully add the Dashboard Home. Please check the logs for details!')
);
return;
}
}
}
}

View File

@ -14,6 +14,15 @@ use Icinga\Web\Dashboard\Dashboard;
class PaneForm extends BaseDashboardForm
{
public function load(BaseDashboard $dashboard): void
{
$this->populate([
'org_title' => $dashboard->getTitle(),
'title' => $dashboard->getTitle(),
'org_name' => $dashboard->getName()
]);
}
protected function assemble()
{
$this->addElement('hidden', 'org_name', ['required' => false]);
@ -24,7 +33,7 @@ class PaneForm extends BaseDashboardForm
'label' => t('Title'),
'placeholder' => t('Create new Dashboard'),
'description' => $this->isUpdating()
? t('Edit the title of this dashboard pane.')
? t('Edit the title of this pane.')
: t('Add new dashboard to this home.')
]);
@ -55,7 +64,7 @@ class PaneForm extends BaseDashboardForm
$formControls = $this->createFormControls();
$formControls->addHtml(
$this->registerSubmitButton($this->isUpdating() ? t('Update Pane') : t('Add Dashboard'))
$this->registerSubmitButton($this->isUpdating() ? t('Update Pane') : t('Add Pane'))
);
if ($this->isUpdating()) {
@ -102,9 +111,9 @@ class PaneForm extends BaseDashboardForm
if ($orgHome->getName() !== $currentHome->getName() && $currentHome->hasEntry($currentPane->getName())) {
Notification::error(sprintf(
t('Failed to move dashboard "%s": Dashbaord pane already exists within the "%s" dashboard home'),
$currentPane->getTitle(),
$currentHome->getTitle()
t('Failed to move a pane: Dashboard Home "%s" has already a Pane called "%s"'),
$currentHome->getTitle(),
$currentPane->getTitle()
));
return;
@ -120,18 +129,29 @@ class PaneForm extends BaseDashboardForm
$conn->commitTransaction();
} catch (\Exception $err) {
Logger::error($err);
$conn->rollBackTransaction();
Logger::error(
'Unable to update pane "%s". An unexpected error occurred: %s',
$currentPane->getTitle(),
$err
);
Notification::error(t('Failed to successfully update the pane. Please check the logs for details!'));
return;
}
Notification::success(sprintf(t('Updated dashboard pane "%s" successfully'), $currentPane->getTitle()));
$this->requestSucceeded = true;
Notification::success(sprintf(t('Updated pane "%s" successfully'), $currentPane->getTitle()));
} else {
$pane = new Pane($this->getPopulatedValue('title'));
if ($currentHome->hasEntry($pane->getName())) {
Notification::error(sprintf(
t('Failed to create dashboard "%s": Dashbaord pane already exists within the "%s" dashboard home'),
$pane->getTitle(),
$currentHome->getTitle()
t('Failed to add pane: Dashboard Home "%s" has already a Pane called "%s"'),
$currentHome->getTitle(),
$pane->getTitle()
));
return;
@ -146,19 +166,21 @@ class PaneForm extends BaseDashboardForm
$conn->commitTransaction();
} catch (\Exception $err) {
$conn->rollBackTransaction();
throw $err;
Logger::error(
'Unable to add pane "%s". An unexpected error occurred: %s',
$pane->getTitle(),
$err
);
Notification::error(t('Failed to successfully add the pane. Please check the logs for details!'));
return;
}
Notification::success(sprintf(t('Added dashboard pane "%s" successfully'), $pane->getName()));
$this->requestSucceeded = true;
Notification::success(sprintf(t('Added pane "%s" successfully'), $pane->getTitle()));
}
}
public function load(BaseDashboard $dashboard)
{
$this->populate([
'org_title' => $dashboard->getTitle(),
'title' => $dashboard->getTitle(),
'org_name' => $dashboard->getName()
]);
}
}

View File

@ -4,9 +4,9 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Logger;
use Icinga\Web\Notification;
use ipl\Html\HtmlElement;
use ipl\Web\Url;
class RemoveDashletForm extends BaseDashboardForm
{
@ -18,8 +18,8 @@ class RemoveDashletForm extends BaseDashboardForm
protected function assemble()
{
$this->addHtml(HtmlElement::create('h1', null, sprintf(
t('Please confirm removal of dashlet "%s"'),
Url::fromRequest()->getParam('dashlet')
t('Please confirm removal of Dashlet "%s"'),
$this->requestUrl->getParam('dashlet')
)));
$submit = $this->registerSubmitButton(t('Remove Dashlet'));
@ -30,13 +30,27 @@ class RemoveDashletForm extends BaseDashboardForm
protected function onSuccess()
{
$requestUrl = Url::fromRequest();
$home = $this->dashboard->getActiveHome();
$pane = $home->getEntry($requestUrl->getParam('pane'));
$pane = $home->getActivePane();
$dashlet = $requestUrl->getParam('dashlet');
$pane->removeEntry($dashlet);
$dashlet = $pane->getEntry($this->requestUrl->getParam('dashlet'));
Notification::success(sprintf(t('Removed dashlet "%s" successfully'), $dashlet));
try {
$pane->removeEntry($dashlet);
$this->requestSucceeded = true;
} catch (\Exception $err) {
Logger::error(
'Unable to remove Dashlet "%s". An unexpected error occurred: %s',
$dashlet->getTitle(),
$err
);
Notification::error(t('Failed to successfully remove the Dashlet. Please check the logs for details!'));
return;
}
Notification::success(sprintf(t('Removed Dashlet "%s" successfully'), $dashlet->getTitle()));
}
}

View File

@ -4,6 +4,7 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Logger;
use Icinga\Web\Notification;
use ipl\Html\HtmlElement;
@ -19,7 +20,7 @@ class RemoveHomeForm extends BaseDashboardForm
$this->addHtml(HtmlElement::create(
'h2',
null,
sprintf(t('Please confirm removal of dashboard home "%s"'), $this->dashboard->getActiveHome()->getTitle())
sprintf(t('Please confirm removal of Dashboard Home "%s"'), $this->dashboard->getActiveHome()->getTitle())
));
$this->addHtml($this->registerSubmitButton('Remove Home')->setName('btn_remove'));
@ -28,8 +29,25 @@ class RemoveHomeForm extends BaseDashboardForm
protected function onSuccess()
{
$home = $this->dashboard->getActiveHome();
$this->dashboard->removeEntry($home);
Notification::success(sprintf(t('Removed dashboard home "%s" successfully'), $home->getTitle()));
try {
$this->dashboard->removeEntry($home);
$this->requestSucceeded = true;
Notification::success(sprintf(t('Removed Dashboard Home "%s" successfully'), $home->getTitle()));
} catch (\Exception $err) {
Logger::error(
'Unable to remove Dashboard Home "%s". An unexpected error occurred: %s',
$home->getTitle(),
$err
);
Notification::error(
t('Failed to successfully remove the Dashboard Home. Please check the logs for details!')
);
return;
}
}
}

View File

@ -4,10 +4,10 @@
namespace Icinga\Forms\Dashboard;
use Exception;
use Icinga\Application\Logger;
use Icinga\Web\Notification;
use Icinga\Web\Dashboard\Dashboard;
use ipl\Html\HtmlElement;
use ipl\Web\Url;
class RemovePaneForm extends BaseDashboardForm
{
@ -21,7 +21,7 @@ class RemovePaneForm extends BaseDashboardForm
$this->addHtml(HtmlElement::create(
'h2',
null,
sprintf(t('Please confirm removal of dashboard pane "%s"'), $this->requestUrl->getParam('pane'))
sprintf(t('Please confirm removal of pane "%s"'), $this->requestUrl->getParam('pane'))
));
$this->addHtml($this->registerSubmitButton(t('Remove Pane'))->setName('btn_remove'));
@ -30,10 +30,22 @@ class RemovePaneForm extends BaseDashboardForm
protected function onSuccess()
{
$home = $this->dashboard->getActiveHome();
$pane = $home->getActivePane();
$pane = $home->getEntry($this->requestUrl->getParam('pane'));
$home->removeEntry($pane);
try {
$home->removeEntry($pane);
Notification::success(sprintf(t('Removed dashboard pane "%s" successfully'), $pane->getTitle()));
$this->requestSucceeded = true;
Notification::success(sprintf(t('Removed pane "%s" successfully'), $pane->getTitle()));
} catch (Exception $err) {
Logger::error(
'Unable to remove pane "%s". An unexpected error occurred: %s',
$pane->getTitle(),
$err
);
Notification::error(t('Failed to successfully remove the pane. Please check the logs for details!'));
}
}
}

View File

@ -4,6 +4,7 @@
namespace Icinga\Forms\Dashboard;
use Icinga\Application\Logger;
use Icinga\Application\Modules;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
@ -31,7 +32,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
/** @var bool Whether the created custom dashlets with custom url & filter already exists */
protected $customDashletAlreadyExists = false;
protected function init()
protected function init(): void
{
parent::init();
@ -73,6 +74,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
}
if (isset($chosenDashlets[$module]) && ! $this->customDashletAlreadyExists) {
// This should never ever happen, but hey, it never harms to play it save!!
$this->customDashletAlreadyExists = array_key_exists(
$this->getPopulatedValue('dashlet'),
$chosenDashlets[$module]
@ -92,6 +94,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
{
$strict = $this->isUpdating() || $this->getPopulatedValue('btn_next') || ! $this->hasBeenSent();
$this->dumpArbitaryDashlets($strict);
$this->assembleNextPageDashboardPart();
$this->assembleNexPageDashletPart();
}
@ -146,7 +149,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
$this->addElement('text', 'pane', [
'required' => true,
'label' => t('Dashboard Title'),
'description' => t('Enter a title for the new dashboard you want to add the dashlets to')
'description' => t('Enter a title for the new dashboard you want to add the dashlets to.')
]);
}
@ -184,14 +187,14 @@ class SetupNewDashboardForm extends BaseDashboardForm
'label' => t('Url'),
'value' => $dashlet->getUrl()->getRelativeUrl(),
'description' => t(
'Enter url to be loaded in the dashlet. You can paste the full URL, including filters'
'Enter url to be loaded in the dashlet. You can paste the full URL, including filters.'
)
]);
$this->addElement('textarea', $elementId . '_description', [
'label' => t('Description'),
'value' => $dashlet->getDescription(),
'description' => t('Enter description for the dashlet')
'description' => t('Enter description for the dashlet.')
]);
}
}
@ -219,7 +222,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
$this->addElement('textarea', 'description', [
'label' => t('Description'),
'placeholder' => t('Enter dashlet description'),
'description' => t('Enter description for the dashlet'),
'description' => t('Enter description for the dashlet.'),
]);
}
@ -264,7 +267,7 @@ class SetupNewDashboardForm extends BaseDashboardForm
if (($name = $this->getPopulatedValue('dashlet')) && ($url = $this->getPopulatedValue('url'))) {
if ($this->customDashletAlreadyExists) {
Notification::error(sprintf(
t('Failed to create new dahlets. Dashlet "%s" exists within the selected module Dashlets.'),
t('Failed to create custom Dashlet! The selected module Dashlet(s) contains Dashlet "%s"'),
$name
));
@ -280,10 +283,24 @@ class SetupNewDashboardForm extends BaseDashboardForm
$conn->commitTransaction();
} catch (\Exception $err) {
$conn->rollBackTransaction();
throw $err;
Logger::error('Unable to create new Dashlet(s). An unexpected error occurred: %s', $err);
Notification::error(t('Failed to create new Dashlet(s). Please check the logs for details!'));
return;
}
Notification::success(t('Added new dashlet(s) successfully'));
$this->requestSucceeded = true;
$count = $pane->countEntries();
$dashlet = $pane->getEntries();
$dashlet = end($dashlet);
Notification::success(sprintf(
tp('Added Dashlet %s successfully', 'Added %d Dashlets successfully', $count),
$count === 1 ? $dashlet->getTitle() : $count
));
}
}

View File

@ -68,11 +68,11 @@ class WelcomeForm extends Form
Logger::error('Unable to apply the system defaults into the DB. An error occurred: %s', $err);
Notification::error(t('Failed to successfully save the data. Please check the logs for details.'));
Notification::error(t('Failed to successfully save the data. Please check the logs for details!'));
return;
}
Notification::success(t('Imported system defaults successfully.'));
Notification::success(t('Imported system defaults successfully'));
}
}