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;
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;

View File

@ -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()