|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Category; |
|
6
|
|
|
use App\Form\CategoryType; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
10
|
|
|
|
|
11
|
|
|
class AdminCategoryController extends AbstractController |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @Route("/admin/category", name="admin_categories") |
|
15
|
|
|
*/ |
|
16
|
|
|
public function index(Request $request) |
|
17
|
|
|
{ |
|
18
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
19
|
|
|
$categories = $em->getRepository(Category::class)->findAll(); |
|
20
|
|
|
|
|
21
|
|
|
$category = new Category(); |
|
22
|
|
|
$form = $this->createForm(CategoryType::class, $category); |
|
23
|
|
|
$form->handleRequest($request); |
|
24
|
|
|
|
|
25
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
26
|
|
|
$em->persist($category); |
|
27
|
|
|
$em->flush(); |
|
28
|
|
|
|
|
29
|
|
|
$this->addFlash('success', 'Cette catégorie a bien été ajoutée'); |
|
30
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return $this->render('admin_category/index.html.twig', [ |
|
34
|
|
|
'categories' => $categories, |
|
35
|
|
|
'formCategory' => $form->createView(), |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @Route("/admin/category/delete/{id}", name="delete_category") |
|
41
|
|
|
*/ |
|
42
|
|
|
public function delete($id) |
|
43
|
|
|
{ |
|
44
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
45
|
|
|
$category = $em->getRepository(Category::class)->find($id); |
|
46
|
|
|
$em->remove($category); |
|
47
|
|
|
$em->flush(); |
|
48
|
|
|
|
|
49
|
|
|
$this->addFlash('success', 'Cette catégorie a bien été supprimée'); |
|
50
|
|
|
return $this->redirectToRoute('admin_categories'); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|