N°6759 - Factorize code in config

This commit is contained in:
Timothee
2025-08-20 14:48:58 +02:00
parent f8e56eda45
commit 9097c8cef1
2 changed files with 75 additions and 73 deletions

View File

@@ -18,33 +18,8 @@ use Combodo\iTop\Config\Validator\iTopConfigSyntaxValidator;
require_once(APPROOT.'application/startup.inc.php');
const CONFIG_ERROR = 0;
const CONFIG_WARNING = 1;
const CONFIG_INFO = 2;
function CheckAsyncTasksRetryConfig(Config $oTempConfig, iTopWebPage $oP)
{
$iWarnings = 0;
foreach (get_declared_classes() as $sPHPClass) {
$oRefClass = new ReflectionClass($sPHPClass);
if ($oRefClass->isSubclassOf('AsyncTask') && !$oRefClass->isAbstract()) {
$aMessages = AsyncTask::CheckRetryConfig($oTempConfig, $oRefClass->getName());
if (count($aMessages) !== 0) {
foreach ($aMessages as $sMessage) {
$oAlert = AlertUIBlockFactory::MakeForWarning('', $sMessage);
$oP->AddUiBlock($oAlert);
$iWarnings++;
}
}
}
}
return $iWarnings;
}
/////////////////////////////////////////////////////////////////////
// Main program
//
@@ -59,50 +34,45 @@ $oP = new iTopConfigEditorPage();
try {
$sOperation = utils::ReadParam('operation', '');
$iEditorTopMargin = 2;
if (UserRights::IsAdministrator() && ExecutionKPI::IsEnabled()) {
$iEditorTopMargin += 6;
}
if (MetaModel::GetConfig()->Get('demo_mode')) {
throw new Exception(Dict::S('config-not-allowed-in-demo'), CONFIG_INFO);
throw new Exception(Dict::S('config-not-allowed-in-demo'), iTopConfigEditorPage::CONFIG_INFO);
}
if (MetaModel::GetModuleSetting('itop-config', 'config_editor', '') == 'disabled') {
throw new Exception(Dict::S('config-interactive-not-allowed'), CONFIG_WARNING);
throw new Exception(Dict::S('config-interactive-not-allowed'), iTopConfigEditorPage::CONFIG_WARNING);
}
$sConfigFile = APPROOT.'conf/'.utils::GetCurrentEnvironment().'/config-itop.php';
$iEditorTopMargin += 9;
$sConfigContent = file_get_contents($sConfigFile);
$sConfigChecksum = md5($sConfigContent);
$sConfig = str_replace("\r\n", "\n", $sConfigContent);
$sOriginalConfig = $sConfig;
if (!empty($sOperation)) {
$iEditorTopMargin += 5;
$sConfig = utils::ReadParam('new_config', '', false, 'raw_data');
}
try {
if ($sOperation == 'revert') {
throw new Exception(Dict::S('config-reverted'), CONFIG_WARNING);
throw new Exception(Dict::S('config-reverted'), iTopConfigEditorPage::CONFIG_WARNING);
}
if ($sOperation == 'save') {
$sTransactionId = utils::ReadParam('transaction_id', '', false, 'transaction_id');
if (!utils::IsTransactionValid($sTransactionId, true)) {
throw new Exception(Dict::S('config-error-transaction'), CONFIG_ERROR);
throw new Exception(Dict::S('config-error-transaction'), iTopConfigEditorPage::CONFIG_ERROR);
}
$sChecksum = utils::ReadParam('checksum');
if ($sChecksum !== $sConfigChecksum) {
throw new Exception(Dict::S('config-error-file-changed'), CONFIG_ERROR);
throw new Exception(Dict::S('config-error-file-changed'), iTopConfigEditorPage::CONFIG_ERROR);
}
if ($sConfig === $sOriginalConfig) {
throw new Exception(Dict::S('config-no-change'), CONFIG_INFO);
throw new Exception(Dict::S('config-no-change'), iTopConfigEditorPage::CONFIG_INFO);
}
Config::Validate($sConfig); // throws exceptions
@@ -132,7 +102,7 @@ try {
}
$oP->AddUiBlock($oAlert);
$iWarnings = CheckAsyncTasksRetryConfig($oTempConfig, $oP);
$oP->CheckAsyncTasksRetryConfig($oTempConfig);
// Read the config from disk after save
$sConfigContent = file_get_contents($sConfigFile);
@@ -145,32 +115,11 @@ try {
$oP->AddAlertFromException($e);
}
// (remove EscapeHtml) N°5914 - Wrong encoding in modules configuration editor
$oP->AddUiBlock(new Html('<p>'.Dict::S('config-edit-intro').'</p>'));
$oP->AddEditor($sOriginalConfig, $sOriginalConfig);
$oForm = new Form();
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('operation', 'save', 'operation'));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('transaction_id', utils::GetNewTransactionId()));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('checksum', $sConfigChecksum));
//--- Cancel button
$oCancelButton = ButtonUIBlockFactory::MakeForCancel(Dict::S('config-cancel'), 'cancel_button', null, true, 'cancel_button');
$oCancelButton->SetOnClickJsCode("return ResetConfig();");
$oForm->AddSubBlock($oCancelButton);
//--- Submit button
$oSubmitButton = ButtonUIBlockFactory::MakeForPrimaryAction(Dict::S('config-apply'), null, Dict::S('config-apply'), true, 'submit_button');
$oForm->AddSubBlock($oSubmitButton);
//--- Config editor
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('prev_config', $sOriginalConfig, 'prev_config'));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('new_config', $sOriginalConfig));
$oForm->AddHtml("<div id =\"new_config\" style=\"position: absolute; top: ".$iEditorTopMargin."em; bottom: 0; left: 5px; right: 5px;\"></div>");
$oP->AddUiBlock($oForm);
$oP->AddConfigScripts();
} catch (Exception $e) {
$oAlert = $oP->AddAlertFromException($e);
$oP->AddAlertFromException($e);
}
$oP->output();

View File

@@ -1,7 +1,9 @@
<?php
namespace Combodo\iTop\Application\WebPage;
use AsyncTask;
use Dict;
use ReflectionClass;
use utils;
use Combodo\iTop\Application\UI\Base\Component\Alert\Alert;
use Combodo\iTop\Application\UI\Base\Component\Alert\AlertUIBlockFactory;
@@ -16,6 +18,10 @@ use Combodo\iTop\Config\Validator\iTopConfigSyntaxValidator;
class iTopConfigEditorPage extends iTopWebPage
{
const CONFIG_ERROR = 0;
const CONFIG_WARNING = 1;
const CONFIG_INFO = 2;
public function __construct()
{
parent::__construct(Dict::S('modules-config-edit-title'));
@@ -31,17 +37,11 @@ class iTopConfigEditorPage extends iTopWebPage
public function AddAlertFromException(\Exception $e)
{
switch ($e->getCode()) {
case CONFIG_WARNING:
$oAlert = AlertUIBlockFactory::MakeForWarning('', $e->getMessage());
break;
case CONFIG_INFO:
$oAlert = AlertUIBlockFactory::MakeForInformation('', $e->getMessage());
break;
case CONFIG_ERROR:
default:
$oAlert = AlertUIBlockFactory::MakeForDanger('', $e->getMessage());
}
$oAlert = match ($e->getCode()) {
self::CONFIG_WARNING => AlertUIBlockFactory::MakeForWarning('', $e->getMessage()),
self::CONFIG_INFO => AlertUIBlockFactory::MakeForInformation('', $e->getMessage()),
default => AlertUIBlockFactory::MakeForDanger('', $e->getMessage()),
};
$this->AddUiBlock($oAlert);
}
@@ -117,7 +117,12 @@ var EditorUtils = (function() {
})();
JS
);
$this->add_ready_script(<<<'JS'
$this->add_ready_script(<<<JS
let oSubmitButton = document.getElementById('submit_button');
let iBottomPosition = oSubmitButton.offsetTop + oSubmitButton.offsetHeight + 10;
document.getElementById('new_config').style.top = iBottomPosition+"px";
console.log('Set config '+iBottomPosition+' from top');
var editor = ace.edit("new_config");
var configurationSource = $('input[name="new_config"]');
@@ -176,4 +181,52 @@ JS
);
}
public function CheckAsyncTasksRetryConfig(\Config $oTempConfig)
{
foreach (get_declared_classes() as $sPHPClass) {
$oRefClass = new ReflectionClass($sPHPClass);
if ($oRefClass->isSubclassOf('AsyncTask') && !$oRefClass->isAbstract()) {
$aMessages = AsyncTask::CheckRetryConfig($oTempConfig, $oRefClass->getName());
if (count($aMessages) !== 0) {
foreach ($aMessages as $sMessage) {
$oAlert = AlertUIBlockFactory::MakeForWarning('', $sMessage);
$this->AddUiBlock($oAlert);
}
}
}
}
}
public function AddEditor(string $sPrevConfig, string $sNewConfig) {
$sConfigChecksum = md5($sPrevConfig);
// (remove EscapeHtml) N°5914 - Wrong encoding in modules configuration editor
$this->AddUiBlock(new Html('<p>'.Dict::S('config-edit-intro').'</p>'));
$oForm = new Form();
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('operation', 'save', 'operation'));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('transaction_id', utils::GetNewTransactionId()));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('checksum', $sConfigChecksum));
//--- Cancel button
$oCancelButton = ButtonUIBlockFactory::MakeForCancel(Dict::S('config-cancel'), 'cancel_button', null, true, 'cancel_button');
$oCancelButton->SetOnClickJsCode("return ResetConfig();");
$oForm->AddSubBlock($oCancelButton);
//--- Submit button
$oSubmitButton = ButtonUIBlockFactory::MakeForPrimaryAction(Dict::S('config-apply'), null, Dict::S('config-apply'), true, 'submit_button');
$oForm->AddSubBlock($oSubmitButton);
//--- Config editor
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('prev_config', $sPrevConfig, 'prev_config'));
$oForm->AddSubBlock(InputUIBlockFactory::MakeForHidden('new_config', $sNewConfig));
$oForm->AddHtml("<div id =\"new_config\" style=\"position: absolute; top: 125px; bottom: 0; left: 5px; right: 5px;\"></div>");
$this->AddUiBlock($oForm);
$this->AddConfigScripts();
}
}