summaryrefslogtreecommitdiffstats
path: root/application/controllers/SchemaController.php
blob: 961b7b1c8afb1735e9809a2bca5843bb8b6aabb6 (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
<?php

namespace Icinga\Module\Director\Controllers;

use Icinga\Module\Director\Web\Controller\ActionController;
use ipl\Html\Html;
use gipfl\IcingaWeb2\Link;

class SchemaController extends ActionController
{
    protected $schemas;

    public function init()
    {
        $this->schemas = [
            'mysql' => $this->translate('MySQL schema'),
            'pgsql' => $this->translate('PostgreSQL schema'),
        ];
    }

    /**
     * @throws \Icinga\Exception\IcingaException
     */
    public function mysqlAction()
    {
        $this->serveSchema('mysql');
    }

    /**
     * @throws \Icinga\Exception\IcingaException
     */
    public function pgsqlAction()
    {
        $this->serveSchema('pgsql');
    }

    /**
     * @param $type
     * @throws \Icinga\Exception\IcingaException
     */
    protected function serveSchema($type)
    {
        $schema = $this->loadSchema($type);

        if ($this->params->get('format') === 'sql') {
            header('Content-type: application/octet-stream');
            header('Content-Disposition: attachment; filename=' . $type . '.sql');
            echo $schema;
            exit;
            // TODO: Shutdown
        }

        $this
            ->addSchemaTabs($type)
            ->addTitle($this->schemas[$type])
            ->addDownloadAction()
            ->content()->add(Html::tag('pre', null, $schema));
    }

    protected function loadSchema($type)
    {
        return file_get_contents(
            sprintf(
                '%s/schema/%s.sql',
                $this->Module()->getBaseDir(),
                $type
            )
        );
    }

    /**
     * @return $this
     * @throws \Icinga\Exception\IcingaException
     * @throws \Icinga\Exception\ProgrammingError
     */
    protected function addDownloadAction()
    {
        $this->actions()->add(
            Link::create(
                $this->translate('Download'),
                $this->url()->with('format', 'sql'),
                null,
                [
                    'target' => '_blank',
                    'class'  => 'icon-download',
                ]
            )
        );

        return $this;
    }

    /**
     * @param $active
     * @return $this
     * @throws \Icinga\Exception\Http\HttpNotFoundException
     * @throws \Icinga\Exception\ProgrammingError
     */
    protected function addSchemaTabs($active)
    {
        $tabs = $this->tabs();
        foreach ($this->schemas as $type => $title) {
            $tabs->add($type, [
                'url'   => 'director/schema/' . $type,
                'label' => $title,
            ]);
        }

        $tabs->activate($active);

        return $this;
    }
}