mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-27 14:36:05 +00:00
Fix duplicate dashboard template
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2026 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Classes\FieldDuplicators\DashboardTemplate;
|
||||
|
||||
use Espo\Core\Record\Duplicator\FieldDuplicator;
|
||||
use Espo\Core\Utils\ObjectUtil;
|
||||
use Espo\Core\Utils\Util;
|
||||
use Espo\Entities\DashboardTemplate;
|
||||
use Espo\ORM\Entity;
|
||||
use UnexpectedValueException;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
class Layout implements FieldDuplicator
|
||||
{
|
||||
public function duplicate(Entity $entity, string $field): stdClass
|
||||
{
|
||||
if (!$entity instanceof DashboardTemplate) {
|
||||
throw new UnexpectedValueException();
|
||||
}
|
||||
|
||||
$layout = $entity->getLayoutRaw();
|
||||
$options = $entity->getDashletsOptionsRaw();
|
||||
|
||||
if (!$layout) {
|
||||
return (object) [];
|
||||
}
|
||||
|
||||
$copyLayout = [];
|
||||
$idMap = [];
|
||||
|
||||
foreach ($layout as $tab) {
|
||||
$copyLayout[] = $this->copyTab($tab, $idMap);
|
||||
}
|
||||
|
||||
$copyOptions = (object) [];
|
||||
|
||||
foreach (get_object_vars($options) as $id => $item) {
|
||||
$copyId = $idMap[$id] ?? null;
|
||||
|
||||
if (!is_string($copyId)) {
|
||||
throw new LogicException();
|
||||
}
|
||||
|
||||
if (!$item instanceof stdClass) {
|
||||
throw new RuntimeException("Bad dashboard options.");
|
||||
}
|
||||
|
||||
$copyOptions->$copyId = ObjectUtil::clone($item);
|
||||
}
|
||||
|
||||
return (object) [
|
||||
DashboardTemplate::FIELD_LAYOUT => $copyLayout,
|
||||
DashboardTemplate::FIELD_DASHLETS_OPTIONS => $copyOptions,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $idMap
|
||||
*/
|
||||
private function copyTab(stdClass $tab, array &$idMap): stdClass
|
||||
{
|
||||
$copy = ObjectUtil::clone($tab);
|
||||
|
||||
$copy->id = Util::generateId();
|
||||
|
||||
$layout = $copy->layout ?? [];
|
||||
|
||||
foreach ($layout as $item) {
|
||||
if (!$item instanceof stdClass) {
|
||||
throw new RuntimeException("Bad layout dashlet definition.");
|
||||
}
|
||||
|
||||
$id = $item->id ?? null;
|
||||
|
||||
if (!is_string($id)) {
|
||||
throw new RuntimeException("No string ID in layout dashlet definition.");
|
||||
}
|
||||
|
||||
$item->id = Util::generateId();
|
||||
|
||||
$idMap[$id] = $item->id;
|
||||
}
|
||||
|
||||
return $copy;
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,31 @@
|
||||
|
||||
namespace Espo\Entities;
|
||||
|
||||
class DashboardTemplate extends \Espo\Core\ORM\Entity
|
||||
use Espo\Core\ORM\Entity;
|
||||
use stdClass;
|
||||
|
||||
class DashboardTemplate extends Entity
|
||||
{
|
||||
public const ENTITY_TYPE = 'DashboardTemplate';
|
||||
public const string ENTITY_TYPE = 'DashboardTemplate';
|
||||
|
||||
public const string FIELD_LAYOUT = 'layout';
|
||||
public const string FIELD_DASHLETS_OPTIONS = 'dashletsOptions';
|
||||
|
||||
/**
|
||||
* @return array<int, mixed>
|
||||
* @since 10.0.0
|
||||
*/
|
||||
public function getLayoutRaw(): array
|
||||
{
|
||||
/** @var array<int, mixed> */
|
||||
return $this->get(self::FIELD_LAYOUT) ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 10.0.0
|
||||
*/
|
||||
public function getDashletsOptionsRaw(): stdClass
|
||||
{
|
||||
return $this->get(self::FIELD_DASHLETS_OPTIONS) ?? (object) [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,13 @@
|
||||
"type": "jsonArray",
|
||||
"view": "views/settings/fields/dashboard-layout",
|
||||
"inlineEditDisabled": true,
|
||||
"required": true
|
||||
"required": true,
|
||||
"duplicatorClassName": "Espo\\Classes\\FieldDuplicators\\DashboardTemplate\\Layout"
|
||||
},
|
||||
"dashletsOptions": {
|
||||
"type": "jsonObject",
|
||||
"utility": true
|
||||
"utility": true,
|
||||
"duplicateIgnore": true
|
||||
},
|
||||
"createdAt": {
|
||||
"type": "datetime",
|
||||
|
||||
78
tests/unit/Espo/Tools/DashboardTemplate/DuplicateTest.php
Normal file
78
tests/unit/Espo/Tools/DashboardTemplate/DuplicateTest.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2026 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\Espo\Tools\DashboardTemplate;
|
||||
|
||||
use Espo\Classes\FieldDuplicators\DashboardTemplate\Layout as LayoutFieldDuplicator;
|
||||
use Espo\Entities\DashboardTemplate;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DuplicateTest extends TestCase
|
||||
{
|
||||
public function testDuplicate(): void
|
||||
{
|
||||
$entity = $this->createMock(DashboardTemplate::class);
|
||||
|
||||
$original = [
|
||||
(object) [
|
||||
'id' => 'tab-01',
|
||||
'layout' => [
|
||||
(object) [
|
||||
'id' => 'd-01',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$originalOptions = (object) [
|
||||
'd-01' => (object) ['k' => 'v'],
|
||||
];
|
||||
|
||||
$entity->method('getLayoutRaw')
|
||||
->willReturn($original);
|
||||
|
||||
$entity->method('getDashletsOptionsRaw')
|
||||
->willReturn($originalOptions);
|
||||
|
||||
$duplicator = new LayoutFieldDuplicator();
|
||||
|
||||
$values = $duplicator->duplicate($entity, DashboardTemplate::FIELD_LAYOUT);
|
||||
|
||||
$copy = $values->{DashboardTemplate::FIELD_LAYOUT} ?? null;
|
||||
$copyOptions = $values->{DashboardTemplate::FIELD_DASHLETS_OPTIONS} ?? null;
|
||||
|
||||
$this->assertIsArray($copy);
|
||||
$this->assertIsString($copy[0]->id);
|
||||
$this->assertNotEquals($original[0]->id, $copy[0]->id);
|
||||
$this->assertNotEquals($original[0]->layout[0]->id, $copy[0]->layout[0]->id);
|
||||
$this->assertIsString($copy[0]->layout[0]->id);
|
||||
$this->assertFalse(property_exists($copyOptions, 'd-01'));
|
||||
$this->assertCount(1, get_object_vars($copyOptions));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user