Skip to content

Commit

Permalink
DoctrineListRepresentationFactory with sorting of ids; hasNextPage wi…
Browse files Browse the repository at this point in the history
…th limit/pagination fix
  • Loading branch information
Manuel Bertrams committed Sep 12, 2024
1 parent d83d923 commit 6a0a90b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 59 deletions.
14 changes: 14 additions & 0 deletions src/Common/DoctrineListRepresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sulu\Bundle\MediaBundle\Media\Manager\MediaManagerInterface;
use Sulu\Component\Rest\ListBuilder\Doctrine\DoctrineListBuilderFactory;
use Sulu\Component\Rest\ListBuilder\Doctrine\FieldDescriptor\DoctrineFieldDescriptor;
use Sulu\Component\Rest\ListBuilder\ListRestHelperInterface;
use Sulu\Component\Rest\ListBuilder\Metadata\FieldDescriptorFactoryInterface;
use Sulu\Component\Rest\ListBuilder\PaginatedRepresentation;
use Sulu\Component\Rest\RestHelperInterface;
Expand All @@ -16,6 +17,7 @@
class DoctrineListRepresentationFactory
{
private RestHelperInterface $restHelper;
private ListRestHelperInterface $listRestHelper;
private DoctrineListBuilderFactory $listBuilderFactory;
private FieldDescriptorFactoryInterface $fieldDescriptorFactory;
private WebspaceManagerInterface $webspaceManager;
Expand All @@ -24,13 +26,15 @@ class DoctrineListRepresentationFactory

public function __construct(
RestHelperInterface $restHelper,
ListRestHelperInterface $listRestHelper,
DoctrineListBuilderFactory $listBuilderFactory,
FieldDescriptorFactoryInterface $fieldDescriptorFactory,
WebspaceManagerInterface $webspaceManager,
EventTranslationRepository $eventTranslationRepository,
MediaManagerInterface $mediaManager
) {
$this->restHelper = $restHelper;
$this->listRestHelper = $listRestHelper;
$this->listBuilderFactory = $listBuilderFactory;
$this->fieldDescriptorFactory = $fieldDescriptorFactory;
$this->webspaceManager = $webspaceManager;
Expand Down Expand Up @@ -66,6 +70,16 @@ public function createDoctrineListRepresentation(

$list = $listBuilder->execute();

// sort the items to reflect the order of the given ids if the list was requested to include specific ids
$requestedIds = $this->listRestHelper->getIds();
if (null !== $requestedIds) {
$idPositions = array_flip($requestedIds);

usort($list, function ($a, $b) use ($idPositions) {
return $idPositions[$a['id']] - $idPositions[$b['id']];
});
}

$list = $this->addGhostLocaleToListElements($list, $parameters['locale'] ?? null);
$list = $this->addImagesToListElements($list, $parameters['locale'] ?? null);

Expand Down
5 changes: 3 additions & 2 deletions src/Content/EventDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Manuxi\SuluEventBundle\Content;

use Countable;
use Manuxi\SuluEventBundle\Admin\EventAdmin;
use Sulu\Component\Serializer\ArraySerializerInterface;
use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface;
Expand Down Expand Up @@ -64,13 +65,13 @@ static function ($item) {
* It combines the limit/query-count with the page and page-size.
*
* @noinspection PhpUnusedPrivateMethodInspection
* @param \Countable $queryResult
* @param Countable $queryResult
* @param int|null $limit
* @param int $page
* @param int|null $pageSize
* @return bool
*/
private function hasNextPage(\Countable $queryResult, ?int $limit, int $page, ?int $pageSize): bool
private function hasNextPage(Countable $queryResult, ?int $limit, int $page, ?int $pageSize): bool
{
$count = $queryResult->count();

Expand Down
84 changes: 30 additions & 54 deletions src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public function findAllForSitemap(int $page, int $limit): array
public function countForSitemap()
{
$query = $this->createQueryBuilder('e')
->select('count(e)');
->select('count(e)')
->where('e.enabled = :enabled')
->setParameter('enabled', 1);
return $query->getQuery()->getSingleScalarResult();
}

Expand Down Expand Up @@ -122,46 +124,6 @@ public function findAllScheduledEvents(int $limit)
return $queryBuilder->getQuery()->getResult();
}

/**
* Returns filtered entities.
* When pagination is active the result count is pageSize + 1 to determine has next page.
*
* @param array $filters array of filters: tags, tagOperator
* @param int $page
* @param int $pageSize
* @param int $limit
* @param string $locale
* @param mixed[] $options
* @param UserInterface|null $user
* @param null $entityClass
* @param null $entityAlias
* @param null $permission
* @return object[]
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpMissingParamTypeInspection
*/
/* public function findByFilters(
$filters,
$page,
$pageSize,
$limit,
$locale,
$options = [],
?UserInterface $user = null,
$entityClass = null,
$entityAlias = null,
$permission = null
) {
$entities = $this->parentFindByFilters($filters, $page, $pageSize, $limit, $locale, $options);
return \array_map(
function (Event $entity) use ($locale) {
return $entity->setLocale($locale);
},
$entities
);
}*/

protected function appendJoins(QueryBuilder $queryBuilder, $alias, $locale): void
{

Expand Down Expand Up @@ -212,9 +174,6 @@ function (Event $entity) use ($locale) {

public function getActiveEvents(array $filters, string $locale, ?int $page, $pageSize, $limit = null, array $options): array
{
// Determine the current page
$pageCurrent = array_key_exists('page', $options) ? (int) $options['page'] : 0;

// Initialize the query builder
$queryBuilder = $this->createQueryBuilder('event')
->leftJoin('event.translations', 'translation')
Expand All @@ -224,24 +183,41 @@ public function getActiveEvents(array $filters, string $locale, ?int $page, $pag
->setParameter('locale', $locale)
->orderBy('event.startDate', 'DESC');

// Apply limit and pagination
if ($limit !== null) {
$queryBuilder->setMaxResults($limit);
}
if ($pageCurrent !== null && $limit !== null) {
$queryBuilder->setFirstResult($pageCurrent * $limit);
}

// Apply additional filters
$this->prepareFilter($queryBuilder, $filters);
$this->prepareFilters($queryBuilder, $filters);

// Apply offset/max results
if (!$this->setOffsetResults($queryBuilder, $page, $pageSize, $limit)) {
return [];
}

// Execute the query and return results
$events = $queryBuilder->getQuery()->getResult();

return $events ?: [];
}

private function prepareFilter(QueryBuilder $queryBuilder, array $filters): void
private function setOffsetResults(QueryBuilder $queryBuilder, $page, $pageSize, $limit = null): bool {
if (null !== $page && $pageSize > 0) {

$pageOffset = ($page - 1) * $pageSize;
$restLimit = $limit - $pageOffset;

$maxResults = (null !== $limit && $pageSize > $restLimit ? $restLimit : $pageSize);

if ($maxResults <= 0) {
return false;
}

$queryBuilder->setMaxResults($maxResults);
$queryBuilder->setFirstResult($pageOffset);
} elseif (null !== $limit) {
$queryBuilder->setMaxResults($limit);
}
return true;
}

private function prepareFilters(QueryBuilder $queryBuilder, array $filters): void
{
if (isset($filters['sortBy'])) {
$queryBuilder->orderBy($filters['sortBy'], $filters['sortMethod']);
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

<service id="sulu_event.doctrine_list_representation_factory" class="Manuxi\SuluEventBundle\Common\DoctrineListRepresentationFactory">
<argument type="service" id="sulu_core.rest_helper"/>
<argument type="service" id="sulu_core.list_rest_helper"/>
<argument type="service" id="sulu_core.doctrine_list_builder_factory"/>
<argument type="service" id="sulu_core.list_builder.field_descriptor_factory"/>
<argument type="service" id="sulu_core.webspace.webspace_manager"/>
Expand Down
4 changes: 1 addition & 3 deletions src/Service/CountryCodeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

class CountryCodeSelect
{
/**
* @return array[]
*/

public function getValues(): array
{
$values = [];
Expand Down

0 comments on commit 6a0a90b

Please sign in to comment.