Dashboard: refactor based on ipl

This commit is contained in:
Thomas Gelf 2017-06-21 20:27:11 +02:00
parent 65ddbb02ab
commit 8f2ac7cc3a

View File

@ -6,14 +6,22 @@ use Countable;
use Exception; use Exception;
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\BetaHostgroupRestriction; use Icinga\Module\Director\Restriction\HostgroupRestriction;
use Icinga\Web\View; use Icinga\Web\View;
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 ipl\Html\BaseElement;
use ipl\Html\Html;
use ipl\Html\HtmlString;
use ipl\Html\Util;
use ipl\Html\ValidHtml;
use ipl\Translation\TranslationHelper;
use Zend_Db_Select as ZfSelect; use Zend_Db_Select as ZfSelect;
abstract class Dashboard implements Countable abstract class Dashboard extends Html implements Countable
{ {
use TranslationHelper;
protected $name; protected $name;
/** @var Dashlet[] */ /** @var Dashlet[] */
@ -24,9 +32,6 @@ abstract class Dashboard implements Countable
/** @var Db */ /** @var Db */
protected $db; protected $db;
/** @var View */
protected $view;
final private function __construct() final private function __construct()
{ {
} }
@ -34,20 +39,61 @@ abstract class Dashboard implements Countable
/** /**
* @param $name * @param $name
* @param Db $db * @param Db $db
* @param View $view
* *
* @return self * @return self
*/ */
public static function loadByName($name, Db $db, View $view) public static function loadByName($name, Db $db)
{ {
$class = __NAMESPACE__ . '\\' . ucfirst($name) . 'Dashboard'; $class = __NAMESPACE__ . '\\' . ucfirst($name) . 'Dashboard';
$dashboard = new $class(); $dashboard = new $class();
$dashboard->db = $db; $dashboard->db = $db;
$dashboard->name = $name; $dashboard->name = $name;
$dashboard->view = $view;
return $dashboard; return $dashboard;
} }
/**
* @param $description
* @return $this
*/
protected function addDescription($description)
{
if ($description !== null) {
$this->add(
Html::tag('p', null, HtmlString::create(
nl2br(Util::escapeForHtml($description)))
)
);
}
return $this;
}
public function render()
{
$this
->setSeparator("\n")
->add(Html::tag('h1', null, $this->getTitle()))
->addDescription($this->getDescription())
->add($this->renderDashlets());
return parent::render();
}
public function renderDashlets()
{
$ul = Html::tag('ul', [
'class' => 'main-actions',
'data-base-target' => '_next'
]);
foreach ($this->dashlets() as $dashlet) {
if ($dashlet->shouldBeShown()) {
$ul->add($dashlet);
}
}
return $ul;
}
public function getName() public function getName()
{ {
@ -76,16 +122,6 @@ abstract class Dashboard implements Countable
return $this->db; return $this->db;
} }
public function getView()
{
return $this->view;
}
protected function translate($msg)
{
return $this->view->translate($msg);
}
public function dashlets() public function dashlets()
{ {
if ($this->dashlets === null) { if ($this->dashlets === null) {
@ -197,12 +233,12 @@ abstract class Dashboard implements Countable
{ {
switch ($type) { switch ($type) {
case 'hostgroup': case 'hostgroup':
$r = new BetaHostgroupRestriction($this->getDb(), $this->getAuth()); $r = new HostgroupRestriction($this->getDb(), $this->getAuth());
$r->applyToHostGroupsQuery($query); $r->applyToQuery($query);
break; break;
case 'host': case 'host':
$r = new BetaHostgroupRestriction($this->getDb(), $this->getAuth()); $r = new HostgroupRestriction($this->getDb(), $this->getAuth());
$r->applyToHostsQuery($query, 'o.id'); $r->applyToQuery($query);
break; break;
} }
@ -211,7 +247,7 @@ abstract class Dashboard implements Countable
protected function applyHostgroupRestrictions($query) protected function applyHostgroupRestrictions($query)
{ {
$restrictions = new BetaHostgroupRestriction($this->getDb(), $this->getAuth()); $restrictions = new HostgroupRestriction($this->getDb(), $this->getAuth());
$restrictions->applyToHostGroupsQuery($query); $restrictions->applyToHostGroupsQuery($query);
} }