|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sulu\Bundle\CommentBundle\Manager; |
|
13
|
|
|
|
|
14
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentInterface; |
|
15
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface; |
|
16
|
|
|
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface; |
|
17
|
|
|
use Sulu\Bundle\CommentBundle\Entity\ThreadRepositoryInterface; |
|
18
|
|
|
use Sulu\Bundle\CommentBundle\Events\CommentEvent; |
|
19
|
|
|
use Sulu\Bundle\CommentBundle\Events\Events; |
|
20
|
|
|
use Sulu\Bundle\CommentBundle\Events\ThreadEvent; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Manager to interact with comments. |
|
25
|
|
|
*/ |
|
26
|
|
|
class CommentManager implements CommentManagerInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ThreadRepositoryInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $threadRepository; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CommentRepositoryInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
private $commentRepository; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var EventDispatcherInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $dispatcher; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ThreadRepositoryInterface $threadRepository |
|
45
|
|
|
* @param CommentRepositoryInterface $commentRepository |
|
46
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
47
|
|
|
*/ |
|
48
|
26 |
|
public function __construct( |
|
49
|
|
|
ThreadRepositoryInterface $threadRepository, |
|
50
|
|
|
CommentRepositoryInterface $commentRepository, |
|
51
|
|
|
EventDispatcherInterface $dispatcher |
|
52
|
|
|
) { |
|
53
|
26 |
|
$this->threadRepository = $threadRepository; |
|
54
|
26 |
|
$this->commentRepository = $commentRepository; |
|
55
|
26 |
|
$this->dispatcher = $dispatcher; |
|
56
|
26 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function findComments($type, $entityId, $page = 1, $pageSize = null) |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->commentRepository->findComments($type, $entityId, $page, $pageSize); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function findPublishedComments($type, $entityId, $page = 1, $pageSize = null) |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->commentRepository->findPublishedComments($type, $entityId, $page, $pageSize); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
7 |
|
public function addComment($type, $entityId, CommentInterface $comment, $threadTitle = null) |
|
78
|
|
|
{ |
|
79
|
7 |
|
$thread = $this->threadRepository->findThread($type, $entityId); |
|
80
|
7 |
|
if (!$thread) { |
|
81
|
5 |
|
$thread = $this->threadRepository->createNew($type, $entityId); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
if ($threadTitle) { |
|
|
|
|
|
|
85
|
6 |
|
$thread->setTitle($threadTitle); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
7 |
|
$this->dispatcher->dispatch(Events::PRE_PERSIST_EVENT, new CommentEvent($type, $entityId, $comment, $thread)); |
|
89
|
7 |
|
$this->commentRepository->persist($comment); |
|
90
|
7 |
|
$thread = $thread->addComment($comment); |
|
91
|
7 |
|
$this->dispatcher->dispatch(Events::POST_PERSIST_EVENT, new CommentEvent($type, $entityId, $comment, $thread)); |
|
92
|
|
|
|
|
93
|
7 |
|
return $thread; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function update(CommentInterface $comment) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$thread = $comment->getThread(); |
|
102
|
2 |
|
$this->dispatcher->dispatch( |
|
103
|
2 |
|
Events::PRE_UPDATE_EVENT, |
|
104
|
2 |
|
new CommentEvent($thread->getType(), $thread->getEntityId(), $comment, $thread) |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
2 |
|
return $comment; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
4 |
|
public function delete($ids) |
|
114
|
|
|
{ |
|
115
|
4 |
|
if (!is_array($ids)) { |
|
116
|
1 |
|
$ids = [$ids]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
4 |
|
$comments = $this->commentRepository->findCommentsByIds($ids); |
|
120
|
4 |
|
foreach ($comments as $comment) { |
|
121
|
4 |
|
$this->deleteComment($comment); |
|
122
|
|
|
} |
|
123
|
4 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* {@inheritdoc} |
|
127
|
|
|
*/ |
|
128
|
2 |
|
public function updateThread(ThreadInterface $thread) |
|
129
|
|
|
{ |
|
130
|
2 |
|
$this->dispatcher->dispatch(Events::THREAD_PRE_UPDATE_EVENT, new ThreadEvent($thread)); |
|
131
|
|
|
|
|
132
|
2 |
|
return $thread; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
4 |
|
public function deleteThreads($ids) |
|
139
|
|
|
{ |
|
140
|
4 |
|
if (!is_array($ids)) { |
|
141
|
1 |
|
$ids = [$ids]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
4 |
|
$threads = $this->threadRepository->findThreadsByIds($ids); |
|
145
|
4 |
|
foreach ($threads as $thread) { |
|
146
|
4 |
|
$this->deleteThread($thread); |
|
147
|
|
|
} |
|
148
|
4 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* {@inheritdoc} |
|
152
|
|
|
*/ |
|
153
|
3 |
View Code Duplication |
public function publish(CommentInterface $comment) |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
3 |
|
if ($comment->isPublished()) { |
|
156
|
1 |
|
return $comment; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
2 |
|
$comment->publish(); |
|
160
|
|
|
|
|
161
|
2 |
|
$thread = $comment->getThread(); |
|
162
|
2 |
|
$this->dispatcher->dispatch( |
|
163
|
2 |
|
Events::PUBLISH_EVENT, |
|
164
|
2 |
|
new CommentEvent($thread->getType(), $thread->getEntityId(), $comment, $thread) |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
2 |
|
return $comment; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
3 |
View Code Duplication |
public function unpublish(CommentInterface $comment) |
|
|
|
|
|
|
174
|
|
|
{ |
|
175
|
3 |
|
if (!$comment->isPublished()) { |
|
176
|
1 |
|
return $comment; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
$comment->unpublish(); |
|
180
|
|
|
|
|
181
|
2 |
|
$thread = $comment->getThread(); |
|
182
|
2 |
|
$this->dispatcher->dispatch( |
|
183
|
2 |
|
Events::UNPUBLISH_EVENT, |
|
184
|
2 |
|
new CommentEvent($thread->getType(), $thread->getEntityId(), $comment, $thread) |
|
185
|
|
|
); |
|
186
|
|
|
|
|
187
|
2 |
|
return $comment; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Delete comment and raise pre/post events. |
|
192
|
|
|
* |
|
193
|
|
|
* @param CommentInterface $comment |
|
194
|
|
|
*/ |
|
195
|
4 |
|
private function deleteComment(CommentInterface $comment) |
|
196
|
|
|
{ |
|
197
|
4 |
|
$thread = $comment->getThread(); |
|
198
|
4 |
|
$preEvent = new CommentEvent($thread->getType(), $thread->getEntityId(), $comment, $thread); |
|
199
|
4 |
|
$this->dispatcher->dispatch(Events::PRE_DELETE_EVENT, $preEvent); |
|
200
|
|
|
|
|
201
|
4 |
|
$thread->removeComment($comment); |
|
202
|
4 |
|
$this->commentRepository->delete($comment); |
|
203
|
|
|
|
|
204
|
4 |
|
$postEvent = new CommentEvent($thread->getType(), $thread->getEntityId(), $comment, $thread); |
|
205
|
4 |
|
$this->dispatcher->dispatch(Events::POST_DELETE_EVENT, $postEvent); |
|
206
|
4 |
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Delete thread and raise pre/post events. |
|
210
|
|
|
* |
|
211
|
|
|
* @param ThreadInterface $thread |
|
212
|
|
|
*/ |
|
213
|
4 |
|
private function deleteThread(ThreadInterface $thread) |
|
214
|
|
|
{ |
|
215
|
4 |
|
$this->dispatcher->dispatch(Events::THREAD_PRE_DELETE_EVENT, new ThreadEvent($thread)); |
|
216
|
|
|
|
|
217
|
4 |
|
$this->threadRepository->delete($thread); |
|
218
|
|
|
|
|
219
|
4 |
|
$this->dispatcher->dispatch(Events::THREAD_POST_DELETE_EVENT, new ThreadEvent($thread)); |
|
220
|
4 |
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: