Use some renamed tratis & move from/to array methods to BaseDashboard

This commit is contained in:
Yonas Habteab 2022-04-22 09:28:20 +02:00
parent 9729b0e99e
commit 90c0633354
5 changed files with 56 additions and 29 deletions

View File

@ -4,8 +4,6 @@
namespace Icinga\Web\Dashboard\Common;
use Icinga\Common\DataExtractor;
/**
* Base class for all dashboard widget types
*
@ -13,8 +11,6 @@ use Icinga\Common\DataExtractor;
*/
abstract class BaseDashboard implements DashboardEntry
{
use DataExtractor;
/**
* Not translatable name of this widget
*
@ -69,7 +65,7 @@ abstract class BaseDashboard implements DashboardEntry
$this->title = $name;
if (! empty($properties)) {
$this->fromArray($properties);
$this->setProperties($properties);
}
}
@ -182,7 +178,7 @@ abstract class BaseDashboard implements DashboardEntry
/**
* Set the widget's description
*
* @param string $description
* @param ?string $description
*
* @return $this
*/
@ -217,6 +213,39 @@ abstract class BaseDashboard implements DashboardEntry
return $this->order;
}
/**
* Set properties from the given list (no matching setter) are ignored
*
* @param array $data
*
* @return $this
*/
public function setProperties(array $data): self
{
foreach ($data as $name => $value) {
$func = 'set' . ucfirst($name);
if (method_exists($this, $func)) {
$this->$func($value);
}
}
return $this;
}
/**
* Get this class's structure as array
*
* Stringifies the attrs or set to null if it doesn't have a value, when $stringify is true
*
* @param bool $stringify Whether, the attributes should be returned unmodified
*
* @return array
*/
public function toArray(bool $stringify = true): array
{
return [];
}
public function hasEntries()
{
}

View File

@ -274,13 +274,13 @@ trait DashboardManager
foreach ($mg->getLoadedModules() as $module) {
foreach ($module->getDashboard() as $dashboard) {
$pane = new Pane($dashboard->getName());
$pane->fromArray($dashboard->getProperties());
$pane->setProperties($dashboard->getProperties());
$priority = 0;
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->setProperties($configPart);
$dashlet
->setUuid($uuid)
->setModuleDashlet(true)
@ -305,7 +305,7 @@ trait DashboardManager
foreach ($module->getDashlets() as $dashlet) {
$identifier = self::getSHA1($module->getName() . $dashlet->getName());
$newDashlet = new Dashlet($dashlet->getName(), $dashlet->getUrl());
$newDashlet->fromArray($dashlet->getProperties());
$newDashlet->setProperties($dashlet->getProperties());
$newDashlet
->setUuid($identifier)
->setModule($module->getName())
@ -382,7 +382,7 @@ trait DashboardManager
$dashlet->setDescription(t($moduleDashlet->description));
}
$dashlet->fromArray([
$dashlet->setProperties([
'label' => t($moduleDashlet->label),
'priority' => $moduleDashlet->priority,
'uuid' => $moduleDashlet->id,

View File

@ -6,7 +6,7 @@ namespace Icinga\Web\Dashboard;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Dashboard\Common\DashboardControls;
use Icinga\Web\Dashboard\Common\DashboardEntries;
use Icinga\Web\Dashboard\Common\DashboardEntry;
use Icinga\Web\Dashboard\Common\DashboardManager;
use ipl\Html\BaseHtmlElement;
@ -27,7 +27,7 @@ use ipl\Web\Widget\Tabs;
class Dashboard extends BaseHtmlElement implements DashboardEntry
{
use DashboardManager;
use DashboardControls;
use DashboardEntries;
/**
* Base path of our new dashboards controller

View File

@ -7,7 +7,7 @@ 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\DashboardEntries;
use Icinga\Web\Dashboard\Common\Sortable;
use ipl\Stdlib\Filter;
@ -15,7 +15,7 @@ use function ipl\Stdlib\get_php_type;
class DashboardHome extends BaseDashboard implements Sortable
{
use DashboardControls;
use DashboardEntries;
/**
* Name of the default home
@ -178,12 +178,11 @@ class DashboardHome extends BaseDashboard implements Sortable
foreach ($panes as $pane) {
$newPane = new Pane($pane->name);
$newPane->fromArray([
'uuid' => $pane->id,
'title' => $pane->label,
'priority' => $pane->priority,
'home' => $this
]);
$newPane
->setHome($this)
->setUuid($pane->id)
->setTitle($pane->label)
->setPriority($pane->priority);
$newPane->loadDashboardEntries($name);
$this->addEntry($newPane);

View File

@ -8,7 +8,7 @@ use Icinga\Web\Dashboard\Common\BaseDashboard;
use Icinga\Exception\ProgrammingError;
use Icinga\Exception\ConfigurationError;
use Icinga\Model;
use Icinga\Web\Dashboard\Common\DashboardControls;
use Icinga\Web\Dashboard\Common\DashboardEntries;
use Icinga\Web\Dashboard\Common\Sortable;
use ipl\Stdlib\Filter;
use ipl\Web\Url;
@ -20,7 +20,7 @@ use function ipl\Stdlib\get_php_type;
*/
class Pane extends BaseDashboard implements Sortable
{
use DashboardControls;
use DashboardEntries;
const TABLE = 'icingaweb_dashboard';
@ -98,13 +98,12 @@ class Pane extends BaseDashboard implements Sortable
$this->setEntries([]);
foreach ($dashlets as $dashlet) {
$newDashlet = new Dashlet($dashlet->name, $dashlet->url, $this);
$newDashlet->fromArray([
'uuid' => $dashlet->id,
'title' => $dashlet->label,
'priority' => $dashlet->priority,
'pane' => $this,
'description' => $dashlet->icingaweb_module_dashlet->description
]);
$newDashlet
->setPane($this)
->setUuid($dashlet->id)
->setTitle($dashlet->label)
->setPriority($dashlet->priority)
->setDescription($dashlet->icingaweb_module_dashlet->description);
$this->addEntry($newDashlet);
}