|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[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 Sonata\NewsBundle\Controller; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\NewsBundle\Model\BlogInterface; |
|
15
|
|
|
use Sonata\NewsBundle\Model\CommentInterface; |
|
16
|
|
|
use Sonata\NewsBundle\Model\CommentManagerInterface; |
|
17
|
|
|
use Sonata\NewsBundle\Model\PostInterface; |
|
18
|
|
|
use Sonata\NewsBundle\Model\PostManagerInterface; |
|
19
|
|
|
use Sonata\SeoBundle\Seo\SeoPageInterface; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
21
|
|
|
use Symfony\Component\Form\FormInterface; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
23
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
26
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
27
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
28
|
|
|
|
|
29
|
|
|
class PostController extends Controller |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @param Request $request |
|
33
|
|
|
* |
|
34
|
|
|
* @return RedirectResponse |
|
35
|
|
|
*/ |
|
36
|
|
|
public function homeAction(Request $request) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
return $this->redirect($this->generateUrl('sonata_news_archive')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @param array $criteria |
|
44
|
|
|
* @param array $parameters |
|
45
|
|
|
* |
|
46
|
|
|
* @return Response |
|
47
|
|
|
*/ |
|
48
|
|
|
public function renderArchive(Request $request, array $criteria = array(), array $parameters = array()) |
|
49
|
|
|
{ |
|
50
|
|
|
$pager = $this->getPostManager()->getPager( |
|
51
|
|
|
$criteria, |
|
52
|
|
|
$request->get('page', 1) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$parameters = array_merge(array( |
|
56
|
|
|
'pager' => $pager, |
|
57
|
|
|
'blog' => $this->getBlog(), |
|
58
|
|
|
'tag' => false, |
|
59
|
|
|
'collection' => false, |
|
60
|
|
|
'route' => $request->get('_route'), |
|
61
|
|
|
'route_parameters' => $request->get('_route_params'), |
|
62
|
|
|
), $parameters); |
|
63
|
|
|
|
|
64
|
|
|
$response = $this->render(sprintf('SonataNewsBundle:Post:archive.%s.twig', $request->getRequestFormat()), $parameters); |
|
65
|
|
|
|
|
66
|
|
|
if ('rss' === $request->getRequestFormat()) { |
|
67
|
|
|
$response->headers->set('Content-Type', 'application/rss+xml'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $response; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param Request $request |
|
75
|
|
|
* |
|
76
|
|
|
* @return Response |
|
77
|
|
|
*/ |
|
78
|
|
|
public function archiveAction(Request $request) |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->renderArchive($request); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Request $request |
|
85
|
|
|
* @param string $tag |
|
86
|
|
|
* |
|
87
|
|
|
* @return Response |
|
88
|
|
|
*/ |
|
89
|
|
|
public function tagAction(Request $request, $tag) |
|
90
|
|
|
{ |
|
91
|
|
|
$tag = $this->get('sonata.classification.manager.tag')->findOneBy(array( |
|
92
|
|
|
'slug' => $tag, |
|
93
|
|
|
'enabled' => true, |
|
94
|
|
|
)); |
|
95
|
|
|
|
|
96
|
|
|
if (!$tag || !$tag->getEnabled()) { |
|
97
|
|
|
throw new NotFoundHttpException('Unable to find the tag'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->renderArchive($request, array( |
|
101
|
|
|
'tag' => $tag->getSlug() |
|
102
|
|
|
), array( |
|
103
|
|
|
'tag' => $tag |
|
104
|
|
|
)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param Request $request |
|
109
|
|
|
* @param string $collection |
|
110
|
|
|
* |
|
111
|
|
|
* @return Response |
|
112
|
|
|
* |
|
113
|
|
|
* @throws NotFoundHttpException |
|
114
|
|
|
*/ |
|
115
|
|
|
public function collectionAction(Request $request, $collection) |
|
116
|
|
|
{ |
|
117
|
|
|
$collection = $this->get('sonata.classification.manager.collection')->findOneBy(array( |
|
118
|
|
|
'slug' => $collection, |
|
119
|
|
|
'enabled' => true, |
|
120
|
|
|
)); |
|
121
|
|
|
|
|
122
|
|
|
if (!$collection || !$collection->getEnabled()) { |
|
123
|
|
|
throw new NotFoundHttpException('Unable to find the collection'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $this->renderArchive($request, array( |
|
127
|
|
|
'collection' => $collection |
|
128
|
|
|
), array( |
|
129
|
|
|
'collection' => $collection |
|
130
|
|
|
)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $year |
|
135
|
|
|
* @param string $month |
|
136
|
|
|
* @param Request $request |
|
137
|
|
|
* |
|
138
|
|
|
* @return Response |
|
139
|
|
|
*/ |
|
140
|
|
|
public function archiveMonthlyAction(Request $request, $year, $month) |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->renderArchive($request, array( |
|
143
|
|
|
'date' => $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, $month, 1), 'month'), |
|
144
|
|
|
)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $year |
|
149
|
|
|
* @param Request $request |
|
150
|
|
|
* |
|
151
|
|
|
* @return Response |
|
152
|
|
|
*/ |
|
153
|
|
|
public function archiveYearlyAction(Request $request, $year) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->renderArchive($request, array( |
|
156
|
|
|
'date' => $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, 1, 1), 'year'), |
|
157
|
|
|
)); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @throws NotFoundHttpException |
|
162
|
|
|
* |
|
163
|
|
|
* @param $permalink |
|
164
|
|
|
* |
|
165
|
|
|
* @return Response |
|
166
|
|
|
*/ |
|
167
|
|
|
public function viewAction(Request $request, $permalink) |
|
|
|
|
|
|
168
|
|
|
{ |
|
169
|
|
|
$post = $this->getPostManager()->findOneByPermalink($permalink, $this->getBlog()); |
|
170
|
|
|
|
|
171
|
|
|
if (!$post || !$post->isPublic()) { |
|
172
|
|
|
throw new NotFoundHttpException('Unable to find the post'); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if ($seoPage = $this->getSeoPage()) { |
|
176
|
|
|
$seoPage |
|
177
|
|
|
->setTitle($post->getTitle()) |
|
178
|
|
|
->addMeta('name', 'description', $post->getAbstract()) |
|
179
|
|
|
->addMeta('property', 'og:title', $post->getTitle()) |
|
180
|
|
|
->addMeta('property', 'og:type', 'blog') |
|
181
|
|
|
->addMeta('property', 'og:url', $this->generateUrl('sonata_news_view', array( |
|
182
|
|
|
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post, true), |
|
|
|
|
|
|
183
|
|
|
), UrlGeneratorInterface::ABSOLUTE_URL)) |
|
184
|
|
|
->addMeta('property', 'og:description', $post->getAbstract()) |
|
185
|
|
|
; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $this->render('SonataNewsBundle:Post:view.html.twig', array( |
|
189
|
|
|
'post' => $post, |
|
190
|
|
|
'form' => false, |
|
191
|
|
|
'blog' => $this->getBlog(), |
|
192
|
|
|
)); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @return SeoPageInterface |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getSeoPage() |
|
199
|
|
|
{ |
|
200
|
|
|
if ($this->has('sonata.seo.page')) { |
|
201
|
|
|
return $this->get('sonata.seo.page'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param Request $request |
|
209
|
|
|
* @param int $postId |
|
210
|
|
|
* |
|
211
|
|
|
* @return Response |
|
212
|
|
|
*/ |
|
213
|
|
|
public function commentsAction(Request $request, $postId) |
|
|
|
|
|
|
214
|
|
|
{ |
|
215
|
|
|
$pager = $this->getCommentManager() |
|
216
|
|
|
->getPager(array( |
|
217
|
|
|
'postId' => $postId, |
|
218
|
|
|
'status' => CommentInterface::STATUS_VALID, |
|
219
|
|
|
), 1, 500); //no limit |
|
220
|
|
|
|
|
221
|
|
|
return $this->render('SonataNewsBundle:Post:comments.html.twig', array( |
|
222
|
|
|
'pager' => $pager, |
|
223
|
|
|
)); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param Request $request |
|
228
|
|
|
* @param string $postId |
|
229
|
|
|
* @param bool $form |
|
230
|
|
|
* |
|
231
|
|
|
* @return Response |
|
232
|
|
|
*/ |
|
233
|
|
|
public function addCommentFormAction(Request $request, $postId, $form = false) |
|
|
|
|
|
|
234
|
|
|
{ |
|
235
|
|
|
if (!$form) { |
|
236
|
|
|
$post = $this->getPostManager()->findOneBy(array( |
|
237
|
|
|
'id' => $postId, |
|
238
|
|
|
)); |
|
239
|
|
|
|
|
240
|
|
|
$form = $this->getCommentForm($post); |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return $this->render('SonataNewsBundle:Post:comment_form.html.twig', array( |
|
244
|
|
|
'form' => $form->createView(), |
|
|
|
|
|
|
245
|
|
|
'post_id' => $postId, |
|
246
|
|
|
)); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param PostInterface $post |
|
251
|
|
|
* |
|
252
|
|
|
* @return FormInterface |
|
253
|
|
|
*/ |
|
254
|
|
|
public function getCommentForm(PostInterface $post) |
|
255
|
|
|
{ |
|
256
|
|
|
$comment = $this->getCommentManager()->create(); |
|
257
|
|
|
$comment->setPost($post); |
|
258
|
|
|
$comment->setStatus($post->getCommentsDefaultStatus()); |
|
259
|
|
|
|
|
260
|
|
|
return $this->get('form.factory')->createNamed('comment', 'sonata_post_comment', $comment, array( |
|
261
|
|
|
'action' => $this->generateUrl('sonata_news_add_comment', array( |
|
262
|
|
|
'id' => $post->getId(), |
|
263
|
|
|
)), |
|
264
|
|
|
'method' => 'POST', |
|
265
|
|
|
)); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @throws NotFoundHttpException |
|
270
|
|
|
* |
|
271
|
|
|
* @param Request $request |
|
272
|
|
|
* @param string $id |
|
273
|
|
|
* |
|
274
|
|
|
* @return Response |
|
275
|
|
|
*/ |
|
276
|
|
|
public function addCommentAction(Request $request, $id) |
|
277
|
|
|
{ |
|
278
|
|
|
$post = $this->getPostManager()->findOneBy(array( |
|
279
|
|
|
'id' => $id, |
|
280
|
|
|
)); |
|
281
|
|
|
|
|
282
|
|
|
if (!$post) { |
|
283
|
|
|
throw new NotFoundHttpException(sprintf('Post (%d) not found', $id)); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
if (!$post->isCommentable()) { |
|
287
|
|
|
// todo add notice |
|
288
|
|
|
return new RedirectResponse($this->generateUrl('sonata_news_view', array( |
|
289
|
|
|
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post), |
|
290
|
|
|
))); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$form = $this->getCommentForm($post); |
|
294
|
|
|
$form->submit($request); |
|
295
|
|
|
|
|
296
|
|
|
if ($form->isValid()) { |
|
297
|
|
|
$comment = $form->getData(); |
|
298
|
|
|
|
|
299
|
|
|
$this->getCommentManager()->save($comment); |
|
300
|
|
|
$this->get('sonata.news.mailer')->sendCommentNotification($comment); |
|
301
|
|
|
|
|
302
|
|
|
// todo : add notice |
|
303
|
|
|
return new RedirectResponse($this->generateUrl('sonata_news_view', array( |
|
304
|
|
|
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post), |
|
305
|
|
|
))); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
return $this->render('SonataNewsBundle:Post:view.html.twig', array( |
|
309
|
|
|
'post' => $post, |
|
310
|
|
|
'form' => $form, |
|
311
|
|
|
)); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* @param Request $request |
|
316
|
|
|
* @param string $commentId |
|
317
|
|
|
* @param string $hash |
|
318
|
|
|
* @param string $status |
|
319
|
|
|
* |
|
320
|
|
|
* @return RedirectResponse |
|
321
|
|
|
*/ |
|
322
|
|
|
public function commentModerationAction(Request $request, $commentId, $hash, $status) |
|
|
|
|
|
|
323
|
|
|
{ |
|
324
|
|
|
$comment = $this->getCommentManager()->findOneBy(array('id' => $commentId)); |
|
325
|
|
|
|
|
326
|
|
|
if (!$comment) { |
|
327
|
|
|
throw new AccessDeniedException(); |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
$computedHash = $this->get('sonata.news.hash.generator')->generate($comment); |
|
331
|
|
|
|
|
332
|
|
|
if ($computedHash != $hash) { |
|
333
|
|
|
throw new AccessDeniedException(); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
$comment->setStatus($status); |
|
337
|
|
|
|
|
338
|
|
|
$this->getCommentManager()->save($comment); |
|
339
|
|
|
|
|
340
|
|
|
return new RedirectResponse($this->generateUrl('sonata_news_view', array( |
|
341
|
|
|
'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($comment->getPost()), |
|
342
|
|
|
))); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* @return PostManagerInterface |
|
347
|
|
|
*/ |
|
348
|
|
|
protected function getPostManager() |
|
349
|
|
|
{ |
|
350
|
|
|
return $this->get('sonata.news.manager.post'); |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @return CommentManagerInterface |
|
355
|
|
|
*/ |
|
356
|
|
|
protected function getCommentManager() |
|
357
|
|
|
{ |
|
358
|
|
|
return $this->get('sonata.news.manager.comment'); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @return BlogInterface |
|
363
|
|
|
*/ |
|
364
|
|
|
protected function getBlog() |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->get('sonata.news.blog'); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.