diff --git a/src/Repository/PageRepository.php b/src/Repository/PageRepository.php index e76bd56..598faed 100644 --- a/src/Repository/PageRepository.php +++ b/src/Repository/PageRepository.php @@ -49,7 +49,42 @@ public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $lo return $count > 0; } - public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool + public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool + { + $queryBuilder = $this->createQueryBuilderExistOne($channel, $locale, $slug); + $queryBuilder + ->andWhere('p.enabled = true') + ; + + $count = (int) $queryBuilder + ->getQuery() + ->getSingleScalarResult() + ; + + return $count > 0; + } + + /** + * @throws NonUniqueResultException + */ + public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface + { + return $this->createQueryBuilder('p') + ->leftJoin('p.translations', 'translation') + ->innerJoin('p.channels', 'channels') + ->where('translation.locale = :localeCode') + ->andWhere('translation.slug = :slug') + ->andWhere('channels.code = :channelCode') + ->andWhere('p.enabled = true') + ->setParameter('localeCode', $localeCode) + ->setParameter('slug', $slug) + ->setParameter('channelCode', $channelCode) + ->getQuery() + ->getOneOrNullResult() + ; + } + + public function existsOneEnabledAndPublishedByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool { $queryBuilder = $this->createQueryBuilderExistOne($channel, $locale, $slug); $queryBuilder @@ -70,7 +105,7 @@ public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?str /** * @throws NonUniqueResultException */ - public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface + public function findOneEnabledAndPublishedBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface { return $this->createQueryBuilder('p') ->leftJoin('p.translations', 'translation') diff --git a/src/Repository/PageRepositoryInterface.php b/src/Repository/PageRepositoryInterface.php index d79404a..bdd83ad 100644 --- a/src/Repository/PageRepositoryInterface.php +++ b/src/Repository/PageRepositoryInterface.php @@ -25,7 +25,13 @@ public function createListQueryBuilder(string $localeCode): QueryBuilder; public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, array $excludedPages = []): bool; - public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool; + /** @deprecated Use existsOneEnabledAndPublishedByChannelAndSlug */ + public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool; - public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface; + /** @deprecated Use findOneEnabledAndPublishedBySlugAndChannelCode */ + public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface; + + public function existsOneEnabledAndPublishedByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool; + + public function findOneEnabledAndPublishedBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface; } diff --git a/src/Resources/config/routing/shop.yaml b/src/Resources/config/routing/shop.yaml index 46a8db9..6e462d2 100644 --- a/src/Resources/config/routing/shop.yaml +++ b/src/Resources/config/routing/shop.yaml @@ -7,7 +7,7 @@ monsieurbiz_cms_page_show: _sylius: template: "@MonsieurBizSyliusCmsPagePlugin/Shop/Page/show.html.twig" repository: - method: findOneEnabledBySlugAndChannelCode + method: findOneEnabledAndPublishedBySlugAndChannelCode arguments: - $slug - "expr:service('sylius.context.locale').getLocaleCode()" diff --git a/src/Routing/PageSlugConditionChecker.php b/src/Routing/PageSlugConditionChecker.php index bb42786..d8a9cff 100644 --- a/src/Routing/PageSlugConditionChecker.php +++ b/src/Routing/PageSlugConditionChecker.php @@ -59,7 +59,7 @@ public function __construct( public function isPageSlug(string $slug): bool { try { - return $this->pageRepository->existsOneEnabledByChannelAndSlug( + return $this->pageRepository->existsOneEnabledAndPublishedByChannelAndSlug( $this->channelContext->getChannel(), $this->localeContext->getLocaleCode(), $slug,