|stdClass|null * @throws NotFound * @throws Error */ public function getOriginal(string $scope, string $name, ?string $setId = null): mixed { $result = null; if ($setId) { $layout = $this->getRecordFromSet($scope, $name, $setId, true); if ($layout && $layout->getData() !== null) { /** @var string $data */ $data = $layout->getData(); $result = Json::decode($data); } } if (!$result) { $data = $this->layoutProvider->get($scope, $name) ?? 'false'; $result = Json::decode($data); } if ($result === false && $name === 'bottomPanelsDetail') { return $this->getOriginalBottomPanelsDetail($scope); } return $result; } /** * @throws Forbidden * @throws NotFound * @throws Error */ public function getForFrontend(string $scope, string $name): mixed { try { if (!$this->acl->checkScope($scope)) { throw new Forbidden(); } } catch (NotImplemented) {} $data = null; $layoutSetId = $this->getUserLayoutSetId(); if ($layoutSetId) { $nameReal = $name; if ( $this->user->isPortal() && str_ends_with($name, 'Portal') ) { $nameReal = substr($name, 0, -6); } $layout = $this->getRecordFromSet($scope, $nameReal, $layoutSetId, true); $data = $layout?->getData(); } if (!$data) { $dataString = $this->layoutProvider->get($scope, $name) ?? 'null'; $data = Json::decode($dataString); } if (is_null($data)) { throw new NotFound("Layout $scope:$name is not found."); } 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; } $foreignEntityType = $this->metadata->get(['entityDefs', $scope, 'links', $link, 'entity']); if ( $foreignEntityType && !$this->acl->tryCheck($foreignEntityType) ) { unset($data[$i]); } } $data = array_values($data); } if ($data === false && $name === 'bottomPanelsDetail') { return $this->getForFrontendBottomPanelsDetail($scope); } return $data; } /** * @throws NotFound */ private function getRecordFromSet( string $scope, string $name, string $setId, bool $skipCheck = false ): ?LayoutRecord { $layoutSet = $this->entityManager ->getRDBRepositoryByClass(LayoutSet::class) ->getById($setId); if (!$layoutSet) { throw new NotFound("LayoutSet $setId not found."); } $fullName = $scope . '.' . $name; if (!in_array($fullName, $layoutSet->getLayoutList())) { if ($skipCheck) { return null; } throw new NotFound("Layout $fullName is no allowed in set."); } return $this->entityManager ->getRDBRepositoryByClass(LayoutRecord::class) ->where([ 'layoutSetId' => $setId, 'name' => $fullName, ]) ->findOne(); } /** * @throws NotFound * @throws Error */ public function update(string $scope, string $name, ?string $setId, mixed $data): mixed { if ($setId) { $layout = $this->getRecordFromSet($scope, $name, $setId); if (!$layout) { $layout = $this->entityManager->getNewEntity(LayoutRecord::ENTITY_TYPE); $layout->set([ 'layoutSetId' => $setId, 'name' => $scope . '.' . $name, ]); } $layout->set('data', Json::encode($data)); $this->entityManager->saveEntity($layout); return Json::decode( $layout->getData() ); } $layoutManager = $this->layoutManager; $layoutManager->set($data, $scope, $name); $layoutManager->save(); $this->dataManager->updateCacheTimestamp(); return $layoutManager->get($scope, $name); } /** * @return array|stdClass|null * @throws NotFound * @throws Error */ public function resetToDefault(string $scope, string $name, ?string $setId = null): mixed { $this->dataManager->updateCacheTimestamp(); if ($setId) { $layout = $this->getRecordFromSet($scope, $name, $setId); if ($layout) { $em = $this->entityManager; $em->removeEntity($layout); } return $this->getOriginal($scope, $name); } $this->layoutManager->resetToDefault($scope, $name); if ($name === 'bottomPanelsDetail') { $this->resetToDefaultBottomPanelsDetail($scope); } return $this->getOriginal($scope, $name); } /** * @throws Error * @throws NotFound */ private function getOriginalBottomPanelsDetail(string $scope): stdClass { $relationships = $this->getOriginal($scope, 'relationships') ?? []; if (!is_array($relationships)) { throw new Error("Bad 'relationships' layout."); } $result = (object) []; foreach ($relationships as $i => $item) { if (is_string($item)) { $item = (object) [ 'name' => $item, ]; } if (!is_object($item)) { continue; } /** @var stdClass $item */ $item = clone $item; $item->index = 5 + 0.001 * $i; if (!isset($item->name)) { continue; } $result->{$item->name} = $item; } return $result; } /** * @throws NotFound * @throws Error */ private function getForFrontendBottomPanelsDetail(string $scope): stdClass { return $this->getOriginalBottomPanelsDetail($scope); } private function resetToDefaultBottomPanelsDetail(string $scope): void { $this->layoutManager->resetToDefault($scope, 'relationships'); } private function getUserLayoutSetId(): ?string { if ($this->user->isPortal()) { $portalId = $this->user->getPortalId(); if (!$portalId) { return null; } $portal = $this->entityManager ->getRDBRepositoryByClass(Portal::class) ->select(['layoutSetId']) ->where(['id' => $portalId]) ->findOne(); if (!$portal) { return null; } return $portal->getLayoutSet()?->getId(); } if ($this->user->getLayoutSet()) { return $this->user->getLayoutSet()->getId(); } $teamId = $this->user->getDefaultTeam()?->getId(); if (!$teamId) { return null; } $team = $this->entityManager ->getRDBRepositoryByClass(Team::class) ->select(['layoutSetId']) ->where(['id' => $teamId]) ->findOne(); if (!$team) { return null; } return $team->getLayoutSet()?->getId(); } }