1414namespace MonsieurBiz \SyliusCmsPagePlugin \Repository ;
1515
1616use Doctrine \ORM \NonUniqueResultException ;
17- use Doctrine \ORM \NoResultException ;
1817use Doctrine \ORM \QueryBuilder ;
1918use MonsieurBiz \SyliusCmsPagePlugin \Entity \PageInterface ;
2019use Sylius \Bundle \ResourceBundle \Doctrine \ORM \EntityRepository ;
@@ -31,22 +30,32 @@ public function createListQueryBuilder(string $localeCode): QueryBuilder
3130 ;
3231 }
3332
34- /**
35- * @throws NoResultException
36- * @throws NonUniqueResultException
37- */
38- public function existsOneByChannelAndSlug (ChannelInterface $ channel , ?string $ locale , string $ slug ): bool
33+ public function existsOneByChannelAndSlug (ChannelInterface $ channel , ?string $ locale , string $ slug , array $ excludedPages = []): bool
3934 {
40- $ count = (int ) $ this
41- ->createQueryBuilder ('p ' )
42- ->select ('COUNT(p.id) ' )
43- ->innerJoin ('p.translations ' , 'translation ' , 'WITH ' , 'translation.locale = :locale ' )
44- ->andWhere ('translation.slug = :slug ' )
45- ->andWhere (':channel MEMBER OF p.channels ' )
35+ $ queryBuilder = $ this ->createQueryBuilderExistOne ($ channel , $ locale , $ slug );
36+ if (!empty ($ excludedPages )) {
37+ $ queryBuilder
38+ ->andWhere ('p.id NOT IN (:excludedPages) ' )
39+ ->setParameter ('excludedPages ' , $ excludedPages )
40+ ;
41+ }
42+
43+ $ count = (int ) $ queryBuilder
44+ ->getQuery ()
45+ ->getSingleScalarResult ()
46+ ;
47+
48+ return $ count > 0 ;
49+ }
50+
51+ public function existsOneEnabledByChannelAndSlug (ChannelInterface $ channel , ?string $ locale , string $ slug ): bool
52+ {
53+ $ queryBuilder = $ this ->createQueryBuilderExistOne ($ channel , $ locale , $ slug );
54+ $ queryBuilder
4655 ->andWhere ('p.enabled = true ' )
47- -> setParameter ( ' channel ' , $ channel )
48- -> setParameter ( ' locale ' , $ locale )
49- -> setParameter ( ' slug ' , $ slug )
56+ ;
57+
58+ $ count = ( int ) $ queryBuilder
5059 ->getQuery ()
5160 ->getSingleScalarResult ()
5261 ;
@@ -73,4 +82,19 @@ public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeC
7382 ->getOneOrNullResult ()
7483 ;
7584 }
85+
86+ private function createQueryBuilderExistOne (ChannelInterface $ channel , ?string $ locale , string $ slug ): QueryBuilder
87+ {
88+ return $ this
89+ ->createQueryBuilder ('p ' )
90+ ->select ('COUNT(p.id) ' )
91+ ->innerJoin ('p.translations ' , 'translation ' , 'WITH ' , 'translation.locale = :locale ' )
92+ ->andWhere ('translation.slug = :slug ' )
93+ ->andWhere (':channel MEMBER OF p.channels ' )
94+ ->andWhere ('p.enabled = true ' )
95+ ->setParameter ('channel ' , $ channel )
96+ ->setParameter ('locale ' , $ locale )
97+ ->setParameter ('slug ' , $ slug )
98+ ;
99+ }
76100}
0 commit comments