summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Web/Navigation/Renderer/NavigationItemRenderer.php
blob: 51136ffd7edcc60f6f15fd57305921261f05f7e8 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\Web\Navigation\Renderer;

use Icinga\Application\Icinga;
use Icinga\Exception\ProgrammingError;
use Icinga\Util\StringHelper;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Url;
use Icinga\Web\View;

/**
 * NavigationItemRenderer
 */
class NavigationItemRenderer
{
    /**
     * View
     *
     * @var View
     */
    protected $view;

    /**
     * The item being rendered
     *
     * @var NavigationItem
     */
    protected $item;

    /**
     * Internal link targets provided by Icinga Web 2
     *
     * @var array
     */
    protected $internalLinkTargets;

    /**
     * Whether to escape the label
     *
     * @var bool
     */
    protected $escapeLabel;

    /**
     * Create a new NavigationItemRenderer
     *
     * @param   array   $options
     */
    public function __construct(array $options = null)
    {
        if (! empty($options)) {
            $this->setOptions($options);
        }

        $this->internalLinkTargets = array('_main', '_self', '_next');
        $this->init();
    }

    /**
     * Initialize this renderer
     */
    public function init()
    {
    }

    /**
     * Set the given options
     *
     * @param   array   $options
     *
     * @return  $this
     */
    public function setOptions(array $options)
    {
        foreach ($options as $name => $value) {
            $setter = 'set' . StringHelper::cname($name);
            if (method_exists($this, $setter)) {
                $this->$setter($value);
            }
        }

        return $this;
    }

    /**
     * Set the view
     *
     * @param   View    $view
     *
     * @return  $this
     */
    public function setView(View $view)
    {
        $this->view = $view;
        return $this;
    }

    /**
     * Return the view
     *
     * @return  View
     */
    public function view()
    {
        if ($this->view === null) {
            $this->setView(Icinga::app()->getViewRenderer()->view);
        }

        return $this->view;
    }

    /**
     * Set the navigation item to render
     *
     * @param   NavigationItem  $item
     *
     * @return  $this
     */
    public function setItem(NavigationItem $item)
    {
        $this->item = $item;
        return $this;
    }

    /**
     * Return the navigation item being rendered
     *
     * @return  NavigationItem
     */
    public function getItem()
    {
        return $this->item;
    }

    /**
     * Set whether to escape the label
     *
     * @param   bool    $state
     *
     * @return  $this
     */
    public function setEscapeLabel($state = true)
    {
        $this->escapeLabel = (bool) $state;
        return $this;
    }

    /**
     * Return whether to escape the label
     *
     * @return  bool
     */
    public function getEscapeLabel()
    {
        return $this->escapeLabel !== null ? $this->escapeLabel : true;
    }

    /**
     * Render the given navigation item as HTML anchor
     *
     * @param   NavigationItem  $item
     *
     * @return  string
     */
    public function render(NavigationItem $item = null)
    {
        if ($item !== null) {
            $this->setItem($item);
        } elseif (($item = $this->getItem()) === null) {
            throw new ProgrammingError(
                'Cannot render nothing. Pass the item to render as part'
                . ' of the call to render() or set it with setItem()'
            );
        }

        $label = $this->getEscapeLabel()
            ? $this->view()->escape($item->getLabel())
            : $item->getLabel();
        if (($icon = $item->getIcon()) !== null) {
            $label = $this->view()->icon($icon) . $label;
        } elseif ($item->getName()) {
            $firstLetter = $item->getName()[0];
            $label = $this->view()->icon('letter', null, ['data-letter' => strtolower($firstLetter)]) . $label;
        }

        if (($url = $item->getUrl()) !== null) {
            $url->overwriteParams($item->getUrlParameters());

            $target = $item->getTarget();
            if ($url->isExternal() && (!$target || in_array($target, $this->internalLinkTargets, true))) {
                $url = Url::fromPath('iframe', array('url' => $url));
            }

            $content = sprintf(
                '<a%s href="%s"%s>%s</a>',
                $this->view()->propertiesToString($item->getAttributes()),
                $this->view()->escape($url->getAbsoluteUrl('&')),
                $this->renderTargetAttribute(),
                $label
            );
        } elseif ($label) {
            $content = sprintf(
                '<%1$s%2$s>%3$s</%1$s>',
                $item::LINK_ALTERNATIVE,
                $this->view()->propertiesToString($item->getAttributes()),
                $label
            );
        } else {
            $content = '';
        }

        return $content;
    }

    /**
     * Render and return the attribute to provide a non-default target for the url
     *
     * @return  string
     */
    protected function renderTargetAttribute()
    {
        $target = $this->getItem()->getTarget();
        if ($target === null || $this->getItem()->getUrl()->getAbsoluteUrl() == '#') {
            return '';
        }

        if (! in_array($target, $this->internalLinkTargets, true)) {
            return ' target="' . $this->view()->escape($target) . '"';
        }

        return ' data-base-target="' . $target . '"';
    }
}