From f1306dfbeee05df33c8c77bcb86bf64cd1e21610 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 13 May 2021 14:43:55 +0300 Subject: [PATCH] refactoring --- application/Espo/Core/Controllers/Record.php | 45 ++++++++++++++----- .../Espo/Core/Controllers/RecordBase.php | 4 +- application/Espo/Core/Record/Service.php | 19 ++------ application/Espo/Core/Select/SearchParams.php | 2 + application/Espo/Services/Team.php | 6 ++- 5 files changed, 45 insertions(+), 31 deletions(-) diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.php index 075610fb96..2fa94a0704 100644 --- a/application/Espo/Core/Controllers/Record.php +++ b/application/Espo/Core/Controllers/Record.php @@ -36,6 +36,7 @@ use Espo\Core\Exceptions\{ use Espo\Core\{ Record\Collection as RecordCollection, Api\Request, + Select\SearchParams, }; use StdClass; @@ -93,19 +94,9 @@ class Record extends RecordBase } if (!empty($data->massRelate)) { - if (!is_array($data->where)) { - throw new BadRequest(); - } + $searchParams = $this->fetchMassLinkSearchParamsFromRequest($request); - $where = json_decode(json_encode($data->where), true); - - $selectData = null; - - if (isset($data->selectData) && is_array($data->selectData)) { - $selectData = json_decode(json_encode($data->selectData), true); - } - - return $this->getRecordService()->massLink($id, $link, $where, $selectData); + return $this->getRecordService()->massLink($id, $link, $searchParams); } $foreignIdList = []; @@ -191,4 +182,34 @@ class Record extends RecordBase return true; } + + private function fetchMassLinkSearchParamsFromRequest(Request $request): SearchParams + { + $data = $request->getParsedBody(); + + $where = $data->where ?? null; + + if ($where !== null) { + $where = json_decode(json_encode($where), true); + } + + $params = json_decode(json_encode( + $data->searchParams ?? $data->selectData ?? (object) [] + ), true); + + if ($where !== null && !is_array($where)) { + throw new BadRequest("Bad 'where."); + } + + if ($where !== null) { + $params['where'] = array_merge( + $params['where'] ?? [], + $where + ); + } + + unset($params['select']); + + return SearchParams::fromRaw($params); + } } diff --git a/application/Espo/Core/Controllers/RecordBase.php b/application/Espo/Core/Controllers/RecordBase.php index b9310f443a..324ff128c9 100644 --- a/application/Espo/Core/Controllers/RecordBase.php +++ b/application/Espo/Core/Controllers/RecordBase.php @@ -47,7 +47,7 @@ use Espo\Core\{ ServiceFactory, Api\Request, Api\Response, - Record\Crud as CrudService, + Record\Service as RecordService, Select\SearchParams, Di, }; @@ -122,7 +122,7 @@ class RecordBase extends Base implements Di\EntityManagerAware return $this->name; } - protected function getRecordService(?string $entityType = null): CrudService + protected function getRecordService(?string $entityType = null): RecordService { return $this->recordServiceContainer->get($entityType ?? $this->getEntityType()); } diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php index 24efa0630a..77ff655fd1 100644 --- a/application/Espo/Core/Record/Service.php +++ b/application/Espo/Core/Record/Service.php @@ -1152,13 +1152,13 @@ class Service implements Crud, $this->getStreamService()->unfollowEntity($entity, $foreignId); } - public function massLink(string $id, string $link, array $where, ?array $selectData = null) + public function massLink(string $id, string $link, SearchParams $searchParams): bool { if (!$this->acl->check($this->entityType, AclTable::ACTION_EDIT)) { throw new Forbidden(); } - if (empty($id) || empty($link)) { + if (!$id || !$link) { throw new BadRequest; } @@ -1177,7 +1177,7 @@ class Service implements Crud, $methodName = 'massLink' . ucfirst($link); if (method_exists($this, $methodName)) { - return $this->$methodName($id, $where, $selectData); + return $this->$methodName($id, $searchParams); } $foreignEntityType = $entity->getRelationParam($link, 'entity'); @@ -1196,21 +1196,10 @@ class Service implements Crud, throw new Forbidden(); } - if (!is_array($where)) { - $where = []; - } - $params['where'] = $where; - - if (is_array($selectData)) { - foreach ($selectData as $k => $v) { - $params[$k] = $v; - } - } - $query = $this->selectBuilderFactory->create() ->from($foreignEntityType) ->withStrictAccessControl() - ->withSearchParams(SearchParams::fromRaw($params)) + ->withSearchParams($searchParams->withSelect(null)) ->build(); if ($this->acl->getLevel($foreignEntityType, $accessActionRequired) === AclTable::LEVEL_ALL) { diff --git a/application/Espo/Core/Select/SearchParams.php b/application/Espo/Core/Select/SearchParams.php index 1de5f16026..5298defe70 100644 --- a/application/Espo/Core/Select/SearchParams.php +++ b/application/Espo/Core/Select/SearchParams.php @@ -122,6 +122,8 @@ class SearchParams } /** + * Attributes to select. NULL means to select all attributes. + * * @param string[]|null $select */ public function withSelect(?array $select): self diff --git a/application/Espo/Services/Team.php b/application/Espo/Services/Team.php index 7476f69744..765e94ea84 100644 --- a/application/Espo/Services/Team.php +++ b/application/Espo/Services/Team.php @@ -31,6 +31,8 @@ namespace Espo\Services; use Espo\ORM\Entity; +use Espo\Core\Select\SearchParams; + use Espo\Core\Di; class Team extends Record implements @@ -82,9 +84,9 @@ class Team extends Record implements } } - public function massLink(string $id, string $link, array $where, ?array $selectData = null) + public function massLink(string $id, string $link, SearchParams $searchParams): bool { - $result = parent::massLink($id, $link, $where, $selectData); + $result = parent::massLink($id, $link, $searchParams); if ($link === 'users') { $this->clearRolesCache();