summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Navigation/Renderer/RecursiveNavigationRenderer.php
blob: 315c2aa4d0e255e13a82be0447d08a19ff59188a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Navigation\Renderer;

use Exception;
use RecursiveIteratorIterator;
use Icinga\Exception\IcingaException;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Navigation\Renderer\NavigationItemRenderer;

/**
 * Renderer for multi level navigation
 *
 * @method NavigationRenderer getInnerIterator() {
 *     {@inheritdoc}
 * }
 */
class RecursiveNavigationRenderer extends RecursiveIteratorIterator implements NavigationRendererInterface
{
    /**
     * The content rendered so far
     *
     * @var array
     */
    protected $content;

    /**
     * Whether to use the standard item renderer
     *
     * @var bool
     */
    protected $useStandardRenderer;

    /**
     * Create a new RecursiveNavigationRenderer
     *
     * @param   Navigation  $navigation
     */
    public function __construct(Navigation $navigation)
    {
        $this->content = array();
        parent::__construct(
            new NavigationRenderer($navigation, true),
            RecursiveIteratorIterator::SELF_FIRST
        );
    }

    /**
     * Set whether to use the standard navigation item renderer
     *
     * @param   bool    $state
     *
     * @return  $this
     */
    public function setUseStandardItemRenderer($state = true)
    {
        $this->useStandardRenderer = (bool) $state;
        return $this;
    }

    /**
     * Return whether to use the standard navigation item renderer
     *
     * @return  bool
     */
    public function getUseStandardItemRenderer()
    {
        return $this->useStandardRenderer;
    }

    /**
     * {@inheritdoc}
     */
    public function setElementTag($tag)
    {
        $this->getInnerIterator()->setElementTag($tag);
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getElementTag()
    {
        return $this->getInnerIterator()->getElementTag();
    }

    /**
     * {@inheritdoc}
     */
    public function setCssClass($class)
    {
        $this->getInnerIterator()->setCssClass($class);
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getCssClass()
    {
        return $this->getInnerIterator()->getCssClass();
    }

    /**
     * {@inheritdoc}
     */
    public function setHeading($heading)
    {
        $this->getInnerIterator()->setHeading($heading);
        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function getHeading()
    {
        return $this->getInnerIterator()->getHeading();
    }

    public function beginIteration(): void
    {
        $this->content[] = $this->getInnerIterator()->beginMarkup();
    }

    public function endIteration(): void
    {
        $this->content[] = $this->getInnerIterator()->endMarkup();
    }

    public function beginChildren(): void
    {
        $this->content[] = $this->getInnerIterator()->beginChildrenMarkup($this->getDepth() + 1);
    }

    public function endChildren(): void
    {
        $this->content[] = $this->getInnerIterator()->endChildrenMarkup();
        $this->content[] = $this->getInnerIterator()->endItemMarkup();
    }

    /**
     * {@inheritdoc}
     */
    public function render()
    {
        foreach ($this as $item) {
            /** @var NavigationItem $item */
            if ($item->shouldRender()) {
                if ($this->getDepth() > 0) {
                    $item->setIcon(null);
                }
                if ($this->getUseStandardItemRenderer()) {
                    $renderer = new NavigationItemRenderer();
                    $content = $renderer->render($item);
                } else {
                    $content = $item->render();
                }
                $this->content[] = $this->getInnerIterator()->beginItemMarkup($item);

                $this->content[] = $content;

                if (! $item->hasChildren()) {
                    $this->content[] = $this->getInnerIterator()->endItemMarkup();
                }
            }
        }

        return join("\n", $this->content);
    }

    /**
     * {@inheritdoc}
     */
    public function __toString()
    {
        try {
            return $this->render();
        } catch (Exception $e) {
            return IcingaException::describe($e);
        }
    }
}