/**
* Factory for settings db host.
* @param class Translator
* @param callable
* @return Form
*/
public function databaseHostFactory($translator, callable $onSuccess)
{
$form = $this->forms->create($translator);
$form->addText('host', 'install.db.host')->setRequired('install.form.empty');
$form->addText('user', 'install.db.user')->setRequired('install.form.empty');
$form->addText('password', 'install.db.pass');
$form->addText('database', 'install.db.name')->setRequired('install.form.empty');
$form->addText('prefix', 'install.db.prefix')->setAttribute('placeholder', 'ns_');
// Database drivers.
$drivers = ['mysql' => 'MySQL', 'mysqli' => 'MySQLi'];
$form->addSelect('driver', 'install.db.driver', $drivers)->setRequired();
$form->addSubmit('send', 'install.db.send');
$form->onSuccess[] = function (Form $form, $values) use($onSuccess) {
try {
// Testing database connection.
if (\dibi::connect($values)) {
// Parameters for generate config neon file.
$arr = ['extensions' => ['dibi' => 'Dibi\\Bridges\\Nette\\DibiExtension22'], 'dibi' => ['host' => $values->host, 'username' => $values->user, 'password' => $values->password, 'database' => $values->database, 'driver' => $values->driver, 'lazy' => TRUE, 'substitutes' => ['prefix' => $values->prefix]]];
// Generate and save the configuration file
$this->loader->save($arr, $this->dirs->getAppDir() . '/modules/app.db.neon');
// Removing the old cache for updating the configuration file.
FileSystem::delete($this->dirs->getTempDir() . '/cache/Nette.Configurator');
// Save the installation step into the cache.
$this->steps->setToCache(Steps::Step1, rand(1, 9));
// Save db prefix.
if ($values->prefix) {
$this->sessions->getSessionSection()->prefix = $values->prefix;
}
}
} catch (\Dibi\Exception $e) {
// Server database type error.
if ($e->getCode() == 0) {
$form->addError('install.db.driver.catch');
// Host server not found.
} elseif ($e->getCode() == 2002) {
$form->addError('install.db.host.catch');
// The user or password was not verified.
} elseif ($e->getCode() == 1045) {
$form->addError('install.db.auth.catch');
// The database name was not found
} elseif ($e->getCode() == 1049) {
$form->addError('install.db.name.catch');
}
return;
}
$onSuccess();
};
return $form;
}