Commit e5de98d6 authored by Tino Goratsch's avatar Tino Goratsch

improved the UX of the installer to automatically retrieve the available databases

parent a3a46173
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Installer\Modules\Install\Controller\Install;
use ACP3\Installer\Core\Controller\AbstractInstallerAction;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class AvailableDatabases
* @package ACP3\Installer\Modules\Install\Controller\Install
*/
class AvailableDatabases extends AbstractInstallerAction
{
/**
* @return JsonResponse
*/
public function execute()
{
$availableDatabases = [];
if (!$this->request->getPost()->isEmpty()) {
$hostName = $this->request->getPost()->get('db_host', '');
$userName = $this->request->getPost()->get('db_user', '');
$password = $this->request->getPost()->get('db_password', '');
$conn = $this->getDatabaseConnection($hostName, $userName, $password);
if ($conn instanceof Connection) {
$availableDatabases = $this->retrieveAvailableDatabases($conn);
}
}
return new JsonResponse($availableDatabases);
}
/**
* @param string $hostname
* @param string $userName
* @param string $password
* @return \Doctrine\DBAL\Connection|null
*/
private function getDatabaseConnection($hostname, $userName, $password)
{
try {
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = [
'user' => $userName,
'password' => $password,
'host' => $hostname,
'driver' => 'pdo_mysql',
'charset' => 'utf8'
];
return \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
} catch (DBALException $e) {
return null;
}
}
/**
* @param Connection $conn
* @return array
*/
private function retrieveAvailableDatabases(Connection $conn)
{
$availableDatabases = [];
$databases = $conn->fetchAll('SHOW DATABASES');
foreach ($databases as $database) {
$availableDatabases[] = $database['Database'];
}
return array_values(array_diff($availableDatabases, $this->getMySQLDefaultDatabases()));
}
/**
* @return array
*/
private function getMySQLDefaultDatabases()
{
return ['information_schema', 'performance_schema', 'mysql', 'test'];
}
}
/*
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
jQuery(document).ready(function ($) {
var ajaxUrl = $('#config-form').data('available-databases-url'),
$dbName = $('#db-name'),
$formFields = $('#db-host, #db-user, #db-password'),
$formGroup = $formFields.closest('.form-group');
$formFields.change(function () {
$dbName
.prop('disabled', true)
.find('option').not(':first').remove();
$formGroup
.removeClass('has-success')
.removeClass('has-error');
$.post(
ajaxUrl,
{
"db_host": $('#db-host').val(),
"db_user": $('#db-user').val(),
"db_password": $('#db-password').val()
},
function (response) {
$dbName.prop('disabled', false);
if (response.length > 0) {
for (var i = 0; i < response.length; ++i) {
$dbName.append('<option value="' + response[i] + '">' + response[i] + '</option>');
}
$formGroup.addClass('has-success');
} else {
$formGroup.addClass('has-error');
}
}
);
});
});
......@@ -4,7 +4,14 @@
{if isset($error_msg)}
{$error_msg}
{/if}
<form action="{uri args="install/install"}" method="post" accept-charset="UTF-8" class="form-horizontal" id="config-form" data-ajax-form="true" data-ajax-form-loading-text="{lang t="install|loading_please_wait"}">
<form action="{uri args="install/install"}"
method="post"
accept-charset="UTF-8"
class="form-horizontal"
id="config-form"
data-available-databases-url="{uri args="install/install/available_databases"}"
data-ajax-form="true"
data-ajax-form-loading-text="{lang t="install|loading_please_wait"}">
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active"><a href="#tabs-1" data-toggle="tab">{lang t="install|db_connection_settings"}</a></li>
......@@ -47,7 +54,9 @@
<label for="db-name" class="col-sm-2 control-label required">{lang t="install|db_name"}</label>
<div class="col-sm-10">
<input class="form-control" type="text" name="db_name" id="db-name" value="{$form.db_name}" required>
<select class="form-control" name="db_name" id="db-name" required>
<option value="">{lang t="install|please_select"}</option>
</select>
</div>
</div>
<div class="form-group">
......@@ -142,5 +151,6 @@
</form>
{javascripts}
<script type="text/javascript" src="{$ROOT_DIR}ACP3/Modules/ACP3/System/Resources/Assets/js/forms.js"></script>
<script type="text/javascript" src="{$INSTALLER_ROOT_DIR}Installer/Modules/Install/Resources/Assets/js/available_databases.js"></script>
{/javascripts}
{/block}
......@@ -13,6 +13,10 @@ services:
- '@core.context.install'
- '@install.helpers.requirements'
install.controller.install.install.available_databases:
class: ACP3\Installer\Modules\Install\Controller\Install\AvailableDatabases
parent: core.controller.install
install.controller.install.install.index:
class: ACP3\Installer\Modules\Install\Controller\Install\Index
arguments:
......@@ -22,4 +26,3 @@ services:
- '@core.helpers.date'
- '@install.helpers.install'
- '@install.validator'
......@@ -82,9 +82,10 @@
<item key="pages_sidebar">Sidebar</item>
<item key="pdo_extension">PDO-Erweiterung</item>
<item key="php_date_function">
<![CDATA[siehe <a href="http://www.php.net/manual/function.date.php" onclick="window.open(this.href); return false">date</a>-Funktion von PHP.]]></item>
<![CDATA[siehe <a href="http://www.php.net/manual/function.date.php" target="_blank">date</a>-Funktion von PHP.]]></item>
<item key="php_settings">PHP Einstellungen</item>
<item key="php_version">PHP Version</item>
<item key="please_select">Bitte auswählen</item>
<item key="pwd">Passwort</item>
<item key="pwd_repeat">Passwort wiederholen</item>
<item key="register_globals">register_globals</item>
......
......@@ -80,9 +80,10 @@
<item key="pages_sidebar">Sidebar</item>
<item key="pdo_extension">PDO Extension</item>
<item key="php_date_function">
<![CDATA[see <a href="http://www.php.net/manual/function.date.php" onclick="window.open(this.href); return false">date</a> function of PHP.]]></item>
<![CDATA[see <a href="http://www.php.net/manual/function.date.php" target="_blank">date</a> function of PHP.]]></item>
<item key="php_settings">PHP settings</item>
<item key="php_version">PHP version</item>
<item key="please_select">Please select</item>
<item key="pwd">Password</item>
<item key="pwd_repeat">Repeat password</item>
<item key="register_globals">register_globals</item>
......
......@@ -7,6 +7,7 @@
namespace ACP3\Installer\Modules\Install\Validation\ValidationRules;
use ACP3\Core\Validation\ValidationRules\AbstractValidationRule;
use Doctrine\DBAL\DBALException;
/**
* Class DatabaseConnectionValidationRule
......@@ -40,7 +41,7 @@ class DatabaseConnectionValidationRule extends AbstractValidationRule
$db->query('USE `' . $data[$dbName] . '`');
return true;
} catch (\Exception $e) {
} catch (DBALException $e) {
return false;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment