1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the OrbitaleCmsBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Orbitale\Bundle\CmsBundle\Repository; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\Tools\Pagination\Paginator; |
15
|
|
|
use Orbitale\Bundle\CmsBundle\Entity\Category; |
16
|
|
|
use Orbitale\Bundle\CmsBundle\Entity\Page; |
17
|
|
|
|
18
|
|
|
class PageRepository extends AbstractCmsRepository |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param Category $category |
22
|
|
|
* @param string $order |
23
|
|
|
* @param string $orderBy |
24
|
|
|
* @param int $page |
25
|
|
|
* @param int $limit |
26
|
|
|
* |
27
|
|
|
* @return Paginator |
28
|
|
|
*/ |
29
|
|
|
public function findByCategory(Category $category, $order, $orderBy, $page, $limit): Paginator |
30
|
|
|
{ |
31
|
|
|
$qb = $this->createQueryBuilder('page') |
32
|
|
|
->where('page.category = :category') |
33
|
|
|
->orderBy('page.'.$orderBy, $order) |
34
|
|
|
->setMaxResults($limit) |
35
|
|
|
->setFirstResult($limit * ($page-1)) |
36
|
|
|
->setParameter('category', $category) |
37
|
|
|
; |
38
|
|
|
|
39
|
|
|
return new Paginator($qb->getQuery()->useResultCache($this->cacheEnabled, $this->cacheTtl)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Will search for pages to show in front depending on the arguments. |
44
|
|
|
* If slugs are defined, there's no problem in looking for nulled host or locale, |
45
|
|
|
* because slugs are unique, so it does not. |
46
|
|
|
* |
47
|
|
|
* @param array $slugs |
48
|
|
|
* @param string|null $host |
49
|
|
|
* @param string|null $locale |
50
|
|
|
* |
51
|
|
|
* @return Page[] |
52
|
|
|
*/ |
53
|
|
|
public function findFrontPages(array $slugs = [], $host = null, $locale = null) |
54
|
|
|
{ |
55
|
|
|
$qb = $this->createQueryBuilder('page') |
56
|
|
|
->where('page.enabled = :enabled') |
57
|
|
|
->leftJoin('page.category', 'category') |
58
|
|
|
->andWhere('page.category is null OR category.enabled = :enabled') |
59
|
|
|
->setParameter('enabled', true) |
60
|
|
|
; |
61
|
|
|
|
62
|
|
|
// Will search differently if we're looking for homepage. |
63
|
|
|
$searchForHomepage = 0 === count($slugs); |
64
|
|
|
|
65
|
|
|
if (true === $searchForHomepage) { |
66
|
|
|
// If we are looking for homepage, let's get only the first one. |
67
|
|
|
$qb |
68
|
|
|
->andWhere('page.homepage = :homepage') |
69
|
|
|
->setParameter('homepage', true) |
70
|
|
|
->setMaxResults(1) |
71
|
|
|
; |
72
|
|
|
} else { |
73
|
|
|
$qb |
74
|
|
|
->andWhere('page.slug IN ( :slugs )') |
75
|
|
|
->setParameter('slugs', $slugs) |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$hostWhere = 'page.host IS NULL'; |
80
|
|
|
if (null !== $host) { |
81
|
|
|
$hostWhere .= ' OR page.host = :host'; |
82
|
|
|
$qb->setParameter('host', $host); |
83
|
|
|
$qb->addOrderBy('page.host', 'asc'); |
84
|
|
|
} |
85
|
|
|
$qb->andWhere($hostWhere); |
86
|
|
|
|
87
|
|
|
$localeWhere = 'page.locale IS NULL'; |
88
|
|
|
if (null !== $locale) { |
89
|
|
|
$localeWhere .= ' OR page.locale = :locale'; |
90
|
|
|
$qb->setParameter('locale', $locale); |
91
|
|
|
$qb->addOrderBy('page.locale', 'asc'); |
92
|
|
|
} |
93
|
|
|
$qb->andWhere($localeWhere); |
94
|
|
|
|
95
|
|
|
// Then the last page will automatically be one that has both properties. |
96
|
|
|
$qb |
97
|
|
|
->orderBy('page.host', 'asc') |
98
|
|
|
->addOrderBy('page.locale', 'asc') |
99
|
|
|
; |
100
|
|
|
|
101
|
|
|
/** @var Page[] $results */ |
102
|
|
|
$results = $qb->getQuery() |
103
|
|
|
->useResultCache($this->cacheEnabled, $this->cacheTtl) |
104
|
|
|
->getResult() |
105
|
|
|
; |
106
|
|
|
|
107
|
|
|
// If we're looking for a homepage, only get the last result (matching more properties). |
108
|
|
|
if (true === $searchForHomepage && count($results) > 0) { |
109
|
|
|
reset($results); |
110
|
|
|
$results = [$results[0]]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$resultsSortedBySlug = []; |
114
|
|
|
|
115
|
|
|
foreach ($results as $page) { |
116
|
|
|
$resultsSortedBySlug[$page->getSlug()] = $page; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $resultsSortedBySlug; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|