diff --git a/install/core/Installer.php b/install/core/Installer.php index 45bb792179..d764e15e2a 100644 --- a/install/core/Installer.php +++ b/install/core/Installer.php @@ -148,7 +148,7 @@ class Installer */ public function saveData($database, $language) { - $initData = include('install/core/init/config.php'); + $initData = include('install/core/afterInstall/config.php'); $siteUrl = $this->getSystemHelper()->getBaseUrl(); @@ -208,7 +208,7 @@ class Installer protected function createRecords() { - $records = include('install/core/init/records.php'); + $records = include('install/core/afterInstall/records.php'); $result = true; foreach ($records as $entityName => $recordList) { @@ -269,8 +269,6 @@ class Installer $result = $this->createRecord('User', $user); - $result &= $this->createRecords(); - return $result; } @@ -313,9 +311,16 @@ class Installer public function setSuccess() { + $this->auth(); + + /** afterInstall scripts */ + $result = $this->createRecords(); + $result &= $this->executeQueries(); + /** END: afterInstall scripts */ + $config = $this->app->getContainer()->get('config'); $config->set('isInstalled', true); - $result = $config->save(); + $result &= $config->save(); return $result; } @@ -377,5 +382,21 @@ class Installer return $this->getContainer()->get('scheduledJob')->getSetupMessage(); } + protected function executeQueries() + { + $queries = include('install/core/afterInstall/queries.php'); + + $pdo = $this->getEntityManager()->getPDO(); + + $result = true; + + foreach ($queries as $query) { + $sth = $pdo->prepare($query); + $result =& $sth->execute(); + } + + return $result; + } + } diff --git a/install/core/SystemHelper.php b/install/core/SystemHelper.php index f9d8b5f42d..92f0587e49 100644 --- a/install/core/SystemHelper.php +++ b/install/core/SystemHelper.php @@ -76,7 +76,7 @@ class SystemHelper extends \Espo\Core\Utils\System return $result; } - public function checkDbConnection($hostName, $port ,$dbUserName, $dbUserPass, $dbName, $dbDriver = 'pdo_mysql') + public function checkDbConnection($hostName, $port, $dbUserName, $dbUserPass, $dbName, $dbDriver = 'pdo_mysql', $isCreateDatabase = true) { $result['success'] = true; @@ -104,6 +104,28 @@ class SystemHelper extends \Espo\Core\Utils\System $result['errors']['dbConnect']['errorMsg'] = $e->getMessage(); $result['success'] = false; } + + /** try to create a database */ + if ($isCreateDatabase && !$result['success'] && $result['errors']['dbConnect']['errorCode'] == '1049') { + + $dsn = "mysql:host={$hostName};" . ((!empty($port)) ? "port={$port}" : ''); + $pdo = new PDO($dsn, $dbUserName, $dbUserPass); + + $isCreated = true; + try { + $pdo->query("CREATE DATABASE IF NOT EXISTS `$dbName`"); + } catch (PDOException $e) { + $isCreated = false; + } + + if ($isCreated) { + return $this->checkDbConnection($hostName, $port, $dbUserName, $dbUserPass, $dbName, $dbDriver, false); + } + } + /** END: try to create a database */ + + + break; } diff --git a/install/core/init/config.php b/install/core/afterInstall/config.php similarity index 100% rename from install/core/init/config.php rename to install/core/afterInstall/config.php diff --git a/install/core/afterInstall/queries.php b/install/core/afterInstall/queries.php new file mode 100644 index 0000000000..6a982e4020 --- /dev/null +++ b/install/core/afterInstall/queries.php @@ -0,0 +1,25 @@ + array( + 0 => array( + 'name' => 'Case-to-Email auto-reply', + 'subject' => 'Case has been created', + 'body' => '

{Person.name},

Case \'{Case.name}\' has been created with number {Case.number} and assigned to {User.name}.

', + 'isHtml ' => '1', + ), + ), + 'ScheduledJob' => array( + 0 => array( + 'name' => 'Check Inbound Emails', + 'job' => 'CheckInboundEmails', + 'status' => 'Active', + 'scheduling' => '/10 * * * *', + ), + ), +); \ No newline at end of file diff --git a/install/core/init/records.php b/install/core/init/records.php deleted file mode 100644 index 2e5530b8ae..0000000000 --- a/install/core/init/records.php +++ /dev/null @@ -1,20 +0,0 @@ - array( - 0 => array( - 'name' => 'Case-to-Email auto-reply', - 'subject' => 'Case has been created', - 'body' => '

Hi {Person.name}

Case \'{Case.name}\' has been created with number \'{Case.number}\' and assigned to {User.name}.

', - 'isHtml ' => '1', - ), - ), - 'ScheduledJob' => array( - 0 => array( - 'name' => 'Check Inbound Emails', - 'job' => 'CheckInboundEmails', - 'status' => 'Active', - 'scheduling' => '/10 * * * *', - ), - ), -); \ No newline at end of file