1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Miliooo\MessagingBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Miliooo\Messaging\Form\FormFactory\NewThreadMessageFormFactory; |
14
|
|
|
use Miliooo\Messaging\Form\FormHandler\NewSingleThreadFormHandler; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Miliooo\Messaging\User\ParticipantProviderInterface; |
18
|
|
|
use Miliooo\Messaging\Helpers\FlashMessages\FlashMessageProviderInterface; |
19
|
|
|
use Symfony\Component\Routing\RouterInterface; |
20
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Controller for adding new threads. |
25
|
|
|
* |
26
|
|
|
* This controller is responsible for creating new threads for the logged in user |
27
|
|
|
* |
28
|
|
|
* @author Michiel Boeckaert <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class NewThreadController |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* A new thread message form factory instance. |
34
|
|
|
* |
35
|
|
|
* @var NewThreadMessageFormFactory |
36
|
|
|
*/ |
37
|
|
|
protected $formFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A new single thread form handler. |
41
|
|
|
* |
42
|
|
|
* @var NewSingleThreadFormHandler |
43
|
|
|
*/ |
44
|
|
|
protected $formHandler; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A participant provider. |
48
|
|
|
* |
49
|
|
|
* @var ParticipantProviderInterface |
50
|
|
|
*/ |
51
|
|
|
protected $participantProvider; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* An templating instance. |
55
|
|
|
* |
56
|
|
|
* @var EngineInterface |
57
|
|
|
*/ |
58
|
|
|
protected $templating; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* A flash message provider. |
62
|
|
|
* |
63
|
|
|
* @var FlashMessageProviderInterface |
64
|
|
|
*/ |
65
|
|
|
protected $flashMessageProvider; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* A router instance. |
69
|
|
|
* |
70
|
|
|
* @var RouterInterface |
71
|
|
|
*/ |
72
|
|
|
protected $router; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Constructor. |
76
|
|
|
* |
77
|
|
|
* @param NewThreadMessageFormFactory $formFactory A formfactory instance for a new thread |
78
|
|
|
* @param NewSingleThreadFormHandler $formHandler A new single thread formhandler instance |
79
|
|
|
* @param ParticipantProviderInterface $participantProvider A participant provider |
80
|
|
|
* @param EngineInterface $templating A templating engine instance |
81
|
|
|
* @param FlashMessageProviderInterface $flashMessageProvider A flash message provider instance |
82
|
|
|
* @param RouterInterface $router A router instance |
83
|
|
|
*/ |
84
|
|
|
public function __construct( |
85
|
|
|
NewThreadMessageFormFactory $formFactory, |
86
|
|
|
NewSingleThreadFormHandler $formHandler, |
87
|
|
|
ParticipantProviderInterface $participantProvider, |
88
|
|
|
EngineInterface $templating, |
89
|
|
|
FlashMessageProviderInterface $flashMessageProvider, |
90
|
|
|
RouterInterface $router |
91
|
|
|
) { |
|
|
|
|
92
|
|
|
$this->formFactory = $formFactory; |
93
|
|
|
$this->formHandler = $formHandler; |
94
|
|
|
$this->participantProvider = $participantProvider; |
95
|
|
|
$this->templating = $templating; |
96
|
|
|
$this->flashMessageProvider = $flashMessageProvider; |
97
|
|
|
$this->router = $router; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create a new thread |
103
|
|
|
* |
104
|
|
|
* This method is responsible for showing the form for creating a new thread |
105
|
|
|
* When the form is submitted it has to process the creation of that thread |
106
|
|
|
* and return an response. |
107
|
|
|
* |
108
|
|
|
* @param Request $request |
109
|
|
|
* |
110
|
|
|
* @return Response |
111
|
|
|
*/ |
112
|
|
|
public function createAction(Request $request) |
113
|
|
|
{ |
114
|
|
|
$sender = $this->participantProvider->getAuthenticatedParticipant(); |
115
|
|
|
|
116
|
|
|
$recipient = $request->query->get('recipient', false); |
117
|
|
|
$form = $this->formFactory->create($sender, $recipient); |
118
|
|
|
$processed = $this->formHandler->process($form); |
119
|
|
|
if ($processed) { |
120
|
|
|
return $this->doProcessValidForm(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$twig = 'MilioooMessagingBundle:NewThread:new_thread.html.twig'; |
124
|
|
|
|
125
|
|
|
return $this->templating->renderResponse($twig, ['form' => $form->createView()]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Processes a valid form. |
130
|
|
|
* |
131
|
|
|
* Here we process a valid submitted form. |
132
|
|
|
* We add a flash message and redirect the user to the new thread page. |
133
|
|
|
* |
134
|
|
|
* @return RedirectResponse |
135
|
|
|
*/ |
136
|
|
|
protected function doProcessValidForm() |
137
|
|
|
{ |
138
|
|
|
$this->flashMessageProvider->addFlash( |
139
|
|
|
FlashMessageProviderInterface::TYPE_SUCCESS, |
140
|
|
|
'flash.thread_created_success' |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$url = $this->router->generate('miliooo_message_thread_new'); |
144
|
|
|
|
145
|
|
|
return new RedirectResponse($url); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|