|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Sulu. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sulu 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\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityManager; |
|
15
|
|
|
use FOS\RestBundle\Controller\Annotations\NamePrefix; |
|
16
|
|
|
use FOS\RestBundle\Controller\Annotations\Post; |
|
17
|
|
|
use FOS\RestBundle\Controller\Annotations\RouteResource; |
|
18
|
|
|
use FOS\RestBundle\Routing\ClassResourceInterface; |
|
19
|
|
|
use Sulu\Bundle\CommentBundle\Entity\Comment; |
|
20
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentInterface; |
|
21
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentRepository; |
|
22
|
|
|
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface; |
|
23
|
|
|
use Sulu\Bundle\CommentBundle\Form\Type\CommentType; |
|
24
|
|
|
use Sulu\Bundle\CommentBundle\Manager\CommentManagerInterface; |
|
25
|
|
|
use Sulu\Component\Rest\RestController; |
|
26
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
27
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
28
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @RouteResource("thread") |
|
32
|
|
|
* @NamePrefix("sulu_comment.") |
|
33
|
|
|
*/ |
|
34
|
|
|
class WebsiteCommentController extends RestController implements ClassResourceInterface |
|
35
|
1 |
|
{ |
|
36
|
|
|
/** |
|
37
|
1 |
|
* Returns list of comments for given thread. |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function cgetCommentsAction(string $threadId, Request $request): Response |
|
40
|
|
|
{ |
|
41
|
1 |
|
list($type, $entityId) = $this->getThreadIdParts($threadId); |
|
42
|
1 |
|
|
|
43
|
|
|
$page = $request->get('page'); |
|
44
|
|
|
$referrer = $request->get('referrer'); |
|
45
|
1 |
|
|
|
46
|
1 |
|
$commentManager = $this->get('sulu_comment.manager'); |
|
47
|
|
|
$comments = $commentManager->findPublishedComments( |
|
48
|
|
|
$type, |
|
49
|
1 |
|
$entityId, |
|
50
|
1 |
|
$page ?: 1, |
|
51
|
|
|
$page ? 20 : null |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
if ('json' === $request->getRequestFormat()) { |
|
55
|
|
|
return $this->handleView($this->view($comments)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$response = new Response(); |
|
59
|
|
|
$response->setPrivate(); |
|
60
|
|
|
$response->setMaxAge(0); |
|
61
|
|
|
$response->setSharedMaxAge(0); |
|
62
|
|
|
|
|
63
|
|
|
$form = $this->createForm( |
|
64
|
|
|
CommentType::class, |
|
65
|
|
|
null, |
|
66
|
|
|
[ |
|
67
|
|
|
'data_class' => $this->getParameter('sulu.model.comment.class'), |
|
68
|
|
|
'threadId' => $threadId, |
|
69
|
|
|
'referrer' => $referrer, |
|
70
|
|
|
] |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
return $this->render( |
|
74
|
|
|
$this->getTemplate($type, 'comments'), |
|
75
|
|
|
[ |
|
76
|
|
|
'form' => $form->createView(), |
|
77
|
|
|
'nestedComments' => $this->getNestedCommentsFlag($type), |
|
78
|
9 |
|
'commentTemplate' => $this->getTemplate($type, 'comment'), |
|
79
|
|
|
'commentsTemplate' => $this->getTemplate($type, 'comments'), |
|
80
|
9 |
|
'comments' => $comments, |
|
81
|
9 |
|
'threadId' => $threadId, |
|
82
|
8 |
|
'referrer' => $referrer, |
|
83
|
7 |
|
], |
|
84
|
9 |
|
$response |
|
85
|
|
|
); |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
5 |
|
* Create new comment for given thread. |
|
90
|
|
|
* If the thread does not exists a new will be created. |
|
91
|
|
|
*/ |
|
92
|
5 |
|
public function postCommentsAction(string $threadId, Request $request): Response |
|
93
|
5 |
|
{ |
|
94
|
|
|
list($type, $entityId) = $this->getThreadIdParts($threadId); |
|
95
|
5 |
|
|
|
96
|
5 |
|
/** @var CommentRepositoryInterface $repository */ |
|
97
|
|
|
$repository = $this->get('sulu.repository.comment'); |
|
98
|
|
|
|
|
99
|
5 |
|
/** @var CommentInterface $comment */ |
|
100
|
5 |
|
$comment = $repository->createNew(); |
|
101
|
|
|
|
|
102
|
5 |
|
if ($parent = $request->get('parent')) { |
|
103
|
|
|
$comment->setParent($repository->findCommentById($parent)); |
|
104
|
5 |
|
} |
|
105
|
5 |
|
|
|
106
|
|
|
$form = $this->createForm( |
|
107
|
|
|
CommentType::class, |
|
108
|
|
|
$comment, |
|
109
|
|
|
[ |
|
110
|
|
|
'data_class' => $this->getParameter('sulu.model.comment.class'), |
|
111
|
|
|
'threadId' => $threadId, |
|
112
|
|
|
] |
|
113
|
|
|
); |
|
114
|
|
|
|
|
115
|
|
|
$form->handleRequest($request); |
|
116
|
|
|
|
|
117
|
|
|
if (!$form->isSubmitted() || !$form->isValid()) { |
|
118
|
|
|
return new Response(null, 400); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$comment = $form->getData(); |
|
122
|
|
|
|
|
123
|
|
|
/** @var CommentManagerInterface $commentManager */ |
|
124
|
5 |
|
$commentManager = $this->get('sulu_comment.manager'); |
|
125
|
|
|
$commentManager->addComment($type, $entityId, $comment, $request->get('threadTitle')); |
|
126
|
5 |
|
|
|
127
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
|
128
|
5 |
|
|
|
129
|
|
|
if ($referrer = $request->query->get('referrer')) { |
|
130
|
|
|
return new RedirectResponse($referrer); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ('json' === $request->getRequestFormat()) { |
|
134
|
|
|
return $this->handleView($this->view($comment)); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $this->render( |
|
138
|
|
|
$this->getTemplate($type, 'comment'), |
|
139
|
|
|
[ |
|
140
|
|
|
'comment' => $comment, |
|
141
|
|
|
'threadId' => $threadId, |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @Post("/threads/{threadId}/comments/{commentId}") |
|
148
|
|
|
*/ |
|
149
|
|
|
public function putCommentAction(string $threadId, string $commentId, Request $request): Response |
|
150
|
|
|
{ |
|
151
|
|
|
list($type, $entityId) = $this->getThreadIdParts($threadId); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** @var CommentRepositoryInterface $repository */ |
|
154
|
|
|
$repository = $this->get('sulu.repository.comment'); |
|
|
|
|
|
|
155
|
|
|
$message = $request->request->get('message'); |
|
156
|
|
|
|
|
157
|
|
|
/** @var EntityManager $entityManager */ |
|
158
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
159
|
|
|
|
|
160
|
|
|
/** @var CommentRepository $commentRepository */ |
|
161
|
|
|
$commentRepository = $entityManager->getRepository(Comment::class); |
|
162
|
|
|
|
|
163
|
|
|
/** @var Comment $comment */ |
|
164
|
|
|
$comment = $commentRepository->findCommentById(intval($commentId)); |
|
165
|
|
|
$comment->setMessage($message); |
|
166
|
|
|
$entityManager->flush(); |
|
167
|
|
|
|
|
168
|
|
|
if ($referrer = $request->query->get('referrer')) { |
|
169
|
|
|
return new RedirectResponse($referrer); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if ('json' === $request->getRequestFormat()) { |
|
173
|
|
|
return $this->handleView($this->view($comment)); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $this->render( |
|
177
|
|
|
$this->getTemplate($type, 'comment'), |
|
178
|
|
|
[ |
|
179
|
|
|
'comment' => $comment, |
|
180
|
|
|
'threadId' => $threadId, |
|
181
|
|
|
] |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function deleteCommentAction(string $threadId, string $commentId, Request $request): Response |
|
|
|
|
|
|
186
|
|
|
{ |
|
187
|
|
|
/** @var EntityManager $entityManager */ |
|
188
|
|
|
$entityManager = $this->get('doctrine.orm.entity_manager'); |
|
189
|
|
|
|
|
190
|
|
|
/** @var CommentRepository $commentRepository */ |
|
191
|
|
|
$commentRepository = $entityManager->getRepository(Comment::class); |
|
192
|
|
|
$referrer = $request->get('referrer'); |
|
|
|
|
|
|
193
|
|
|
/** @var Comment $comment */ |
|
194
|
|
|
$comment = $commentRepository->findCommentById(intval($commentId)); |
|
195
|
|
|
|
|
196
|
|
|
$entityManager->remove($comment); |
|
197
|
|
|
$entityManager->flush(); |
|
198
|
|
|
|
|
199
|
|
|
if ($referrer = $request->query->get('referrer')) { |
|
200
|
|
|
return new RedirectResponse($referrer); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if ('json' === $request->getRequestFormat()) { |
|
204
|
|
|
return $this->handleView($this->view()); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
return new Response(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Splits the thread-id into type and entity-id. |
|
212
|
|
|
* |
|
213
|
|
|
* @return string[] list($type, $entityId) |
|
214
|
|
|
*/ |
|
215
|
|
|
private function getThreadIdParts(string $threadId): array |
|
216
|
|
|
{ |
|
217
|
|
|
$pos = strpos($threadId, '-'); |
|
218
|
|
|
if (false === $pos) { |
|
219
|
|
|
throw new \RuntimeException('Thread id is not valid.'); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return [substr($threadId, 0, $pos), substr($threadId, $pos + 1)]; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
View Code Duplication |
private function getTemplate(string $type, string $templateType): string |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
$defaults = $this->getParameter('sulu_comment.default_templates'); |
|
228
|
|
|
|
|
229
|
|
|
$types = $this->getParameter('sulu_comment.types'); |
|
230
|
|
|
if (array_key_exists($type, $types)) { |
|
231
|
|
|
return $types[$type]['templates'][$templateType]; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $defaults[$templateType]; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
View Code Duplication |
private function getNestedCommentsFlag(string $type): string |
|
|
|
|
|
|
238
|
|
|
{ |
|
239
|
|
|
$default = $this->getParameter('sulu_comment.nested_comments'); |
|
240
|
|
|
|
|
241
|
|
|
$types = $this->getParameter('sulu_comment.types'); |
|
242
|
|
|
if (array_key_exists($type, $types)) { |
|
243
|
|
|
return $types[$type]['nested_comments']; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $default; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.