1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Entity\Book; |
6
|
|
|
use App\Repository\BookRepository; |
7
|
|
|
use App\UploadManager; |
8
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
14
|
|
|
|
15
|
|
|
class BookController extends AbstractController |
16
|
|
|
{ |
17
|
|
|
private const FILE_TYPES_SUPPORTED = ['image/jpeg']; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* getInputs. |
21
|
|
|
* |
22
|
|
|
* Returns a array that contains all the user inputs |
23
|
|
|
* |
24
|
|
|
* @return array<mixed> |
25
|
|
|
*/ |
26
|
1 |
|
private function getInputs(Request $request): array |
27
|
|
|
{ |
28
|
1 |
|
$id = htmlspecialchars((string) $request->request->get('id')); |
29
|
1 |
|
$title = htmlspecialchars((string) $request->request->get('title')); |
30
|
1 |
|
$isbn = htmlspecialchars((string) $request->request->get('isbn')); |
31
|
1 |
|
$author = htmlspecialchars((string) $request->request->get('author')); |
32
|
|
|
/** @var UploadedFile|null $file */ |
33
|
1 |
|
$file = $request->files->get('img'); |
34
|
|
|
|
35
|
1 |
|
$inputs = [ |
36
|
1 |
|
'id' => $id, |
37
|
1 |
|
'title' => $title, |
38
|
1 |
|
'isbn' => $isbn, |
39
|
1 |
|
'author' => $author, |
40
|
1 |
|
'file' => $file, |
41
|
1 |
|
]; |
42
|
|
|
|
43
|
1 |
|
return $inputs; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* saveBook. |
48
|
|
|
* |
49
|
|
|
* Saves the book to database |
50
|
|
|
*/ |
51
|
1 |
|
private function saveBook(Request $request, ManagerRegistry $doctrine): void |
52
|
|
|
{ |
53
|
|
|
// Get the inputs |
54
|
1 |
|
$inputs = $this->getInputs($request); |
55
|
|
|
/** @var string $title */ |
56
|
1 |
|
$title = $inputs['title']; |
57
|
|
|
/** @var string $isbn */ |
58
|
1 |
|
$isbn = $inputs['isbn']; |
59
|
|
|
/** @var string $author */ |
60
|
1 |
|
$author = $inputs['author']; |
61
|
|
|
/** @var UploadedFile|null $file */ |
62
|
1 |
|
$file = $inputs['file']; |
63
|
|
|
|
64
|
|
|
// Check if empty title |
65
|
1 |
|
if ('' === $title) { |
66
|
1 |
|
$this->addFlash( |
67
|
1 |
|
'warning', |
68
|
1 |
|
'Could not read the title!' |
69
|
1 |
|
); |
70
|
|
|
|
71
|
1 |
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var string $projectDir */ |
75
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
76
|
1 |
|
$uploadManager = new UploadManager($projectDir); |
77
|
|
|
|
78
|
|
|
try { |
79
|
1 |
|
$img = $uploadManager->saveUploadedFile($file, self::FILE_TYPES_SUPPORTED); |
80
|
|
|
// After setting $img |
81
|
|
|
} catch (\RuntimeException $e) { |
82
|
|
|
$this->addFlash( |
83
|
|
|
'warning', |
84
|
|
|
$e->getMessage() |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
91
|
|
|
|
92
|
1 |
|
$bookRepository->saveBook($title, $isbn, $author, $img); |
93
|
|
|
|
94
|
1 |
|
$this->addFlash( |
95
|
1 |
|
'notice', |
96
|
1 |
|
'You have gone created a book!' |
97
|
1 |
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* updateBook. |
102
|
|
|
* |
103
|
|
|
* Update the book in database |
104
|
|
|
*/ |
105
|
1 |
|
private function updateBook(Request $request, ManagerRegistry $doctrine): void |
106
|
|
|
{ |
107
|
1 |
|
$inputs = $this->getInputs($request); |
108
|
|
|
/** @var string $id */ |
109
|
1 |
|
$id = $inputs['id']; |
110
|
|
|
/** @var string $title */ |
111
|
1 |
|
$title = $inputs['title']; |
112
|
|
|
/** @var string $isbn */ |
113
|
1 |
|
$isbn = $inputs['isbn']; |
114
|
|
|
/** @var string $author */ |
115
|
1 |
|
$author = $inputs['author']; |
116
|
|
|
/** @var UploadedFile|null $file */ |
117
|
1 |
|
$file = $inputs['file']; |
118
|
|
|
|
119
|
|
|
// Check if empty id |
120
|
1 |
|
if ('' === $id) { |
121
|
|
|
$this->addFlash( |
122
|
|
|
'warning', |
123
|
|
|
'Could not read the id!' |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Retrieve the existing book entity |
130
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
131
|
|
|
|
132
|
1 |
|
$book = $bookRepository->find($id); |
133
|
|
|
|
134
|
|
|
// Check if book exists |
135
|
1 |
|
if (!$book) { |
136
|
1 |
|
$this->addFlash( |
137
|
1 |
|
'warning', |
138
|
1 |
|
'Book not found with ID '.$id |
139
|
1 |
|
); |
140
|
|
|
|
141
|
1 |
|
return; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Check if empty title |
145
|
1 |
|
if ('' === $title) { |
146
|
1 |
|
$this->addFlash( |
147
|
1 |
|
'warning', |
148
|
1 |
|
'Could not read the title!' |
149
|
1 |
|
); |
150
|
|
|
|
151
|
1 |
|
return; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Save new image |
155
|
|
|
/** @var string $projectDir */ |
156
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
157
|
1 |
|
$uploadManager = new UploadManager($projectDir); |
158
|
|
|
|
159
|
|
|
try { |
160
|
1 |
|
$img = $uploadManager->saveUploadedFile($file, self::FILE_TYPES_SUPPORTED); |
161
|
|
|
// After setting $img |
162
|
|
|
} catch (\RuntimeException $e) { |
163
|
|
|
$this->addFlash( |
164
|
|
|
'warning', |
165
|
|
|
$e->getMessage() |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// If you have new image |
172
|
1 |
|
if (null !== $img) { |
173
|
|
|
// If old image existed remove it |
174
|
|
|
$oldImage = $book->getImg(); |
175
|
|
|
if (null !== $oldImage) { |
176
|
|
|
if (!$uploadManager->deleteUploadedFile($oldImage)) { |
177
|
|
|
$this->addFlash( |
178
|
|
|
'notice', |
179
|
|
|
'Could not remove old img!' |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// Update the book |
186
|
1 |
|
$bookRepository->updateBook((int) $id, $title, $isbn, $author, $img); |
187
|
|
|
|
188
|
1 |
|
$this->addFlash( |
189
|
1 |
|
'notice', |
190
|
1 |
|
'The book has been updated successfully!' |
191
|
1 |
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* deleteBook. |
196
|
|
|
* |
197
|
|
|
* Update the book in database |
198
|
|
|
*/ |
199
|
1 |
|
private function deleteBook(int $id, ManagerRegistry $doctrine): void |
200
|
|
|
{ |
201
|
|
|
// Retrieve the existing book entity |
202
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
203
|
|
|
|
204
|
1 |
|
$book = $bookRepository->find($id); |
205
|
|
|
|
206
|
|
|
// Check if book don't exists |
207
|
1 |
|
if (!$book) { |
208
|
1 |
|
$this->addFlash( |
209
|
1 |
|
'warning', |
210
|
1 |
|
'Book not found with ID '.$id |
211
|
1 |
|
); |
212
|
|
|
|
213
|
1 |
|
return; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Delete image |
217
|
1 |
|
$img = $book->getImg(); |
218
|
|
|
/** @var string $projectDir */ |
219
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
220
|
1 |
|
$uploadManager = new UploadManager($projectDir); |
221
|
|
|
|
222
|
1 |
|
if (null !== $img) { |
223
|
|
|
if (!$uploadManager->deleteUploadedFile($img)) { |
224
|
|
|
$this->addFlash( |
225
|
|
|
'notice', |
226
|
|
|
'Could not remove img: '.$img |
227
|
|
|
); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
1 |
|
$bookRepository->deleteBook($id); |
232
|
|
|
|
233
|
1 |
|
$this->addFlash( |
234
|
1 |
|
'notice', |
235
|
1 |
|
'The book has been deleted!' |
236
|
1 |
|
); |
237
|
|
|
} |
238
|
|
|
|
239
|
1 |
|
#[Route('/library', name: 'library')] |
240
|
|
|
public function library(): Response |
241
|
|
|
{ |
242
|
1 |
|
return $this->render('book/index.html.twig'); |
243
|
|
|
} |
244
|
|
|
|
245
|
1 |
|
#[Route('/book/create', name: 'book_create_get', methods: 'GET')] |
246
|
|
|
public function createBookGET(): Response |
247
|
|
|
{ |
248
|
1 |
|
return $this->render('book/create.html.twig'); |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
#[Route('/book/create', name: 'book_create_post', methods: 'POST')] |
252
|
|
|
public function createBookPOST( |
253
|
|
|
Request $request, |
254
|
|
|
ManagerRegistry $doctrine, |
255
|
|
|
): Response { |
256
|
|
|
// Save book to database |
257
|
1 |
|
$this->saveBook($request, $doctrine); |
258
|
|
|
|
259
|
1 |
|
return $this->redirectToRoute('book_create_get'); |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
#[Route('/book/show', name: 'book_show_all')] |
263
|
|
|
public function showAllBooks( |
264
|
|
|
ManagerRegistry $doctrine, |
265
|
|
|
): Response { |
266
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
267
|
|
|
|
268
|
1 |
|
$data = $bookRepository->readAllBooks(); |
269
|
|
|
|
270
|
1 |
|
return $this->render('book/show.html.twig', $data); |
271
|
|
|
} |
272
|
|
|
|
273
|
1 |
|
#[Route('/book/show/{id<\d+>}', name: 'book_show_one')] |
274
|
|
|
public function showOneBook( |
275
|
|
|
int $id, |
276
|
|
|
ManagerRegistry $doctrine, |
277
|
|
|
): Response { |
278
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
279
|
|
|
|
280
|
1 |
|
$data = $bookRepository->readOneBook($id); |
281
|
|
|
|
282
|
1 |
|
return $this->render('book/show_one.html.twig', $data); |
283
|
|
|
} |
284
|
|
|
|
285
|
1 |
|
#[Route('/book/show/update', name: 'book_show_all_update')] |
286
|
|
|
public function showAllBooksUpdate( |
287
|
|
|
ManagerRegistry $doctrine, |
288
|
|
|
): Response { |
289
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
290
|
|
|
|
291
|
1 |
|
$data = $bookRepository->readAllBooks(); |
292
|
|
|
|
293
|
1 |
|
return $this->render('book/show_update.html.twig', $data); |
294
|
|
|
} |
295
|
|
|
|
296
|
1 |
|
#[Route('/book/update/{id<\d+>}', name: 'book_update_get', methods: ['GET'])] |
297
|
|
|
public function showAllBooksUpdateGET( |
298
|
|
|
int $id, |
299
|
|
|
ManagerRegistry $doctrine, |
300
|
|
|
): Response { |
301
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
302
|
|
|
|
303
|
1 |
|
$data = $bookRepository->readOneBook($id); |
304
|
|
|
|
305
|
1 |
|
return $this->render('book/update.html.twig', $data); |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
#[Route('/book/update', name: 'book_update_post', methods: ['POST'])] |
309
|
|
|
public function showAllBooksUpdatePOST( |
310
|
|
|
Request $request, |
311
|
|
|
ManagerRegistry $doctrine, |
312
|
|
|
): Response { |
313
|
1 |
|
$inputs = $this->getInputs($request); |
314
|
|
|
|
315
|
|
|
// Update book to database |
316
|
1 |
|
$this->updateBook($request, $doctrine); |
317
|
|
|
|
318
|
1 |
|
return $this->redirectToRoute('book_update_get', ['id' => $inputs['id']]); |
319
|
|
|
} |
320
|
|
|
|
321
|
1 |
|
#[Route('/book/show/delete', name: 'book_show_all_delete_get', methods: ['GET'])] |
322
|
|
|
public function showAllBooksDeleteGET( |
323
|
|
|
ManagerRegistry $doctrine, |
324
|
|
|
): Response { |
325
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
326
|
|
|
|
327
|
1 |
|
$data = $bookRepository->readAllBooks(); |
328
|
|
|
|
329
|
1 |
|
return $this->render('book/show_delete.html.twig', $data); |
330
|
|
|
} |
331
|
|
|
|
332
|
1 |
|
#[Route('/book/show/delete/{id<\d+>}', name: 'book_delete_post', methods: ['POST'])] |
333
|
|
|
public function showAllBooksDeletePOST( |
334
|
|
|
int $id, |
335
|
|
|
ManagerRegistry $doctrine, |
336
|
|
|
): Response { |
337
|
|
|
// Delete book to database |
338
|
1 |
|
$this->deleteBook($id, $doctrine); |
339
|
|
|
|
340
|
1 |
|
return $this->redirectToRoute('book_show_all_delete_get'); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|