1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Action; |
15
|
|
|
|
16
|
|
|
use Sonata\NewsBundle\Form\Type\CommentType; |
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\Response; |
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
25
|
|
|
|
26
|
|
|
final class CreateCommentFormAction extends Controller |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RouterInterface |
30
|
|
|
*/ |
31
|
|
|
private $router; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PostManagerInterface |
35
|
|
|
*/ |
36
|
|
|
private $postManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var CommentManagerInterface |
40
|
|
|
*/ |
41
|
|
|
private $commentManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var FormFactoryInterface |
45
|
|
|
*/ |
46
|
|
|
private $formFactory; |
47
|
|
|
|
48
|
|
|
public function __construct( |
49
|
|
|
RouterInterface $router, |
50
|
|
|
PostManagerInterface $postManager, |
51
|
|
|
CommentManagerInterface $commentManager, |
52
|
|
|
FormFactoryInterface $formFactory |
53
|
|
|
) { |
54
|
|
|
$this->router = $router; |
55
|
|
|
$this->postManager = $postManager; |
56
|
|
|
$this->commentManager = $commentManager; |
57
|
|
|
$this->formFactory = $formFactory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $postId |
62
|
|
|
* @param bool $form |
63
|
|
|
* |
64
|
|
|
* @return Response |
65
|
|
|
*/ |
66
|
|
|
public function __invoke($postId, $form = false) |
67
|
|
|
{ |
68
|
|
|
if (!$form) { |
69
|
|
|
$post = $this->postManager->findOneBy([ |
70
|
|
|
'id' => $postId, |
71
|
|
|
]); |
72
|
|
|
|
73
|
|
|
$form = $this->getCommentForm($post); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this->render('@SonataNews/Post/comment_form.html.twig', [ |
77
|
|
|
'form' => $form->createView(), |
|
|
|
|
78
|
|
|
'post_id' => $postId, |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return FormInterface |
84
|
|
|
*/ |
85
|
|
|
private function getCommentForm(PostInterface $post) |
86
|
|
|
{ |
87
|
|
|
$comment = $this->commentManager->create(); |
88
|
|
|
$comment->setPost($post); |
89
|
|
|
$comment->setStatus($post->getCommentsDefaultStatus()); |
90
|
|
|
|
91
|
|
|
return $this->formFactory->createNamed('comment', CommentType::class, $comment, [ |
92
|
|
|
'action' => $this->router->generate('sonata_news_add_comment', [ |
93
|
|
|
'id' => $post->getId(), |
94
|
|
|
]), |
95
|
|
|
'method' => 'POST', |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.