-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleController.php
More file actions
128 lines (108 loc) · 4.8 KB
/
ArticleController.php
File metadata and controls
128 lines (108 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace App\Controller;
use App\Entity\Article;
use App\Form\ArticleType;
use App\Repository\ArticleRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use App\Exception\FileUploadException;
use App\Service\FileUploader;
#[Route('/article')]
class ArticleController extends AbstractController
{
#[Route('/list', name: 'app_article_list', methods: ['GET'])]
public function list(Request $request, ArticleRepository $articlesRepository): Response
{
$offset = max(0, $request->query->getInt('offset', 0));
$paginator = $articlesRepository->getArticlePaginator($offset);
return $this->render('article/list.html.twig', [
'articles' => $paginator,
'offset' => $offset,
'previous' => $offset - ArticleRepository::PAGINATOR_PER_PAGE,
'next' => min(count($paginator), $offset + ArticleRepository::PAGINATOR_PER_PAGE)
]);
}
#[Route('/show/{id<\d+>}', name: 'app_article_show', methods: ['GET'])]
public function show(Article $article): Response
{
return $this->render('article/show.html.twig', [
'article' => $article,
]);
}
#[Route('/new', name: 'app_article_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$error = false;
$article = new Article();
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
try {
$imageFilename = $fileUploader->upload($imageFile);
$article->setImage($imageFilename);
} catch (FileUploadException $e) {
$form->get('image')->addError(new FormError($e->getMessage()));
$error = true;
}
if (!$error) {
$entityManager->persist($article);//obligatoire lors d'une création
$entityManager->flush();
return $this->redirectToRoute('app_article_show', ['id' => $article->getId()], Response::HTTP_SEE_OTHER);
}
}
return $this->render('article/new.html.twig', [
'article' => $article,
'form' => $form,
]);
}
#[Route('/edit/{id<\d+>}', name: 'app_article_edit', methods: ['GET', 'POST'])]
public function edit(Request $request, Article $article, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$error = false;
$form = $this->createForm(ArticleType::class, $article);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$imageFile = $form->get('image')->getData();
if ($imageFile) {
try {
$fileUploader->remove($article->getImage());
$imageFilename = $fileUploader->upload($imageFile);
$article->setImage($imageFilename);
} catch (FileUploadException | FileException $e) {
$form->get('image')->addError(new FormError($e->getMessage()));
$error = true;
}
}
if (!$error) {
$entityManager->flush();
return $this->redirectToRoute('app_article_show', ['id' => $article->getId()], Response::HTTP_SEE_OTHER);
}
}
return $this->render('article/edit.html.twig', [
'article' => $article,
'form' => $form,
]);
}
#[Route('/delete/{id<\d+>}', name: 'app_article_delete', methods: ['POST'])]
public function delete(Request $request, Article $article, EntityManagerInterface $entityManager, FileUploader $fileUploader): Response
{
$offset = max(0, $request->query->getInt('offset', 0));
if ($this->isCsrfTokenValid('delete' . $article->getId(), $request->getPayload()->get('_token'))) {
try {
$fileUploader->remove($article->getImage());
} catch (FileException $e) {
$this->addFlash('error', $e->getMessage());
return $this->redirectToRoute('app_article_list', ['offset' => $offset], Response::HTTP_SEE_OTHER);
}
$entityManager->remove($article);
$entityManager->flush();
}
return $this->redirectToRoute('app_article_list', [], Response::HTTP_SEE_OTHER);
}
}