|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xdaysaysay\AdminBundle\FormTrait; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class FormTrait |
|
10
|
|
|
* @package Xdaysaysay\AdminBundle\FormTrait |
|
11
|
|
|
*/ |
|
12
|
|
|
trait FormTrait |
|
13
|
|
|
{ |
|
14
|
|
|
private $entityClassName; |
|
15
|
|
|
private $repositoryName; |
|
16
|
|
|
private $twigFormDirectory; |
|
17
|
|
|
private $formRoute; |
|
18
|
|
|
private $translation; |
|
19
|
|
|
private $formType; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Displays list of existing entities. |
|
23
|
|
|
* |
|
24
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \LogicException |
|
27
|
|
|
* @throws \UnexpectedValueException |
|
28
|
|
|
*/ |
|
29
|
|
|
public function indexAction() |
|
30
|
|
|
{ |
|
31
|
|
|
$entities = $this->getDoctrine()->getRepository($this->entityClassName)->findBy([], ['id' => 'DESC']); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
return $this->render( |
|
|
|
|
|
|
34
|
|
|
$this->twigFormDirectory.':index.html.twig', [ |
|
35
|
|
|
'entities' => $entities, |
|
36
|
|
|
] |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Displays a form to edit an existing entity. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Request $request |
|
44
|
|
|
* @param int $id |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
47
|
|
|
* |
|
48
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
49
|
|
|
* @throws \LogicException |
|
50
|
|
|
* @throws \InvalidArgumentException |
|
51
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
52
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
53
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function editAction(Request $request, $id) |
|
56
|
|
|
{ |
|
57
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
$entity = $em->getRepository($this->entityClassName)->find($id); |
|
60
|
|
|
|
|
61
|
|
|
if (!$entity) { |
|
62
|
|
|
throw $this->createNotFoundException('Unable to find entity.'); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$form = $this->createNewEditForm($entity); |
|
66
|
|
|
|
|
67
|
|
|
$form->handleRequest($request); |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
|
|
70
|
|
|
$em->persist($entity); |
|
71
|
|
|
$em->flush(); |
|
72
|
|
|
|
|
73
|
|
|
$this->addFlash('success', $this->get('translator')->trans('admin.'.$this->translation.'.flash.edit', [], 'admin')); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
return $this->redirect($this->generateUrl('xdaysaysay_admin_'.$this->formRoute.'_edit', ['id' => $entity->getId()])); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this->render( |
|
|
|
|
|
|
79
|
|
|
$this->twigFormDirectory.':edit.html.twig', [ |
|
80
|
|
|
'entity' => $entity, |
|
81
|
|
|
'form' => $form->createView(), |
|
82
|
|
|
] |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Displays a form to edit an existing entity. |
|
88
|
|
|
* |
|
89
|
|
|
* @param Request $request |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
92
|
|
|
* |
|
93
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
94
|
|
|
* @throws \LogicException |
|
95
|
|
|
* @throws \InvalidArgumentException |
|
96
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
97
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
98
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function newAction(Request $request) |
|
101
|
|
|
{ |
|
102
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$entity = new $this->entityClassName; |
|
105
|
|
|
|
|
106
|
|
|
$form = $this->createNewEditForm($entity); |
|
107
|
|
|
|
|
108
|
|
|
$form->handleRequest($request); |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
|
|
111
|
|
|
$em->persist($entity); |
|
112
|
|
|
$em->flush(); |
|
113
|
|
|
|
|
114
|
|
|
$this->addFlash('success', $this->get('translator')->trans('admin.'.$this->translation.'.flash.create', [], 'admin')); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
return $this->redirect($this->generateUrl('xdaysaysay_admin_'.$this->formRoute.'_edit', ['id' => $entity->getId()])); |
|
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $this->render( |
|
|
|
|
|
|
120
|
|
|
$this->twigFormDirectory.':new.html.twig', [ |
|
121
|
|
|
'entity' => $entity, |
|
122
|
|
|
'form' => $form->createView(), |
|
123
|
|
|
] |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Creates a form to create a entity. |
|
129
|
|
|
* |
|
130
|
|
|
* @param $entity |
|
131
|
|
|
* |
|
132
|
|
|
* @return \Symfony\Component\Form\Form The form |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \InvalidArgumentException |
|
135
|
|
|
* @throws \Symfony\Component\Form\Exception\AlreadySubmittedException |
|
136
|
|
|
* @throws \Symfony\Component\Form\Exception\LogicException |
|
137
|
|
|
* @throws \Symfony\Component\Form\Exception\UnexpectedTypeException |
|
138
|
|
|
*/ |
|
139
|
|
|
private function createNewEditForm($entity) |
|
140
|
|
|
{ |
|
141
|
|
|
$action = $this->generateUrl('xdaysaysay_admin_'.$this->formRoute.'_new'); |
|
|
|
|
|
|
142
|
|
|
$submitText = $this->get('translator')->trans('admin.common.form.create', [], 'admin'); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
if ($entity->getId() !== null) { |
|
145
|
|
|
$action = $this->generateUrl('xdaysaysay_admin_'.$this->formRoute.'_edit', ['id' => $entity->getId()]); |
|
|
|
|
|
|
146
|
|
|
$submitText = $this->get('translator')->trans('admin.common.form.update', [], 'admin'); |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$form = $this->createForm( |
|
|
|
|
|
|
150
|
|
|
$this->formType, $entity, [ |
|
151
|
|
|
'action' => $action, |
|
152
|
|
|
'method' => 'PUT', |
|
153
|
|
|
] |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$form->add( |
|
157
|
|
|
'submit', SubmitType::class, [ |
|
158
|
|
|
'label' => $submitText, |
|
159
|
|
|
'attr' => [ |
|
160
|
|
|
'class' => 'btn btn-primary', |
|
161
|
|
|
], |
|
162
|
|
|
] |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
return $form; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param $id |
|
170
|
|
|
* |
|
171
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
172
|
|
|
* |
|
173
|
|
|
* @throws \LogicException |
|
174
|
|
|
* @throws \InvalidArgumentException |
|
175
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
176
|
|
|
*/ |
|
177
|
|
|
public function deleteConfirmAction($id) |
|
178
|
|
|
{ |
|
179
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
$entity = $em->getRepository($this->entityClassName)->find($id); |
|
182
|
|
|
|
|
183
|
|
|
if (!$entity) { |
|
184
|
|
|
throw $this->createNotFoundException('Unable to find entity.'); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
return $this->render( |
|
|
|
|
|
|
188
|
|
|
$this->twigFormDirectory.':confirm_delete.html.twig', [ |
|
189
|
|
|
'entity' => $entity, |
|
190
|
|
|
'form' => $this->createDeleteForm($entity)->createView(), |
|
191
|
|
|
] |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param Request $request |
|
197
|
|
|
* @param $id |
|
198
|
|
|
* |
|
199
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
200
|
|
|
* |
|
201
|
|
|
* @throws \LogicException |
|
202
|
|
|
* @throws \InvalidArgumentException |
|
203
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
|
204
|
|
|
*/ |
|
205
|
|
|
public function deleteAction(Request $request, $id) |
|
206
|
|
|
{ |
|
207
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
$entity = $em->getRepository($this->entityClassName)->find($id); |
|
210
|
|
|
|
|
211
|
|
|
if (!$entity) { |
|
212
|
|
|
throw $this->createNotFoundException('Unable to find entity.'); |
|
|
|
|
|
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$form = $this->createDeleteForm($entity); |
|
216
|
|
|
|
|
217
|
|
|
$form->handleRequest($request); |
|
218
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
|
|
219
|
|
|
$em->remove($entity); |
|
220
|
|
|
$em->flush(); |
|
221
|
|
|
|
|
222
|
|
|
$this->addFlash('success', $this->get('translator')->trans('admin.'.$this->translation.'.flash.delete', [], 'admin')); |
|
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
return $this->redirectToRoute('xdaysaysay_admin_'.$this->formRoute.'_index'); |
|
|
|
|
|
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
return $this->redirectToRoute('xdaysaysay_admin_'.$this->formRoute.'_delete_confirm', ['id' => $id]); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param $entity |
|
232
|
|
|
* |
|
233
|
|
|
* @return \Symfony\Component\Form\Form |
|
234
|
|
|
* |
|
235
|
|
|
* @throws \InvalidArgumentException |
|
236
|
|
|
*/ |
|
237
|
|
|
private function createDeleteForm($entity) |
|
238
|
|
|
{ |
|
239
|
|
|
$form = $this->createFormBuilder() |
|
|
|
|
|
|
240
|
|
|
->setAction($this->generateUrl('xdaysaysay_admin_'.$this->formRoute.'_delete', ['id' => $entity->getId()])) |
|
|
|
|
|
|
241
|
|
|
->setMethod('DELETE') |
|
242
|
|
|
->add('submit', SubmitType::class, [ |
|
243
|
|
|
'label' => $this->get('translator')->trans('admin.common.action.delete', [], 'admin'), |
|
|
|
|
|
|
244
|
|
|
'attr' => [ |
|
245
|
|
|
'class' => 'btn btn-danger', |
|
246
|
|
|
], |
|
247
|
|
|
]) |
|
248
|
|
|
->getForm(); |
|
249
|
|
|
|
|
250
|
|
|
return $form; |
|
251
|
|
|
} |
|
252
|
|
|
} |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.