Installer: improvements

This commit is contained in:
Taras Machyshyn
2019-12-12 12:24:28 +02:00
parent bf7f0d5cbc
commit 93bfa83b46
7 changed files with 142 additions and 53 deletions

View File

@@ -95,8 +95,6 @@ if (preg_match('/"success":false/i', $result)) {
exit;
}
fwrite(\STDOUT, "Error: ". (!empty($resultData['errors']) ? print_r($resultData['errors'], true) : $resultData['errorMsg']) .".\n");
fwrite(\STDOUT, "Error: ". (!empty($resultData['errors']) ? print_r($resultData['errors'], true) : $resultData['errorMsg']) ."\n");
exit;
}
fwrite(\STDOUT, "Done.\n");

View File

@@ -51,14 +51,26 @@ class Installer
private $passwordHash;
protected $settingList = array(
protected $defaultSettings;
protected $permittedSettingList = array(
'dateFormat',
'timeFormat',
'timeZone',
'weekStart',
'defaultCurrency',
'smtpSecurity',
'language',
'thousandSeparator',
'decimalMark',
'smtpServer',
'smtpPort',
'smtpAuth',
'smtpSecurity',
'smtpUsername',
'smtpPassword',
'outboundEmailFromName',
'outboundEmailFromAddress',
'outboundEmailIsShared',
);
public function __construct()
@@ -311,6 +323,8 @@ class Installer
public function setPreferences($preferences)
{
$preferences = $this->normalizeSettingParams($preferences);
$currencyList = $this->getConfig()->get('currencyList', []);
if (isset($preferences['defaultCurrency']) && !in_array($preferences['defaultCurrency'], $currencyList)) {
@@ -457,30 +471,76 @@ class Installer
return $result;
}
public function getSettingDefaults()
public function getDefaultSettings()
{
$settingDefs = $this->app->getMetadata()->get('entityDefs.Settings.fields');
if (!$this->defaultSettings) {
$defaults = array();
$settingDefs = $this->app->getMetadata()->get('entityDefs.Settings.fields');
foreach ($this->settingList as $fieldName) {
$defaults = array();
foreach ($this->permittedSettingList as $fieldName) {
if (!isset($settingDefs[$fieldName])) continue;
if (!isset($settingDefs[$fieldName])) continue;
switch ($fieldName) {
case 'defaultCurrency':
$settingDefs['defaultCurrency']['options'] = $this->getCurrencyList();
break;
switch ($fieldName) {
case 'defaultCurrency':
$settingDefs['defaultCurrency']['options'] = $this->getCurrencyList();
break;
case 'language':
$settingDefs['language']['options'] = $this->getLanguageList(false);
break;
case 'language':
$settingDefs['language']['options'] = $this->getLanguageList(false);
break;
}
$defaults[$fieldName] = $this->translateSetting($fieldName, $settingDefs[$fieldName]);
}
$defaults[$fieldName] = $this->translateSetting($fieldName, $settingDefs[$fieldName]);
$this->defaultSettings = $defaults;
}
return $defaults;
return $this->defaultSettings;
}
protected function normalizeSettingParams(array $params)
{
$defaultSettings = $this->getDefaultSettings();
$normalizedParams = [];
foreach ($params as $name => $value) {
if (!isset($defaultSettings[$name])) continue;
$paramDefs = $defaultSettings[$name];
$paramType = isset($paramDefs['type']) ? $paramDefs['type'] : 'varchar';
switch ($paramType) {
case 'enumInt':
$value = (int) $value;
case 'enum':
if (isset($paramDefs['options']) && array_key_exists($value, $paramDefs['options'])) {
$normalizedParams[$name] = $value;
} else if (array_key_exists('default', $paramDefs)) {
$normalizedParams[$name] = $paramDefs['default'];
$GLOBALS['log']->warning('Incorrect value ['. $value .'] for Settings parameter ['. $name .']. Use default value ['. $paramDefs['default'] .'].');
}
break;
case 'bool':
$normalizedParams[$name] = (bool) $value;
break;
case 'int':
$normalizedParams[$name] = (int) $value;
break;
case 'varchar':
default:
$normalizedParams[$name] = $value;
break;
}
}
return $normalizedParams;
}
protected function translateSetting($name, array $settingDefs)

View File

@@ -25,23 +25,46 @@
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
************************************************************************/
ob_start();
$result = array('success' => false, 'errorMsg' => '');
if (!empty($_SESSION['install'])) {
$preferences = array(
'smtpServer' => $_SESSION['install']['smtpServer'],
'smtpPort' => $_SESSION['install']['smtpPort'],
'smtpAuth' => (empty($_SESSION['install']['smtpAuth']) || $_SESSION['install']['smtpAuth'] == 'false' || !$_SESSION['install']['smtpAuth'])? false : true,
'smtpSecurity' => $_SESSION['install']['smtpSecurity'],
'smtpUsername' => $_SESSION['install']['smtpUsername'],
'smtpPassword' => $_SESSION['install']['smtpPassword'],
'outboundEmailFromName' => $_SESSION['install']['outboundEmailFromName'],
'outboundEmailFromAddress' => $_SESSION['install']['outboundEmailFromAddress'],
'outboundEmailIsShared' => (empty($_SESSION['install']['smtpAuth']) || $_SESSION['install']['outboundEmailIsShared'] == 'false' || !$_SESSION['install']['smtpAuth'])? false : true,
);
$paramList = [
'smtpServer',
'smtpPort',
'smtpAuth',
'smtpSecurity',
'smtpUsername',
'smtpPassword',
'outboundEmailFromName',
'outboundEmailFromAddress',
'outboundEmailIsShared',
];
$preferences = [];
foreach ($paramList as $paramName) {
switch ($paramName) {
case 'smtpAuth':
$preferences['smtpAuth'] = (empty($_SESSION['install']['smtpAuth']) || $_SESSION['install']['smtpAuth'] == 'false' || !$_SESSION['install']['smtpAuth']) ? false : true;
break;
case 'outboundEmailIsShared':
$preferences['outboundEmailIsShared'] = (empty($_SESSION['install']['smtpAuth']) || $_SESSION['install']['outboundEmailIsShared'] == 'false' || !$_SESSION['install']['smtpAuth']) ? false : true;
break;
default:
if (array_key_exists($paramName, $_SESSION['install'])) {
$preferences[$paramName] = $_SESSION['install'][$paramName];
}
break;
}
}
$res = $installer->setPreferences($preferences);
if (!empty($res)) {
$result['success'] = true;

View File

@@ -25,22 +25,30 @@
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
************************************************************************/
ob_start();
$result = array('success' => false, 'errorMsg' => '');
if (!empty($_SESSION['install'])) {
$preferences = array(
'dateFormat' => $_SESSION['install']['dateFormat'],
'timeFormat' => $_SESSION['install']['timeFormat'],
'timeZone' => $_SESSION['install']['timeZone'],
'weekStart' => (int)$_SESSION['install']['weekStart'],
'defaultCurrency' => $_SESSION['install']['defaultCurrency'],
'thousandSeparator' => $_SESSION['install']['thousandSeparator'],
'decimalMark' => $_SESSION['install']['decimalMark'],
'language' => $_SESSION['install']['language'],
);
$paramList = [
'dateFormat',
'timeFormat',
'timeZone',
'weekStart',
'defaultCurrency',
'thousandSeparator',
'decimalMark',
'language',
];
$preferences = [];
foreach ($paramList as $paramName) {
if (array_key_exists($paramName, $_SESSION['install'])) {
$preferences[$paramName] = $_SESSION['install'][$paramName];
}
}
$res = $installer->setPreferences($preferences);
if (!empty($res)) {
$result['success'] = true;

View File

@@ -10,7 +10,7 @@
</label>
<div class="field field-dateFormat">
<select name="dateFormat" class="form-control main-element">
{foreach from=$settingsDefaults['dateFormat'].options item=lbl key=val}
{foreach from=$defaultSettings['dateFormat'].options item=lbl key=val}
{if $val == $fields['dateFormat'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}
@@ -27,7 +27,7 @@
</label>
<div class="field field-timeFormat">
<select name="timeFormat" class="form-control main-element">
{foreach from=$settingsDefaults['timeFormat'].options item=lbl key=val}
{foreach from=$defaultSettings['timeFormat'].options item=lbl key=val}
{if $val == $fields['timeFormat'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}
@@ -46,7 +46,7 @@
</label>
<div class="field field-timeZone">
<select name="timeZone" class="form-control main-element">
{foreach from=$settingsDefaults['timeZone'].options item=lbl key=val}
{foreach from=$defaultSettings['timeZone'].options item=lbl key=val}
{if $val == $fields['timeZone'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}
@@ -63,7 +63,7 @@
</label>
<div class="field field-weekStart">
<select name="weekStart" class="form-control main-element">
{foreach from=$settingsDefaults['weekStart'].options item=lbl key=val}
{foreach from=$defaultSettings['weekStart'].options item=lbl key=val}
{if $val == $fields['weekStart'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}
@@ -82,7 +82,7 @@
</label>
<div class="field field-defaultCurrency">
<select name="defaultCurrency" class="form-control main-element">
{foreach from=$settingsDefaults['defaultCurrency'].options item=lbl key=val}
{foreach from=$defaultSettings['defaultCurrency'].options item=lbl key=val}
{if $val == $fields['defaultCurrency'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}
@@ -120,7 +120,7 @@
</label>
<div class="field field-language">
<select name="language" class="form-control main-element">
{foreach from=$settingsDefaults['language'].options item=lbl key=val}
{foreach from=$defaultSettings['language'].options item=lbl key=val}
{if $val == $fields['language'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}

View File

@@ -70,7 +70,7 @@
</label>
<div class="field field-smtpSecurity">
<select name="smtpSecurity" class="form-control main-element">
{foreach from=$settingsDefaults['smtpSecurity'].options item=lbl key=val}
{foreach from=$defaultSettings['smtpSecurity'].options item=lbl key=val}
{if $val == $fields['smtpSecurity'].value}
<option selected="selected" value="{$val}">{$lbl}</option>
{else}

View File

@@ -142,13 +142,13 @@ switch ($action) {
break;
case 'step4':
$settingsDefaults = $installer->getSettingDefaults();
$smarty->assign("settingsDefaults", $settingsDefaults);
$defaultSettings = $installer->getDefaultSettings();
$smarty->assign("defaultSettings", $defaultSettings);
break;
case 'step5':
$settingsDefaults = $installer->getSettingDefaults();
$smarty->assign("settingsDefaults", $settingsDefaults);
$defaultSettings = $installer->getDefaultSettings();
$smarty->assign("defaultSettings", $defaultSettings);
break;
}