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

namespace Icinga\Module\Doc;

use Icinga\Module\Doc\Renderer\DocSectionRenderer;
use Icinga\Module\Doc\Renderer\DocTocRenderer;
use Icinga\Web\Controller;
use Icinga\Web\Url;
use Icinga\Web\Widget\Tabextension\OutputFormat;

class DocController extends Controller
{
    /**
     * {@inheritdoc}
     */
    protected function moduleInit()
    {
        // Our UrlParams object does not take parameters from custom routes into account which is why we have to set
        // them explicitly
        if ($this->hasParam('chapter')) {
            $this->params->set('chapter', $this->getParam('chapter'));
        }
        if ($this->hasParam('image')) {
            $this->params->set('image', $this->getParam('image'));
        }
        if ($this->hasParam('moduleName')) {
            $this->params->set('moduleName', $this->getParam('moduleName'));
        }
    }

    /**
     * Render a chapter
     *
     * @param string    $path       Path to the documentation
     * @param string    $chapter    ID of the chapter
     * @param string    $url        URL to replace links with
     * @param string    $imageUrl   URL to images
     * @param array     $urlParams  Additional URL parameters
     */
    protected function renderChapter($path, $chapter, $url, $imageUrl = null, array $urlParams = array())
    {
        $parser = new DocParser($path);
        $section = new DocSectionRenderer($parser->getDocTree(), DocSectionRenderer::decodeUrlParam($chapter));
        $this->view->section = $section
            ->setHighlightSearch($this->params->get('highlight-search'))
            ->setImageUrl($imageUrl)
            ->setUrl($url)
            ->setUrlParams($urlParams);
        $first = null;
        foreach ($section as $first) {
            break;
        }
        $title = $first === null ? ucfirst($chapter) : $first->getTitle();
        $this->view->title = $title;
        $this->getTabs()
            ->add('toc', array(
                'active'    => true,
                'title'     => $title,
                'url'       => Url::fromRequest()
            ))
            ->extend(new OutputFormat(array(OutputFormat::TYPE_CSV, OutputFormat::TYPE_JSON)));
        $this->render('chapter', null, true);
    }

    /**
     * Render a toc
     *
     * @param string    $path       Path to the documentation
     * @param string    $name       Name of the documentation
     * @param string    $url        URL to replace links with
     * @param array     $urlParams  Additional URL parameters
     */
    protected function renderToc($path, $name, $url, array $urlParams = array())
    {
        $parser = new DocParser($path);
        $toc = new DocTocRenderer($parser->getDocTree()->getIterator());
        $this->view->toc = $toc
            ->setUrl($url)
            ->setUrlParams($urlParams);
        $name = ucfirst($name);
        $title = sprintf($this->translate('%s Documentation'), $name);
        $this->getTabs()
            ->add('toc', array(
                'active'    => true,
                'title'     => $title,
                'url'       => Url::fromRequest()
            ))
            ->extend(new OutputFormat(array(OutputFormat::TYPE_CSV, OutputFormat::TYPE_JSON)));
        $this->render('toc', null, true);
    }

    /**
     * Render a pdf
     *
     * @param string    $path           Path to the documentation
     * @param string    $name           Name of the documentation
     * @param string    $url
     * @param array     $urlParams
     */
    protected function renderPdf($path, $name, $url, array $urlParams = array())
    {
        $parser = new DocParser($path);
        $toc = new DocTocRenderer($parser->getDocTree()->getIterator());
        $this->view->toc = $toc
            ->setUrl($url)
            ->setUrlParams($urlParams);
        $section = new DocSectionRenderer($parser->getDocTree());
        $this->view->section = $section
            ->setUrl($url)
            ->setUrlParams($urlParams);
        $this->view->title = sprintf($this->translate('%s Documentation'), $name);
        $this->_request->setParam('format', 'pdf');
        $this->_helper->viewRenderer->setRender('pdf', null, true);
    }
}