From 6957035f9329271662753064866d6d8ef53fa426 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 20 Jul 2020 15:53:38 +0200 Subject: [PATCH] Dashboard/Dashlet: refactor, clean up --- library/Director/Dashboard/Dashboard.php | 17 ++- .../Director/Dashboard/Dashlet/Dashlet.php | 111 ++++-------------- 2 files changed, 32 insertions(+), 96 deletions(-) diff --git a/library/Director/Dashboard/Dashboard.php b/library/Director/Dashboard/Dashboard.php index 99e4fba3..b3d380e6 100644 --- a/library/Director/Dashboard/Dashboard.php +++ b/library/Director/Dashboard/Dashboard.php @@ -2,18 +2,18 @@ namespace Icinga\Module\Director\Dashboard; -use ipl\Html\HtmlDocument; use Exception; +use gipfl\Translation\TranslationHelper; +use gipfl\IcingaWeb2\Widget\Tabs; +use ipl\Html\Html; +use ipl\Html\HtmlDocument; +use ipl\Html\HtmlString; use Icinga\Authentication\Auth; use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Restriction\HostgroupRestriction; use Icinga\Module\Director\Dashboard\Dashlet\Dashlet; use Icinga\Module\Director\Db; use Icinga\Web\Widget\Tab; -use ipl\Html\Html; -use ipl\Html\HtmlString; -use gipfl\Translation\TranslationHelper; -use gipfl\IcingaWeb2\Widget\Tabs; use Zend_Db_Select as ZfSelect; abstract class Dashboard extends HtmlDocument @@ -180,7 +180,7 @@ abstract class Dashboard extends HtmlDocument } else { $this->dashlets = Dashlet::loadByNames( $this->dashletNames, - $this + $this->getDb() ); } } @@ -271,11 +271,8 @@ abstract class Dashboard extends HtmlDocument protected function applyRestrictions($type, $query) { switch ($type) { - case 'hostgroup': - $r = new HostgroupRestriction($this->getDb(), $this->getAuth()); - $r->applyToQuery($query); - break; case 'host': + case 'hostgroup': $r = new HostgroupRestriction($this->getDb(), $this->getAuth()); $r->applyToQuery($query); break; diff --git a/library/Director/Dashboard/Dashlet/Dashlet.php b/library/Director/Dashboard/Dashlet/Dashlet.php index 984abfc4..f8bc708c 100644 --- a/library/Director/Dashboard/Dashlet/Dashlet.php +++ b/library/Director/Dashboard/Dashlet/Dashlet.php @@ -2,10 +2,8 @@ namespace Icinga\Module\Director\Dashboard\Dashlet; -use DirectoryIterator; -use Icinga\Exception\ProgrammingError; use Icinga\Module\Director\Acl; -use Icinga\Module\Director\Dashboard\Dashboard; +use Icinga\Module\Director\Db; use ipl\Html\BaseHtmlElement; use ipl\Html\Html; use gipfl\IcingaWeb2\Icon; @@ -16,24 +14,20 @@ abstract class Dashlet extends BaseHtmlElement { use TranslationHelper; - protected $tag = 'li'; + /** @var Db */ + protected $db; - protected $sectionName; + protected $tag = 'li'; protected $icon = 'help'; - protected $supportsLegacyConfig; - - /** @var \Icinga\Module\Director\Db */ - protected $db; - protected $stats; - protected $requiredStats = array(); + protected $requiredStats = []; - public function __construct(Dashboard $dashboard) + public function __construct(Db $db) { - $this->db = $dashboard->getDb(); + $this->db = $db; } /** @@ -50,45 +44,22 @@ abstract class Dashlet extends BaseHtmlElement } /** - * @deprecated This is obsolete, should not be used - * @param Dashboard $dashboard - * @return array + * @param $name + * @param Db $db + * @return Dashlet */ - public static function loadAll(Dashboard $dashboard) - { - $dashlets = array(); - - foreach (new DirectoryIterator(__DIR__) as $file) { - if ($file->isDot()) { - continue; - } - $filename = $file->getFilename(); - if (preg_match('/^(\w+)Dashlet\.php$/', $filename, $match)) { - $dashlet = static::loadByName($match[1], $dashboard); - - if ($dashlet->isAllowed()) { - $dashlets[] = $dashlet; - } - } - } - - return $dashlets; - } - - public static function loadByName($name, Dashboard $dashboard) + public static function loadByName($name, Db $db) { + /** @var Dashlet */ $class = __NAMESPACE__ . '\\' . $name . 'Dashlet'; - return new $class($dashboard); + return new $class($db); } - public static function loadByNames(array $names, Dashboard $dashboard) + public static function loadByNames(array $names, Db $db) { - $prefix = __NAMESPACE__ . '\\'; - $dashlets = array(); + $dashlets = []; foreach ($names as $name) { - $class = $prefix . $name . 'Dashlet'; - /** @var Dashlet $dashlet */ - $dashlet = new $class($dashboard); + $dashlet = static::loadByName($name, $db); if ($dashlet->isAllowed()) { $dashlets[] = $dashlet; @@ -98,34 +69,9 @@ abstract class Dashlet extends BaseHtmlElement return $dashlets; } - public function renderClassAttribute() - { - $classes = $this->listCssClasses(); - if (empty($classes)) { - return ''; - } else { - if (! is_array($classes)) { - $classes = array($classes); - } - - return ' class="' . implode(' ', $classes) . '"'; - } - } - public function listCssClasses() { - return array(); - } - - public function getSectionName() - { - if ($this->sectionName === null) { - throw new ProgrammingError( - 'Dashlets without a sectionName are not allowed' - ); - } - - return $this->sectionName; + return []; } public function getIconName() @@ -137,22 +83,15 @@ abstract class Dashlet extends BaseHtmlElement abstract public function getUrl(); - public function renderContent() + protected function assemble() { - $this->add( - Link::create( - [ - $this->getTitle(), - Icon::create($this->getIconName()), - Html::tag('p', null, $this->getSummary()) - ], - $this->getUrl(), - null, - ['class' => $this->listCssClasses()] - ) - ); - - return parent::renderContent(); + $this->add(Link::create([ + $this->getTitle(), + Icon::create($this->getIconName()), + Html::tag('p', null, $this->getSummary()) + ], $this->getUrl(), null, [ + 'class' => $this->listCssClasses() + ])); } public function listRequiredPermissions()