Dashboard/Dashlet: refactor, clean up

This commit is contained in:
Thomas Gelf 2020-07-20 15:53:38 +02:00
parent 36141c5f98
commit 6957035f93
2 changed files with 32 additions and 96 deletions

View File

@ -2,18 +2,18 @@
namespace Icinga\Module\Director\Dashboard; namespace Icinga\Module\Director\Dashboard;
use ipl\Html\HtmlDocument;
use Exception; 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\Authentication\Auth;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Restriction\HostgroupRestriction; use Icinga\Module\Director\Restriction\HostgroupRestriction;
use Icinga\Module\Director\Dashboard\Dashlet\Dashlet; use Icinga\Module\Director\Dashboard\Dashlet\Dashlet;
use Icinga\Module\Director\Db; use Icinga\Module\Director\Db;
use Icinga\Web\Widget\Tab; 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; use Zend_Db_Select as ZfSelect;
abstract class Dashboard extends HtmlDocument abstract class Dashboard extends HtmlDocument
@ -180,7 +180,7 @@ abstract class Dashboard extends HtmlDocument
} else { } else {
$this->dashlets = Dashlet::loadByNames( $this->dashlets = Dashlet::loadByNames(
$this->dashletNames, $this->dashletNames,
$this $this->getDb()
); );
} }
} }
@ -271,11 +271,8 @@ abstract class Dashboard extends HtmlDocument
protected function applyRestrictions($type, $query) protected function applyRestrictions($type, $query)
{ {
switch ($type) { switch ($type) {
case 'hostgroup':
$r = new HostgroupRestriction($this->getDb(), $this->getAuth());
$r->applyToQuery($query);
break;
case 'host': case 'host':
case 'hostgroup':
$r = new HostgroupRestriction($this->getDb(), $this->getAuth()); $r = new HostgroupRestriction($this->getDb(), $this->getAuth());
$r->applyToQuery($query); $r->applyToQuery($query);
break; break;

View File

@ -2,10 +2,8 @@
namespace Icinga\Module\Director\Dashboard\Dashlet; namespace Icinga\Module\Director\Dashboard\Dashlet;
use DirectoryIterator;
use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Acl; use Icinga\Module\Director\Acl;
use Icinga\Module\Director\Dashboard\Dashboard; use Icinga\Module\Director\Db;
use ipl\Html\BaseHtmlElement; use ipl\Html\BaseHtmlElement;
use ipl\Html\Html; use ipl\Html\Html;
use gipfl\IcingaWeb2\Icon; use gipfl\IcingaWeb2\Icon;
@ -16,24 +14,20 @@ abstract class Dashlet extends BaseHtmlElement
{ {
use TranslationHelper; use TranslationHelper;
protected $tag = 'li'; /** @var Db */
protected $db;
protected $sectionName; protected $tag = 'li';
protected $icon = 'help'; protected $icon = 'help';
protected $supportsLegacyConfig;
/** @var \Icinga\Module\Director\Db */
protected $db;
protected $stats; 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 $name
* @param Dashboard $dashboard * @param Db $db
* @return array * @return Dashlet
*/ */
public static function loadAll(Dashboard $dashboard) public static function loadByName($name, Db $db)
{
$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)
{ {
/** @var Dashlet */
$class = __NAMESPACE__ . '\\' . $name . '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 = [];
$dashlets = array();
foreach ($names as $name) { foreach ($names as $name) {
$class = $prefix . $name . 'Dashlet'; $dashlet = static::loadByName($name, $db);
/** @var Dashlet $dashlet */
$dashlet = new $class($dashboard);
if ($dashlet->isAllowed()) { if ($dashlet->isAllowed()) {
$dashlets[] = $dashlet; $dashlets[] = $dashlet;
@ -98,34 +69,9 @@ abstract class Dashlet extends BaseHtmlElement
return $dashlets; 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() public function listCssClasses()
{ {
return array(); return [];
}
public function getSectionName()
{
if ($this->sectionName === null) {
throw new ProgrammingError(
'Dashlets without a sectionName are not allowed'
);
}
return $this->sectionName;
} }
public function getIconName() public function getIconName()
@ -137,22 +83,15 @@ abstract class Dashlet extends BaseHtmlElement
abstract public function getUrl(); abstract public function getUrl();
public function renderContent() protected function assemble()
{ {
$this->add( $this->add(Link::create([
Link::create( $this->getTitle(),
[ Icon::create($this->getIconName()),
$this->getTitle(), Html::tag('p', null, $this->getSummary())
Icon::create($this->getIconName()), ], $this->getUrl(), null, [
Html::tag('p', null, $this->getSummary()) 'class' => $this->listCssClasses()
], ]));
$this->getUrl(),
null,
['class' => $this->listCssClasses()]
)
);
return parent::renderContent();
} }
public function listRequiredPermissions() public function listRequiredPermissions()