schema: no more view script, refactor controller

This commit is contained in:
Thomas Gelf 2017-08-16 19:13:07 +02:00
parent 04a78f734a
commit 6f20e6c35e
2 changed files with 55 additions and 37 deletions

View File

@ -3,29 +3,19 @@
namespace Icinga\Module\Director\Controllers; namespace Icinga\Module\Director\Controllers;
use Icinga\Module\Director\Web\Controller\ActionController; use Icinga\Module\Director\Web\Controller\ActionController;
use ipl\Html\Html;
use ipl\Html\Link;
class SchemaController extends ActionController class SchemaController extends ActionController
{ {
protected $schema; protected $schemas;
public function init() public function init()
{ {
$this->schemas = array( $this->schemas = [
'mysql' => $this->translate('MySQL schema'), 'mysql' => $this->translate('MySQL schema'),
'pgsql' => $this->translate('PostgreSQL schema'), 'pgsql' => $this->translate('PostgreSQL schema'),
); ];
}
protected function myTabs()
{
$tabs = $this->getTabs();
foreach ($this->schemas as $type => $title) {
$tabs->add($type, array(
'url' => 'director/schema/' . $type,
'label' => $title,
));
}
return $tabs;
} }
public function mysqlAction() public function mysqlAction()
@ -40,13 +30,7 @@ class SchemaController extends ActionController
protected function serveSchema($type) protected function serveSchema($type)
{ {
$schema = file_get_contents( $schema = $this->loadSchema($type);
sprintf(
'%s/schema/%s.sql',
$this->Module()->getBasedir(),
$type
)
);
if ($this->params->get('format') === 'sql') { if ($this->params->get('format') === 'sql') {
header('Content-type: application/octet-stream'); header('Content-type: application/octet-stream');
@ -54,11 +38,55 @@ class SchemaController extends ActionController
echo $schema; echo $schema;
exit; exit;
// TODO: Shutdown // TODO: Shutdown
} else {
$this->myTabs()->activate($type);
$this->view->title = $this->schemas[$type];
$this->view->schema = $schema;
$this->render('schema');
} }
$this
->addSchemaTabs($type)
->addTitle($this->schemas[$type])
->addDownloadAction()
->content()->add(Html::pre($schema));
}
protected function loadSchema($type)
{
return file_get_contents(
sprintf(
'%s/schema/%s.sql',
$this->Module()->getBasedir(),
$type
)
);
}
protected function addDownloadAction()
{
$this->actions()->add(
Link::create(
$this->translate('Download'),
$this->url()->with('format', 'sql'),
null,
[
'target' => '_blank',
'class' => 'icon-download',
]
)
);
return $this;
}
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;
} }
} }

View File

@ -1,10 +0,0 @@
<div class="controls">
<?= $tabs ?>
<h1><?= $this->escape($title) ?></h1>
<a href="<?= $this->url()->with('format', 'sql') ?>" target="_blank"><?= $this->icon('download') ?>Download</a>
</div>
<div class="content">
<pre><?= $this->escape($schema) ?></pre>
</div>