metadata = $metadata; $this->entityManager = $entityManager; $this->user = $user; $this->listLoadProcessor = $listLoadProcessor; } /** * @param array{ * offset: ?int, * maxSize: ?int, * } $params * @return array{ * total: int, * collection: \Espo\ORM\Collection<\Espo\Entities\ActionHistoryRecord>, * } */ public function getList(array $params): array { $repository = $this->entityManager->getRDBRepository('ActionHistoryRecord'); $scopes = $this->metadata->get('scopes'); $targetTypeList = array_filter( array_keys($scopes), function ($item) use ($scopes) { return !empty($scopes[$item]['object']) || !empty($scopes[$item]['lastViewed']); } ); $offset = $params['offset'] ?? 0; $maxSize = $params['maxSize'] ?? 0; /** @var \Espo\ORM\Collection<\Espo\Entities\ActionHistoryRecord> $collection */ $collection = $repository ->where([ 'userId' => $this->user->getId(), 'action' => 'read', 'targetType' => $targetTypeList, ]) ->order('MAX:createdAt', 'DESC') ->select([ 'targetId', 'targetType', 'MAX:number', ['MAX:createdAt', 'createdAt'], ]) ->group(['targetId', 'targetType']) ->limit($offset, $params['maxSize'] + 1) ->find(); foreach ($collection as $entity) { $this->listLoadProcessor->process($entity); $entity->set('id', Util::generateId()); } if ( $maxSize && is_countable($collection) && count($collection) > $maxSize && $collection instanceof ArrayAccess ) { $total = -1; unset($collection[count($collection) - 1]); } else { $total = -2; } return [ 'total' => $total, 'collection' => $collection, ]; } }