|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Form\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
6
|
|
|
use LAG\AdminBundle\Form\Type\MassActionType; |
|
7
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
8
|
|
|
use Symfony\Component\Form\FormInterface; |
|
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
10
|
|
|
use Twig_Environment; |
|
11
|
|
|
|
|
12
|
|
|
class ListFormHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var FormFactoryInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $formFactory; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Twig_Environment |
|
20
|
|
|
*/ |
|
21
|
|
|
private $twig; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(FormFactoryInterface $formFactory, Twig_Environment $twig) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->formFactory = $formFactory; |
|
26
|
|
|
$this->twig = $twig; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function handle(FormInterface $form, AdminInterface $admin) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!$form->isValid()) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
$data = $form->getData(); |
|
35
|
|
|
|
|
36
|
|
|
if (!array_key_exists('id', $data)) { |
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
$entityClass = $admin |
|
40
|
|
|
->getConfiguration() |
|
41
|
|
|
->getParameter('entity') |
|
42
|
|
|
; |
|
43
|
|
|
$entityProperty = $form |
|
44
|
|
|
->getConfig() |
|
45
|
|
|
->getOption('entity_property') |
|
46
|
|
|
; |
|
47
|
|
|
$entityValues = []; |
|
48
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
49
|
|
|
|
|
50
|
|
|
foreach ($data as $entity) { |
|
51
|
|
|
$entityValues[] = $accessor->getValue($entity, $entityProperty); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$massDeleteForm = $this |
|
55
|
|
|
->formFactory |
|
56
|
|
|
->create(MassActionType::class, [ |
|
57
|
|
|
'entity_class' => $entityClass, |
|
58
|
|
|
'entity_property' => $entityProperty, |
|
59
|
|
|
'entity_values' => $entityValues, |
|
60
|
|
|
]) |
|
61
|
|
|
; |
|
62
|
|
|
|
|
63
|
|
|
return $this |
|
64
|
|
|
->twig |
|
65
|
|
|
->render('@LAGAdmin/List/delete.html.twig', [ |
|
66
|
|
|
'form' => $massDeleteForm->createView(), |
|
67
|
|
|
|
|
68
|
|
|
]) |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
var_dump($data); |
|
|
|
|
|
|
73
|
|
|
die; |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
// use EntityLabelTrait; |
|
|
|
|
|
|
81
|
|
|
// |
|
82
|
|
|
// /** |
|
83
|
|
|
// * Return entities ids checked by user |
|
84
|
|
|
// * |
|
85
|
|
|
// * @param FormInterface $form |
|
86
|
|
|
// * @return array |
|
87
|
|
|
// */ |
|
88
|
|
|
// public function handle(FormInterface $form) |
|
89
|
|
|
// { |
|
90
|
|
|
// $data = $form->getData(); |
|
91
|
|
|
// $batchItems = []; |
|
92
|
|
|
// $cleanData = [ |
|
93
|
|
|
// 'ids' => [], |
|
94
|
|
|
// 'batch_action' => $data['batch_action'], |
|
95
|
|
|
// 'labels' => $this->getLabels($data['entities']) |
|
96
|
|
|
// ]; |
|
97
|
|
|
// |
|
98
|
|
|
// // find batch items checkbox values |
|
99
|
|
|
// foreach ($data as $name => $batchItem) { |
|
100
|
|
|
// if (substr($name, 0, 6) == 'batch_') { |
|
101
|
|
|
// $batchItems[$name] = $batchItem; |
|
102
|
|
|
// } |
|
103
|
|
|
// } |
|
104
|
|
|
// // check if they exists in entities displayed and if checkbox is checked |
|
105
|
|
|
// foreach ($batchItems as $name => $batchItem) { |
|
106
|
|
|
// $batchId = (int) str_replace('batch_', '', $name); |
|
107
|
|
|
// |
|
108
|
|
|
// if (array_key_exists($batchId, $cleanData['labels']) && $batchItem === true) { |
|
109
|
|
|
// $cleanData['ids'][] = $batchId; |
|
110
|
|
|
// } |
|
111
|
|
|
// } |
|
112
|
|
|
// return $cleanData; |
|
113
|
|
|
// } |
|
114
|
|
|
// |
|
115
|
|
|
// /** |
|
116
|
|
|
// * @param $entities |
|
117
|
|
|
// * @return array |
|
118
|
|
|
// */ |
|
119
|
|
|
// protected function getLabels($entities) |
|
120
|
|
|
// { |
|
121
|
|
|
// $labels = []; |
|
122
|
|
|
// |
|
123
|
|
|
// foreach ($entities as $entity) { |
|
124
|
|
|
// $labels[$entity->getId()] = $this->getEntityLabel($entity); |
|
125
|
|
|
// } |
|
126
|
|
|
// return $labels; |
|
127
|
|
|
// } |
|
128
|
|
|
} |
|
129
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.