summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Widget/Documentation.php
blob: 8665e30192a56a5a34d46b7bf5606ec8a3c7c6f7 (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
<?php

namespace Icinga\Module\Director\Web\Widget;

use gipfl\IcingaWeb2\Link;
use gipfl\Translation\TranslationHelper;
use Icinga\Application\ApplicationBootstrap;
use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use ipl\Html\Html;

class Documentation
{
    use TranslationHelper;

    /** @var ApplicationBootstrap */
    protected $app;

    /** @var Auth */
    protected $auth;

    public function __construct(ApplicationBootstrap $app, Auth $auth)
    {
        $this->app = $app;
        $this->auth = $auth;
    }

    public static function link($label, $module, $chapter, $title = null)
    {
        $doc = new static(Icinga::app(), Auth::getInstance());
        return $doc->getModuleLink($label, $module, $chapter, $title);
    }

    public function getModuleLink($label, $module, $chapter, $title = null)
    {
        if ($title !== null) {
            $title = sprintf(
                $this->translate('Click to read our documentation: %s'),
                $title
            );
        }
        $linkToGitHub = false;
        $baseParams = [
            'class' => 'icon-book',
            'title' => $title,
        ];
        if ($this->hasAccessToDocumentationModule()) {
            return Link::create(
                $label,
                $this->getDirectorDocumentationUrl($chapter),
                null,
                ['data-base-target' => '_next'] + $baseParams
            );
        }

        $baseParams['target'] = '_blank';
        if ($linkToGitHub) {
            return Html::tag('a', [
                'href' => $this->githubDocumentationUrl($module, $chapter),
            ] + $baseParams, $label);
        }

        return Html::tag('a', [
            'href' => $this->icingaDocumentationUrl($module, $chapter),
        ] + $baseParams, $label);
    }

    protected function getDirectorDocumentationUrl($chapter)
    {
        return 'doc/module/director/chapter/'
            . \preg_replace('/^\d+-/', '', \rawurlencode($chapter));
    }

    protected function githubDocumentationUrl($module, $chapter)
    {
        return sprintf(
            "https://github.com/Icinga/icingaweb2-module-%s/blob/master/doc/%s.md",
            \rawurlencode($module),
            \rawurlencode($chapter)
        );
    }

    protected function icingaDocumentationUrl($module, $chapter)
    {
        return sprintf(
            'https://icinga.com/docs/%s/latest/doc/%s/',
            \rawurlencode($module),
            \rawurlencode($chapter)
        );
    }

    protected function hasAccessToDocumentationModule()
    {
        return $this->app->getModuleManager()->hasLoaded('doc')
            && $this->auth->hasPermission('module/doc');
    }
}