|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AbstractController controller des méthodes communes. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @author Quétier Laurent <[email protected]> |
|
8
|
|
|
* @copyright 2014 Dev-Int GLSR |
|
9
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
10
|
|
|
* |
|
11
|
|
|
* @version since 1.0.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/Dev-Int/glsr |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace AppBundle\Controller; |
|
16
|
|
|
|
|
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
|
19
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
20
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
21
|
|
|
|
|
22
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Abstract controller. |
|
26
|
|
|
* |
|
27
|
|
|
* @category Controller |
|
28
|
|
|
*/ |
|
29
|
|
|
abstract class AbstractController extends Controller |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Lists all items entity. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $entityName Name of Entity |
|
35
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request Sort request |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function abstractIndexAction($entityName, Request $request = null) |
|
39
|
|
|
{ |
|
40
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
41
|
|
|
$paginator = ''; |
|
42
|
|
|
$entities = $this->getEntity($entityName, $etm); |
|
43
|
|
|
|
|
44
|
|
|
if ($request !== null) { |
|
45
|
|
|
$item = $this->container->getParameter('knp_paginator.page_range'); |
|
46
|
|
|
$this->addQueryBuilderSort($entities, strtolower($entityName)); |
|
47
|
|
|
$paginator = $this->get('knp_paginator')->paginate($entities, $request->query->get('page', 1), $item); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return array('entities' => $entities, 'ctEntity' => count($entities), 'paginator' => $paginator,); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the entity |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $entityName Name of Entity |
|
57
|
|
|
* @param \Doctrine\Common\Persistence\ObjectManager $etm ObjectManager instances |
|
58
|
|
|
* @return type |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function getEntity($entityName, $etm) |
|
61
|
|
|
{ |
|
62
|
|
|
$roles = ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN']; |
|
63
|
|
|
switch ($entityName) { |
|
64
|
|
View Code Duplication |
case 'Article': |
|
|
|
|
|
|
65
|
|
|
if ($this->getUser() !== null && in_array($this->getUser()->getRoles()[0], $roles)) { |
|
66
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->getAllArticles(); |
|
67
|
|
|
} else { |
|
68
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->getArticles(); |
|
69
|
|
|
} |
|
70
|
|
|
break; |
|
71
|
|
View Code Duplication |
case 'Supplier': |
|
|
|
|
|
|
72
|
|
|
if ($this->getUser() !== null && in_array($this->getUser()->getRoles()[0], $roles)) { |
|
73
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->getAllSuppliers(); |
|
74
|
|
|
} else { |
|
75
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->getSuppliers(); |
|
76
|
|
|
} |
|
77
|
|
|
break; |
|
78
|
|
|
case 'User': |
|
79
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->getUsers(); |
|
80
|
|
|
break; |
|
81
|
|
|
case 'FamilyLog': |
|
82
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->childrenHierarchy(); |
|
83
|
|
|
break; |
|
84
|
|
|
case 'UnitStorage': |
|
85
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->createQueryBuilder('u'); |
|
86
|
|
|
break; |
|
87
|
|
|
default: |
|
88
|
|
|
$entities = $etm->getRepository('AppBundle:'.$entityName)->findAll(); |
|
89
|
|
|
} |
|
90
|
|
|
return $entities; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Finds and displays an item entity. |
|
95
|
|
|
* |
|
96
|
|
|
* @param Object $entity Entity |
|
97
|
|
|
* @param string $entityName Name of Entity |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function abstractShowAction($entity, $entityName) |
|
101
|
|
|
{ |
|
102
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
103
|
|
|
|
|
104
|
|
|
return array( |
|
105
|
|
|
$entityName => $entity, |
|
106
|
|
|
'delete_form' => $deleteForm->createView(), |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Displays a form to create a new item entity. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $entity Entity |
|
114
|
|
|
* @param string $entityPath Path of Entity |
|
115
|
|
|
* @param string $typePath Path of FormType |
|
116
|
|
|
* @param string|null $options Options of Form |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function abstractNewAction($entity, $entityPath, $typePath, $options = null) |
|
120
|
|
|
{ |
|
121
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
122
|
|
|
$ctEntity = count($etm->getRepository('AppBundle:'.$entity)->findAll()); |
|
123
|
|
|
|
|
124
|
|
|
if ($entity === 'Company' || $entity === 'Settings' && $ctEntity >= 1) { |
|
125
|
|
|
$return = $this->redirectToRoute('_home'); |
|
126
|
|
|
$this->addFlash('danger', 'gestock.settings.'.strtolower($entity).'.add2'); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
|
130
|
|
|
if ($entity === 'User') { |
|
131
|
|
|
$form = $this->createForm($typePath, $entityNew, array( |
|
132
|
|
|
'action' => $this->generateUrl(strtolower($entity).'_create'), |
|
133
|
|
|
'roles' => $options['roles'], |
|
134
|
|
|
)); |
|
135
|
|
|
} else { |
|
136
|
|
|
$form = $this->createForm($typePath, $entityNew, array( |
|
137
|
|
|
'action' => $this->generateUrl(strtolower($entity).'_create'), |
|
138
|
|
|
)); |
|
139
|
|
|
} |
|
140
|
|
|
if ($entity === 'Group') { |
|
141
|
|
|
$this->addRolesAction($form, $entityNew); |
|
142
|
|
|
} |
|
143
|
|
|
$return = array(strtolower($entity) => $entityNew, 'form' => $form->createView(),); |
|
144
|
|
|
|
|
145
|
|
|
return $return; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Creates a new item entity. |
|
150
|
|
|
* |
|
151
|
|
|
* @param Request $request Request in progress |
|
152
|
|
|
* @param string $entity Entity <i>First letter Upper</i> |
|
153
|
|
|
* @param string $entityPath Path of Entity |
|
154
|
|
|
* @param string $typePath Path of FormType |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function abstractCreateAction(Request $request, $entity, $entityPath, $typePath) |
|
158
|
|
|
{ |
|
159
|
|
|
$param = array(); |
|
160
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
161
|
|
|
$entityNew = $etm->getClassMetadata($entityPath)->newInstance(); |
|
162
|
|
|
$form = $this->createForm($typePath, $entityNew, array( |
|
163
|
|
|
'action' => $this->generateUrl(strtolower($entity).'_create'), |
|
164
|
|
|
)); |
|
165
|
|
|
if ($entity === 'Group') { |
|
166
|
|
|
$this->addRolesAction($form, $entityNew); |
|
167
|
|
|
} |
|
168
|
|
|
$form->handleRequest($request); |
|
169
|
|
|
$return = [$entity => $entityNew, 'form' => $form->createView(),]; |
|
170
|
|
|
|
|
171
|
|
|
if ($form->isValid()) { |
|
172
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
173
|
|
|
$etm->persist($entityNew); |
|
174
|
|
|
$etm->flush(); |
|
175
|
|
|
$this->addFlash('info', 'gestock.create.ok'); |
|
176
|
|
|
|
|
177
|
|
|
$param = $this->testReturnParam($entityNew, strtolower($entity)); |
|
178
|
|
|
$route = $form->get('addmore')->isClicked() ? strtolower($entity).'_new' : strtolower($entity).'_show'; |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
$return = $this->redirectToRoute($route, $param); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $return; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Displays a form to edit an existing item entity. |
|
188
|
|
|
* |
|
189
|
|
|
* @param Object $entity Entity |
|
190
|
|
|
* @param string $entityName Name of Entity |
|
191
|
|
|
* @param string $typePath Path of FormType |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function abstractEditAction($entity, $entityName, $typePath) |
|
195
|
|
|
{ |
|
196
|
|
|
$param = $this->testReturnParam($entity, $entityName); |
|
197
|
|
|
$editForm = $this->createForm($typePath, $entity, array( |
|
198
|
|
|
'action' => $this->generateUrl($entityName.'_update', $param), |
|
199
|
|
|
'method' => 'PUT', |
|
200
|
|
|
)); |
|
201
|
|
|
if ($entityName === 'group') { |
|
202
|
|
|
$this->addRolesAction($editForm, $entity); |
|
203
|
|
|
} |
|
204
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
205
|
|
|
|
|
206
|
|
|
return array( |
|
207
|
|
|
$entityName => $entity, |
|
208
|
|
|
'edit_form' => $editForm->createView(), |
|
209
|
|
|
'delete_form' => $deleteForm->createView(), |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Edits an existing item entity. |
|
215
|
|
|
* |
|
216
|
|
|
* @param Object $entity Entity |
|
217
|
|
|
* @param Request $request Request in progress |
|
218
|
|
|
* @param string $entityName Name of Entity |
|
219
|
|
|
* @param string $typePath Path of FormType |
|
220
|
|
|
* @return array |
|
221
|
|
|
*/ |
|
222
|
|
|
public function abstractUpdateAction($entity, Request $request, $entityName, $typePath) |
|
223
|
|
|
{ |
|
224
|
|
|
$param = $this->testReturnParam($entity, $entityName); |
|
225
|
|
|
$editForm = $this->createForm($typePath, $entity, array( |
|
226
|
|
|
'action' => $this->generateUrl($entityName.'_update', $param), |
|
227
|
|
|
'method' => 'PUT', |
|
228
|
|
|
)); |
|
229
|
|
|
if ($entityName === 'group') { |
|
230
|
|
|
$this->addRolesAction($editForm, $entity); |
|
231
|
|
|
} |
|
232
|
|
|
$editForm->handleRequest($request); |
|
233
|
|
|
$deleteForm = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
234
|
|
|
|
|
235
|
|
|
$return = array( |
|
236
|
|
|
$entityName => $entity, |
|
237
|
|
|
'edit_form' => $editForm->createView(), |
|
238
|
|
|
'delete_form' => $deleteForm->createView(), |
|
239
|
|
|
); |
|
240
|
|
|
|
|
241
|
|
|
if ($editForm->isValid()) { |
|
242
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
243
|
|
|
$this->addFlash('info', 'gestock.edit.ok'); |
|
244
|
|
|
|
|
245
|
|
|
$return = $this->redirectToRoute($entityName.'_edit', $param); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $return; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Deletes an item entity. |
|
253
|
|
|
* |
|
254
|
|
|
* @param Object $entity Entity |
|
255
|
|
|
* @param Request $request Request in progress |
|
256
|
|
|
* @param string $entityName Name of Entity |
|
257
|
|
|
* @return array |
|
258
|
|
|
*/ |
|
259
|
|
|
public function abstractDeleteAction($entity, Request $request, $entityName) |
|
260
|
|
|
{ |
|
261
|
|
|
$form = $this->createDeleteForm($entity->getId(), $entityName.'_delete'); |
|
262
|
|
|
if ($form->handleRequest($request)->isValid()) { |
|
263
|
|
|
$etm = $this->getDoctrine()->getManager(); |
|
264
|
|
|
$etm->remove($entity); |
|
265
|
|
|
$etm->flush(); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
private function testReturnParam($entity, $entityName) |
|
270
|
|
|
{ |
|
271
|
|
|
$entityArray = ['company', 'settings', 'group', 'tva']; |
|
272
|
|
|
if (in_array($entityName, $entityArray, true)) { |
|
273
|
|
|
$param = array('id' => $entity->getId()); |
|
274
|
|
|
} else { |
|
275
|
|
|
$param = array('slug' => $entity->getSlug()); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
return $param; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* SetOrder for the SortAction in views. |
|
283
|
|
|
* |
|
284
|
|
|
* @param string $name session name |
|
285
|
|
|
* @param string $entity entity name |
|
286
|
|
|
* @param string $field field name |
|
287
|
|
|
* @param string $type sort type ("ASC"/"DESC") |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function setOrder($name, $entity, $field, $type = 'ASC') |
|
290
|
|
|
{ |
|
291
|
|
|
$session = new Session(); |
|
292
|
|
|
|
|
293
|
|
|
$session->set('sort.'.$name, array('entity' => $entity, 'field' => $field, 'type' => $type)); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* GetOrder for the SortAction in views. |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $name session name |
|
300
|
|
|
* |
|
301
|
|
|
* @return array |
|
302
|
|
|
*/ |
|
303
|
|
|
protected function getOrder($name) |
|
304
|
|
|
{ |
|
305
|
|
|
$session = new Session(); |
|
306
|
|
|
|
|
307
|
|
|
return $session->has('sort.' . $name) ? $session->get('sort.' . $name) : null; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* AddQueryBuilderSort for the SortAction in views. |
|
312
|
|
|
* |
|
313
|
|
|
* @param QueryBuilder $qbd |
|
314
|
|
|
* @param string $name |
|
315
|
|
|
*/ |
|
316
|
|
|
protected function addQueryBuilderSort(QueryBuilder $qbd, $name) |
|
317
|
|
|
{ |
|
318
|
|
|
$alias = ''; |
|
319
|
|
|
if (is_array($order = $this->getOrder($name))) { |
|
320
|
|
|
if ($name !== $order['entity']) { |
|
321
|
|
|
$rootAlias = current($qbd->getDQLPart('from'))->getAlias(); |
|
322
|
|
|
$join = current($qbd->getDQLPart('join')); |
|
323
|
|
|
foreach ($join as $item) { |
|
324
|
|
|
if ($item->getJoin() === $rootAlias.'.'.$order['entity']) { |
|
325
|
|
|
$alias = $item->getAlias(); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} else { |
|
329
|
|
|
$alias = current($qbd->getDQLPart('from'))->getAlias(); |
|
330
|
|
|
} |
|
331
|
|
|
$qbd->orderBy($alias . '.' . $order['field'], $order['type']); |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Create Delete form. |
|
337
|
|
|
* |
|
338
|
|
|
* @param int $id |
|
339
|
|
|
* @param string $route |
|
340
|
|
|
* |
|
341
|
|
|
* @return \Symfony\Component\Form\Form |
|
342
|
|
|
*/ |
|
343
|
|
|
protected function createDeleteForm($id, $route) |
|
344
|
|
|
{ |
|
345
|
|
|
return $this->createFormBuilder(null, array('attr' => array('id' => 'delete'))) |
|
346
|
|
|
->setAction($this->generateUrl($route, array('id' => $id))) |
|
347
|
|
|
->setMethod('DELETE') |
|
348
|
|
|
->getForm() |
|
349
|
|
|
; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Array of file (`pdf`) layout. |
|
354
|
|
|
* |
|
355
|
|
|
* @param string $date File date |
|
356
|
|
|
* @param string $title File title |
|
357
|
|
|
* @return array<string,integer|string|boolean> |
|
358
|
|
|
*/ |
|
359
|
|
|
protected function getArray($date, $title) |
|
360
|
|
|
{ |
|
361
|
|
|
$array = array( |
|
362
|
|
|
'margin-top' => 15, |
|
363
|
|
|
'header-spacing' => 5, |
|
364
|
|
|
'header-font-size' => 8, |
|
365
|
|
|
'header-left' => 'G.L.S.R.', |
|
366
|
|
|
'header-center' => $title, |
|
367
|
|
|
'header-right' => $date, |
|
368
|
|
|
'header-line' => true, |
|
369
|
|
|
'margin-bottom' => 15, |
|
370
|
|
|
'footer-spacing' => 5, |
|
371
|
|
|
'footer-font-size' => 8, |
|
372
|
|
|
'footer-left' => 'GLSR © 2014 and beyond.', |
|
373
|
|
|
'footer-right' => 'Page [page]/[toPage]', |
|
374
|
|
|
'footer-line' => true, |
|
375
|
|
|
); |
|
376
|
|
|
return $array; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Get the existing roles |
|
381
|
|
|
* |
|
382
|
|
|
* @return array Array of roles |
|
383
|
|
|
*/ |
|
384
|
|
|
private function getExistingRoles() |
|
385
|
|
|
{ |
|
386
|
|
|
$roleHierarchy = $this->container->getParameter('security.role_hierarchy.roles'); |
|
387
|
|
|
$roles = array_keys($roleHierarchy); |
|
388
|
|
|
$theRoles = array(); |
|
389
|
|
|
|
|
390
|
|
|
foreach ($roles as $role) { |
|
391
|
|
|
$theRoles[$role] = $role; |
|
392
|
|
|
} |
|
393
|
|
|
return $theRoles; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* Add roles to form |
|
398
|
|
|
* |
|
399
|
|
|
* @param \Symfony\Component\Form\Form $form The form in which to insert the roles |
|
400
|
|
|
* @param \AppBundle\Entity\Group $group The entity to deal |
|
401
|
|
|
* @return \Symfony\Component\Form\Form The form |
|
402
|
|
|
*/ |
|
403
|
|
|
public function addRolesAction($form, $group) |
|
404
|
|
|
{ |
|
405
|
|
|
$form->add('roles', ChoiceType::class, array( |
|
406
|
|
|
'choices' => $this->getExistingRoles(), |
|
407
|
|
|
'choices_as_values' => true, |
|
408
|
|
|
'data' => $group->getRoles(), |
|
409
|
|
|
'label' => 'Roles', |
|
410
|
|
|
'expanded' => true, |
|
411
|
|
|
'multiple' => true, |
|
412
|
|
|
'mapped' => true, |
|
413
|
|
|
)); |
|
414
|
|
|
|
|
415
|
|
|
return $form; |
|
416
|
|
|
} |
|
417
|
|
|
} |
|
418
|
|
|
|
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.