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