Skip to content

EspoCRM – Open Source CRM Application

language list fix

Yuri Kuznetsovc1a23a9d49b0
diff --git a/application/Espo/Core/Utils/DataUtil.php b/application/Espo/Core/Utils/DataUtil.php
index e05d80bda2..a8c6db0a48 100644
--- a/application/Espo/Core/Utils/DataUtil.php
+++ b/application/Espo/Core/Utils/DataUtil.php
@@ -55,9 +55,9 @@ class DataUtil
foreach ($unsetList as $unsetItem) {
if (is_array($unsetItem)) {
- $arr = $unsetItem;
+ $path = $unsetItem;
} else if (is_string($unsetItem)) {
- $arr = explode('.', $unsetItem);
+ $path = explode('.', $unsetItem);
} else {
throw new LogicException('Bad unset parameter');
}
@@ -67,8 +67,8 @@ class DataUtil
$elementArr = [];
$elementArr[] = &$pointer;
- foreach ($arr as $i => $key) {
- if ($i === count($arr) - 1) {
+ foreach ($path as $i => $key) {
+ if ($i === count($path) - 1) {
if (is_array($pointer)) {
if (array_key_exists($key, $pointer)) {
unset($pointer[$key]);
@@ -94,7 +94,7 @@ class DataUtil
$previous =& $elementArr[$j - 1];
if (is_object($previous)) {
- $key = $arr[$j - 1];
+ $key = $path[$j - 1];
unset($previous->$key);
}
}
@@ -106,6 +106,10 @@ class DataUtil
if (is_array($pointer)) {
$pointer = &$pointer[$key];
} else if (is_object($pointer)) {
+ if (!property_exists($pointer, $key)) {
+ break;
+ }
+
$pointer = &$pointer->$key;
}
diff --git a/application/Espo/Tools/LabelManager/LabelManager.php b/application/Espo/Tools/LabelManager/LabelManager.php
index 0621fe0e34..676f5666f1 100644
--- a/application/Espo/Tools/LabelManager/LabelManager.php
+++ b/application/Espo/Tools/LabelManager/LabelManager.php
@@ -229,8 +229,21 @@ class LabelManager implements
'noFallback' => true,
]);
+ $languageNoCustomObj = $this->injectableFactory->createWith(Language::class, [
+ 'language' => $language,
+ 'noFallback' => true,
+ 'noCustom' => true,
+ ]);
+
+ $languageMainObj = $this->injectableFactory->createWith(Language::class, [
+ 'language' => $language,
+ ]);
+
$customData = $languageObj->getScopeCustom($scope) ?? (object) [];
+ $listPaths = [];
+ $lists = [];
+
foreach ($labels as $key => $value) {
$arr = explode('[.]', $key);
$category = $arr[0];
@@ -243,18 +256,76 @@ class LabelManager implements
DataUtil::unsetByKey($customData, [[$category, $name]], true);
/** @var stdClass $customData */
}
- } else if (count($arr) === 3) {
- $attribute = $arr[2];
- if ($value !== '') {
- DataUtil::setByPath($customData, [$category, $name, $attribute], $value);
- } else {
- DataUtil::unsetByKey($customData, [[$category, $name, $attribute]], true);
- /** @var stdClass $customData */
+ continue;
+ }
+
+ if (count($arr) !== 3) {
+ continue;
+ }
+
+ $attribute = $arr[2];
+
+ $stringPath = "$category.$name";
+
+ $setValue = $lists[$stringPath] ?? $languageMainObj->get([$scope, $category, $name]);
+
+ if (is_array($setValue) && array_is_list($setValue)) {
+ if (!in_array($stringPath, $listPaths)) {
+ $listPaths[] = $stringPath;
}
+
+ $index = intval($attribute);
+
+ $list = $setValue;
+
+ if ($value === '') {
+ $originalList = $languageNoCustomObj->get([$scope, $category, $name]);
+
+ if (!array_is_list($originalList)) {
+ throw new RuntimeException("Not a list.");
+ }
+
+ if (!array_key_exists($index, $originalList)) {
+ throw new RuntimeException("No value in original list.");
+ }
+
+ $value = $originalList[$index];
+ }
+
+ if (!array_key_exists($index, $list)) {
+ throw new RuntimeException("No value in list.");
+ }
+
+ $list[$index] = $value;
+
+ DataUtil::setByPath($customData, [$category, $name], $list);
+ /** @var stdClass $customData */
+
+ $lists[$stringPath] = $list;
+
+ continue;
+ }
+
+ if ($value !== '') {
+ DataUtil::setByPath($customData, [$category, $name, $attribute], $value);
+ } else {
+ DataUtil::unsetByKey($customData, [[$category, $name, $attribute]], true);
+ /** @var stdClass $customData */
}
}
+ foreach ($lists as $path => $list) {
+ $originalList = $languageNoCustomObj->get("$scope.$path");
+
+ if ($originalList === $list) {
+ DataUtil::unsetByKey($customData, [$path], true);
+ /** @var stdClass $customData */
+ }
+ }
+
+ //die;
+
$languageObj->saveScopeCustom($scope, $customData);
return $this->getScopeData($language, $scope);
diff --git a/tests/unit/Espo/Core/Utils/DataUtilTest.php b/tests/unit/Espo/Core/Utils/DataUtilTest.php
index eb2ec71f39..97c70adcf5 100644
--- a/tests/unit/Espo/Core/Utils/DataUtilTest.php
+++ b/tests/unit/Espo/Core/Utils/DataUtilTest.php
@@ -128,6 +128,36 @@ class DataUtilTest extends \PHPUnit\Framework\TestCase
$this->assertEquals($expectedResultData, $data1);
}
+ public function testUnsetRemoveEmpty(): void
+ {
+ $data = (object) [
+ 'scopeNames' => [
+ 'a' => 'test',
+ 'b' => 'test',
+ ],
+ 'scopeNamesPlural' => [
+ 'a' => 'test',
+ 'b' => 'test',
+ ],
+ ];
+
+ DataUtil::unsetByKey($data, [['lists', 'k']], true);
+
+ $this->assertEquals(
+ (object) [
+ 'scopeNames' => [
+ 'a' => 'test',
+ 'b' => 'test',
+ ],
+ 'scopeNamesPlural' => [
+ 'a' => 'test',
+ 'b' => 'test',
+ ],
+ ],
+ $data
+ );
+ }
+
public function testUnsetByValue()
{
$data = json_decode('{