diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
commit | cd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /application/controllers/SchemaController.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-upstream.tar.xz icingaweb2-module-director-upstream.zip |
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'application/controllers/SchemaController.php')
-rw-r--r-- | application/controllers/SchemaController.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/application/controllers/SchemaController.php b/application/controllers/SchemaController.php new file mode 100644 index 0000000..b0ca24e --- /dev/null +++ b/application/controllers/SchemaController.php @@ -0,0 +1,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; + } +} |