Clean up codes

This commit is contained in:
Yonas Habteab 2022-04-08 14:57:41 +02:00
parent da4edb2274
commit a1ed493ad1
26 changed files with 157 additions and 220 deletions

View File

@ -307,8 +307,8 @@ class DashboardsController extends CompatController
break;
}
/** @var DashboardHome $home */
$home = $this->dashboard->getEntry($home);
/** @var DashboardHome $home */
if (! is_array($value)) {
$this->dashboard->reorderWidget($home, (int) $value);
@ -323,8 +323,8 @@ class DashboardsController extends CompatController
break;
}
/** @var Pane $pane */
$pane = $home->hasEntry($pane) ? $home->getEntry($pane) : $orgHome->getEntry($pane);
/** @var Pane $pane */
if (! is_array($indexOrValues)) {
if ($orgHome && $orgHome->hasEntry($pane->getName()) && $home->hasEntry($pane->getName())) {
Notification::error(sprintf(

View File

@ -91,7 +91,7 @@ abstract class BaseDashboardForm extends CompatForm
*
* @return FormElement
*/
protected function createRemoveButton(Url $action, $label)
protected function createRemoveButton(Url $action, string $label)
{
return $this->createElement('submitButton', 'btn_remove', [
'class' => 'remove-button',
@ -107,7 +107,7 @@ abstract class BaseDashboardForm extends CompatForm
*
* @return FormElement
*/
protected function registerSubmitButton($label)
protected function registerSubmitButton(string $label)
{
$submitElement = $this->createElement('submit', 'submit', ['class' => 'btn-primary', 'label' => $label]);
$this->registerElement($submitElement);

View File

@ -1,6 +1,5 @@
<?php
/* Icinga Web 2 | (c) 2013-2022 Icinga GmbH | GPLv2+ */
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Forms\Dashboard;
@ -274,16 +273,16 @@ class DashletForm extends BaseDashboardForm
}
}
public function load(BaseDashboard $dashlet)
public function load(BaseDashboard $dashboard)
{
$home = Url::fromRequest()->getParam('home');
/** @var Dashlet $dashlet */
/** @var Dashlet $dashboard */
$this->populate(array(
'org_home' => $home,
'org_pane' => $dashlet->getPane()->getName(),
'org_dashlet' => $dashlet->getName(),
'dashlet' => $dashlet->getTitle(),
'url' => $dashlet->getUrl()->getRelativeUrl()
'org_pane' => $dashboard->getPane()->getName(),
'org_dashlet' => $dashboard->getName(),
'dashlet' => $dashboard->getTitle(),
'url' => $dashboard->getUrl()->getRelativeUrl()
));
}
}

View File

@ -1,5 +1,7 @@
<?php
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
namespace Icinga\Forms\Dashboard;
use Icinga\Web\Dashboard\Dashboard;

View File

@ -17,7 +17,7 @@ class WelcomeForm extends CompatForm
public function __construct(Dashboard $dashboard)
{
$this->dashboard = $dashboard;
$this->setRedirectUrl(Url::fromPath(Dashboard::BASE_ROUTE));
$this->setRedirectUrl((string) Url::fromPath(Dashboard::BASE_ROUTE));
}
public function hasBeenSubmitted()

View File

@ -13,7 +13,7 @@ class DashboardContainer extends NavigationItemContainer
*
* @var array
*/
protected $dashlets;
protected $dashlets = [];
/**
* Set this dashboard's dashlets
@ -35,7 +35,11 @@ class DashboardContainer extends NavigationItemContainer
*/
public function getDashlets()
{
return $this->dashlets ?: array();
uasort($this->dashlets, function (array $x, array $y) {
return $x['priority'] - $y['priority'];
});
return $this->dashlets;
}
/**

View File

@ -43,7 +43,7 @@ class DashletContainer extends NavigationItemContainer
}
/**
* Set url if this dashlet item
* Set url of this dashlet item
*
* @param Url|string $url
*

View File

@ -217,7 +217,7 @@ class Module
/**
* A set of Pane elements
*
* @var DashboardContainer
* @var DashboardContainer[]
*/
protected $paneItems = [];
@ -317,49 +317,12 @@ class Module
/**
* Return this module's dashboard
*
* @return Navigation
* @return DashboardContainer[]
*/
public function getDashboard()
{
$this->launchConfigScript();
return $this->createDashboard($this->paneItems);
}
/**
* Create and return a new navigation for the given dashboard panes
*
* @param DashboardContainer[] $panes
*
* @return Navigation
*/
public function createDashboard(array $panes)
{
$navigation = new Navigation();
foreach ($panes as $pane) {
/** @var DashboardContainer $pane */
$dashlets = [];
foreach ($pane->getDashlets() as $dashletName => $dashletConfig) {
$dashlets[$dashletName] = [
'label' => $this->translate($dashletName),
'url' => $dashletConfig['url'],
'priority' => $dashletConfig['priority']
];
}
$navigation->addItem(
$pane->getName(),
array_merge(
$pane->getProperties(),
array(
'label' => $this->translate($pane->getName()),
'type' => 'dashboard-pane',
'children' => $dashlets
)
)
);
}
return $navigation;
return $this->paneItems;
}
/**
@ -401,7 +364,7 @@ class Module
*
* @return DashletContainer
*/
protected function provideDashlet($name, $url, array $properties = [])
protected function provideDashlet(string $name, $url, array $properties = [])
{
if (array_key_exists($name, $this->dashletItems)) {
$this->dashletItems[$name]->setProperties($properties);

View File

@ -11,7 +11,7 @@ trait DataExtractor
*
* @return $this
*/
public function fromArray(array $data)
public function fromArray(array $data): self
{
foreach ($data as $name => $value) {
$func = 'set' . ucfirst($name);
@ -32,7 +32,7 @@ trait DataExtractor
*
* @return array
*/
public function toArray($stringify = true)
public function toArray(bool $stringify = true): array
{
return [];
}

View File

@ -63,7 +63,7 @@ abstract class BaseDashboard implements DashboardEntry
* @param string $name
* @param array $properties
*/
public function __construct($name, array $properties = [])
public function __construct(string $name, array $properties = [])
{
$this->name = $name;
$this->title = $name;
@ -80,7 +80,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return $this
*/
public function setUuid($uuid)
public function setUuid($uuid): self
{
$this->uuid = $uuid;
@ -90,7 +90,7 @@ abstract class BaseDashboard implements DashboardEntry
/**
* Get this widget's unique identifier
*
* @return string
* @return string|int
*/
public function getUuid()
{
@ -101,10 +101,14 @@ abstract class BaseDashboard implements DashboardEntry
* Set the name of this widget
*
* @param string $name
*
* @return $this
*/
public function setName($name)
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
@ -112,7 +116,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@ -124,7 +128,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return $this
*/
public function setTitle($title)
public function setTitle(string $title): self
{
$this->title = $title;
@ -136,7 +140,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return string
*/
public function getTitle()
public function getTitle(): string
{
return $this->title !== null ? $this->title : $this->getName();
}
@ -148,7 +152,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return $this
*/
public function setOwner($owner)
public function setOwner(string $owner): self
{
$this->owner = $owner;
@ -158,7 +162,7 @@ abstract class BaseDashboard implements DashboardEntry
/**
* Get owner of this widget
*
* @return string
* @return ?string
*/
public function getOwner()
{
@ -168,7 +172,7 @@ abstract class BaseDashboard implements DashboardEntry
/**
* Get the widget's description
*
* @return string
* @return ?string
*/
public function getDescription()
{
@ -182,7 +186,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return $this
*/
public function setDescription($description)
public function setDescription(string $description = null): self
{
$this->description = $description;
@ -196,7 +200,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return $this
*/
public function setPriority(int $order)
public function setPriority(int $order): self
{
$this->order = $order;
@ -208,7 +212,7 @@ abstract class BaseDashboard implements DashboardEntry
*
* @return int
*/
public function getPriority()
public function getPriority(): int
{
return $this->order;
}
@ -217,11 +221,11 @@ abstract class BaseDashboard implements DashboardEntry
{
}
public function getEntry($name)
public function getEntry(string $name)
{
}
public function hasEntry($name)
public function hasEntry(string $name)
{
}
@ -237,7 +241,7 @@ abstract class BaseDashboard implements DashboardEntry
{
}
public function createEntry($name, $url = null)
public function createEntry(string $name, $url = null)
{
}
@ -253,11 +257,11 @@ abstract class BaseDashboard implements DashboardEntry
{
}
public function manageEntry($entry, BaseDashboard $origin = null, $manageRecursive = false)
public function manageEntry($entry, BaseDashboard $origin = null, bool $manageRecursive = false)
{
}
public function loadDashboardEntries($name = '')
public function loadDashboardEntries(string $name = '')
{
}

View File

@ -22,7 +22,7 @@ trait DashboardControls
return ! empty($this->dashboards);
}
public function getEntry($name)
public function getEntry(string $name)
{
if (! $this->hasEntry($name)) {
throw new ProgrammingError('Trying to retrieve invalid dashboard entry "%s"', $name);
@ -31,7 +31,7 @@ trait DashboardControls
return $this->dashboards[$name];
}
public function hasEntry($name)
public function hasEntry(string $name)
{
return array_key_exists($name, $this->dashboards);
}
@ -85,7 +85,7 @@ trait DashboardControls
return $this;
}
public function createEntry($name, $url = null)
public function createEntry(string $name, $url = null)
{
}
@ -94,7 +94,7 @@ trait DashboardControls
return reset($this->dashboards);
}
public function reorderWidget(BaseDashboard $dashboard, $position, Sortable $origin = null)
public function reorderWidget(BaseDashboard $dashboard, int $position, Sortable $origin = null)
{
if ($origin && ! $origin instanceof $this) {
throw new \InvalidArgumentException(sprintf(

View File

@ -25,7 +25,7 @@ interface DashboardEntry
*
* @return BaseDashboard
*/
public function getEntry($name);
public function getEntry(string $name);
/**
* Get whether the given dashboard entry exists
@ -34,7 +34,7 @@ interface DashboardEntry
*
* @return bool
*/
public function hasEntry($name);
public function hasEntry(string $name);
/**
* Get all dashboard entries of this widget
@ -69,7 +69,7 @@ interface DashboardEntry
*
* @return $this
*/
public function createEntry($name, $url = null);
public function createEntry(string $name, $url = null);
/**
* Get an array with entry name=>title format
@ -112,7 +112,7 @@ interface DashboardEntry
*
* @return $this
*/
public function manageEntry($entry, BaseDashboard $origin = null, $manageRecursive = false);
public function manageEntry($entry, BaseDashboard $origin = null, bool $manageRecursive = false);
/**
* Load all the assigned entries to this widget
@ -121,7 +121,7 @@ interface DashboardEntry
*
* @return $this
*/
public function loadDashboardEntries($name = '');
public function loadDashboardEntries(string $name = '');
/**
* Reset the current position of the internal dashboard entries pointer

View File

@ -58,7 +58,7 @@ trait DashboardManager
*
* @return Connection
*/
public static function getConn()
public static function getConn(): Connection
{
if (self::$conn === null) {
self::$conn = (new self())->getDb();
@ -74,12 +74,12 @@ trait DashboardManager
*
* @return string
*/
public static function getSHA1($name)
public static function getSHA1(string $name): string
{
return sha1($name, true);
}
public function loadDashboardEntries($name = '')
public function loadDashboardEntries(string $name = '')
{
if ($name && $this->hasEntry($name)) {
$home = $this->getEntry($name);
@ -113,7 +113,7 @@ trait DashboardManager
*
* @return $this
*/
public function activateHome(DashboardHome $home)
public function activateHome(DashboardHome $home): self
{
$activeHome = $this->getActiveHome();
if ($activeHome && $activeHome->getName() !== $home->getName()) {
@ -166,12 +166,12 @@ trait DashboardManager
/** @var DashboardHome $home */
foreach ($homes as $home) {
if (! $this->hasEntry($home->getName())) {
// Highest priority is 0, so count($entries) are always lowest prio + 1
$priority = $home->getName() === DashboardHome::DEFAULT_HOME ? 0 : count($this->getEntries());
$conn->insert(DashboardHome::TABLE, [
'name' => $home->getName(),
'label' => $home->getTitle(),
'username' => self::getUser()->getUsername(),
// highest priority is 0, so count($entries) are always lowest prio + 1
'priority' => $priority,
'type' => $home->getType() !== Dashboard::SYSTEM ? $home->getType() : Dashboard::PRIVATE_DS
]);
@ -191,9 +191,9 @@ trait DashboardManager
/**
* Get and|or init the default dashboard home
*
* @return BaseDashboard
* @return DashboardHome
*/
public function initGetDefaultHome()
public function initGetDefaultHome(): DashboardHome
{
if ($this->hasEntry(DashboardHome::DEFAULT_HOME)) {
return $this->getEntry(DashboardHome::DEFAULT_HOME);
@ -213,7 +213,7 @@ trait DashboardManager
*
* @return $this
*/
public function setUser(User $user)
public function setUser(User $user): self
{
self::$user = $user;
@ -225,7 +225,7 @@ trait DashboardManager
*
* @return User
*/
public static function getUser()
public static function getUser(): User
{
if (self::$user === null) {
self::$user = Auth::getInstance()->getUser();
@ -240,7 +240,7 @@ trait DashboardManager
*
* @return Pane[]
*/
public static function getSystemDefaults()
public static function getSystemDefaults(): array
{
return self::$defaultPanes;
}
@ -251,26 +251,29 @@ trait DashboardManager
*
* @return void
*/
public static function deployModuleDashlets()
public static function deployModuleDashlets(): void
{
$moduleManager = Icinga::app()->getModuleManager();
foreach ($moduleManager->getLoadedModules() as $module) {
/** @var DashboardPane $dashboardPane */
foreach ($module->getDashboard() as $dashboardPane) {
$pane = new Pane($dashboardPane->getName());
$pane->setTitle($dashboardPane->getLabel());
$pane->fromArray($dashboardPane->getAttributes());
$mg = Icinga::app()->getModuleManager();
foreach ($mg->getLoadedModules() as $module) {
foreach ($module->getDashboard() as $dashboard) {
$pane = new Pane($dashboard->getName());
$pane->fromArray($dashboard->getProperties());
$priority = 0;
foreach ($dashboardPane->getIterator()->getItems() as $dashletItem) {
$uuid = self::getSHA1($module->getName() . $pane->getName() . $dashletItem->getName());
$dashlet = new Dashlet($dashletItem->getName(), $dashletItem->getUrl(), $pane);
$dashlet->fromArray($dashletItem->getAttributes());
foreach ($dashboard->getDashlets() as $name => $configPart) {
$uuid = self::getSHA1($module->getName() . $pane->getName() . $name);
$dashlet = new Dashlet($name, $configPart['url'], $pane);
$dashlet->fromArray($configPart);
$dashlet
->setUuid($uuid)
->setModule($module->getName())
->setModuleDashlet(true)
->setPriority($priority++);
->setPriority($priority++)
->setModule($module->getName());
// As we don't have a setter for labels, this might be ignored by the data extractor
if (isset($configPart['label'])) {
$dashlet->setTitle($configPart['label']);
}
self::updateOrInsertModuleDashlet($dashlet);
$pane->addEntry($dashlet);
@ -305,7 +308,7 @@ trait DashboardManager
*
* @return bool
*/
public static function moduleDashletExist(Dashlet $dashlet)
public static function moduleDashletExist(Dashlet $dashlet): bool
{
$query = Model\ModuleDashlet::on(self::getConn())->filter(Filter::equal('id', $dashlet->getUuid()));
$query->getSelectBase()->columns(new Expression('1'));
@ -317,11 +320,10 @@ trait DashboardManager
* Insert or update the given module dashlet
*
* @param Dashlet $dashlet
* @param string $module
*
* @return void
*/
public static function updateOrInsertModuleDashlet(Dashlet $dashlet)
public static function updateOrInsertModuleDashlet(Dashlet $dashlet): void
{
if (! $dashlet->isModuleDashlet()) {
return;
@ -351,9 +353,11 @@ trait DashboardManager
/**
* Get module dashlets from the database
*
* @param Query $query
*
* @return array
*/
public static function getModuleDashlets(Query $query)
public static function getModuleDashlets(Query $query): array
{
$dashlets = [];
foreach ($query as $moduleDashlet) {

View File

@ -19,40 +19,40 @@ abstract class ItemListControl extends BaseHtmlElement
*
* @return string
*/
abstract protected function getHtmlId();
abstract protected function getHtmlId(): string;
/**
* Get a class name for the collapsible control
*
* @return string
*/
abstract protected function getCollapsibleControlClass();
abstract protected function getCollapsibleControlClass(): string;
/**
* Create an action link to be added at the end of the list
*
* @return HtmlElement
* @return BaseHtmlElement
*/
abstract protected function createActionLink();
abstract protected function createActionLink(): BaseHtmlElement;
/**
* Create the appropriate item list of this control
*
* @return HtmlElement
* @return BaseHtmlElement
*/
abstract protected function createItemList();
abstract protected function createItemList(): BaseHtmlElement;
/**
* Assemble a header element for this item list
*
* @param Url $url
* @param string $header
* @param string $title
*
* @return void
*/
protected function assembleHeader(Url $url, $header)
protected function assembleHeader(Url $url, string $title)
{
$header = HtmlElement::create('h1', ['class' => 'collapsible-header'], $header);
$header = HtmlElement::create('h1', ['class' => 'collapsible-header'], $title);
$header->addHtml(new Link(t('Edit'), $url, [
'data-icinga-modal' => true,
'data-no-icinga-ajax' => true

View File

@ -23,7 +23,7 @@ trait ModuleDashlet
/**
* Get the name of the module which provides this dashlet
*
* @return string
* @return ?string
*/
public function getModule()
{
@ -37,7 +37,7 @@ trait ModuleDashlet
*
* @return $this
*/
public function setModule($module)
public function setModule(string $module): self
{
$this->module = $module;
@ -49,7 +49,7 @@ trait ModuleDashlet
*
* @return bool
*/
public function isModuleDashlet()
public function isModuleDashlet(): bool
{
return $this->moduleDashlet;
}
@ -61,7 +61,7 @@ trait ModuleDashlet
*
* @return $this
*/
public function setModuleDashlet(bool $moduleDashlet)
public function setModuleDashlet(bool $moduleDashlet): self
{
$this->moduleDashlet = $moduleDashlet;

View File

@ -14,10 +14,10 @@ interface Sortable
* Insert the dashboard entry at the given position within this dashboard entries
*
* @param BaseDashboard $dashboard
* @param $position
* @param int $position
* @param Sortable|null $origin
*
* @return $this
*/
public function reorderWidget(BaseDashboard $dashboard, $position, Sortable $origin = null);
public function reorderWidget(BaseDashboard $dashboard, int $position, Sortable $origin = null);
}

View File

@ -97,7 +97,7 @@ class Dashboard extends BaseHtmlElement implements DashboardEntry
* @param string $name The tab name to activate
*
*/
public function activate($name)
public function activate(string $name)
{
$this->getTabs()->activate($name);
}

View File

@ -73,7 +73,7 @@ class DashboardHome extends BaseDashboard implements Sortable
*
* @return $this
*/
public function setActive($active = true)
public function setActive(bool $active = true)
{
$this->active = $active;
@ -97,7 +97,7 @@ class DashboardHome extends BaseDashboard implements Sortable
*
* @return $this
*/
public function setType($type)
public function setType(string $type)
{
$this->type = $type;
@ -132,7 +132,7 @@ class DashboardHome extends BaseDashboard implements Sortable
return $this;
}
public function loadDashboardEntries($name = '')
public function loadDashboardEntries(string $name = '')
{
if (! $this->getActive()) {
return $this;
@ -160,7 +160,7 @@ class DashboardHome extends BaseDashboard implements Sortable
return $this;
}
public function createEntry($name, $url = null)
public function createEntry(string $name, $url = null)
{
$entry = new Pane($name);
$entry->setHome($this);
@ -170,7 +170,7 @@ class DashboardHome extends BaseDashboard implements Sortable
return $this;
}
public function manageEntry($entry, BaseDashboard $origin = null, $manageRecursive = false)
public function manageEntry($entry, BaseDashboard $origin = null, bool $manageRecursive = false)
{
$user = Dashboard::getUser();
$conn = Dashboard::getConn();
@ -236,7 +236,7 @@ class DashboardHome extends BaseDashboard implements Sortable
}
}
public function toArray($stringify = true)
public function toArray(bool $stringify = true): array
{
return [
'id' => $this->getUuid(),

View File

@ -9,6 +9,7 @@ use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Web\Dashboard\Common\ModuleDashlet;
use Icinga\Web\Request;
use Icinga\Web\Url;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Web\Widget\Link;
@ -52,7 +53,7 @@ class Dashlet extends BaseDashboard
* @param Url|string $url The url this dashlet uses for displaying information
* @param Pane|null $pane The pane this Dashlet will be added to
*/
public function __construct($name, $url, Pane $pane = null)
public function __construct(string $name, $url, Pane $pane = null)
{
parent::__construct($name);
@ -63,7 +64,7 @@ class Dashlet extends BaseDashboard
/**
* Retrieve the dashlets url
*
* @return Url|null
* @return ?Url
*/
public function getUrl()
{
@ -85,7 +86,7 @@ class Dashlet extends BaseDashboard
*
* @return $this
*/
public function setUrl($url)
public function setUrl($url): self
{
$this->url = $url;
@ -99,7 +100,7 @@ class Dashlet extends BaseDashboard
*
* @return $this
*/
public function setProgressLabel($label)
public function setProgressLabel(string $label): self
{
$this->progressLabel = $label;
@ -111,7 +112,7 @@ class Dashlet extends BaseDashboard
*
* @return string
*/
public function getProgressLabel()
public function getProgressLabel(): string
{
if ($this->progressLabel === null) {
return $this->progressLabel = t('Loading');
@ -127,7 +128,7 @@ class Dashlet extends BaseDashboard
*
* @return Dashlet
*/
public function setPane(Pane $pane)
public function setPane(Pane $pane): self
{
$this->pane = $pane;
@ -137,7 +138,7 @@ class Dashlet extends BaseDashboard
/**
* Get the pane of this dashlet
*
* @return Pane
* @return ?Pane
*/
public function getPane()
{
@ -147,9 +148,9 @@ class Dashlet extends BaseDashboard
/**
* Generate a html widget for this dashlet
*
* @return HtmlElement
* @return BaseHtmlElement
*/
public function getHtml()
public function getHtml(): BaseHtmlElement
{
$dashletHtml = HtmlElement::create('div', ['class' => 'container']);
if (! $this->getUrl()) {
@ -191,7 +192,7 @@ class Dashlet extends BaseDashboard
return $dashletHtml;
}
public function toArray($stringify = true)
public function toArray(bool $stringify = true): array
{
$pane = $this->getPane();
return [

View File

@ -7,6 +7,7 @@ namespace Icinga\Web\Dashboard\ItemList;
use Icinga\Web\Dashboard\Common\ItemListControl;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\DashboardHome;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Web\Url;
use ipl\Web\Widget\ActionLink;
@ -30,17 +31,17 @@ class DashboardHomeList extends ItemListControl
});
}
protected function getHtmlId()
protected function getHtmlId(): string
{
return $this->home->getUuid();
}
protected function getCollapsibleControlClass()
protected function getCollapsibleControlClass(): string
{
return 'dashboard-list-info';
}
protected function createItemList()
protected function createItemList(): BaseHtmlElement
{
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/edit-home')
->setParams(['home' => $this->home->getName()]);
@ -58,7 +59,7 @@ class DashboardHomeList extends ItemListControl
return $list;
}
protected function createActionLink()
protected function createActionLink(): BaseHtmlElement
{
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/new-pane');
$url->setParams(['home' => $this->home->getName()]);

View File

@ -7,6 +7,7 @@ namespace Icinga\Web\Dashboard\ItemList;
use Icinga\Web\Dashboard\Common\ItemListControl;
use Icinga\Web\Dashboard\Dashboard;
use Icinga\Web\Dashboard\Pane;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlElement;
use ipl\Web\Url;
use ipl\Web\Widget\ActionLink;
@ -28,17 +29,17 @@ class DashboardList extends ItemListControl
});
}
protected function getHtmlId()
protected function getHtmlId(): string
{
return bin2hex($this->pane->getUuid());
}
protected function getCollapsibleControlClass()
protected function getCollapsibleControlClass(): string
{
return 'dashlets-list-info';
}
protected function createItemList()
protected function createItemList(): BaseHtmlElement
{
$pane = $this->pane;
$this->getAttributes()->set('data-toggle-element', '.dashlets-list-info');
@ -61,7 +62,7 @@ class DashboardList extends ItemListControl
return $list;
}
protected function createActionLink()
protected function createActionLink(): BaseHtmlElement
{
$url = Url::fromPath(Dashboard::BASE_ROUTE . '/new-dashlet');
$url->setParams([

View File

@ -46,14 +46,14 @@ class DashletListItem extends BaseHtmlElement
*
* @return $this
*/
protected function setDetailUrl(bool $value)
protected function setDetailUrl(bool $value): self
{
$this->renderEditButton = $value;
return $this;
}
protected function assembleTitle()
protected function assembleHeader(): HtmlElement
{
$title = HtmlElement::create('h1', ['class' => 'dashlet-header']);
@ -98,12 +98,10 @@ class DashletListItem extends BaseHtmlElement
if (! $this->dashlet) {
$section->add(t('Create a dashlet with custom url and filter'));
} else {
$section->getAttributes()->set(
'title',
$this->dashlet->getDescription() ?: t('There is no provided description.')
);
$description = $this->dashlet->getDescription() ?: t('There is no provided description.');
$section->getAttributes()->set('title', $description);
$section->add($this->dashlet->getDescription() ?: t('There is no provided dashlet description.'));
$section->add($description);
}
return $section;
@ -111,7 +109,7 @@ class DashletListItem extends BaseHtmlElement
protected function assemble()
{
$this->addHtml($this->assembleTitle());
$this->addHtml($this->assembleHeader());
$this->addHtml($this->assembleSummary());
}
}

View File

@ -19,14 +19,14 @@ class DashletListMultiSelect extends DashletListItem
*
* @return $this
*/
public function setCheckBox(FormElement $checkbox)
public function setCheckBox(FormElement $checkbox): self
{
$this->checkbox = $checkbox;
return $this;
}
protected function createLabel()
protected function createLabel(): HtmlElement
{
$label = HtmlElement::create('label');
$label->addHtml($this->checkbox);

View File

@ -50,59 +50,21 @@ class Pane extends BaseDashboard implements Sortable
*
* @return $this
*/
public function setHome(DashboardHome $home)
public function setHome(DashboardHome $home): self
{
$this->home = $home;
return $this;
}
public function createEntry($name, $url = null)
public function createEntry(string $name, $url = null)
{
$dashlet = new Dashlet($name, $url, $this);
$this->addDashlet($dashlet);
return $this;
}
/**
* Add a dashlet to this pane, optionally creating it if $dashlet is a string
*
* @param string|Dashlet $dashlet
* @param ?string $url
*
* @return $this
* @throws \Icinga\Exception\ConfigurationError
*/
public function addDashlet($dashlet, $url = null)
{
if ($dashlet instanceof Dashlet) {
$this->addEntry($dashlet);
} elseif (is_string($dashlet) && $url !== null) {
$this->createEntry($dashlet, $url);
} else {
throw new ConfigurationError('Invalid dashlet added: %s', $dashlet);
if ($url === null) {
throw new ConfigurationError('Can\'t create a dashlet "%s" without a valid url', $name);
}
return $this;
}
/**
* Add a dashlet to the current pane
*
* @param string $name
* @param Url|string $url
*
* @return $this
* @see addDashlet()
*/
public function add($name, $url, $priority = 0, $description = null)
{
$this->createEntry($name, $url);
$dashlet = $this->getEntry($name);
$dashlet
->setDescription($description)
->setPriority($priority);
$dashlet = new Dashlet($name, $url, $this);
$this->addEntry($dashlet);
return $this;
}
@ -126,7 +88,7 @@ class Pane extends BaseDashboard implements Sortable
return $this;
}
public function loadDashboardEntries($name = '')
public function loadDashboardEntries(string $name = '')
{
$dashlets = Model\Dashlet::on(Dashboard::getConn())
->utilize(self::TABLE)
@ -144,13 +106,13 @@ class Pane extends BaseDashboard implements Sortable
'description' => $dashlet->icingaweb_module_dashlet->description
]);
$this->addDashlet($newDashlet);
$this->addEntry($newDashlet);
}
return $this;
}
public function manageEntry($entry, BaseDashboard $origin = null, $manageRecursive = false)
public function manageEntry($entry, BaseDashboard $origin = null, bool $manageRecursive = false)
{
if ($origin && ! $origin instanceof Pane) {
throw new \InvalidArgumentException(sprintf(
@ -171,7 +133,7 @@ class Pane extends BaseDashboard implements Sortable
$conn = Dashboard::getConn();
$dashlets = is_array($entry) ? $entry : [$entry];
// highest priority is 0, so count($entries) are all always lowest prio + 1
// Highest priority is 0, so count($entries) are always lowest prio + 1
$order = count($this->getEntries());
foreach ($dashlets as $dashlet) {
if (is_array($dashlet)) {
@ -240,7 +202,7 @@ class Pane extends BaseDashboard implements Sortable
return $this;
}
public function toArray($stringify = true)
public function toArray(bool $stringify = true): array
{
$home = $this->getHome();
return [

View File

@ -36,7 +36,7 @@ class SetupNewDashboard extends BaseDashboardForm
*
* @return $this
*/
public function initDashlets(array $dashlets)
public function initDashlets(array $dashlets): self
{
$this->dashlets = $dashlets;
@ -236,9 +236,9 @@ class SetupNewDashboard extends BaseDashboardForm
* Dump all module dashlets which are not selected by the user
* from the member variable
*
* @return $this
* @return void
*/
private function dumpArbitaryDashlets()
private function dumpArbitaryDashlets(): void
{
$choosenDashlets = [];
foreach ($this->dashlets as $module => $dashlets) {
@ -252,8 +252,6 @@ class SetupNewDashboard extends BaseDashboardForm
}
$this->dashlets = $choosenDashlets;
return $this;
}
/**
@ -261,7 +259,7 @@ class SetupNewDashboard extends BaseDashboardForm
*
* @return ValidHtml
*/
private function createFormListControl()
private function createFormListControl(): ValidHtml
{
return HtmlElement::create('div', [
'class' => ['control-group', 'form-list-control', 'collapsible'],

View File

@ -1,5 +1,5 @@
<?php
/* Icinga Web 2 | (c) 2014-2022 Icinga Development Team | GPLv2+ */
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget\Tabextension;