diff --git a/application/controllers/DashboardsController.php b/application/controllers/DashboardsController.php index 9cd791b21..d576a9535 100644 --- a/application/controllers/DashboardsController.php +++ b/application/controllers/DashboardsController.php @@ -7,15 +7,19 @@ namespace Icinga\Controllers; use GuzzleHttp\Psr7\ServerRequest; use Icinga\Forms\Dashboard\DashletForm; use Icinga\Forms\Dashboard\HomePaneForm; +use Icinga\Forms\Dashboard\NewHomePaneForm; use Icinga\Forms\Dashboard\RemoveDashletForm; use Icinga\Forms\Dashboard\RemoveHomePaneForm; use Icinga\Forms\Dashboard\WelcomeForm; use Icinga\Model\ModuleDashlet; +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\Html\HtmlElement; use ipl\Web\Compat\CompatController; use ipl\Web\Url; use ipl\Web\Widget\ActionLink; @@ -40,7 +44,7 @@ class DashboardsController extends CompatController $this->createTabs(); $activeHome = $this->dashboard->getActiveHome(); - if (! $activeHome || ! $activeHome->hasPanes()) { + if (! $activeHome || ! $activeHome->hasEntries()) { $this->getTabs()->add('dashboard', [ 'active' => true, 'title' => t('Welcome'), @@ -54,8 +58,6 @@ class DashboardsController extends CompatController })->handleRequest(ServerRequest::fromGlobals()); $this->dashboard->setWelcomeForm($welcomeForm); - } elseif (empty($activeHome->getPanes(true))) { - // TODO(TBD): What to do when the user has only disabled dashboards? Should we render the welcome screen? } elseif (($pane = $this->getParam('pane'))) { $this->getTabs()->activate($pane); } @@ -71,38 +73,43 @@ class DashboardsController extends CompatController public function homeAction() { $home = $this->params->getRequired('home'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } - $this->createTabs(); - $activeHome = $this->dashboard->getActiveHome(); - if (! $activeHome || empty($activeHome->getPanes(true))) { - $this->getTabs()->add($home, [ + if (! $activeHome->getEntries()) { + $this->getTabs()->add($activeHome->getName(), [ 'active' => true, - 'title' => $home, + 'title' => $activeHome->getTitle(), 'url' => Url::fromRequest() ]); + + // Not to render the cog icon before the above tab + $this->createTabs(); } elseif (($pane = $this->getParam('pane'))) { + $this->createTabs(); + $this->dashboard->activate($pane); } $this->content = $this->dashboard; } - public function renameHomeAction() + public function editHomeAction() { $this->setTitle(t('Update Home')); $home = $this->params->getRequired('home'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } $homeForm = (new HomePaneForm($this->dashboard)) ->on(HomePaneForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); }) ->handleRequest(ServerRequest::fromGlobals()); @@ -115,19 +122,41 @@ class DashboardsController extends CompatController $this->setTitle(t('Remove Home')); $home = $this->params->getRequired('home'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } $homeForm = (new RemoveHomePaneForm($this->dashboard)) ->on(RemoveHomePaneForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); }) ->handleRequest(ServerRequest::fromGlobals()); $this->addContent($homeForm); } + public function newPaneAction() + { + $this->setTitle(t('Add new Dashboard')); + + $home = $this->params->getRequired('home'); + if (! $this->dashboard->hasEntry($home)) { + $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); + } + + $paneForm = (new NewHomePaneForm($this->dashboard)) + ->on(NewHomePaneForm::ON_SUCCESS, function () { + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); + }) + ->handleRequest(ServerRequest::fromGlobals()); + + $this->addContent($paneForm); + } + public function editPaneAction() { $this->setTitle(t('Update Pane')); @@ -135,21 +164,23 @@ class DashboardsController extends CompatController $pane = $this->params->getRequired('pane'); $home = $this->params->getRequired('home'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } - if (! $this->dashboard->getActiveHome()->hasPane($pane)) { + if (! $this->dashboard->getActiveHome()->hasEntry($pane)) { $this->httpNotFound(sprintf(t('Pane "%s" not found'), $pane)); } $paneForm = (new HomePaneForm($this->dashboard)) ->on(HomePaneForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); }) ->handleRequest(ServerRequest::fromGlobals()); - $paneForm->load($this->dashboard->getActiveHome()->getPane($pane)); + $paneForm->load($this->dashboard->getActiveHome()->getEntry($pane)); $this->addContent($paneForm); } @@ -161,25 +192,21 @@ class DashboardsController extends CompatController $home = $this->params->getRequired('home'); $paneParam = $this->params->getRequired('pane'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } - if (! $this->dashboard->getActiveHome()->hasPane($paneParam)) { + if (! $this->dashboard->getActiveHome()->hasEntry($paneParam)) { $this->httpNotFound(sprintf(t('Pane "%s" not found'), $paneParam)); } $paneForm = new RemoveHomePaneForm($this->dashboard); $paneForm->populate(['org_name' => $paneParam]); $paneForm->on(RemoveHomePaneForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); - })->handleRequest(ServerRequest::fromGlobals()); + $this->getResponse()->setAutoRefreshInterval(1); - $paneForm->getElement('btn_remove')->setLabel(t('Remove Pane')); - $paneForm->prependHtml(HtmlElement::create('h1', null, sprintf( - t('Please confirm removal of dashboard pane "%s"'), - $paneParam - ))); + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); + })->handleRequest(ServerRequest::fromGlobals()); $this->addContent($paneForm); } @@ -191,16 +218,11 @@ class DashboardsController extends CompatController $dashletForm = new DashletForm($this->dashboard); $dashletForm->populate($this->getRequest()->getPost()); $dashletForm->on(DashletForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); })->handleRequest(ServerRequest::fromGlobals()); - $params = $this->getAllParams(); - if ($this->getParam('url')) { - $params['url'] = rawurldecode($this->getParam('url')); - } - - $dashletForm->populate($params); - $this->addContent($dashletForm); } @@ -209,11 +231,13 @@ class DashboardsController extends CompatController $this->setTitle(t('Edit Dashlet')); $pane = $this->validateDashletParams(); - $dashlet = $pane->getDashlet($this->getParam('dashlet')); + $dashlet = $pane->getEntry($this->getParam('dashlet')); $dashletForm = (new DashletForm($this->dashboard)) ->on(DashletForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); }) ->handleRequest(ServerRequest::fromGlobals()); @@ -225,12 +249,14 @@ class DashboardsController extends CompatController public function removeDashletAction() { - $this->validateDashletParams(); $this->setTitle(t('Remove Dashlet')); + $this->validateDashletParams(); $removeForm = (new RemoveDashletForm($this->dashboard)) ->on(RemoveDashletForm::ON_SUCCESS, function () { - $this->redirectNow('__CLOSE__'); + $this->getResponse()->setAutoRefreshInterval(1); + + $this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings')); }) ->handleRequest(ServerRequest::fromGlobals()); @@ -243,60 +269,109 @@ class DashboardsController extends CompatController public function reorderWidgetsAction() { $this->assertHttpMethod('post'); - if (! $this->getRequest()->isApiRequest()) { - $this->httpBadRequest('No API request'); - } - - if (! preg_match('/([^;]*);?/', $this->getRequest()->getHeader('Content-Type'), $matches) - || $matches[1] !== 'application/json') { - $this->httpBadRequest('No JSON content'); - } - $dashboards = $this->getRequest()->getPost(); - $widgetType = array_pop($dashboards); + if (! isset($dashboards['dashboardData'])) { + $this->httpBadRequest(t('Invalid request data')); + } - foreach ($dashboards as $key => $panes) { - $home = $widgetType === 'Homes' ? $panes : $key; - if (! $this->dashboard->hasHome($home)) { - $this->httpNotFound(sprintf(t('Dashboard home "%s" not found'), $home)); + $dashboards = Json::decode($dashboards['dashboardData'], true); + $originals = $dashboards['originals']; + unset($dashboards['Type']); + unset($dashboards['originals']); + + $orgHome = null; + $orgPane = null; + if ($originals && isset($originals['originalHome'])) { + /** @var DashboardHome $orgHome */ + $orgHome = $this->dashboard->getEntry($originals['originalHome']); + $orgHome->setActive()->loadDashboardEntries(); + + if (isset($originals['originalPane'])) { + $orgPane = $orgHome->getEntry($originals['originalPane']); + $orgHome->setEntries([$orgPane->getName() => $orgPane]); + } + } + + $reroutePath = $dashboards['redirectPath']; + unset($dashboards['redirectPath']); + + foreach ($dashboards as $home => $value) { + if (! $this->dashboard->hasEntry($home)) { + Notification::error(sprintf(t('Dashboard home "%s" not found'), $home)); + break; } - $home = $this->dashboard->getHome($home); - if ($widgetType === 'Homes') { - $home->setPriority($key); - $this->dashboard->manageHome($home); + /** @var DashboardHome $home */ + $home = $this->dashboard->getEntry($home); + if (! is_array($value)) { + $this->dashboard->reorderWidget($home, (int) $value); - continue; + Notification::success(sprintf(t('Updated dashboard home "%s" successfully'), $home->getTitle())); + break; } - $home->setActive(); - $home->loadPanesFromDB(); - - foreach ($panes as $innerKey => $value) { - $pane = $widgetType === 'Dashboards' ? $value : $innerKey; - if (! $home->hasPane($pane)) { - $this->httpNotFound(sprintf(t('Dashboard pane "%s" not found'), $pane)); + $home->setActive()->loadDashboardEntries(); + foreach ($value as $pane => $indexOrValues) { + if (! $home->hasEntry($pane) && (! $orgHome || ! $orgHome->hasEntry($pane))) { + Notification::error(sprintf(t('Dashboard pane "%s" not found'), $pane)); + break; } - $pane = $home->getPane($pane); - if ($widgetType === 'Dashboards') { - $pane->setPriority($innerKey); - $home->managePanes($pane); - } else { - foreach ($value as $order => $dashlet) { - if (! $pane->hasDashlet($dashlet)) { - $this->httpNotFound(sprintf(t('Dashlet "%s" not found'), $dashlet)); - } - - $dashlet = $pane->getDashlet($dashlet); - $dashlet->setPriority($order); - $pane->manageDashlets($dashlet); + /** @var Pane $pane */ + $pane = $home->hasEntry($pane) ? $home->getEntry($pane) : $orgHome->getEntry($pane); + if (! is_array($indexOrValues)) { + if ($orgHome && $orgHome->hasEntry($pane->getName()) && $home->hasEntry($pane->getName())) { + Notification::error(sprintf( + t('Dashboard "%s" already exists within "%s" home'), + $pane->getTitle(), + $home->getTitle() + )); + break; } + + // Perform DB updates + $home->reorderWidget($pane, (int) $indexOrValues, $orgHome); + if ($orgHome) { + // In order to properly update the dashlets id (user + home + pane + dashlet) + $pane->manageEntry($pane->getEntries()); + } + + Notification::success(sprintf( + t('%s dashboard pane "%s" successfully'), + $orgHome ? 'Moved' : 'Updated', + $pane->getTitle() + )); + break; + } + + foreach ($indexOrValues as $dashlet => $index) { + if (! $pane->hasEntry($dashlet) && (! $orgPane || ! $orgPane->hasEntry($dashlet))) { + Notification::error(sprintf(t('Dashlet "%s" not found'), $dashlet)); + break; + } + + if ($orgPane && $orgPane->hasEntry($dashlet) && $pane->hasEntry($dashlet)) { + Notification::error(sprintf( + t('Dashlet "%s" already exists within "%s" dashboard pane'), + $dashlet, + $pane->getTitle() + )); + break; + } + + $dashlet = $pane->hasEntry($dashlet) ? $pane->getEntry($dashlet) : $orgPane->getEntry($dashlet); + $pane->reorderWidget($dashlet, (int) $index, $orgPane); + + Notification::success(sprintf( + t('%s dashlet "%s" successfully'), + $orgPane ? 'Moved' : 'Updated', + $dashlet->getTitle() + )); } } } - exit; + $this->redirectNow($reroutePath); } /** @@ -320,10 +395,6 @@ class DashboardsController extends CompatController $setupForm = new SetupNewDashboard($this->dashboard); $setupForm->initDashlets(Dashboard::getModuleDashlets($query)); $setupForm->on(SetupNewDashboard::ON_SUCCESS, function () use ($setupForm) { - if ($setupForm->getPopulatedValue('btn_cancel')) { - $this->redirectNow('__CLOSE__'); - } - $this->redirectNow($setupForm->getRedirectUrl()); })->handleRequest(ServerRequest::fromGlobals()); @@ -333,8 +404,12 @@ class DashboardsController extends CompatController public function settingsAction() { $this->createTabs(); - // TODO(yh): This may raise an exception when the given tab name doesn't exist. - // But as ipl::Tabs() doesn't offer the possibility to check this beforehand, just ignore it for now!! + $activeHome = $this->dashboard->getActiveHome(); + // We can't grant access the user to the dashboard manager if there aren't any dashboards to manage + if (! $activeHome || (! $activeHome->hasEntries() && count($this->dashboard->getEntries()) === 1)) { + $this->redirectNow(Dashboard::BASE_ROUTE); + } + $this->dashboard->activate('dashboard_settings'); $this->addControl(new ActionLink( @@ -358,7 +433,7 @@ class DashboardsController extends CompatController { $tabs = $this->dashboard->getTabs(); $activeHome = $this->dashboard->getActiveHome(); - if ($activeHome && $activeHome->hasPanes()) { + if (($activeHome && $activeHome->hasEntries()) || count($this->dashboard->getEntries()) > 1) { $tabs->extend(new DashboardSettings()); } @@ -371,16 +446,16 @@ class DashboardsController extends CompatController $pane = $this->params->getRequired('pane'); $dashlet = $this->params->getRequired('dashlet'); - if (! $this->dashboard->hasHome($home)) { + if (! $this->dashboard->hasEntry($home)) { $this->httpNotFound(sprintf(t('Home "%s" not found'), $home)); } - if (! $this->dashboard->getActiveHome()->hasPane($pane)) { + if (! $this->dashboard->getActiveHome()->hasEntry($pane)) { $this->httpNotFound(sprintf(t('Pane "%s" not found'), $pane)); } - $pane = $this->dashboard->getActiveHome()->getPane($pane); - if (! $pane->hasDashlet($dashlet)) { + $pane = $this->dashboard->getActiveHome()->getEntry($pane); + if (! $pane->hasEntry($dashlet)) { $this->httpNotFound(sprintf(t('Dashlet "%s" not found'), $dashlet)); } diff --git a/application/forms/Dashboard/BaseDashboardForm.php b/application/forms/Dashboard/BaseDashboardForm.php new file mode 100644 index 000000000..5490caade --- /dev/null +++ b/application/forms/Dashboard/BaseDashboardForm.php @@ -0,0 +1,113 @@ +dashboard = $dashboard; + + // This is needed for the modal views + $this->setAction((string) Url::fromRequest()); + } + + public function hasBeenSubmitted() + { + // We don't use addElement() for the form controls, so the form has no way of knowing + // that we do have a submit button and will always be submitted with autosubmit elements + return $this->hasBeenSent() && $this->getPopulatedValue('submit'); + } + + /** + * Populate form data from config + * + * @param BaseDashboard $dashboard + * + * @return void + */ + public function load(BaseDashboard $dashboard) + { + } + + /** + * Create custom form controls + * + * @return HtmlElement + */ + protected function createFormControls() + { + return HtmlElement::create('div', ['class' => 'control-group form-controls']); + } + + /** + * Create a cancel button + * + * @return FormElement + */ + protected function createCancelButton() + { + return $this->createElement('submitButton', 'btn_cancel', ['class' => 'modal-cancel', 'label' => t('Cancel')]); + } + + /** + * Create a remove button + * + * @param Url $action + * @param string $label + * + * @return FormElement + */ + protected function createRemoveButton(Url $action, $label) + { + return $this->createElement('submitButton', 'btn_remove', [ + 'class' => 'remove-button', + 'label' => [new Icon('trash'), $label], + 'formaction' => (string) $action + ]); + } + + /** + * Create and register a submit button + * + * @param string $label + * + * @return FormElement + */ + protected function registerSubmitButton($label) + { + $submitElement = $this->createElement('submit', 'submit', ['class' => 'btn-primary', 'label' => $label]); + $this->registerElement($submitElement); + + return $submitElement; + } +} diff --git a/application/forms/Dashboard/DashletForm.php b/application/forms/Dashboard/DashletForm.php index 53b5abb8d..5d20634b3 100644 --- a/application/forms/Dashboard/DashletForm.php +++ b/application/forms/Dashboard/DashletForm.php @@ -6,136 +6,93 @@ namespace Icinga\Forms\Dashboard; use Exception; use Icinga\Application\Logger; +use Icinga\Web\Dashboard\Common\BaseDashboard; use Icinga\Web\Dashboard\Dashboard; -use Icinga\Web\Navigation\DashboardHome; +use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Notification; use Icinga\Web\Dashboard\Dashlet; use Icinga\Web\Dashboard\Pane; use ipl\Html\HtmlElement; -use ipl\Web\Compat\CompatForm; use ipl\Web\Url; -/** - * Form to add an url a dashboard pane - */ -class DashletForm extends CompatForm +class DashletForm extends BaseDashboardForm { - /** - * @var Dashboard - */ - private $dashboard; - - public function __construct(Dashboard $dashboard) - { - $this->dashboard = $dashboard; - - $this->setAction((string) Url::fromRequest()); - } - - public function hasBeenSubmitted() - { - return $this->hasBeenSent() && $this->getPopulatedValue('submit'); - } - protected function assemble() { $requestUrl = Url::fromRequest(); - $homes = $this->dashboard->getHomeKeyTitleArr(); + $homes = $this->dashboard->getEntryKeyTitleArr(); $activeHome = $this->dashboard->getActiveHome(); $currentHome = $requestUrl->getParam('home', reset($homes)); $populatedHome = $this->getPopulatedValue('home', $currentHome); $panes = []; - if ($currentHome === $populatedHome && $this->getPopulatedValue('create_new_home') !== 'y') { + 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->rewindHomes(); + $firstHome = $this->dashboard->rewindEntries(); if ($firstHome) { - $this->dashboard->loadDashboards($firstHome->getName()); + $this->dashboard->loadDashboardEntries($firstHome->getName()); - $panes = $firstHome->getPaneKeyTitleArr(); + $panes = $firstHome->getEntryKeyTitleArr(); } } else { - $panes = $activeHome->getPaneKeyTitleArr(); + $panes = $activeHome->getEntryKeyTitleArr(); } - } elseif ($this->dashboard->hasHome($populatedHome)) { - $this->dashboard->loadDashboards($populatedHome); + } elseif ($this->dashboard->hasEntry($populatedHome)) { + $this->dashboard->loadDashboardEntries($populatedHome); - $panes = $this->dashboard->getActiveHome()->getPaneKeyTitleArr(); + $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->addElement('checkbox', 'create_new_home', [ - 'class' => 'autosubmit', - 'required' => false, - 'disabled' => empty($homes) ?: null, - 'label' => t('New Dashboard Home'), - 'description' => t('Check this box if you want to add the dashboard to a new dashboard home.'), + $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'), + 'descriptions' => t('Select a dashboard home you want to add the dashboard pane to.') ]); - if (empty($homes) || $this->getPopulatedValue('create_new_home') === 'y') { - // $el->attrs->set() has no effect here anymore, so we need to register a proper callback - $this->getElement('create_new_home') - ->getAttributes() - ->registerAttributeCallback('checked', function () { - return true; - }); - - $this->addElement('text', 'home', [ + if (empty($homes) || $populatedHome === self::CREATE_NEW_HOME) { + $this->addElement('text', 'new_home', [ 'required' => true, - 'label' => t('Dashboard Home'), + 'label' => t('Home Title'), + 'placeholder' => t('Enter dashboard home title'), 'description' => t('Enter a title for the new dashboard home.') ]); - } else { - $this->addElement('select', 'home', [ - 'required' => true, - 'class' => 'autosubmit', - 'value' => $currentHome, - 'multiOptions' => $homes, - 'label' => t('Dashboard Home'), - 'descriptions' => t('Select a home you want to add the dashboard pane to.') - ]); } - $disable = empty($panes) || $this->getPopulatedValue('create_new_home') === 'y'; - $this->addElement('checkbox', 'create_new_pane', [ - 'required' => false, - 'class' => 'autosubmit', - 'disabled' => $disable ?: null, - 'label' => t('New Dashboard'), - 'description' => t('Check this box if you want to add the dashlet to a new dashboard.'), - ]); - + $populatedPane = $this->getPopulatedValue('pane'); // Pane element's values are depending on the home element's value - if (! in_array($this->getPopulatedValue('pane'), $panes)) { + if ($populatedPane !== self::CREATE_NEW_PANE && ! in_array($populatedPane, $panes)) { $this->clearPopulatedValue('pane'); } - if ($disable || $this->getValue('create_new_pane') === 'y') { - // $el->attrs->set() has no effect here anymore, so we need to register a proper callback - $this->getElement('create_new_pane') - ->getAttributes() - ->registerAttributeCallback('checked', function () { - return true; - }); + $populatedPane = $this->getPopulatedValue('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, // Cheat the browser complains + '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.'), + ]); - $this->addElement('text', 'pane', [ + if ($disable || $this->getPopulatedValue('pane') === self::CREATE_NEW_PANE) { + $this->addElement('text', 'new_pane', [ 'required' => true, - 'label' => t('New Dashboard Title'), + 'label' => t('Dashboard Title'), + 'placeholder' => t('Enter dashboard title'), 'description' => t('Enter a title for the new dashboard.'), ]); - } else { - $this->addElement('select', 'pane', [ - 'required' => true, - 'value' => reset($panes), - 'multiOptions' => $panes, - 'label' => t('Dashboard'), - 'description' => t('Select a dashboard you want to add the dashlet to.'), - ]); } $this->addHtml(new HtmlElement('hr')); @@ -143,6 +100,7 @@ class DashletForm extends CompatForm $this->addElement('textarea', 'url', [ 'required' => true, 'label' => t('Url'), + 'placeholder' => t('Enter dashlet url'), 'description' => t( 'Enter url to be loaded in the dashlet. You can paste the full URL, including filters.' ), @@ -151,40 +109,24 @@ class DashletForm extends CompatForm $this->addElement('text', 'dashlet', [ 'required' => true, 'label' => t('Dashlet Title'), + 'placeholder' => t('Enter a dashlet title'), 'description' => t('Enter a title for the dashlet.'), ]); - $url = (string) Url::fromPath(Dashboard::BASE_ROUTE . '/browse'); + $removeButton = null; + if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/edit-dashlet') { + $targetUrl = (clone $requestUrl)->setPath(Dashboard::BASE_ROUTE . '/remove-dashlet'); + $removeButton = $this->createRemoveButton($targetUrl, t('Remove Dashlet')); + } - $element = $this->createElement('submit', 'submit', ['label' => t('Add to Dashboard')]); - $this->registerElement($element)->decorate($element); - - // We might need this later to allow the user to browse dashlets when creating a dashlet - $this->addElement('submit', 'btn_browse', [ - 'label' => t('Browse Dashlets'), - 'href' => $url, - 'formaction' => $url, + $formControls = $this->createFormControls(); + $formControls->add([ + $this->registerSubmitButton(t('Add to Dashboard')), + $removeButton, + $this->createCancelButton() ]); - $this->getElement('btn_browse')->setWrapper($element->getWrapper()); - } - - /** - * Populate form data from config - * - * @param Dashlet $dashlet - */ - public function load(Dashlet $dashlet) - { - $home = Url::fromRequest()->getParam('home'); - $this->populate(array( - 'org_home' => $home, - 'org_pane' => $dashlet->getPane()->getName(), - 'pane' => $dashlet->getPane()->getTitle(), - 'org_dashlet' => $dashlet->getName(), - 'dashlet' => $dashlet->getTitle(), - 'url' => $dashlet->getUrl()->getRelativeUrl() - )); + $this->addHtml($formControls); } protected function onSuccess() @@ -192,27 +134,38 @@ class DashletForm extends CompatForm $conn = Dashboard::getConn(); $dashboard = $this->dashboard; + $selectedHome = $this->getPopulatedValue('home'); + if (! $selectedHome || $selectedHome === self::CREATE_NEW_HOME) { + $selectedHome = $this->getPopulatedValue('new_home'); + } + + $selectedPane = $this->getPopulatedValue('pane'); + // If "pane" element is disabled, there will be no populated value for it + if (! $selectedPane || $selectedPane === self::CREATE_NEW_PANE) { + $selectedPane = $this->getPopulatedValue('new_pane'); + } + if (Url::fromRequest()->getPath() === Dashboard::BASE_ROUTE . '/new-dashlet') { - $home = new DashboardHome($this->getValue('home')); - if ($dashboard->hasHome($home->getName())) { - $home = $dashboard->getHome($home->getName()); - if ($home->getName() !== $dashboard->getActiveHome()->getName()) { - $home->setActive(); - $home->loadPanesFromDB(); + $currentHome = new DashboardHome($selectedHome); + if ($dashboard->hasEntry($currentHome->getName())) { + $currentHome = clone $dashboard->getEntry($currentHome->getName()); + if ($currentHome->getName() !== $dashboard->getActiveHome()->getName()) { + $currentHome->setActive(); + $currentHome->loadDashboardEntries(); } } - $pane = new Pane($this->getValue('pane')); - if ($home->hasPane($pane->getName())) { - $pane = $home->getPane($pane->getName()); + $currentPane = new Pane($selectedPane); + if ($currentHome->hasEntry($currentPane->getName())) { + $currentPane = clone $currentHome->getEntry($currentPane->getName()); } - $dashlet = new Dashlet($this->getValue('dashlet'), $this->getValue('url'), $pane); - if ($pane->hasDashlet($dashlet->getName())) { + $dashlet = new Dashlet($this->getValue('dashlet'), $this->getValue('url'), $currentPane); + if ($currentPane->hasEntry($dashlet->getName())) { Notification::error(sprintf( t('Dashlet "%s" already exists within the "%s" dashboard pane'), $dashlet->getTitle(), - $pane->getTitle() + $currentPane->getTitle() )); return; @@ -221,12 +174,12 @@ class DashletForm extends CompatForm $conn->beginTransaction(); try { - $dashboard->manageHome($home); - $home->managePanes($pane); - $pane->manageDashlets($dashlet); + $dashboard->manageEntry($currentHome); + $currentHome->manageEntry($currentPane); + $currentPane->manageEntry($dashlet); $conn->commitTransaction(); - } catch (Exception $err) { // This error handling is just for debugging purpose! Will be removed! + } catch (Exception $err) { Logger::error($err); $conn->rollBackTransaction(); @@ -235,26 +188,35 @@ class DashletForm extends CompatForm Notification::success(sprintf(t('Created dashlet "%s" successfully'), $dashlet->getTitle())); } else { - $orgHome = $dashboard->getHome($this->getValue('org_home')); - $orgPane = $orgHome->getPane($this->getValue('org_pane')); - $orgDashlet = $orgPane->getDashlet($this->getValue('org_dashlet')); + $orgHome = $dashboard->getEntry($this->getValue('org_home')); + $orgPane = $orgHome->getEntry($this->getValue('org_pane')); + $orgDashlet = $orgPane->getEntry($this->getValue('org_dashlet')); - $currentHome = new DashboardHome($this->getValue('home')); - if ($dashboard->hasHome($currentHome->getName())) { - $currentHome = $dashboard->getHome($currentHome->getName()); + $currentHome = new DashboardHome($selectedHome); + if ($dashboard->hasEntry($currentHome->getName())) { + $currentHome = clone $dashboard->getEntry($currentHome->getName()); $activeHome = $dashboard->getActiveHome(); if ($currentHome->getName() !== $activeHome->getName()) { $currentHome->setActive(); - $currentHome->loadPanesFromDB(); + $currentHome->loadDashboardEntries(); } } - $currentPane = new Pane($this->getValue('pane')); - if ($currentHome->hasPane($currentPane->getName())) { - $currentPane = $currentHome->getPane($currentPane->getName()); + $currentPane = new Pane($selectedPane); + if ($currentHome->hasEntry($currentPane->getName())) { + $currentPane = clone $currentHome->getEntry($currentPane->getName()); } $currentPane->setHome($currentHome); + // When the user wishes to create a new dashboard pane, we have to explicitly reset the dashboard panes + // of the original home, so that it isn't considered as we want to move the pane even though it isn't + // supposed to when the original home contains a dashboard with the same name + // @see DashboardHome::managePanes() for details + $selectedPane = $this->getPopulatedValue('pane'); + if ((! $selectedPane || $selectedPane === self::CREATE_NEW_PANE) + && ! $currentHome->hasEntry($currentPane->getName())) { + $orgHome->setEntries([]); + } $currentDashlet = clone $orgDashlet; $currentDashlet @@ -263,7 +225,7 @@ class DashletForm extends CompatForm ->setTitle($this->getValue('dashlet')); if ($orgPane->getName() !== $currentPane->getName() - && $currentPane->hasDashlet($currentDashlet->getName())) { + && $currentPane->hasEntry($currentDashlet->getName())) { Notification::error(sprintf( t('Failed to move dashlet "%s": Dashlet already exists within the "%s" dashboard pane'), $currentDashlet->getTitle(), @@ -287,12 +249,18 @@ class DashletForm extends CompatForm return; } + if (empty($paneDiff)) { + // No dashboard diff means the dashlet is still in the same pane, so just + // reset the dashlets of the original pane + $orgPane->setEntries([]); + } + $conn->beginTransaction(); try { - $dashboard->manageHome($currentHome); - $currentHome->managePanes($currentPane, $orgHome); - $currentPane->manageDashlets($currentDashlet, $orgPane); + $dashboard->manageEntry($currentHome); + $currentHome->manageEntry($currentPane, $orgHome); + $currentPane->manageEntry($currentDashlet, $orgPane); $conn->commitTransaction(); } catch (Exception $err) { @@ -305,4 +273,21 @@ class DashletForm extends CompatForm Notification::success(sprintf(t('Updated dashlet "%s" successfully'), $currentDashlet->getTitle())); } } + + public function load(BaseDashboard $dashlet) + { + $home = Url::fromRequest()->getParam('home'); + /** @var Dashlet $dashlet */ + $this->populate(array( + 'org_home' => $home, + 'org_pane' => $dashlet->getPane()->getName(), + 'org_dashlet' => $dashlet->getName(), + 'dashlet' => $dashlet->getTitle(), + 'url' => $dashlet->getUrl()->getRelativeUrl() + )); + + if ($this->getPopulatedValue('pane') !== self::CREATE_NEW_PANE) { + $this->populate(['pane' => $dashlet->getPane()->getTitle()]); + } + } } diff --git a/application/forms/Dashboard/HomePaneForm.php b/application/forms/Dashboard/HomePaneForm.php index 84f1d5db3..088f737fb 100644 --- a/application/forms/Dashboard/HomePaneForm.php +++ b/application/forms/Dashboard/HomePaneForm.php @@ -5,81 +5,53 @@ namespace Icinga\Forms\Dashboard; use Icinga\Application\Logger; -use Icinga\Web\Navigation\DashboardHome; +use Icinga\Web\Dashboard\Common\BaseDashboard; +use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Notification; use Icinga\Web\Dashboard\Dashboard; -use Icinga\Web\Dashboard\Pane; -use ipl\Web\Compat\CompatForm; use ipl\Web\Url; -class HomePaneForm extends CompatForm +class HomePaneForm extends BaseDashboardForm { - /** @var Dashboard */ - protected $dashboard; - - public function __construct(Dashboard $dashboard) - { - $this->dashboard = $dashboard; - - // We need to set this explicitly needed for modals - $this->setAction((string) Url::fromRequest()); - } - - /** - * Populate form data from config - * - * @param DashboardHome|Pane $widget - */ - public function load($widget) - { - $title = $widget instanceof Pane ? $widget->getTitle() : $widget->getLabel(); - $this->populate([ - 'org_title' => $title, - 'title' => $title, - 'org_name' => $widget->getName() - ]); - } - protected function assemble() { + $titleDesc = t('Edit the title of this dashboard home'); + $buttonLabel = t('Update Home'); + $removeButtonLabel = t('Remove Home'); + + $requestUrl = Url::fromRequest(); + $removeTargetUrl = (clone $requestUrl)->setPath(Dashboard::BASE_ROUTE . '/remove-home'); + $this->addElement('hidden', 'org_name', ['required' => false]); $this->addElement('hidden', 'org_title', ['required' => false]); - $titleDesc = t('Edit the title of this dashboard home'); - $buttonLabel = t('Update Home'); - if (Url::fromRequest()->getPath() === Dashboard::BASE_ROUTE . '/edit-pane') { + if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/edit-pane') { $titleDesc = t('Edit the title of this dashboard pane'); $buttonLabel = t('Update Pane'); + $removeButtonLabel = t('Remove Pane'); - $homes = $this->dashboard->getHomeKeyTitleArr(); - $this->addElement('checkbox', 'create_new_home', [ - 'required' => false, - 'class' => 'autosubmit', - 'disabled' => empty($homes) ?: null, - 'label' => t('New Dashboard Home'), - 'description' => t('Check this box if you want to move the pane to a new dashboard home.'), - ]); + $removeTargetUrl = (clone $requestUrl)->setPath(Dashboard::BASE_ROUTE . '/remove-pane'); + $homes = $this->dashboard->getEntryKeyTitleArr(); $activeHome = $this->dashboard->getActiveHome(); $populatedHome = $this->getPopulatedValue('home', $activeHome->getName()); - if (empty($homes) || $this->getPopulatedValue('create_new_home') === 'y') { - $this->getElement('create_new_home')->addAttributes(['checked' => 'checked']); + $this->addElement('select', 'home', [ + 'class' => 'autosubmit', + 'required' => true, + 'value' => $populatedHome, + 'multiOptions' => array_merge([self::CREATE_NEW_HOME => self::CREATE_NEW_HOME], $homes), + 'label' => t('Assign to Home'), + 'description' => t('Select a dashboard home you want to move the dashboard to.'), + ]); - $this->addElement('text', 'home', [ + if (empty($homes) || $this->getPopulatedValue('home') === self::CREATE_NEW_HOME) { + $this->addElement('text', 'new_home', [ 'required' => true, 'label' => t('Dashboard Home'), + 'placeholder' => t('Enter dashboard home title'), 'description' => t('Enter a title for the new dashboard home.'), ]); - } else { - $this->addElement('select', 'home', [ - 'required' => true, - 'class' => 'autosubmit', - 'value' => $populatedHome, - 'multiOptions' => $homes, - 'label' => t('Move to Home'), - 'description' => t('Select a dashboard home you want to move the dashboard to.'), - ]); } } @@ -89,46 +61,64 @@ class HomePaneForm extends CompatForm 'description' => $titleDesc ]); - $this->addElement('submit', 'btn_update', ['label' => $buttonLabel]); + $formControls = $this->createFormControls(); + $formControls->add([ + $this->registerSubmitButton($buttonLabel), + $this->createRemoveButton($removeTargetUrl, $removeButtonLabel), + $this->createCancelButton() + ]); + + $this->addHtml($formControls); } protected function onSuccess() { $requestUrl = Url::fromRequest(); if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/edit-pane') { - $orgHome = $this->dashboard->getHome($requestUrl->getParam('home')); + $orgHome = $this->dashboard->getEntry($requestUrl->getParam('home')); - $currentHome = new DashboardHome($this->getValue('home')); - if ($this->dashboard->hasHome($currentHome->getName())) { - $currentHome = $this->dashboard->getHome($currentHome->getName()); + $selectedHome = $this->getPopulatedValue('home'); + if (! $selectedHome || $selectedHome === self::CREATE_NEW_HOME) { + $selectedHome = $this->getPopulatedValue('new_home'); + } + + $currentHome = new DashboardHome($selectedHome); + if ($this->dashboard->hasEntry($currentHome->getName())) { + /** @var DashboardHome $currentHome */ + $currentHome = clone $this->dashboard->getEntry($currentHome->getName()); $activeHome = $this->dashboard->getActiveHome(); if ($currentHome->getName() !== $activeHome->getName()) { - $currentHome->setActive(); - $currentHome->loadPanesFromDB(); + $currentHome->setActive()->loadDashboardEntries(); } } - $currentPane = $orgHome->getPane($this->getValue('org_name')); + $currentPane = clone $orgHome->getEntry($this->getValue('org_name')); $currentPane ->setHome($currentHome) ->setTitle($this->getValue('title')); - if ($orgHome->getName() !== $currentHome->getName() && $currentHome->hasPane($currentPane->getName())) { + 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->getLabel() + $currentHome->getTitle() )); return; } + if ($currentHome->getName() === $orgHome->getName()) { + // There is no dashboard home diff so clear all the dashboard pane + // of the org home + $orgHome->setEntries([]); + } + $conn = Dashboard::getConn(); $conn->beginTransaction(); try { - $this->dashboard->manageHome($currentHome); - $currentHome->managePanes($currentPane, $orgHome); + $this->dashboard->manageEntry($currentHome); + $currentHome->manageEntry($currentPane, $orgHome); $conn->commitTransaction(); } catch (\Exception $err) { @@ -139,10 +129,19 @@ class HomePaneForm extends CompatForm Notification::success(sprintf(t('Updated dashboard pane "%s" successfully'), $currentPane->getTitle())); } else { $home = $this->dashboard->getActiveHome(); - $home->setLabel($this->getValue('title')); + $home->setTitle($this->getValue('title')); - $this->dashboard->manageHome($home); - Notification::success(sprintf(t('Updated dashboard home "%s" successfully'), $home->getLabel())); + $this->dashboard->manageEntry($home); + Notification::success(sprintf(t('Updated dashboard home "%s" successfully'), $home->getTitle())); } } + + public function load(BaseDashboard $dashboard) + { + $this->populate([ + 'org_title' => $dashboard->getTitle(), + 'title' => $dashboard->getTitle(), + 'org_name' => $dashboard->getName() + ]); + } } diff --git a/application/forms/Dashboard/NewHomePaneForm.php b/application/forms/Dashboard/NewHomePaneForm.php index 5066fdd9f..7be3834c6 100644 --- a/application/forms/Dashboard/NewHomePaneForm.php +++ b/application/forms/Dashboard/NewHomePaneForm.php @@ -3,80 +3,92 @@ namespace Icinga\Forms\Dashboard; use Icinga\Web\Dashboard\Dashboard; +use Icinga\Web\Dashboard\DashboardHome; use Icinga\Web\Dashboard\Pane; use Icinga\Web\Notification; -use ipl\Web\Compat\CompatForm; use ipl\Web\Url; -class NewHomePaneForm extends CompatForm +class NewHomePaneForm extends BaseDashboardForm { - /** @var Dashboard */ - protected $dashboard; - public function __construct(Dashboard $dashboard) { - $this->dashboard = $dashboard; + parent::__construct($dashboard); $requestUrl = Url::fromRequest(); - - // We need to set this explicitly needed for modals - $this->setAction((string) $requestUrl); - if ($requestUrl->hasParam('home')) { $this->populate(['home' => $requestUrl->getParam('home')]); } } - public function hasBeenSubmitted() - { - return $this->hasBeenSent() - && ($this->getPopulatedValue('btn_cancel') - || $this->getPopulatedValue('submit')); - } - protected function assemble() { $populatedHome = Url::fromRequest()->getParam('home'); $this->addElement('text', 'pane', [ 'required' => true, 'label' => t('Title'), + 'placeholder' => t('Create new Dashboard'), 'description' => t('Add new dashboard to this home.') ]); + $homes = array_merge([self::CREATE_NEW_HOME => self::CREATE_NEW_HOME], $this->dashboard->getEntryKeyTitleArr()); $this->addElement('select', 'home', [ 'required' => true, 'class' => 'autosubmit', 'value' => $populatedHome, - 'multiOptions' => $this->dashboard->getHomeKeyTitleArr(), + 'multiOptions' => $homes, 'label' => t('Assign to Home'), 'description' => t('A dashboard home you want to assign the new dashboard to.'), ]); - $submitButton = $this->createElement('submit', 'submit', [ - 'class' => 'autosubmit', - 'label' => t('Add Dashboard'), - ]); - $this->registerElement($submitButton)->decorate($submitButton); + if ($this->getPopulatedValue('home') === self::CREATE_NEW_HOME) { + $this->addElement('text', 'new_home', [ + 'required' => true, + 'label' => t('Dashboard Home'), + 'placeholder' => t('Enter dashboard home title'), + 'description' => t('Enter a title for the new dashboard home.'), + ]); + } - $this->addElement('submit', 'btn_cancel', ['label' => t('Cancel')]); - $this->getElement('btn_cancel')->setWrapper($submitButton->getWrapper()); + $submitButton = $this->createElement('submit', 'submit', [ + 'class' => 'btn-primary', + 'label' => t('Add Dashboard') + ]); + $this->registerElement($submitButton); + + $formControls = $this->createFormControls(); + $formControls->add([ + $this->registerSubmitButton(t('Add Dashboard')), + $this->createCancelButton() + ]); + + $this->addHtml($formControls); } protected function onSuccess() { $requestUrl = Url::fromRequest(); - $dashboard = $this->dashboard; $conn = Dashboard::getConn(); - if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/new-pane') { - $home = $this->getPopulatedValue('home'); - $home = $dashboard->getHome($home); + $selectedHome = $this->getPopulatedValue('home'); + if (! $selectedHome || $selectedHome === self::CREATE_NEW_HOME) { + $selectedHome = $this->getPopulatedValue('new_home'); + } + if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/new-pane') { + $currentHome = new DashboardHome($selectedHome); + if ($this->dashboard->hasEntry($currentHome->getName())) { + $currentHome = clone $this->dashboard->getEntry($currentHome->getName()); + if ($currentHome->getName() !== $this->dashboard->getActiveHome()->getName()) { + $currentHome->setActive()->loadDashboardEntries(); + } + } + + $pane = new Pane($this->getPopulatedValue('pane')); $conn->beginTransaction(); try { - $pane = new Pane($this->getPopulatedValue('pane')); - $home->managePanes($pane); + $this->dashboard->manageEntry($currentHome); + $currentHome->manageEntry($pane); $conn->commitTransaction(); } catch (\Exception $err) { @@ -85,8 +97,6 @@ class NewHomePaneForm extends CompatForm } Notification::success('Added dashboard successfully'); - } else { - } } } diff --git a/application/forms/Dashboard/RemoveDashletForm.php b/application/forms/Dashboard/RemoveDashletForm.php index a5dbd2b25..3aad58716 100644 --- a/application/forms/Dashboard/RemoveDashletForm.php +++ b/application/forms/Dashboard/RemoveDashletForm.php @@ -5,21 +5,14 @@ namespace Icinga\Forms\Dashboard; use Icinga\Web\Notification; -use Icinga\Web\Dashboard\Dashboard; use ipl\Html\HtmlElement; -use ipl\Web\Compat\CompatForm; use ipl\Web\Url; -class RemoveDashletForm extends CompatForm +class RemoveDashletForm extends BaseDashboardForm { - /** @var Dashboard */ - protected $dashboard; - - public function __construct(Dashboard $dashboard) + public function hasBeenSubmitted() { - $this->dashboard = $dashboard; - - $this->setAction((string) Url::fromRequest()); + return $this->hasBeenSent() && $this->getPopulatedValue('btn_remove'); } protected function assemble() @@ -29,17 +22,20 @@ class RemoveDashletForm extends CompatForm Url::fromRequest()->getParam('dashlet') ))); - $this->addElement('submit', 'remove_dashlet', ['label' => t('Remove Dashlet')]); + $submit = $this->registerSubmitButton(t('Remove Dashlet')); + $submit->setName('btn_remove'); + + $this->addHtml($submit); } protected function onSuccess() { $requestUrl = Url::fromRequest(); $home = $this->dashboard->getActiveHome(); - $pane = $home->getPane($requestUrl->getParam('pane')); + $pane = $home->getEntry($requestUrl->getParam('pane')); $dashlet = $requestUrl->getParam('dashlet'); - $pane->removeDashlet($dashlet); + $pane->removeEntry($dashlet); Notification::success(sprintf(t('Removed dashlet "%s" successfully'), $dashlet)); } diff --git a/application/forms/Dashboard/RemoveHomePaneForm.php b/application/forms/Dashboard/RemoveHomePaneForm.php index c29818f1c..8116da81b 100644 --- a/application/forms/Dashboard/RemoveHomePaneForm.php +++ b/application/forms/Dashboard/RemoveHomePaneForm.php @@ -6,24 +6,32 @@ namespace Icinga\Forms\Dashboard; use Icinga\Web\Notification; use Icinga\Web\Dashboard\Dashboard; -use ipl\Web\Compat\CompatForm; +use ipl\Html\HtmlElement; use ipl\Web\Url; -class RemoveHomePaneForm extends CompatForm +class RemoveHomePaneForm extends BaseDashboardForm { - /** @var Dashboard */ - protected $dashboard; - - public function __construct(Dashboard $dashboard) + public function hasBeenSubmitted() { - $this->dashboard = $dashboard; - - $this->setAction((string) Url::fromRequest()); + return $this->hasBeenSent() && $this->getPopulatedValue('btn_remove'); } protected function assemble() { - $this->addElement('submit', 'btn_remove', ['label' => t('Remove Home')]); + $requestRoute = Url::fromRequest(); + $label = t('Remove Home'); + $message = sprintf(t('Please confirm removal of dashboard home "%s"'), $requestRoute->getParam('home')); + if ($requestRoute->getPath() === Dashboard::BASE_ROUTE . '/remove-pane') { + $label = t('Remove Pane'); + $message = sprintf(t('Please confirm removal of dashboard pane "%s"'), $requestRoute->getParam('pane')); + } + + $this->addHtml(HtmlElement::create('h1', null, $message)); + + $submit = $this->registerSubmitButton($label); + $submit->setName('btn_remove'); + + $this->addHtml($submit); } protected function onSuccess() @@ -32,12 +40,12 @@ class RemoveHomePaneForm extends CompatForm $home = $this->dashboard->getActiveHome(); if ($requestUrl->getPath() === Dashboard::BASE_ROUTE . '/remove-home') { - $this->dashboard->removeHome($home); + $this->dashboard->removeEntry($home); - Notification::success(sprintf(t('Removed dashboard home "%s" successfully'), $home->getLabel())); + Notification::success(sprintf(t('Removed dashboard home "%s" successfully'), $home->getTitle())); } else { - $pane = $home->getPane($requestUrl->getParam('pane')); - $home->removePane($pane); + $pane = $home->getEntry($requestUrl->getParam('pane')); + $home->removeEntry($pane); Notification::success(sprintf(t('Removed dashboard pane "%s" successfully'), $pane->getTitle())); } diff --git a/application/forms/Dashboard/WelcomeForm.php b/application/forms/Dashboard/WelcomeForm.php index 41673d823..d0278dec6 100644 --- a/application/forms/Dashboard/WelcomeForm.php +++ b/application/forms/Dashboard/WelcomeForm.php @@ -5,7 +5,7 @@ namespace Icinga\Forms\Dashboard; use Icinga\Web\Dashboard\Dashboard; -use Icinga\Web\Navigation\DashboardHome; +use Icinga\Web\Dashboard\DashboardHome; use ipl\Web\Compat\CompatForm; use ipl\Web\Url; @@ -43,12 +43,12 @@ class WelcomeForm extends CompatForm protected function onSuccess() { if ($this->getPopulatedValue('btn_use_defaults')) { - $home = $this->dashboard->getHome(DashboardHome::DEFAULT_HOME); + $home = $this->dashboard->getEntry(DashboardHome::DEFAULT_HOME); $conn = Dashboard::getConn(); $conn->beginTransaction(); try { - $home->managePanes($this->dashboard->getSystemDefaults(), null, true); + $home->manageEntry($this->dashboard->getSystemDefaults(), null, true); $conn->commitTransaction(); } catch (\Exception $err) { diff --git a/application/views/scripts/layout/menu.phtml b/application/views/scripts/layout/menu.phtml index fd7ba20f7..1fbc24f49 100644 --- a/application/views/scripts/layout/menu.phtml +++ b/application/views/scripts/layout/menu.phtml @@ -6,7 +6,7 @@ use Icinga\Web\Widget\SearchDashboard; $searchDashboard = new SearchDashboard(); $searchDashboard->setUser($this->Auth()->getUser()); -if ($searchDashboard->search('dummy')->getActiveHome()->getPane('search')->hasDashlets()): ?> +if (! $searchDashboard->search('dummy')->getActiveHome()->getEntry('search')->hasEntries()): ?>