summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Tree
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:43:12 +0000
commitcd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Web/Tree
parentInitial commit. (diff)
downloadicingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.tar.xz
icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.zip
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Web/Tree')
-rw-r--r--library/Director/Web/Tree/InspectTreeRenderer.php97
-rw-r--r--library/Director/Web/Tree/TemplateTreeRenderer.php91
2 files changed, 188 insertions, 0 deletions
diff --git a/library/Director/Web/Tree/InspectTreeRenderer.php b/library/Director/Web/Tree/InspectTreeRenderer.php
new file mode 100644
index 0000000..54a177f
--- /dev/null
+++ b/library/Director/Web/Tree/InspectTreeRenderer.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Icinga\Module\Director\Web\Tree;
+
+use Icinga\Module\Director\Objects\IcingaEndpoint;
+use ipl\Html\BaseHtmlElement;
+use ipl\Html\Html;
+use gipfl\IcingaWeb2\Link;
+use gipfl\Translation\TranslationHelper;
+
+class InspectTreeRenderer extends BaseHtmlElement
+{
+ use TranslationHelper;
+
+ protected $tag = 'ul';
+
+ protected $defaultAttributes = [
+ 'class' => 'tree',
+ 'data-base-target' => '_next',
+ ];
+
+ protected $tree;
+
+ /** @var IcingaEndpoint */
+ protected $endpoint;
+
+ public function __construct(IcingaEndpoint $endpoint)
+ {
+ $this->endpoint = $endpoint;
+ }
+
+ protected function getNodes()
+ {
+ $rootNodes = array();
+ $types = $this->endpoint->api()->getTypes();
+ foreach ($types as $name => $type) {
+ if (property_exists($type, 'base')) {
+ $base = $type->base;
+ if (! property_exists($types[$base], 'children')) {
+ $types[$base]->children = array();
+ }
+
+ $types[$base]->children[$name] = $type;
+ } else {
+ $rootNodes[$name] = $type;
+ }
+ }
+
+ return $rootNodes;
+ }
+
+ public function assemble()
+ {
+ $this->add($this->renderNodes($this->getNodes()));
+ }
+
+ protected function renderNodes($nodes, $showLinks = false, $level = 0)
+ {
+ $result = [];
+ foreach ($nodes as $child) {
+ $result[] = $this->renderNode($child, $showLinks, $level + 1);
+ }
+
+ if ($level === 0) {
+ return $result;
+ } else {
+ return Html::tag('ul', null, $result);
+ }
+ }
+
+ protected function renderNode($node, $forceLinks = false, $level = 0)
+ {
+ $name = $node->name;
+ $showLinks = $forceLinks || $name === 'ConfigObject';
+ $hasChildren = property_exists($node, 'children');
+ $li = Html::tag('li');
+ if (! $hasChildren) {
+ $li->getAttributes()->add('class', 'collapsed');
+ }
+
+ if ($hasChildren) {
+ $li->add(Html::tag('span', ['class' => 'handle']));
+ }
+
+ $class = $node->abstract ? 'icon-sitemap' : 'icon-doc-text';
+ $li->add(Link::create($name, 'director/inspect/type', [
+ 'endpoint' => $this->endpoint->getObjectName(),
+ 'type' => $name
+ ], ['class' => $class]));
+
+ if ($hasChildren) {
+ $li->add($this->renderNodes($node->children, $showLinks, $level + 1));
+ }
+
+ return $li;
+ }
+}
diff --git a/library/Director/Web/Tree/TemplateTreeRenderer.php b/library/Director/Web/Tree/TemplateTreeRenderer.php
new file mode 100644
index 0000000..e238ded
--- /dev/null
+++ b/library/Director/Web/Tree/TemplateTreeRenderer.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Icinga\Module\Director\Web\Tree;
+
+use Icinga\Module\Director\Db;
+use Icinga\Module\Director\Resolver\TemplateTree;
+use ipl\Html\BaseHtmlElement;
+use ipl\Html\Html;
+use gipfl\IcingaWeb2\Link;
+use gipfl\Translation\TranslationHelper;
+use gipfl\IcingaWeb2\Widget\ControlsAndContent;
+
+class TemplateTreeRenderer extends BaseHtmlElement
+{
+ use TranslationHelper;
+
+ protected $tag = 'ul';
+
+ protected $defaultAttributes = [
+ 'class' => 'tree',
+ 'data-base-target' => '_next',
+ ];
+
+ protected $tree;
+
+ public function __construct(TemplateTree $tree)
+ {
+ $this->tree = $tree;
+ }
+
+ public static function showType($type, ControlsAndContent $controller, Db $db)
+ {
+ $controller->content()->add(
+ new static(new TemplateTree($type, $db))
+ );
+ }
+
+ public function renderContent()
+ {
+ $this->add(
+ $this->dumpTree(
+ array(
+ 'name' => $this->translate('Templates'),
+ 'children' => $this->tree->getTree()
+ )
+ )
+ );
+
+ return parent::renderContent();
+ }
+
+ protected function dumpTree($tree, $level = 0)
+ {
+ $hasChildren = ! empty($tree['children']);
+ $type = $this->tree->getType();
+
+ $li = Html::tag('li');
+ if (! $hasChildren) {
+ $li->getAttributes()->add('class', 'collapsed');
+ }
+
+ if ($hasChildren) {
+ $li->add(Html::tag('span', ['class' => 'handle']));
+ }
+
+ if ($level === 0) {
+ $li->add(Html::tag('a', [
+ 'name' => $tree['name'],
+ 'class' => 'icon-globe'
+ ], $tree['name']));
+ } else {
+ $li->add(Link::create(
+ $tree['name'],
+ "director/${type}template/usage",
+ array('name' => $tree['name']),
+ array('class' => 'icon-' .$type)
+ ));
+ }
+
+ if ($hasChildren) {
+ $li->add(
+ $ul = Html::tag('ul')
+ );
+ foreach ($tree['children'] as $child) {
+ $ul->add($this->dumpTree($child, $level + 1));
+ }
+ }
+
+ return $li;
+ }
+}