1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Controller\Admin; |
6
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
8
|
|
|
use App\Entity\Category; |
9
|
|
|
use App\Form\Type\CategoryType; |
10
|
|
|
use App\Repository\CategoryRepository; |
11
|
|
|
use App\Service\Admin\CategoryService; |
12
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
13
|
|
|
use Symfony\Component\Form\ClickableInterface; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
|
19
|
|
|
final class CategoryController extends BaseController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/admin/category", name="admin_category") |
23
|
|
|
*/ |
24
|
|
|
public function index(Request $request, CategoryRepository $repository): Response |
25
|
|
|
{ |
26
|
|
|
$categories = $repository->findAll(); |
27
|
|
|
|
28
|
|
|
return $this->render('admin/category/index.html.twig', [ |
29
|
|
|
'site' => $this->site($request), |
30
|
|
|
'categories' => $categories, |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Route("/admin/category/new", name="admin_category_new") |
36
|
|
|
*/ |
37
|
|
|
public function new(Request $request, CategoryService $service): Response |
38
|
|
|
{ |
39
|
|
|
$category = new Category(); |
40
|
|
|
|
41
|
|
|
$form = $this->createForm(CategoryType::class, $category) |
42
|
|
|
->add('saveAndCreateNew', SubmitType::class); |
43
|
|
|
$form->handleRequest($request); |
44
|
|
|
|
45
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
46
|
|
|
$service->create($category); |
47
|
|
|
|
48
|
|
|
/** @var ClickableInterface $button */ |
49
|
|
|
$button = $form->get('saveAndCreateNew'); |
50
|
|
|
if ($button->isClicked()) { |
51
|
|
|
return $this->redirectToRoute('admin_category_new'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->redirectToRoute('admin_category'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->render('admin/category/new.html.twig', [ |
58
|
|
|
'site' => $this->site($request), |
59
|
|
|
'category' => $category, |
60
|
|
|
'form' => $form->createView(), |
61
|
|
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Displays a form to edit an existing Category entity. |
66
|
|
|
* |
67
|
|
|
* @Route("/admin/category/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_category_edit") |
68
|
|
|
*/ |
69
|
|
|
public function edit(Request $request, Category $category, CategoryService $service): Response |
70
|
|
|
{ |
71
|
|
|
$form = $this->createForm(CategoryType::class, $category); |
72
|
|
|
$form->handleRequest($request); |
73
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
74
|
|
|
$service->update($category); |
75
|
|
|
|
76
|
|
|
return $this->redirectToRoute('admin_category'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->render('admin/category/edit.html.twig', [ |
80
|
|
|
'site' => $this->site($request), |
81
|
|
|
'form' => $form->createView(), |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Deletes a Category entity. |
87
|
|
|
* |
88
|
|
|
* @Route("/category/{id<\d+>}/delete", methods={"POST"}, name="admin_category_delete") |
89
|
|
|
* @IsGranted("ROLE_ADMIN") |
90
|
|
|
*/ |
91
|
|
|
public function delete(Request $request, Category $category, CategoryService $service): Response |
92
|
|
|
{ |
93
|
|
|
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) { |
94
|
|
|
return $this->redirectToRoute('admin_category'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$service->remove($category); |
98
|
|
|
|
99
|
|
|
return $this->redirectToRoute('admin_category'); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|