refactoring

This commit is contained in:
Yuri Kuznetsov
2021-05-13 14:43:55 +03:00
parent db4663cbf9
commit f1306dfbee
5 changed files with 45 additions and 31 deletions

View File

@@ -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);
}
}

View File

@@ -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());
}

View File

@@ -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) {

View File

@@ -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

View File

@@ -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();