Skip to content

Commit

Permalink
Decouple menu and dashboard code
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Apr 20, 2022
1 parent 8df2c9e commit 63802df
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 120 deletions.
52 changes: 45 additions & 7 deletions application/controllers/DashboardsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public function init()
$this->dashboard = new Dashboard();
$this->dashboard->setUser($this->Auth()->getUser());
$this->dashboard->setTabs($this->getTabs());
$this->dashboard->load();
}

public function indexAction()
{
$this->dashboard->load(DashboardHome::DEFAULT_HOME);

$this->createTabs();

$activeHome = $this->dashboard->getActiveHome();
Expand Down Expand Up @@ -73,10 +74,7 @@ public function indexAction()
*/
public function homeAction()
{
$home = $this->params->getRequired('home');
if (! $this->dashboard->hasEntry($home)) {
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}
$this->dashboard->load($this->params->getRequired('home'));

$activeHome = $this->dashboard->getActiveHome();
if (! $activeHome->getEntries()) {
Expand All @@ -101,6 +99,7 @@ public function homeAction()
public function newHomeAction()
{
$this->setTitle(t('Add new Dashboard Home'));
$this->dashboard->load();

$paneForm = (new NewHomePaneForm($this->dashboard))
->on(NewHomePaneForm::ON_SUCCESS, function () {
Expand All @@ -114,12 +113,17 @@ public function newHomeAction()
public function editHomeAction()
{
$this->setTitle(t('Update Home'));
$this->dashboard->load();

$home = $this->params->getRequired('home');
if (! $this->dashboard->hasEntry($home)) {
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

$homeForm = (new HomePaneForm($this->dashboard))
->on(HomePaneForm::ON_SUCCESS, function () {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
Expand All @@ -133,12 +137,17 @@ public function editHomeAction()
public function removeHomeAction()
{
$this->setTitle(t('Remove Home'));
$this->dashboard->load();

$home = $this->params->getRequired('home');
if (! $this->dashboard->hasEntry($home)) {
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

$homeForm = (new RemoveHomePaneForm($this->dashboard))
->on(RemoveHomePaneForm::ON_SUCCESS, function () {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
Expand All @@ -151,12 +160,17 @@ public function removeHomeAction()
public function newPaneAction()
{
$this->setTitle(t('Add new Dashboard'));
$this->dashboard->load();

$home = $this->params->getRequired('home');
if (! $this->dashboard->hasEntry($home)) {
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

$paneForm = (new NewHomePaneForm($this->dashboard))
->on(NewHomePaneForm::ON_SUCCESS, function () {
$this->redirectNow(Url::fromPath(Dashboard::BASE_ROUTE . '/settings'));
Expand All @@ -169,6 +183,7 @@ public function newPaneAction()
public function editPaneAction()
{
$this->setTitle(t('Update Pane'));
$this->dashboard->load();

$pane = $this->params->getRequired('pane');
$home = $this->params->getRequired('home');
Expand All @@ -177,6 +192,10 @@ public function editPaneAction()
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

if (! $this->dashboard->getActiveHome()->hasEntry($pane)) {
$this->httpNotFound(sprintf(t('Pane "%s" not found'), $pane));
}
Expand All @@ -195,6 +214,7 @@ public function editPaneAction()
public function removePaneAction()
{
$this->setTitle(t('Remove Pane'));
$this->dashboard->load();

$home = $this->params->getRequired('home');
$paneParam = $this->params->getRequired('pane');
Expand All @@ -203,6 +223,10 @@ public function removePaneAction()
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

if (! $this->dashboard->getActiveHome()->hasEntry($paneParam)) {
$this->httpNotFound(sprintf(t('Pane "%s" not found'), $paneParam));
}
Expand All @@ -224,6 +248,8 @@ public function newDashletAction()
$this->setTitle(t('Select Dashlets'));
}

$this->dashboard->load();

$dashletForm = new DashletForm($this->dashboard);
$dashletForm->populate($this->getRequest()->getPost());
$dashletForm->on(DashletForm::ON_SUCCESS, function () {
Expand All @@ -236,6 +262,7 @@ public function newDashletAction()
public function editDashletAction()
{
$this->setTitle(t('Edit Dashlet'));
$this->dashboard->load();

$pane = $this->validateDashletParams();
$dashlet = $pane->getEntry($this->getParam('dashlet'));
Expand All @@ -255,6 +282,8 @@ public function editDashletAction()
public function removeDashletAction()
{
$this->setTitle(t('Remove Dashlet'));
$this->dashboard->load();

$this->validateDashletParams();

$removeForm = (new RemoveDashletForm($this->dashboard))
Expand All @@ -281,6 +310,8 @@ public function reorderWidgetsAction()
$originals = $dashboards['originals'];
unset($dashboards['originals']);

$this->dashboard->load();

$orgHome = null;
$orgPane = null;
if ($originals && isset($originals['originalHome'])) {
Expand Down Expand Up @@ -402,6 +433,8 @@ public function setupDashboardAction()
$this->setTitle(t('Add Dashlet'));
}

$this->dashboard->load();

$setupForm = new SetupNewDashboardForm($this->dashboard);
$setupForm->on(SetupNewDashboardForm::ON_SUCCESS, function () use ($setupForm) {
$this->redirectNow($setupForm->getRedirectUrl());
Expand All @@ -412,7 +445,9 @@ public function setupDashboardAction()

public function settingsAction()
{
$this->dashboard->load();
$this->createTabs();

$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)) {
Expand Down Expand Up @@ -445,8 +480,7 @@ private function createTabs()
{
$tabs = $this->dashboard->getTabs();
$activeHome = $this->dashboard->getActiveHome();
if (($activeHome && $activeHome->hasEntries()) ||
($activeHome && ! $activeHome->isDisabled() && count($this->dashboard->getEntries()) > 1)) {
if ($activeHome && ($activeHome->getName() !== DashboardHome::DEFAULT_HOME || $activeHome->hasEntries())) {
$tabs->extend(new DashboardSettings());
}

Expand All @@ -463,6 +497,10 @@ private function validateDashletParams()
$this->httpNotFound(sprintf(t('Home "%s" not found'), $home));
}

// TODO: Shouldn't be necessary. load() should get the name already and
// the form should otherwise ensure it has the required data
$this->dashboard->activateHome($this->dashboard->getEntry($home));

if (! $this->dashboard->getActiveHome()->hasEntry($pane)) {
$this->httpNotFound(sprintf(t('Pane "%s" not found'), $pane));
}
Expand Down
68 changes: 40 additions & 28 deletions library/Icinga/Web/Dashboard/Common/DashboardManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,19 @@
namespace Icinga\Web\Dashboard\Common;

use Icinga\Application\Icinga;
use Icinga\Application\Modules\DashletContainer;
use Icinga\Authentication\Auth;
use Icinga\Common\Database;
use Icinga\Exception\Http\HttpNotFoundException;
use Icinga\Exception\ProgrammingError;
use Icinga\Model;
use Icinga\User;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
use Icinga\Web\Dashboard\Dashlet;
use Icinga\Web\Dashboard\Pane;
use Icinga\Web\Menu;
use Icinga\Web\Navigation\DashboardPane;
use ipl\Orm\Query;
use ipl\Sql\Connection;
use ipl\Sql\Expression;
use ipl\Stdlib\Filter;
use ipl\Web\Url;

trait DashboardManager
{
Expand All @@ -40,12 +36,46 @@ trait DashboardManager
*/
private static $defaultPanes = [];

public function load()
/**
* Load the given or all homes (null)
*
* @param ?string $name
*
* @return void
*/
public function load(string $name = null)
{
$this->setEntries((new Menu())->loadHomes());
$this->loadDashboardEntries();
$query = Model\Home::on(self::getConn());
$query->filter(Filter::equal('username', $this::getUser()->getUsername()));

if ($name !== null) {
$query->filter(Filter::equal('name', $name));

/** @var Model\Home $row */
if (($row = $query->first()) === null) {
if ($name === DashboardHome::DEFAULT_HOME) {
$home = $this->initGetDefaultHome();
} else {
throw new HttpNotFoundException(t('Home "%s" not found'), $name);
}
} else {
$home = DashboardHome::create($row);
$this->addEntry($home);
}

$this->activateHome($home);
$home->loadDashboardEntries();
} else {
foreach ($query as $row) {
$this->addEntry(DashboardHome::create($row));
}

if (($firstHome = $this->rewindEntries())) {
$this->activateHome($firstHome);
$firstHome->loadDashboardEntries();
}
}

$this->initGetDefaultHome();
self::deployModuleDashlets();
}

Expand Down Expand Up @@ -81,25 +111,7 @@ public static function getSHA1(string $name): string

public function loadDashboardEntries(string $name = '')
{
if ($name && $this->hasEntry($name)) {
$home = $this->getEntry($name);
} else {
$requestRoute = Url::fromRequest();
if ($requestRoute->getPath() === Dashboard::BASE_ROUTE) {
$home = $this->initGetDefaultHome();
} else {
$homeParam = $requestRoute->getParam('home');
if (empty($homeParam) || ! $this->hasEntry($homeParam)) {
if (! ($home = $this->rewindEntries())) {
// No dashboard homes
return $this;
}
} else {
$home = $this->getEntry($homeParam);
}
}
}

$home = $this->getEntry($name);
$this->activateHome($home);
$home->loadDashboardEntries();

Expand Down
20 changes: 10 additions & 10 deletions library/Icinga/Web/Dashboard/DashboardHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
namespace Icinga\Web\Dashboard;

use Icinga\Exception\ProgrammingError;
use Icinga\Model\Home;
use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Web\Dashboard\Common\DashboardControls;
use Icinga\Web\Dashboard\Common\Sortable;
use Icinga\Web\Navigation\DashboardHomeItem;
use ipl\Stdlib\Filter;

use function ipl\Stdlib\get_php_type;
Expand Down Expand Up @@ -53,21 +53,21 @@ class DashboardHome extends BaseDashboard implements Sortable
protected $disabled = false;

/**
* Create a new dashboard home from the given home item
* Create a new dashboard home from the given model
*
* @param DashboardHomeItem $homeItem
* @param Home $home
*
* @return DashboardHome
*/
public static function create(DashboardHomeItem $homeItem): self
public static function create(Home $home): self
{
$self = new self($homeItem->getName());
$self = new self($home->name);
$self
->setTitle($homeItem->getLabel())
->setPriority($homeItem->getPriority())
->setType($homeItem->getAttribute('type'))
->setUuid($homeItem->getAttribute('uuid'))
->setDisabled($homeItem->getAttribute('disabled'));
->setTitle($home->label)
->setPriority($home->priority)
->setType($home->type)
->setUuid($home->id)
->setDisabled((bool) $home->disabled);

return $self;
}
Expand Down
Loading

0 comments on commit 63802df

Please sign in to comment.