|
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
|
|
|
// Save new image |
|
75
|
|
|
/** @var string $projectDir */ |
|
76
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
|
77
|
|
|
|
|
78
|
|
|
// Construct the path to /var |
|
79
|
1 |
|
$targetFolder = $projectDir.'/var'; |
|
80
|
|
|
|
|
81
|
1 |
|
$uploadManager = new UploadManager($targetFolder); |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
1 |
|
$img = $uploadManager->saveUploadedFile($file, self::FILE_TYPES_SUPPORTED); |
|
85
|
|
|
// After setting $img |
|
86
|
|
|
} catch (\RuntimeException $e) { |
|
87
|
|
|
$this->addFlash( |
|
88
|
|
|
'warning', |
|
89
|
|
|
$e->getMessage() |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
96
|
|
|
|
|
97
|
1 |
|
$bookRepository->saveBook($title, $isbn, $author, $img); |
|
98
|
|
|
|
|
99
|
1 |
|
$this->addFlash( |
|
100
|
1 |
|
'notice', |
|
101
|
1 |
|
'You have gone created a book!' |
|
102
|
1 |
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* updateBook. |
|
107
|
|
|
* |
|
108
|
|
|
* Update the book in database |
|
109
|
|
|
*/ |
|
110
|
1 |
|
private function updateBook(Request $request, ManagerRegistry $doctrine): void |
|
111
|
|
|
{ |
|
112
|
1 |
|
$inputs = $this->getInputs($request); |
|
113
|
|
|
/** @var string $id */ |
|
114
|
1 |
|
$id = $inputs['id']; |
|
115
|
|
|
/** @var string $title */ |
|
116
|
1 |
|
$title = $inputs['title']; |
|
117
|
|
|
/** @var string $isbn */ |
|
118
|
1 |
|
$isbn = $inputs['isbn']; |
|
119
|
|
|
/** @var string $author */ |
|
120
|
1 |
|
$author = $inputs['author']; |
|
121
|
|
|
/** @var UploadedFile|null $file */ |
|
122
|
1 |
|
$file = $inputs['file']; |
|
123
|
|
|
|
|
124
|
|
|
// Check if empty id |
|
125
|
1 |
|
if ('' === $id) { |
|
126
|
|
|
$this->addFlash( |
|
127
|
|
|
'warning', |
|
128
|
|
|
'Could not read the id!' |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Retrieve the existing book entity |
|
135
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
136
|
|
|
|
|
137
|
1 |
|
$book = $bookRepository->find($id); |
|
138
|
|
|
|
|
139
|
|
|
// Check if book exists |
|
140
|
1 |
|
if (!$book) { |
|
141
|
1 |
|
$this->addFlash( |
|
142
|
1 |
|
'warning', |
|
143
|
1 |
|
'Book not found with ID '.$id |
|
144
|
1 |
|
); |
|
145
|
|
|
|
|
146
|
1 |
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
// Check if empty title |
|
150
|
1 |
|
if ('' === $title) { |
|
151
|
1 |
|
$this->addFlash( |
|
152
|
1 |
|
'warning', |
|
153
|
1 |
|
'Could not read the title!' |
|
154
|
1 |
|
); |
|
155
|
|
|
|
|
156
|
1 |
|
return; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
// Save new image |
|
160
|
|
|
/** @var string $projectDir */ |
|
161
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
|
162
|
|
|
|
|
163
|
|
|
// Construct the path to /var |
|
164
|
1 |
|
$targetFolder = $projectDir.'/var'; |
|
165
|
|
|
|
|
166
|
1 |
|
$uploadManager = new UploadManager($targetFolder); |
|
167
|
|
|
|
|
168
|
|
|
try { |
|
169
|
1 |
|
$img = $uploadManager->saveUploadedFile($file, self::FILE_TYPES_SUPPORTED); |
|
170
|
|
|
// After setting $img |
|
171
|
|
|
} catch (\RuntimeException $e) { |
|
172
|
|
|
$this->addFlash( |
|
173
|
|
|
'warning', |
|
174
|
|
|
$e->getMessage() |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
return; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// If you have new image |
|
181
|
1 |
|
if (null !== $img) { |
|
182
|
|
|
// If old image existed remove it |
|
183
|
|
|
$oldImage = $book->getImg(); |
|
184
|
|
|
if (null !== $oldImage) { |
|
185
|
|
|
if (!$uploadManager->deleteUploadedFile($oldImage)) { |
|
186
|
|
|
$this->addFlash( |
|
187
|
|
|
'notice', |
|
188
|
|
|
'Could not remove old img!' |
|
189
|
|
|
); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// Update the book |
|
195
|
1 |
|
$bookRepository->updateBook((int) $id, $title, $isbn, $author, $img); |
|
196
|
|
|
|
|
197
|
1 |
|
$this->addFlash( |
|
198
|
1 |
|
'notice', |
|
199
|
1 |
|
'The book has been updated successfully!' |
|
200
|
1 |
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* deleteBook. |
|
205
|
|
|
* |
|
206
|
|
|
* Update the book in database |
|
207
|
|
|
*/ |
|
208
|
1 |
|
private function deleteBook(int $id, ManagerRegistry $doctrine): void |
|
209
|
|
|
{ |
|
210
|
|
|
// Retrieve the existing book entity |
|
211
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
212
|
|
|
|
|
213
|
1 |
|
$book = $bookRepository->find($id); |
|
214
|
|
|
|
|
215
|
|
|
// Check if book don't exists |
|
216
|
1 |
|
if (!$book) { |
|
217
|
1 |
|
$this->addFlash( |
|
218
|
1 |
|
'warning', |
|
219
|
1 |
|
'Book not found with ID '.$id |
|
220
|
1 |
|
); |
|
221
|
|
|
|
|
222
|
1 |
|
return; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
// Delete image |
|
226
|
1 |
|
$img = $book->getImg(); |
|
227
|
|
|
/** @var string $projectDir */ |
|
228
|
1 |
|
$projectDir = $this->getParameter('kernel.project_dir'); |
|
229
|
|
|
|
|
230
|
|
|
// Construct the path to /var |
|
231
|
1 |
|
$targetFolder = $projectDir.'/var'; |
|
232
|
|
|
|
|
233
|
1 |
|
$uploadManager = new UploadManager($targetFolder); |
|
234
|
|
|
|
|
235
|
1 |
|
if (null !== $img) { |
|
236
|
|
|
if (!$uploadManager->deleteUploadedFile($img)) { |
|
237
|
|
|
$this->addFlash( |
|
238
|
|
|
'notice', |
|
239
|
|
|
'Could not remove img: '.$img |
|
240
|
|
|
); |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
1 |
|
$bookRepository->deleteBook($id); |
|
245
|
|
|
|
|
246
|
1 |
|
$this->addFlash( |
|
247
|
1 |
|
'notice', |
|
248
|
1 |
|
'The book has been deleted!' |
|
249
|
1 |
|
); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
1 |
|
#[Route('/library', name: 'library')] |
|
253
|
|
|
public function library(): Response |
|
254
|
|
|
{ |
|
255
|
1 |
|
return $this->render('book/index.html.twig'); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
1 |
|
#[Route('/book/create', name: 'book_create_get', methods: 'GET')] |
|
259
|
|
|
public function createBookGET(): Response |
|
260
|
|
|
{ |
|
261
|
1 |
|
return $this->render('book/create.html.twig'); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
1 |
|
#[Route('/book/create', name: 'book_create_post', methods: 'POST')] |
|
265
|
|
|
public function createBookPOST( |
|
266
|
|
|
Request $request, |
|
267
|
|
|
ManagerRegistry $doctrine, |
|
268
|
|
|
): Response { |
|
269
|
|
|
// Save book to database |
|
270
|
1 |
|
$this->saveBook($request, $doctrine); |
|
271
|
|
|
|
|
272
|
1 |
|
return $this->redirectToRoute('book_create_get'); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
1 |
|
#[Route('/book/show', name: 'book_show_all')] |
|
276
|
|
|
public function showAllBooks( |
|
277
|
|
|
ManagerRegistry $doctrine, |
|
278
|
|
|
): Response { |
|
279
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
280
|
|
|
|
|
281
|
1 |
|
$data = $bookRepository->readAllBooks(); |
|
282
|
|
|
|
|
283
|
1 |
|
return $this->render('book/show.html.twig', $data); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
1 |
|
#[Route('/book/show/{id<\d+>}', name: 'book_show_one')] |
|
287
|
|
|
public function showOneBook( |
|
288
|
|
|
int $id, |
|
289
|
|
|
ManagerRegistry $doctrine, |
|
290
|
|
|
): Response { |
|
291
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
292
|
|
|
|
|
293
|
1 |
|
$data = $bookRepository->readOneBook($id); |
|
294
|
|
|
|
|
295
|
1 |
|
return $this->render('book/show_one.html.twig', $data); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
1 |
|
#[Route('/book/show/update', name: 'book_show_all_update')] |
|
299
|
|
|
public function showAllBooksUpdate( |
|
300
|
|
|
ManagerRegistry $doctrine, |
|
301
|
|
|
): Response { |
|
302
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
303
|
|
|
|
|
304
|
1 |
|
$data = $bookRepository->readAllBooks(); |
|
305
|
|
|
|
|
306
|
1 |
|
return $this->render('book/show_update.html.twig', $data); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
1 |
|
#[Route('/book/update/{id<\d+>}', name: 'book_update_get', methods: ['GET'])] |
|
310
|
|
|
public function showAllBooksUpdateGET( |
|
311
|
|
|
int $id, |
|
312
|
|
|
ManagerRegistry $doctrine, |
|
313
|
|
|
): Response { |
|
314
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
315
|
|
|
|
|
316
|
1 |
|
$data = $bookRepository->readOneBook($id); |
|
317
|
|
|
|
|
318
|
1 |
|
return $this->render('book/update.html.twig', $data); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
1 |
|
#[Route('/book/update', name: 'book_update_post', methods: ['POST'])] |
|
322
|
|
|
public function showAllBooksUpdatePOST( |
|
323
|
|
|
Request $request, |
|
324
|
|
|
ManagerRegistry $doctrine, |
|
325
|
|
|
): Response { |
|
326
|
1 |
|
$inputs = $this->getInputs($request); |
|
327
|
|
|
|
|
328
|
|
|
// Update book to database |
|
329
|
1 |
|
$this->updateBook($request, $doctrine); |
|
330
|
|
|
|
|
331
|
1 |
|
return $this->redirectToRoute('book_update_get', ['id' => $inputs['id']]); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
1 |
|
#[Route('/book/show/delete', name: 'book_show_all_delete_get', methods: ['GET'])] |
|
335
|
|
|
public function showAllBooksDeleteGET( |
|
336
|
|
|
ManagerRegistry $doctrine, |
|
337
|
|
|
): Response { |
|
338
|
1 |
|
$bookRepository = new BookRepository($doctrine); |
|
339
|
|
|
|
|
340
|
1 |
|
$data = $bookRepository->readAllBooks(); |
|
341
|
|
|
|
|
342
|
1 |
|
return $this->render('book/show_delete.html.twig', $data); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
1 |
|
#[Route('/book/show/delete/{id<\d+>}', name: 'book_delete_post', methods: ['POST'])] |
|
346
|
|
|
public function showAllBooksDeletePOST( |
|
347
|
|
|
int $id, |
|
348
|
|
|
ManagerRegistry $doctrine, |
|
349
|
|
|
): Response { |
|
350
|
|
|
// Delete book to database |
|
351
|
1 |
|
$this->deleteBook($id, $doctrine); |
|
352
|
|
|
|
|
353
|
1 |
|
return $this->redirectToRoute('book_show_all_delete_get'); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|