1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Category; |
6
|
|
|
use AppBundle\Entity\Meal; |
7
|
|
|
use AppBundle\Form\Type\CategoryType; |
8
|
|
|
use AppBundle\Form\Type\MealType; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class AdminMenuController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @Route(path="/admin/menu", name="admin_menu_index") |
19
|
|
|
*/ |
20
|
|
|
public function indexAction() |
21
|
|
|
{ |
22
|
|
|
$categories = $this |
23
|
|
|
->getDoctrine() |
24
|
|
|
->getRepository(Category::class) |
25
|
|
|
->findOrderedByPosition() |
26
|
|
|
; |
27
|
|
|
|
28
|
|
|
return $this->render('admin_menu/index.html.twig', array( |
29
|
|
|
'categories' => $categories |
30
|
|
|
)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @Route(path="/admin/menu/category/{id}", name="admin_menu_category_edit") |
35
|
|
|
* @ParamConverter |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function categoryEditAction(Request $request, Category $category) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$form = $this->createForm(CategoryType::class, $category); |
40
|
|
|
$form->handleRequest($request); |
41
|
|
|
|
42
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
43
|
|
|
$this->getDoctrine()->getManager()->flush(); |
44
|
|
|
|
45
|
|
|
$this->addFlash('success', sprintf( |
46
|
|
|
'Catégorie "%s" mise à jour.', |
47
|
|
|
$category->getName() |
48
|
|
|
)); |
49
|
|
|
|
50
|
|
|
return $this->redirectToRoute('admin_menu_index'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->render('admin_menu/category_edit.html.twig', array( |
54
|
|
|
'category' => $category, |
55
|
|
|
'form' => $form->createView() |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @Route(path="/admin/menu/create-category", name="admin_menu_category_create") |
61
|
|
|
* @ParamConverter |
62
|
|
|
*/ |
63
|
|
|
public function categoryCreateAction(Request $request) |
64
|
|
|
{ |
65
|
|
|
$form = $this->createForm(CategoryType::class); |
66
|
|
|
$form->handleRequest($request); |
67
|
|
|
|
68
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
69
|
|
|
$em = $this->getDoctrine()->getManager(); |
70
|
|
|
|
71
|
|
|
$category = $form->getData(); |
72
|
|
|
|
73
|
|
|
$em->persist($category); |
74
|
|
|
$em->flush(); |
75
|
|
|
|
76
|
|
|
$this->addFlash('success', sprintf( |
77
|
|
|
'Catégorie "%s" créée.', |
78
|
|
|
$category->getName() |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
return $this->redirectToRoute('admin_menu_index'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->render('admin_menu/category_create.html.twig', array( |
85
|
|
|
'form' => $form->createView() |
86
|
|
|
)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @Route(path="/admin/menu/category/{id}/delete", name="admin_menu_category_delete") |
91
|
|
|
* @ParamConverter |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function categoryDeleteAction(Request $request, Category $category) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if ($request->isMethod('POST')) { |
96
|
|
|
$em = $this->getDoctrine()->getManager(); |
97
|
|
|
$em->remove($category); |
98
|
|
|
$em->flush(); |
99
|
|
|
|
100
|
|
|
$request->getSession()->getFlashBag()->add('success', 'La catégorie '.$category->getName().' a bien été supprimée.'); |
101
|
|
|
|
102
|
|
|
return $this->redirectToRoute('admin_menu_index'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->render('admin_menu/category_delete.html.twig', array( |
106
|
|
|
'category' => $category |
107
|
|
|
)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @Route(path="/admin/menu/meal/{id}", name="admin_menu_meal_edit") |
112
|
|
|
* @ParamConverter |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
public function mealEditAction(Request $request, Meal $meal) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$form = $this->createForm(MealType::class, $meal); |
117
|
|
|
$form->handleRequest($request); |
118
|
|
|
|
119
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
120
|
|
|
$this->getDoctrine()->getManager()->flush(); |
121
|
|
|
|
122
|
|
|
$this->addFlash('success', sprintf( |
123
|
|
|
'Plat "%s" mis à jour.', |
124
|
|
|
$meal->getName() |
125
|
|
|
)); |
126
|
|
|
|
127
|
|
|
return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId())); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->render('admin_menu/meal_edit.html.twig', array( |
131
|
|
|
'meal' => $meal, |
132
|
|
|
'form' => $form->createView() |
133
|
|
|
)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @Route(path="/admin/menu/create-meal/{id}", name="admin_menu_meal_create") |
138
|
|
|
* @ParamConverter |
139
|
|
|
*/ |
140
|
|
|
public function mealCreateAction(Request $request, Category $category) |
141
|
|
|
{ |
142
|
|
|
$form = $this->createForm(MealType::class); |
143
|
|
|
$form->handleRequest($request); |
144
|
|
|
|
145
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
146
|
|
|
$em = $this->getDoctrine()->getManager(); |
147
|
|
|
$meal = $form->getData(); |
148
|
|
|
|
149
|
|
|
$meal->setCategory($category); |
150
|
|
|
|
151
|
|
|
$em->persist($meal); |
152
|
|
|
$em->flush(); |
153
|
|
|
|
154
|
|
|
$this->addFlash('success', sprintf( |
155
|
|
|
'Plat "%s" créé.', |
156
|
|
|
$meal->getName() |
157
|
|
|
)); |
158
|
|
|
|
159
|
|
|
return $this->redirectToRoute('admin_menu_meal_edit', array('id' => $meal->getId())); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->render('admin_menu/meal_create.html.twig', array( |
163
|
|
|
'form' => $form->createView(), |
164
|
|
|
'category' => $category |
165
|
|
|
)); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @Route(path="/admin/menu/meal/{id}/delete", name="admin_menu_meal_delete") |
170
|
|
|
* @ParamConverter |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function mealDeleteAction(Request $request, Meal $meal) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
if ($request->isMethod('POST')) { |
175
|
|
|
$em = $this->getDoctrine()->getManager(); |
176
|
|
|
$em->remove($meal); |
177
|
|
|
$em->flush(); |
178
|
|
|
|
179
|
|
|
$request->getSession()->getFlashBag()->add('success', 'Le plat '.$meal->getName().' a bien été supprimé.'); |
180
|
|
|
|
181
|
|
|
return $this->redirectToRoute('admin_menu_category_edit', array('id' => $meal->getCategory()->getId())); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->render('admin_menu/meal_delete.html.twig', array( |
185
|
|
|
'meal' => $meal |
186
|
|
|
)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.