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; namespace Icinga\Web\Dashboard\Common;
use Icinga\Common\DataExtractor;
/** /**
* Base class for all dashboard widget types * Base class for all dashboard widget types
* *
@ -13,8 +11,6 @@ use Icinga\Common\DataExtractor;
*/ */
abstract class BaseDashboard implements DashboardEntry abstract class BaseDashboard implements DashboardEntry
{ {
use DataExtractor;
/** /**
* Not translatable name of this widget * Not translatable name of this widget
* *
@ -69,7 +65,7 @@ abstract class BaseDashboard implements DashboardEntry
$this->title = $name; $this->title = $name;
if (! empty($properties)) { if (! empty($properties)) {
$this->fromArray($properties); $this->setProperties($properties);
} }
} }
@ -182,7 +178,7 @@ abstract class BaseDashboard implements DashboardEntry
/** /**
* Set the widget's description * Set the widget's description
* *
* @param string $description * @param ?string $description
* *
* @return $this * @return $this
*/ */
@ -217,6 +213,39 @@ abstract class BaseDashboard implements DashboardEntry
return $this->order; 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() public function hasEntries()
{ {
} }

View File

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

View File

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

View File

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

View File

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