1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Question\Controllers; |
4
|
|
|
|
5
|
|
|
use Ramsey\Uuid\UuidFactoryInterface; |
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
7
|
|
|
use Symfony\Component\Form\FormInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
11
|
|
|
use VSV\GVQ_API\Common\ValueObjects\Languages; |
12
|
|
|
use VSV\GVQ_API\Image\Controllers\ImageController; |
13
|
|
|
use VSV\GVQ_API\Question\Forms\QuestionFormType; |
14
|
|
|
use VSV\GVQ_API\Question\Models\Question; |
15
|
|
|
use VSV\GVQ_API\Question\Repositories\CategoryRepository; |
16
|
|
|
use VSV\GVQ_API\Question\Repositories\QuestionRepository; |
17
|
|
|
|
18
|
|
|
class QuestionViewController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var UuidFactoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $uuidFactory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var QuestionRepository |
27
|
|
|
*/ |
28
|
|
|
private $questionRepository; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CategoryRepository |
32
|
|
|
*/ |
33
|
|
|
private $categoryRepository; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ImageController |
37
|
|
|
*/ |
38
|
|
|
private $imageController; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var TranslatorInterface |
42
|
|
|
*/ |
43
|
|
|
private $translator; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var QuestionFormType |
47
|
|
|
*/ |
48
|
|
|
private $questionFormType; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param UuidFactoryInterface $uuidFactory |
52
|
|
|
* @param QuestionRepository $questionRepository |
53
|
|
|
* @param CategoryRepository $categoryRepository |
54
|
|
|
* @param ImageController $imageController |
55
|
|
|
* @param TranslatorInterface $translator |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
UuidFactoryInterface $uuidFactory, |
59
|
|
|
QuestionRepository $questionRepository, |
60
|
|
|
CategoryRepository $categoryRepository, |
61
|
|
|
ImageController $imageController, |
62
|
|
|
TranslatorInterface $translator |
63
|
|
|
) { |
64
|
|
|
$this->uuidFactory = $uuidFactory; |
65
|
|
|
$this->questionRepository = $questionRepository; |
66
|
|
|
$this->categoryRepository = $categoryRepository; |
67
|
|
|
$this->imageController = $imageController; |
68
|
|
|
$this->translator = $translator; |
69
|
|
|
|
70
|
|
|
$this->questionFormType = new QuestionFormType(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Response |
75
|
|
|
*/ |
76
|
|
|
public function index(): Response |
77
|
|
|
{ |
78
|
|
|
$questions = $this->questionRepository->getAll(); |
79
|
|
|
|
80
|
|
|
return $this->render( |
81
|
|
|
'questions/index.html.twig', |
82
|
|
|
[ |
83
|
|
|
'questions' => $questions ? $questions->toArray() : [], |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Request $request |
90
|
|
|
* @return Response |
91
|
|
|
* @throws \League\Flysystem\FileExistsException |
92
|
|
|
*/ |
93
|
|
|
public function add(Request $request): Response |
94
|
|
|
{ |
95
|
|
|
$form = $this->createQuestionForm(null); |
96
|
|
|
$form->handleRequest($request); |
97
|
|
|
|
98
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
99
|
|
|
$data = $form->getData(); |
100
|
|
|
|
101
|
|
|
$fileName = $this->imageController->handleImage($data['image']); |
102
|
|
|
|
103
|
|
|
$question = $this->questionFormType->newQuestionFromData( |
104
|
|
|
$this->uuidFactory, |
105
|
|
|
$fileName, |
106
|
|
|
$data |
107
|
|
|
); |
108
|
|
|
$this->questionRepository->save($question); |
109
|
|
|
|
110
|
|
|
$this->addFlash('success', 'De nieuwe vraag '.$question->getId()->toString().' is bewaard.'); |
111
|
|
|
return $this->redirectToRoute('questions_view_index'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->render( |
115
|
|
|
'questions/add.html.twig', |
116
|
|
|
[ |
117
|
|
|
'form' => $form->createView() |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Request $request |
124
|
|
|
* @param string $id |
125
|
|
|
* @return Response |
126
|
|
|
*/ |
127
|
|
|
public function edit(Request $request, string $id): Response |
128
|
|
|
{ |
129
|
|
|
$question = $this->questionRepository->getById( |
130
|
|
|
$this->uuidFactory->fromString($id) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
if (!$question) { |
134
|
|
|
$this->addFlash('warning', 'Geen vraag gevonden met id '.$id.' om aan te passen.'); |
135
|
|
|
return $this->redirectToRoute('questions_view_index'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$form = $this->createQuestionForm($question); |
139
|
|
|
$form->handleRequest($request); |
140
|
|
|
|
141
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
142
|
|
|
$question = $this->questionFormType->updateQuestionFromData( |
143
|
|
|
$question, |
144
|
|
|
$form->getData() |
145
|
|
|
); |
146
|
|
|
$this->questionRepository->update($question); |
147
|
|
|
|
148
|
|
|
$this->addFlash('success', 'Vraag '.$id.' is aangepast.'); |
149
|
|
|
return $this->redirectToRoute('questions_view_index'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->render( |
153
|
|
|
'questions/add.html.twig', |
154
|
|
|
[ |
155
|
|
|
'form' => $form->createView() |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Request $request |
162
|
|
|
* @param string $id |
163
|
|
|
* @return Response |
164
|
|
|
*/ |
165
|
|
|
public function delete(Request $request, string $id): Response |
166
|
|
|
{ |
167
|
|
|
if ($request->getMethod() === 'POST') { |
168
|
|
|
$this->questionRepository->delete( |
169
|
|
|
$this->uuidFactory->fromString($id) |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
$this->addFlash('success', 'Vraag '.$id.' is verwijderd.'); |
173
|
|
|
|
174
|
|
|
return $this->redirectToRoute('questions_view_index'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $this->render( |
178
|
|
|
'questions/delete.html.twig', |
179
|
|
|
[ |
180
|
|
|
'id' => $id, |
181
|
|
|
] |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param null|Question $question |
187
|
|
|
* @return FormInterface |
188
|
|
|
*/ |
189
|
|
|
private function createQuestionForm(?Question $question): FormInterface |
190
|
|
|
{ |
191
|
|
|
$formBuilder = $this->createFormBuilder(); |
192
|
|
|
|
193
|
|
|
$this->questionFormType->buildForm( |
194
|
|
|
$formBuilder, |
195
|
|
|
[ |
196
|
|
|
'languages' => new Languages(), |
197
|
|
|
'categories' => $this->categoryRepository->getAll(), |
198
|
|
|
'question' => $question, |
199
|
|
|
'translator' => $this->translator, |
200
|
|
|
] |
201
|
|
|
); |
202
|
|
|
|
203
|
|
|
return $formBuilder->getForm(); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|