summaryrefslogtreecommitdiffstats
path: root/modules/doc/library/Doc/Renderer/DocTocRenderer.php
blob: 09e9a1d8176471706c82afd6c979de56068baf32 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Doc\Renderer;

use Icinga\Data\Tree\TreeNodeIterator;
use RecursiveIteratorIterator;

/**
 * TOC renderer
 */
class DocTocRenderer extends DocRenderer
{
    /**
     * CSS class for the HTML list element
     *
     * @var string
     */
    const CSS_CLASS = 'toc';

    /**
     * Tag for the HTML list element
     *
     * @var string
     */
    const HTML_LIST_TAG = 'ol';

    /**
     * Content to render
     *
     * @var array
     */
    protected $content = array();

    /**
     * Create a new toc renderer
     *
     * @param TreeNodeIterator $iterator
     */
    public function __construct(TreeNodeIterator $iterator)
    {
        parent::__construct($iterator, RecursiveIteratorIterator::SELF_FIRST);
    }

    public function beginIteration(): void
    {
        $this->content[] = sprintf('<nav role="navigation"><%s class="%s">', static::HTML_LIST_TAG, static::CSS_CLASS);
    }

    public function endIteration(): void
    {
        $this->content[] = sprintf('</%s></nav>', static::HTML_LIST_TAG);
    }

    public function beginChildren(): void
    {
        $this->content[] = sprintf('<%s class="%s">', static::HTML_LIST_TAG, static::CSS_CLASS);
    }

    public function endChildren(): void
    {
        $this->content[] = sprintf('</%s>', static::HTML_LIST_TAG);
    }

    public function render()
    {
        if ($this->getInnerIterator()->isEmpty()) {
            return '<p>' . mt('doc', 'Documentation is empty.') . '</p>';
        }
        $view = $this->getView();
        $zendUrlHelper = $view->getHelper('Url');
        foreach ($this as $section) {
            $path = $zendUrlHelper->url(
                array_merge(
                    $this->urlParams,
                    array(
                        'chapter' => $this->encodeUrlParam($section->getChapter()->getId())
                    )
                ),
                $this->url,
                false,
                false
            );
            $url = $view->url($path);
            /** @var \Icinga\Web\Url $url */
            if ($this->getDepth() > 0) {
                $url->setAnchor($this->encodeAnchor($section->getId()));
            }
            $urlAttributes = array(
                'data-base-target'  => '_next',
                'title'             => $section->getId() === $section->getChapter()->getId()
                    ? sprintf(
                        $view->translate('Show the chapter "%s"', 'toc.render.section.link'),
                        $section->getChapter()->getTitle()
                    )
                    : sprintf(
                        $view->translate('Show the section "%s" of the chapter "%s"', 'toc.render.section.link'),
                        $section->getTitle(),
                        $section->getChapter()->getTitle()
                    )
            );
            if ($section->getNoFollow()) {
                $urlAttributes['rel'] = 'nofollow';
            }
            $this->content[] = '<li>' . $this->getView()->qlink(
                $section->getTitle(),
                $url->getAbsoluteUrl(),
                null,
                $urlAttributes
            );
            if (! $section->hasChildren()) {
                $this->content[] = '</li>';
            }
        }
        return implode("\n", $this->content);
    }
}