Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Aug 11, 2021
1 parent fec29e2 commit 00f702e
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 47 deletions.
10 changes: 6 additions & 4 deletions application/controllers/DashboardsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ public function homeAction()

$activeHome = $this->dashboard->getActiveHome();

if ($home === Dashboard::AVAILABLE_DASHLETS || $home === Dashboard::SHARED_DASHBOARDS) {
if ($home === DashboardHome::AVAILABLE_DASHLETS || $home === DashboardHome::SHARED_DASHBOARDS) {
$this->getTabs()->add($home, [
'active' => true,
'label' => $home,
'url' => Url::fromRequest()
]);

if ($home === Dashboard::AVAILABLE_DASHLETS) {
if ($home === DashboardHome::AVAILABLE_DASHLETS) {
$moduleManager = Icinga::app()->getModuleManager();
$dashlets = [];

Expand Down Expand Up @@ -220,7 +220,9 @@ public function newPaneAction()

$this->addContent($paneForm);
} else {
$this->redirectNow(Url::fromPath('dashboard/home')->addParams(['home' => Dashboard::AVAILABLE_DASHLETS]));
$this->redirectNow(Url::fromPath('dashboard/home')->addParams([
'home' => DashboardHome::AVAILABLE_DASHLETS
]));
}
}

Expand Down Expand Up @@ -396,7 +398,7 @@ private function createTabs()
$homeParam = $this->getParam('home');
if ($homeParam && (
// Only dashlets provided by modules can be listed in this home
$homeParam !== Dashboard::AVAILABLE_DASHLETS
$homeParam !== DashboardHome::AVAILABLE_DASHLETS
&& $this->dashboard->hasHome($homeParam))
) {
$home = $this->dashboard->getHome($homeParam);
Expand Down
29 changes: 21 additions & 8 deletions application/forms/Dashboard/DashletForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ public function updateDashlet()
$createNewPane = true;

if ($activeHome && $activeHome->hasPane($paneName)) {
$newPane = $activeHome->getPane($paneName);
$paneId = $newPane->getPaneId();
$paneId = $activeHome->getPane($paneName)->getPaneId();

$createNewPane = false;
}
Expand All @@ -456,6 +455,10 @@ public function updateDashlet()
$username . $homeName . $orgPane->getName() . $orgDashlet->getName()
);

if (! $orgPane->isUserWidget()) {
$paneId = DashboardHome::getSHA1($username . $homeName . $orgPane->getName());
}

$db->insert(Dashlet::OVERRIDING_TABLE, [
'dashlet_id' => $dashletId,
'dashboard_id' => $paneId,
Expand All @@ -465,15 +468,22 @@ public function updateDashlet()
'disabled' => (int) $dashletDisabled
]);
} elseif ($orgDashlet->isOverridingWidget()) { // Custom dashelts that overwrites system dashlets
$newPaneId = DashboardHome::getSHA1($username . $homeName . $orgPane->getName());
if ($orgPane->getName() !== $paneName) {
Notification::error(sprintf(
t('Dashlet "%s" can\'t be moved, as it overwrites a system dashlet'),
$orgDashlet->getName()
));

return;
}

$db->update(Dashlet::OVERRIDING_TABLE, [
'label' => $dashletLabel,
'url' => $dashletUrl,
'disabled' => (int) $dashletDisabled
], [
'dashlet_id = ?' => $orgDashlet->getDashletId(),
'dashboard_id = ?' => $newPaneId
'dashboard_id = ?' => $paneId
]);
} else { // Custom
if ($createNewHome || ($homeName !== DashboardHome::DEFAULT_HOME &&
Expand Down Expand Up @@ -549,11 +559,14 @@ public function onSuccess()
$dashlet = Url::fromRequest()->getParam('dashlet');
$pane = $activeHome->getPane(Url::fromRequest()->getParam('pane'));

$dashletId = DashboardHome::getSHA1(
$activeHome->getUser()->getUsername() . $activeHome->getName() . $pane->getName() . $dashlet
);
if (! $pane->getDashlet($dashlet)->isUserWidget()) {
$dashletId = DashboardHome::getSHA1(
$activeHome->getUser()->getUsername() . $activeHome->getName() . $pane->getName() . $dashlet
);

$pane->getDashlet($dashlet)->setDashletId($dashletId);
}

$pane->getDashlet($dashlet)->setDashletId($dashletId);
$pane->removeDashlet($dashlet);

Notification::success(t('Dashlet has been removed from.') . ' ' . $pane->getTitle());
Expand Down
9 changes: 9 additions & 0 deletions application/forms/Dashboard/HomePaneForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,15 @@ public function onSuccess()
return;
}

if ($pane->isOverridingWidget() && $orgHome->getName() !== $newHome) {
Notification::error(sprintf(
t('Pane "%s" can\'t be moved, as it overwrites a system pane'),
$pane->getName()
));

return;
}

$createNewHome = true;
if ($this->dashboard->hasHome($newHome)) {
$home = $this->dashboard->getHome($newHome);
Expand Down
13 changes: 7 additions & 6 deletions library/Icinga/Web/Dashboard/Pane.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ class Pane
/**
* Unique identifier of this pane
*
* @var integer
* @var string
*/
private $paneId;

/**
* A flag whether the user has created a dashlet in a system pane
* A type of this pane
*
* @var string
*/
Expand Down Expand Up @@ -168,7 +168,7 @@ public function getHome()
/**
* Set unique identifier of this pane
*
* @param integer $id
* @param string $id
*/
public function setPaneId($id)
{
Expand All @@ -180,7 +180,7 @@ public function setPaneId($id)
/**
* Get the unique identifier of this pane
*
* @return integer
* @return string
*/
public function getPaneId()
{
Expand Down Expand Up @@ -314,6 +314,7 @@ public function getDashlet($title)
if ($this->hasDashlet($title)) {
return $this->dashlets[$title];
}

throw new ProgrammingError(
'Trying to access invalid dashlet: %s',
$title
Expand Down Expand Up @@ -355,8 +356,8 @@ public function removeDashlet($dashlet)
]);
}
} elseif (! $dashlet->getDisabled() && ! $this->getDisabled()) {
// When modifying system dashlets, we need also to change the id accordingly,
// so that we won't have id mismatch in DashboardHome when it is loading.
// When modifying system dashlets, we need also to change the pane id accordingly,
// so that we won't have id mismatch in DashboardHome class when it is loading.
$paneId = DashboardHome::getSHA1(
$owner . $this->getHome()->getName() . $this->getName()
);
Expand Down
44 changes: 32 additions & 12 deletions library/Icinga/Web/Navigation/DashboardHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ class DashboardHome extends NavigationItem
*/
const DEFAULT_HOME = 'Default Home';

/**
* A Home where all collected dashlets provided by modules are
*
* being presented in a special view
*
* @var string
*/
const AVAILABLE_DASHLETS = 'Available Dashlets';

/**
* A Home where all dashboards shared with this user are
*
* displayed in a dedicated view
*
* @var string
*/
const SHARED_DASHBOARDS = 'Shared Dashboards';

/**
* A default user of some system homes
*
Expand Down Expand Up @@ -352,8 +370,8 @@ public function loadUserDashboards()
->joinLeft('dashboard_order do', 'd.id = do.dashboard_id')
->where(['dh.id = ?' => $this->getIdentifier()])
->where([
'd.owner = ?' => $this->user->getUsername(),
'dh.owner = ?' => $this->user->getUsername()
'd.owner = ?' => $this->getUser()->getUsername(),
'dh.owner = ?' => $this->getUser()->getUsername()
], 'OR')
->orderBy('do.priority', 'DESC'));

Expand All @@ -376,8 +394,8 @@ public function loadUserDashboards()
->joinLeft('dashlet_order do', 'ds.id = do.dashlet_id')
->where(['d.id = ?' => $pane->getPaneId()])
->where([
'ds.owner = ?' => $this->user->getUsername(),
'd.owner = ?' => $this->user->getUsername()
'ds.owner = ?' => $this->getUser()->getUsername(),
'd.owner = ?' => $this->getUser()->getUsername()
], 'OR')
->orderBy('do.priority', 'DESC'));

Expand Down Expand Up @@ -520,12 +538,12 @@ private function modifyPaneProperties(Pane $pane)
{
// Check whether the pane is a system or cloned pane
if ($pane->getOwner() === self::DEFAULT_IW2_USER || $pane->getType() === Pane::SYSTEM) {
$paneId = self::getSHA1($this->user->getUsername() . $this->getName() . $pane->getName());
$paneId = self::getSHA1($this->getUser()->getUsername() . $this->getName() . $pane->getName());
$overridingPane = $this->getDb()->select((new Select())
->columns('*')
->from(Pane::OVERRIDING_TABLE)
->where([
'owner = ?' => $this->user->getUsername(),
'owner = ?' => $this->getUser()->getUsername(),
'dashboard_id = ?' => $paneId
]))->fetch();

Expand All @@ -534,7 +552,7 @@ private function modifyPaneProperties(Pane $pane)
if ((! $overridingPane->label || $overridingPane->label === $pane->getTitle()) &&
! (bool) $overridingPane->disabled) {
$this->getDb()->delete(Pane::OVERRIDING_TABLE, [
'owner = ?' => $this->user->getUsername(),
'owner = ?' => $this->getUser()->getUsername(),
'dashboard_id = ?' => $paneId
]);
} else {
Expand All @@ -559,13 +577,13 @@ private function modifyPaneProperties(Pane $pane)
// Since the system dashlet ids are being modified when writing them into the
// DB, we have to regenerate them here as well.
$dashletId = self::getSHA1(
$this->user->getUsername() . $this->getName() . $pane->getName() . $dashlet->getName()
$this->getUser()->getUsername() . $this->getName() . $pane->getName() . $dashlet->getName()
);

$paneId = $pane->getPaneId();
if ($pane->getOwner() === self::DEFAULT_IW2_USER) {
$paneId = self::getSHA1(
$this->user->getUsername() . $this->getName() . $pane->getName()
$this->getUser()->getUsername() . $this->getName() . $pane->getName()
);
}

Expand All @@ -585,9 +603,11 @@ private function modifyPaneProperties(Pane $pane)
! (bool) $overridingDashlet->disabled) {
$this->getDb()->delete(Dashlet::OVERRIDING_TABLE, [
'dashlet_id = ?' => $dashletId,
'owner = ?' => $this->user->getUsername()
'owner = ?' => $this->getUser()->getUsername()
]);
} else {
$pane->setPaneId($paneId);

$dashlet->setUserWidget();
$dashlet->setOverride(true);
$dashlet
Expand Down Expand Up @@ -679,13 +699,13 @@ public function removePane($pane)

$this->getDb()->delete($table, [$condition => $pane->getPaneId(), 'owner = ?' => $pane->getOwner()]);
} elseif (! $pane->getDisabled() && ! $this->getDisabled()) {
$paneId = self::getSHA1($this->user->getUsername() . $this->getName() . $pane->getName());
$paneId = self::getSHA1($this->getUser()->getUsername() . $this->getName() . $pane->getName());

// User is going to disable a system pane
$this->getDb()->insert(Pane::OVERRIDING_TABLE, [
'dashboard_id' => $paneId,
'home_id' => $this->getIdentifier(),
'owner' => $this->user->getUsername(),
'owner' => $this->getUser()->getUsername(),
'disabled' => true
]);
}
Expand Down
28 changes: 11 additions & 17 deletions library/Icinga/Web/Widget/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,6 @@
*/
class Dashboard extends BaseHtmlElement
{
/**
* Preserve key name for coming features
*
* @var string
*/
const AVAILABLE_DASHLETS = 'Available Dashlets';

/**
* Preserve key name for coming features
*
* @var string
*/
const SHARED_DASHBOARDS = 'Shared Dashboards';

/**
* Upon mysql duplicate key error raised error code
*
Expand Down Expand Up @@ -117,6 +103,14 @@ public function load()
$this->loadHomeItems();
$this->loadDashboards();

if (! $this->hasHome(DashboardHome::AVAILABLE_DASHLETS)) {
DashboardHome::getConn()->insert(DashboardHome::TABLE, [
'name' => DashboardHome::AVAILABLE_DASHLETS,
'label' => DashboardHome::AVAILABLE_DASHLETS,
'owner' => DashboardHome::DEFAULT_IW2_USER
]);
}

return $this;
}

Expand Down Expand Up @@ -191,7 +185,7 @@ public function loadHomeItems()
{
$menu = new Menu();
/** @var DashboardHome $home */
foreach ($menu->getItem('dashboard')->getChildren()->order() as $home) {
foreach ($menu->getItem('dashboard')->getChildren() as $home) {
$this->homes[$home->getName()] = $home;
}
}
Expand Down Expand Up @@ -384,8 +378,8 @@ public function getHomeKeyNameArray($skipDisabled = true)
}

// User is not allowed to add new content directly to this dashboard home
if ($home->getName() === self::AVAILABLE_DASHLETS
|| $home->getName() === self::SHARED_DASHBOARDS) {
if ($home->getName() === DashboardHome::AVAILABLE_DASHLETS
|| $home->getName() === DashboardHome::SHARED_DASHBOARDS) {
continue;
}

Expand Down
9 changes: 9 additions & 0 deletions public/css/icinga/dashboard.less
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
text-align: left;
padding: 0.5em;
}

.dashlet-class-td {
table-layout: fixed;
overflow: hidden;
Expand Down Expand Up @@ -36,6 +37,7 @@
border: 3px dotted #00C3ED;
}

// Create dashboard from pinned dashlets
.pinned-item {
color: #00C3ED;
}
Expand All @@ -50,3 +52,10 @@
padding: 0 8px 0 8px;
}
}

.pinned-form {
a {
color: #00C3ED;
font-weight: bolder;
}
}

0 comments on commit 00f702e

Please sign in to comment.