forked from sonata-project/SonataNewsBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostController.php
324 lines (278 loc) · 8.37 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\NewsBundle\Controller;
// NEXT_MAJOR: remove this file
@trigger_error(
'The '.__NAMESPACE__.'\PostController class is deprecated since version 3.x and will be removed in 4.0.'
.' Use '.__NAMESPACE__.'\Action\* classes instead.',
E_USER_DEPRECATED
);
use Sonata\NewsBundle\Action\CollectionPostArchiveAction;
use Sonata\NewsBundle\Action\CommentListAction;
use Sonata\NewsBundle\Action\HomeAction;
use Sonata\NewsBundle\Action\ModerateCommentAction;
use Sonata\NewsBundle\Action\MonthlyPostArchiveAction;
use Sonata\NewsBundle\Action\PostArchiveAction;
use Sonata\NewsBundle\Action\TagPostArchiveAction;
use Sonata\NewsBundle\Action\ViewPostAction;
use Sonata\NewsBundle\Action\YearlyPostArchiveAction;
use Sonata\NewsBundle\Form\Type\CommentType;
use Sonata\NewsBundle\Model\BlogInterface;
use Sonata\NewsBundle\Model\CommentManagerInterface;
use Sonata\NewsBundle\Model\PostInterface;
use Sonata\NewsBundle\Model\PostManagerInterface;
use Sonata\SeoBundle\Seo\SeoPageInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class PostController extends Controller
{
/**
* @return RedirectResponse
*/
public function homeAction()
{
$action = $this->container->get(HomeAction::class);
return $action();
}
/**
* @param array $criteria
* @param array $parameters
* @param Request $request
*
* @return Response
*/
public function renderArchive(array $criteria = [], array $parameters = [], Request $request = null)
{
$action = $this->container->get(PostArchiveAction::class);
return $action->renderArchive($this->resolveRequest($request), $criteria, $parameters);
}
/**
* @param Request $request
*
* @return Response
*/
public function archiveAction(Request $request = null)
{
$action = $this->container->get(PostArchiveAction::class);
return $action($this->resolveRequest($request));
}
/**
* @param string $tag
* @param Request $request
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function tagAction($tag, Request $request = null)
{
$action = $this->container->get(TagPostArchiveAction::class);
return $action($this->resolveRequest($request), $tag);
}
/**
* @param string $collection
* @param Request $request
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function collectionAction($collection, Request $request = null)
{
$action = $this->container->get(CollectionPostArchiveAction::class);
return $action($this->resolveRequest($request), $collection);
}
/**
* @param string $year
* @param string $month
* @param Request $request
*
* @return Response
*/
public function archiveMonthlyAction($year, $month, Request $request = null)
{
$action = $this->container->get(MonthlyPostArchiveAction::class);
return $action($this->resolveRequest($request), $year, $month);
}
/**
* @param string $year
* @param Request $request
*
* @return Response
*/
public function archiveYearlyAction($year, Request $request = null)
{
$action = $this->container->get(YearlyPostArchiveAction::class);
return $action($this->resolveRequest($request), $year);
}
/**
* @param string $permalink
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function viewAction($permalink)
{
$action = $this->container->get(ViewPostAction::class);
return $action($permalink);
}
/**
* @return SeoPageInterface|null
*/
public function getSeoPage()
{
if ($this->has('sonata.seo.page')) {
return $this->get('sonata.seo.page');
}
return null;
}
/**
* @param int $postId
*
* @return Response
*/
public function commentsAction($postId)
{
$action = $this->container->get(CommentListAction::class);
return $action($postId);
}
/**
* @param $postId
* @param bool $form
*
* @return Response
*/
public function addCommentFormAction($postId, $form = false)
{
if (!$form) {
$post = $this->getPostManager()->findOneBy([
'id' => $postId,
]);
$form = $this->getCommentForm($post);
}
return $this->render('@SonataNews/Post/comment_form.html.twig', [
'form' => $form->createView(),
'post_id' => $postId,
]);
}
/**
* @param PostInterface $post
*
* @return FormInterface
*/
public function getCommentForm(PostInterface $post)
{
$comment = $this->getCommentManager()->create();
$comment->setPost($post);
$comment->setStatus($post->getCommentsDefaultStatus());
return $this->get('form.factory')->createNamed('comment', CommentType::class, $comment, [
'action' => $this->generateUrl('sonata_news_add_comment', [
'id' => $post->getId(),
]),
'method' => 'POST',
]);
}
/**
* @param string $id
* @param Request $request
*
* @throws NotFoundHttpException
*
* @return Response
*/
public function addCommentAction($id, Request $request = null)
{
$request = $this->resolveRequest($request);
$post = $this->getPostManager()->findOneBy([
'id' => $id,
]);
if (!$post) {
throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
}
if (!$post->isCommentable()) {
// todo add notice.
return new RedirectResponse($this->generateUrl('sonata_news_view', [
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post),
]));
}
$form = $this->getCommentForm($post);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment = $form->getData();
$this->getCommentManager()->save($comment);
$this->get('sonata.news.mailer')->sendCommentNotification($comment);
// todo : add notice
return new RedirectResponse($this->generateUrl('sonata_news_view', [
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post),
]));
}
return $this->render('@SonataNews/Post/view.html.twig', [
'post' => $post,
'form' => $form,
]);
}
/**
* @param string $commentId
* @param string $hash
* @param string $status
*
* @throws AccessDeniedException
*
* @return RedirectResponse
*/
public function commentModerationAction($commentId, $hash, $status)
{
$action = $this->container->get(ModerateCommentAction::class);
return $action($commentId, $hash, $status);
}
/**
* @return PostManagerInterface
*/
protected function getPostManager()
{
return $this->get('sonata.news.manager.post');
}
/**
* @return CommentManagerInterface
*/
protected function getCommentManager()
{
return $this->get('sonata.news.manager.comment');
}
/**
* @return BlogInterface
*/
protected function getBlog()
{
return $this->get('sonata.news.blog');
}
/**
* To keep backwards compatibility with older Sonata News code.
*
* @internal
*
* @param Request $request
*
* @return Request
*/
private function resolveRequest(Request $request = null)
{
if (null === $request) {
return $this->get('request_stack')->getCurrentRequest();
}
return $request;
}
}