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

namespace Icinga\Module\Doc\Renderer;

use RecursiveIteratorIterator;
use Icinga\Module\Doc\Search\DocSearchIterator;
use Icinga\Module\Doc\Search\DocSearchMatch;

/**
 * Renderer for doc searches
 */
class DocSearchRenderer extends DocRenderer
{
    /**
     * The content to render
     *
     * @var array
     */
    protected $content = array();

    /**
     * Create a new renderer for doc searches
     *
     * @param DocSearchIterator $iterator
     */
    public function __construct(DocSearchIterator $iterator)
    {
        parent::__construct($iterator, RecursiveIteratorIterator::SELF_FIRST);
    }

    public function beginIteration(): void
    {
        $this->content[] = '<nav role="navigation"><ul class="toc">';
    }

    public function endIteration(): void
    {
        $this->content[] = '</ul></nav>';
    }

    public function beginChildren(): void
    {
        if ($this->getInnerIterator()->getMatches()) {
            $this->content[] = '<ul class="toc">';
        }
    }

    public function endChildren(): void
    {
        if ($this->getInnerIterator()->getMatches()) {
            $this->content[] = '</ul>';
        }
    }

    public function render()
    {
        foreach ($this as $section) {
            if (($matches = $this->getInnerIterator()->getMatches()) === null) {
                continue;
            }
            $title = $this->getView()->escape($section->getTitle());
            $contentMatches = array();
            foreach ($matches as $match) {
                if ($match->getMatchType() === DocSearchMatch::MATCH_HEADER) {
                    $title = $match->highlight();
                } else {
                    $contentMatches[] = sprintf(
                        '<p>%s</p>',
                        $match->highlight()
                    );
                }
            }
            $path = $this->getView()->getHelper('Url')->url(
                array_merge(
                    $this->getUrlParams(),
                    array(
                        'chapter' => $this->encodeUrlParam($section->getChapter()->getId())
                    )
                ),
                $this->url,
                false,
                false
            );
            $url = $this->getView()->url(
                $path,
                array('highlight-search' => $this->getInnerIterator()->getSearch()->getInput())
            );
            /** @var \Icinga\Web\Url $url */
            $url->setAnchor($this->encodeAnchor($section->getId()));
            $urlAttributes = array(
                'data-base-target'  => '_next',
                'title'             => $section->getId() === $section->getChapter()->getId()
                    ? sprintf(
                        $this->getView()->translate(
                            'Show all matches of "%s" in the chapter "%s"',
                            'search.render.section.link'
                        ),
                        $this->getInnerIterator()->getSearch()->getInput(),
                        $section->getChapter()->getTitle()
                    )
                    : sprintf(
                        $this->getView()->translate(
                            'Show all matches of "%s" in the section "%s" of the chapter "%s"',
                            'search.render.section.link'
                        ),
                        $this->getInnerIterator()->getSearch()->getInput(),
                        $section->getTitle(),
                        $section->getChapter()->getTitle()
                    )
            );
            if ($section->getNoFollow()) {
                $urlAttributes['rel'] = 'nofollow';
            }
            $this->content[] = '<li>' . $this->getView()->qlink(
                $title,
                $url->getAbsoluteUrl(),
                null,
                $urlAttributes,
                false
            );
            if (! empty($contentMatches)) {
                $this->content = array_merge($this->content, $contentMatches);
            }
            if (! $section->hasChildren()) {
                $this->content[] = '</li>';
            }
        }
        return implode("\n", $this->content);
    }
}