|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smart\ContentBundle\Entity\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use Smart\ContentBundle\Entity\Category; |
|
7
|
|
|
use Smart\ContentBundle\Entity\Post; |
|
8
|
|
|
use Smart\ContentBundle\Entity\Tag; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Nicolas Bastien <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class PostRepository extends EntityRepository |
|
14
|
|
|
{ |
|
15
|
|
|
const DIRECTION_PREVIOUS = 'previous'; |
|
16
|
|
|
const DIRECTION_NEXT = 'next'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @return \DateTime|null |
|
20
|
|
|
*/ |
|
21
|
|
|
public function getLastPublishedPostDate() |
|
22
|
|
|
{ |
|
23
|
|
|
if (null !== $post = $this->getLastPublishedPost()) { |
|
24
|
|
|
return $post->getPublishedAt(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return null; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $count |
|
32
|
|
|
* @param Category|null $category |
|
33
|
|
|
* @param Tag|null $tag |
|
34
|
|
|
* |
|
35
|
|
|
* @return Post[] |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getLastPublishedPosts($count = 3, $category = null, $tag = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$queryBuilder = $this->createQueryBuilder('p'); |
|
40
|
|
|
if ($category !== null) { |
|
41
|
|
|
$queryBuilder |
|
42
|
|
|
->andWhere('p.category = :category') |
|
43
|
|
|
->setParameter(':category', $category); |
|
44
|
|
|
} |
|
45
|
|
|
if ($tag !== null) { |
|
46
|
|
|
$queryBuilder |
|
47
|
|
|
->leftJoin('p.tags', 't') |
|
48
|
|
|
->andWhere('t.id = :tag') |
|
49
|
|
|
->setParameter(':tag', $tag); |
|
50
|
|
|
} |
|
51
|
|
|
$queryBuilder |
|
52
|
|
|
->andWhere('p.publishedAt IS NOT NULL') |
|
53
|
|
|
->andWhere('p.enabled = 1') |
|
54
|
|
|
->orderBy('p.publishedAt', 'DESC') |
|
55
|
|
|
->setMaxResults($count); |
|
56
|
|
|
|
|
57
|
|
|
return $queryBuilder->getQuery()->getResult(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param Category|null $category |
|
62
|
|
|
* @param Tag|null $tag |
|
63
|
|
|
* @return Post|null |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getLastPublishedPost($category = null, $tag = null) |
|
66
|
|
|
{ |
|
67
|
|
|
$posts = $this->getLastPublishedPosts(1, $category, $tag); |
|
68
|
|
|
|
|
69
|
|
|
if (count($posts)) { |
|
70
|
|
|
return array_pop($posts); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param Post|null $post |
|
78
|
|
|
* @param Category|null $category |
|
79
|
|
|
* @param Tag|null $tag |
|
80
|
|
|
* |
|
81
|
|
|
* @return Post|null |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getPreviousPost(Post $post = null, $category = null, $tag = null) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getClosestPost($post, 'previous', $category, $tag); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param Post|null $post |
|
90
|
|
|
* @param Category|null $category |
|
91
|
|
|
* @param Tag|null $tag |
|
92
|
|
|
* |
|
93
|
|
|
* @return Post|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getNextPost(Post $post = null, $category = null, $tag = null) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->getClosestPost($post, 'next', $category, $tag); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param Post|null $post |
|
102
|
|
|
* @param string $direction |
|
103
|
|
|
* @param Category|null $category |
|
104
|
|
|
* @param Tag|null $tag |
|
105
|
|
|
* |
|
106
|
|
|
* @return Post|null |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function getClosestPost(Post $post = null, $direction = 'next', $category = null, $tag = null) |
|
109
|
|
|
{ |
|
110
|
|
|
if (null === $post || null === $post->getPublishedAt()) { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$queryBuilder = $this->createQueryBuilder('p'); |
|
115
|
|
|
|
|
116
|
|
|
if ($direction === 'next') { |
|
117
|
|
|
$queryBuilder |
|
118
|
|
|
->andWhere('p.publishedAt < :date') |
|
119
|
|
|
->orderBy('p.publishedAt', 'DESC'); |
|
120
|
|
|
} else { |
|
121
|
|
|
$queryBuilder |
|
122
|
|
|
->andWhere('p.publishedAt >= :date') |
|
123
|
|
|
->orderBy('p.publishedAt', 'ASC'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
if ($category !== null) { |
|
127
|
|
|
$queryBuilder |
|
128
|
|
|
->andWhere('p.category = :category') |
|
129
|
|
|
->setParameter(':category', $category); |
|
130
|
|
|
} |
|
131
|
|
|
if ($tag !== null) { |
|
132
|
|
|
$queryBuilder |
|
133
|
|
|
->leftJoin('p.tags', 't') |
|
134
|
|
|
->andWhere('t.id = :tag') |
|
135
|
|
|
->setParameter(':tag', $tag); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $queryBuilder |
|
139
|
|
|
->setParameter(':date', $post->getPublishedAt()) |
|
140
|
|
|
->andWhere('p.publishedAt IS NOT NULL') |
|
141
|
|
|
->andWhere('p.enabled = 1') |
|
142
|
|
|
->andWhere('p.id != ' . $post->getId()) |
|
143
|
|
|
->setMaxResults(1) |
|
144
|
|
|
->getQuery() |
|
145
|
|
|
->getOneOrNullResult(); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|