/** * Экшен для настройки БД: * * @return nothing **/ public function actionDbsettings() { $this->_markFinished('requirements'); $dbConfFile = Yii::app()->basePath . '/config/' . 'db.php'; $form = new InstallForm('dbSettings'); if (isset($this->session['InstallForm']['dbSettings'])) { $form->setAttributes($this->session['InstallForm']['dbSettings']); if ($form->validate() && $this->session['InstallForm']['dbSettingsStep'] === true) { $this->session['InstallForm'] = array_merge($this->session['InstallForm'], ['dbSettings' => $form->attributes, 'dbSettingsStep' => false, 'dbSettingsFile' => true]); $this->_setSession(); $this->_markFinished('dbsettings'); $this->redirect(['/install/default/modulesinstall']); } } if (Yii::app()->getRequest()->getIsPostRequest() && isset($_POST['InstallForm'])) { $form->setAttributes($_POST['InstallForm']); if ($form->validate()) { $socket = $form->socket == '' ? '' : 'unix_socket=' . $form->socket . ';'; $port = $form->port == '' ? '' : 'port=' . $form->port . ';'; $dbName = empty($form->createDb) ? 'dbname=' . $form->dbName : ''; $dbTypes = $form->getDbTypes(); $dbType = isset($dbTypes[$form->dbType]) ? $dbTypes[$form->dbType] : $dbTypes[InstallForm::DB_MYSQL]; $socket = $form->socket == '' ? '' : 'unix_socket=' . $form->socket . ';'; $port = $form->port == '' ? '' : 'port=' . $form->port . ';'; $connectionString = "{$dbType}:host={$form->host};{$port}{$socket}{$dbName}"; try { $connection = new CDbConnection($connectionString, $form->dbUser, $form->dbPassword); } catch (Exception $e) { $form->addError('', Yii::t('InstallModule.install', 'Couldn\'t connect to DB with these params!') . '<br />' . $connectionString . '<br />' . $e->getMessage()); Yii::log($e->__toString(), CLogger::LEVEL_ERROR); Yii::log($e->getTraceAsString(), CLogger::LEVEL_ERROR); } if ($form->createDb) { try { $sql = 'CREATE DATABASE ' . ($connection->schema instanceof CMysqlSchema ? ' `' . $form->dbName . '` CHARACTER SET=utf8' : $form->dbName); $connection->createCommand($sql)->execute(); $connectionString .= 'dbname=' . $form->dbName; } catch (Exception $e) { $form->addError('', Yii::t('InstallModule.install', 'Failed to create the database!') . '<br />' . $connectionString . '<br />' . $e->getMessage()); Yii::log($e->__toString(), CLogger::LEVEL_ERROR); Yii::log($e->getTraceAsString(), CLogger::LEVEL_ERROR); } } $connection->connectionString = $connectionString; try { $connection->username = $form->dbUser; $connection->password = $form->dbPassword; $connection->emulatePrepare = true; $connection->charset = 'utf8'; if (!$form->hasErrors()) { $connection->tablePrefix = $form->tablePrefix; Yii::app()->setComponent('db', $connection); $dbParams = ['class' => 'CDbConnection', 'connectionString' => $connectionString, 'username' => $form->dbUser, 'password' => $form->dbPassword, 'emulatePrepare' => true, 'charset' => 'utf8', 'enableParamLogging' => "{debug}", 'enableProfiling' => "{debug}", 'schemaCachingDuration' => 108000, 'tablePrefix' => $form->tablePrefix, 'pdoClass' => 'yupe\\extensions\\NestedPDO']; $dbConfString = "<?php\n return " . str_replace("'{debug}'", "defined('YII_DEBUG') && YII_DEBUG ? true : 0", var_export($dbParams, true)) . ";\n"; $fh = fopen($dbConfFile, 'w+'); if (!$fh) { $form->addError('', Yii::t('InstallModule.install', "Can not open file '{file}' in write mode!", ['{file}' => $dbConfFile])); } else { if (fwrite($fh, $dbConfString) && fclose($fh)) { $this->session['InstallForm'] = array_merge($this->session['InstallForm'], ['dbSettings' => $form->attributes, 'dbSettingsStep' => true, 'dbSettingsFile' => true]); $this->_setSession(); $this->redirect(['/install/default/dbsettings']); } else { $form->addError('', Yii::t('InstallModule.install', "There was an error writing to file '{file}'!", ['{file}' => $dbConfFile])); } } } } catch (Exception $e) { $form->addError('', Yii::t('InstallModule.install', 'Couldn\'t connect to DB!') . '<br />' . $connectionString . '<br />' . $e->getMessage()); Yii::log($e->__toString(), CLogger::LEVEL_ERROR); Yii::log($e->getTraceAsString(), CLogger::LEVEL_ERROR); } } } $result = false; if (file_exists($dbConfFile) && is_writable($dbConfFile)) { $result = true; } $this->render('_view', ['data' => ['model' => $form, 'result' => $result, 'file' => $dbConfFile]]); }