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\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use VSV\GVQ_API\Common\ValueObjects\Languages; |
10
|
|
|
use VSV\GVQ_API\Image\Controllers\ImageController; |
11
|
|
|
use VSV\GVQ_API\Question\Forms\QuestionFormDTO; |
12
|
|
|
use VSV\GVQ_API\Question\Forms\QuestionFormType; |
13
|
|
|
use VSV\GVQ_API\Question\Repositories\CategoryRepository; |
14
|
|
|
use VSV\GVQ_API\Question\Repositories\QuestionRepository; |
15
|
|
|
|
16
|
|
|
class QuestionViewController extends AbstractController |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var UuidFactoryInterface |
20
|
|
|
*/ |
21
|
|
|
private $uuidFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var QuestionRepository |
25
|
|
|
*/ |
26
|
|
|
private $questionRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CategoryRepository |
30
|
|
|
*/ |
31
|
|
|
private $categoryRepository; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ImageController |
35
|
|
|
*/ |
36
|
|
|
private $imageController; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param UuidFactoryInterface $uuidFactory |
40
|
|
|
* @param QuestionRepository $questionRepository |
41
|
|
|
* @param CategoryRepository $categoryRepository |
42
|
|
|
* @param ImageController $imageController |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
UuidFactoryInterface $uuidFactory, |
46
|
|
|
QuestionRepository $questionRepository, |
47
|
|
|
CategoryRepository $categoryRepository, |
48
|
|
|
ImageController $imageController |
49
|
|
|
) { |
50
|
|
|
$this->uuidFactory = $uuidFactory; |
51
|
|
|
$this->questionRepository = $questionRepository; |
52
|
|
|
$this->categoryRepository = $categoryRepository; |
53
|
|
|
$this->imageController = $imageController; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Response |
58
|
|
|
*/ |
59
|
|
|
public function index(): Response |
60
|
|
|
{ |
61
|
|
|
$questions = $this->questionRepository->getAll(); |
62
|
|
|
|
63
|
|
|
return $this->render( |
64
|
|
|
'questions/index.html.twig', |
65
|
|
|
[ |
66
|
|
|
'questions' => $questions ? $questions->toArray() : [], |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Request $request |
73
|
|
|
* @return Response |
74
|
|
|
* @throws \League\Flysystem\FileExistsException |
75
|
|
|
*/ |
76
|
|
|
public function add(Request $request): Response |
77
|
|
|
{ |
78
|
|
|
$categories = $this->categoryRepository->getAll(); |
79
|
|
|
$questionFormDTO = new QuestionFormDTO(); |
80
|
|
|
|
81
|
|
|
$form = $this->createForm( |
82
|
|
|
QuestionFormType::class, |
83
|
|
|
$questionFormDTO, |
84
|
|
|
[ |
85
|
|
|
'languages' => new Languages(), |
86
|
|
|
'categories' => $categories, |
87
|
|
|
] |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
$form->handleRequest($request); |
91
|
|
|
|
92
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
93
|
|
|
$fileName = $this->imageController->handleImage($questionFormDTO->image); |
94
|
|
|
|
95
|
|
|
$question = $questionFormDTO->toNewQuestion( |
96
|
|
|
$this->uuidFactory, |
97
|
|
|
$fileName |
98
|
|
|
); |
99
|
|
|
$this->questionRepository->save($question); |
100
|
|
|
|
101
|
|
|
$this->addFlash('success', 'De nieuwe vraag is bewaard.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->render( |
105
|
|
|
'questions/add.html.twig', |
106
|
|
|
[ |
107
|
|
|
'categories' => $categories ? $categories->toArray() : [], |
108
|
|
|
'form' => $form->createView() |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Request $request |
115
|
|
|
* @param string $id |
116
|
|
|
* @return Response |
117
|
|
|
*/ |
118
|
|
|
public function edit(Request $request, string $id): Response |
119
|
|
|
{ |
120
|
|
|
$categories = $this->categoryRepository->getAll(); |
121
|
|
|
|
122
|
|
|
$question = $this->questionRepository->getById( |
123
|
|
|
$this->uuidFactory->fromString($id) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$questionFormDTO = new QuestionFormDTO(); |
127
|
|
|
$questionFormDTO->fromQuestion($question); |
128
|
|
|
|
129
|
|
|
$form = $this->createForm( |
130
|
|
|
QuestionFormType::class, |
131
|
|
|
$questionFormDTO, |
132
|
|
|
[ |
133
|
|
|
'languages' => new Languages(), |
134
|
|
|
'categories' => $categories, |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$form->handleRequest($request); |
139
|
|
|
|
140
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
141
|
|
|
$question = $questionFormDTO->toExistingQuestion($question); |
142
|
|
|
$this->questionRepository->update($question); |
143
|
|
|
|
144
|
|
|
$this->addFlash('success', 'De vraag is aangepast.'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->render( |
148
|
|
|
'questions/add.html.twig', |
149
|
|
|
[ |
150
|
|
|
'categories' => $categories ? $categories->toArray() : [], |
151
|
|
|
'form' => $form->createView() |
152
|
|
|
] |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param Request $request |
158
|
|
|
* @param string $id |
159
|
|
|
* @return Response |
160
|
|
|
*/ |
161
|
|
|
public function delete(Request $request, string $id): Response |
162
|
|
|
{ |
163
|
|
|
if ($request->getMethod() === 'POST') { |
164
|
|
|
$this->questionRepository->delete( |
165
|
|
|
$this->uuidFactory->fromString($id) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->addFlash('success', 'De vraag is verwijderd.'); |
169
|
|
|
|
170
|
|
|
return $this->redirectToRoute('questions_view_index'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->render( |
174
|
|
|
'questions/delete.html.twig', |
175
|
|
|
[ |
176
|
|
|
'id' => $id, |
177
|
|
|
] |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|