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\Action; |
13
|
|
|
|
14
|
|
|
use Sonata\NewsBundle\Form\Type\CommentType; |
15
|
|
|
use Sonata\NewsBundle\Mailer\MailerInterface; |
16
|
|
|
use Sonata\NewsBundle\Model\BlogInterface; |
17
|
|
|
use Sonata\NewsBundle\Model\CommentManagerInterface; |
18
|
|
|
use Sonata\NewsBundle\Model\PostInterface; |
19
|
|
|
use Sonata\NewsBundle\Model\PostManagerInterface; |
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
21
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
22
|
|
|
use Symfony\Component\Form\FormInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\HttpFoundation\Response; |
26
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27
|
|
|
use Symfony\Component\Routing\RouterInterface; |
28
|
|
|
|
29
|
|
|
final class CreateCommentAction extends Controller |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var RouterInterface |
33
|
|
|
*/ |
34
|
|
|
private $router; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var BlogInterface |
38
|
|
|
*/ |
39
|
|
|
private $blog; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var PostManagerInterface |
43
|
|
|
*/ |
44
|
|
|
private $postManager; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var CommentManagerInterface |
48
|
|
|
*/ |
49
|
|
|
private $commentManager; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var FormFactoryInterface |
53
|
|
|
*/ |
54
|
|
|
private $formFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var MailerInterface |
58
|
|
|
*/ |
59
|
|
|
private $mailer; |
60
|
|
|
|
61
|
|
|
public function __construct( |
62
|
|
|
RouterInterface $router, |
63
|
|
|
BlogInterface $blog, |
64
|
|
|
PostManagerInterface $postManager, |
65
|
|
|
CommentManagerInterface $commentManager, |
66
|
|
|
FormFactoryInterface $formFactory, |
67
|
|
|
MailerInterface $mailer |
68
|
|
|
) { |
69
|
|
|
$this->router = $router; |
70
|
|
|
$this->blog = $blog; |
71
|
|
|
$this->postManager = $postManager; |
72
|
|
|
$this->commentManager = $commentManager; |
73
|
|
|
$this->formFactory = $formFactory; |
74
|
|
|
$this->mailer = $mailer; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $id |
79
|
|
|
* @param Request $request |
80
|
|
|
* |
81
|
|
|
* @throws NotFoundHttpException |
82
|
|
|
* |
83
|
|
|
* @return Response |
84
|
|
|
*/ |
85
|
|
|
public function __invoke(Request $request, $id) |
86
|
|
|
{ |
87
|
|
|
$post = $this->postManager->findOneBy([ |
88
|
|
|
'id' => $id, |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
if (!$post) { |
92
|
|
|
throw new NotFoundHttpException(sprintf('Post (%d) not found', $id)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (!$post->isCommentable()) { |
96
|
|
|
// todo add notice. |
97
|
|
|
return new RedirectResponse($this->router->generate('sonata_news_view', [ |
98
|
|
|
'permalink' => $this->blog->getPermalinkGenerator()->generate($post), |
99
|
|
|
])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$form = $this->getCommentForm($post); |
103
|
|
|
$form->handleRequest($request); |
104
|
|
|
|
105
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
106
|
|
|
$comment = $form->getData(); |
107
|
|
|
|
108
|
|
|
$this->commentManager->save($comment); |
109
|
|
|
$this->mailer->sendCommentNotification($comment); |
110
|
|
|
|
111
|
|
|
// todo : add notice |
112
|
|
|
return new RedirectResponse($this->router->generate('sonata_news_view', [ |
113
|
|
|
'permalink' => $this->blog->getPermalinkGenerator()->generate($post), |
114
|
|
|
])); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->render('@SonataNews/Post/view.html.twig', [ |
118
|
|
|
'post' => $post, |
119
|
|
|
'form' => $form, |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return FormInterface |
125
|
|
|
*/ |
126
|
|
|
private function getCommentForm(PostInterface $post) |
127
|
|
|
{ |
128
|
|
|
$comment = $this->commentManager->create(); |
129
|
|
|
$comment->setPost($post); |
130
|
|
|
$comment->setStatus($post->getCommentsDefaultStatus()); |
131
|
|
|
|
132
|
|
|
return $this->formFactory->createNamed('comment', CommentType::class, $comment, [ |
133
|
|
|
'action' => $this->router->generate('sonata_news_add_comment', [ |
134
|
|
|
'id' => $post->getId(), |
135
|
|
|
]), |
136
|
|
|
'method' => 'POST', |
137
|
|
|
]); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|