From d46f9ebd1f093b15e54d467a4f9f82b57e057bfe Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 17 Jul 2023 17:00:47 +0300 Subject: [PATCH] ref --- application/Espo/Entities/LayoutRecord.php | 9 +- application/Espo/Entities/LayoutSet.php | 12 ++- application/Espo/Entities/Portal.php | 6 ++ application/Espo/Entities/Team.php | 6 ++ .../Espo/Tools/Layout/CustomLayoutService.php | 4 +- application/Espo/Tools/Layout/Service.php | 93 +++++++++---------- 6 files changed, 77 insertions(+), 53 deletions(-) diff --git a/application/Espo/Entities/LayoutRecord.php b/application/Espo/Entities/LayoutRecord.php index e6cc25fb8d..407ad20318 100644 --- a/application/Espo/Entities/LayoutRecord.php +++ b/application/Espo/Entities/LayoutRecord.php @@ -29,7 +29,14 @@ namespace Espo\Entities; -class LayoutRecord extends \Espo\Core\ORM\Entity +use Espo\Core\ORM\Entity; + +class LayoutRecord extends Entity { public const ENTITY_TYPE = 'LayoutRecord'; + + public function getData(): mixed + { + return $this->get('data'); + } } diff --git a/application/Espo/Entities/LayoutSet.php b/application/Espo/Entities/LayoutSet.php index ce1f50c15a..0b84a39431 100644 --- a/application/Espo/Entities/LayoutSet.php +++ b/application/Espo/Entities/LayoutSet.php @@ -29,7 +29,17 @@ namespace Espo\Entities; -class LayoutSet extends \Espo\Core\ORM\Entity +use Espo\Core\ORM\Entity; + +class LayoutSet extends Entity { public const ENTITY_TYPE = 'LayoutSet'; + + /** + * @return string[] + */ + public function getLayoutList(): array + { + return $this->get('layoutList') ?? []; + } } diff --git a/application/Espo/Entities/Portal.php b/application/Espo/Entities/Portal.php index 1607359d3c..9098d6483f 100644 --- a/application/Espo/Entities/Portal.php +++ b/application/Espo/Entities/Portal.php @@ -80,4 +80,10 @@ class Portal extends \Espo\Core\ORM\Entity /** @var ?Link */ return $this->getValueObject('authenticationProvider'); } + + public function getLayoutSet(): ?Link + { + /** @var ?Link */ + return $this->getValueObject('layoutSet'); + } } diff --git a/application/Espo/Entities/Team.php b/application/Espo/Entities/Team.php index 5ac5aaf8ab..e1d801e303 100644 --- a/application/Espo/Entities/Team.php +++ b/application/Espo/Entities/Team.php @@ -43,4 +43,10 @@ class Team extends \Espo\Core\ORM\Entity /** @var ?Link */ return $this->getValueObject('workingTimeCalendar'); } + + public function getLayoutSet(): ?Link + { + /** @var ?Link */ + return $this->getValueObject('layoutSet'); + } } diff --git a/application/Espo/Tools/Layout/CustomLayoutService.php b/application/Espo/Tools/Layout/CustomLayoutService.php index 936e2ab3ec..542c406fcd 100644 --- a/application/Espo/Tools/Layout/CustomLayoutService.php +++ b/application/Espo/Tools/Layout/CustomLayoutService.php @@ -41,6 +41,8 @@ use RuntimeException; class CustomLayoutService { + private const TYPE_LIST = 'list'; + public function __construct( private Metadata $metadata, private FileManager $fileManager, @@ -63,7 +65,7 @@ class CustomLayoutService $this->checkName($name); - if ($type !== 'list') { + if ($type !== self::TYPE_LIST) { throw new BadRequest("Not supported type."); } diff --git a/application/Espo/Tools/Layout/Service.php b/application/Espo/Tools/Layout/Service.php index f150b52a3b..5cecc39c0d 100644 --- a/application/Espo/Tools/Layout/Service.php +++ b/application/Espo/Tools/Layout/Service.php @@ -85,10 +85,8 @@ class Service $result = Json::decode($data); } - if ($result === false) { - if ($name === 'bottomPanelsDetail') { - return $this->getOriginalBottomPanelsDetail($scope); - } + if ($result === false && $name === 'bottomPanelsDetail') { + return $this->getOriginalBottomPanelsDetail($scope); } return $result; @@ -112,35 +110,32 @@ class Service $layoutSetId = null; $data = null; - $em = $this->entityManager; - $user = $this->user; - - if ($user->isPortal()) { - $portalId = $user->get('portalId'); + if ($this->user->isPortal()) { + $portalId = $this->user->getPortalId(); if ($portalId) { - $portal = $em + $portal = $this->entityManager ->getRDBRepositoryByClass(Portal::class) ->select(['layoutSetId']) ->where(['id' => $portalId]) ->findOne(); if ($portal) { - $layoutSetId = $portal->get('layoutSetId'); + $layoutSetId = $portal->getLayoutSet()?->getId(); } } } else { - $teamId = $user->get('defaultTeamId'); + $teamId = $this->user->getDefaultTeam()?->getId(); if ($teamId) { - $team = $em + $team = $this->entityManager ->getRDBRepositoryByClass(Team::class) ->select(['layoutSetId']) ->where(['id' => $teamId]) ->findOne(); if ($team) { - $layoutSetId = $team->get('layoutSetId'); + $layoutSetId = $team->getLayoutSet()?->getId(); } } } @@ -148,15 +143,16 @@ class Service if ($layoutSetId) { $nameReal = $name; - if ($user->isPortal()) { - if (str_ends_with($name, 'Portal')) { - $nameReal = substr($name, 0, -6); - } + if ( + $this->user->isPortal() && + str_ends_with($name, 'Portal') + ) { + $nameReal = substr($name, 0, -6); } $layout = $this->getRecordFromSet($scope, $nameReal, $layoutSetId, true); - $data = $layout?->get('data'); + $data = $layout?->getData(); } if (!$data) { @@ -169,36 +165,35 @@ class Service throw new NotFound("Layout $scope:$name is not found."); } - if (!$this->user->isAdmin()) { - if ($name === 'relationships') { - if (is_array($data)) { - foreach ($data as $i => $item) { - $link = $item; + if ( + !$this->user->isAdmin() && + $name === 'relationships' && + is_array($data) + ) { + foreach ($data as $i => $item) { + $link = $item; - if (is_object($item)) { - /** @var stdClass $item */ - $link = $item->name ?? null; - } + if (is_object($item)) { + /** @var stdClass $item */ + $link = $item->name ?? null; + } - $foreignEntityType = $this->metadata - ->get(['entityDefs', $scope, 'links', $link, 'entity']); + $foreignEntityType = $this->metadata + ->get(['entityDefs', $scope, 'links', $link, 'entity']); - if ($foreignEntityType) { - if (!$this->acl->tryCheck($foreignEntityType)) { - unset($data[$i]); - } - } - } - - $data = array_values($data); + if ( + $foreignEntityType && + !$this->acl->tryCheck($foreignEntityType) + ) { + unset($data[$i]); } } + + $data = array_values($data); } - if ($data === false) { - if ($name === 'bottomPanelsDetail') { - return $this->getForFrontendBottomPanelsDetail($scope); - } + if ($data === false && $name === 'bottomPanelsDetail') { + return $this->getForFrontendBottomPanelsDetail($scope); } return $data; @@ -207,26 +202,24 @@ class Service /** * @throws NotFound */ - protected function getRecordFromSet( + private function getRecordFromSet( string $scope, string $name, string $setId, bool $skipCheck = false ): ?LayoutRecord { - $entityManager = $this->entityManager; - - $layoutSet = $entityManager->getEntityById(LayoutSet::ENTITY_TYPE, $setId); + $layoutSet = $this->entityManager + ->getRDBRepositoryByClass(LayoutSet::class) + ->getById($setId); if (!$layoutSet) { throw new NotFound("LayoutSet $setId not found."); } - $layoutList = $layoutSet->get('layoutList') ?? []; - $fullName = $scope . '.' . $name; - if (!in_array($fullName, $layoutList)) { + if (!in_array($fullName, $layoutSet->getLayoutList())) { if ($skipCheck) { return null; } @@ -234,7 +227,7 @@ class Service throw new NotFound("Layout $fullName is no allowed in set."); } - return $entityManager + return $this->entityManager ->getRDBRepositoryByClass(LayoutRecord::class) ->where([ 'layoutSetId' => $setId,