1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\GridBundle\Form\EventSubscriber\Batch; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\GridBundle\Form\Type\Batch\GridBatchValueType; |
15
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
16
|
|
|
use Lug\Component\Grid\Model\GridInterface; |
17
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
18
|
|
|
use Lug\Component\Resource\Repository\RepositoryInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use Symfony\Component\Form\FormEvent; |
21
|
|
|
use Symfony\Component\Form\FormEvents; |
22
|
|
|
use Symfony\Component\Form\FormInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
abstract class AbstractGridBatchSubscriber implements EventSubscriberInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var RegistryInterface |
31
|
|
|
*/ |
32
|
|
|
private $repositoryRegistry; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ParameterResolverInterface |
36
|
|
|
*/ |
37
|
|
|
private $parameterResolver; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param RegistryInterface $repositoryRegistry |
41
|
|
|
* @param ParameterResolverInterface $parameterResolver |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
RegistryInterface $repositoryRegistry, |
45
|
|
|
ParameterResolverInterface $parameterResolver |
46
|
|
|
) { |
47
|
|
|
$this->repositoryRegistry = $repositoryRegistry; |
48
|
|
|
$this->parameterResolver = $parameterResolver; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param FormEvent $event |
53
|
|
|
*/ |
54
|
|
|
public function onPreSetData(FormEvent $event) |
55
|
|
|
{ |
56
|
|
|
$form = $event->getForm(); |
57
|
|
|
$grid = $form->getConfig()->getOption('grid'); |
58
|
|
|
|
59
|
|
|
$this->buildForm( |
60
|
|
|
$form, |
61
|
|
|
!$this->parameterResolver->resolveApi() ? iterator_to_array($grid->getDataSource()) : [] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param FormEvent $event |
67
|
|
|
*/ |
68
|
|
|
public function onPreSubmit(FormEvent $event) |
69
|
|
|
{ |
70
|
|
|
$form = $event->getForm(); |
71
|
|
|
$data = $event->getData(); |
72
|
|
|
|
73
|
|
|
if (isset($data['all'])) { |
74
|
|
|
$data['value'] = $this->handleAll($form); |
75
|
|
|
$event->setData($data); |
76
|
|
|
} else { |
77
|
|
|
$this->handleValue($form, $data); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public static function getSubscribedEvents() |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
FormEvents::PRE_SET_DATA => 'onPreSetData', |
88
|
|
|
FormEvents::PRE_SUBMIT => 'onPreSubmit', |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param GridInterface $grid |
94
|
|
|
* @param RepositoryInterface $repository |
95
|
|
|
* @param mixed[] $choices |
96
|
|
|
* |
97
|
|
|
* @return object[] |
98
|
|
|
*/ |
99
|
|
|
abstract protected function findChoices(GridInterface $grid, RepositoryInterface $repository, array $choices); |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param FormInterface $form |
103
|
|
|
* @param object[] $choices |
104
|
|
|
*/ |
105
|
|
|
private function buildForm(FormInterface $form, array $choices) |
106
|
|
|
{ |
107
|
|
|
$form |
108
|
|
|
->remove('value') |
109
|
|
|
->add('value', GridBatchValueType::class, [ |
110
|
|
|
'choices' => $choices, |
111
|
|
|
'grid' => $form->getConfig()->getOption('grid'), |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param FormInterface $form |
117
|
|
|
* |
118
|
|
|
* @return mixed[] |
119
|
|
|
*/ |
120
|
|
|
private function handleAll(FormInterface $form) |
121
|
|
|
{ |
122
|
|
|
$config = $form->get('value')->getConfig(); |
123
|
|
|
$choiceValue = $config->getOption('choice_value'); |
124
|
|
|
$choices = iterator_to_array($config->getOption('grid')->getDataSource(['all' => true])); |
125
|
|
|
|
126
|
|
|
$this->buildForm($form, $choices); |
127
|
|
|
|
128
|
|
|
return array_map(function ($choice) use ($choiceValue) { |
129
|
|
|
return call_user_func($choiceValue, $choice); |
130
|
|
|
}, $choices); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param FormInterface $form |
135
|
|
|
* @param mixed[] $data |
136
|
|
|
*/ |
137
|
|
|
private function handleValue(FormInterface $form, $data) |
138
|
|
|
{ |
139
|
|
|
$values = isset($data['value']) ? $data['value'] : []; |
140
|
|
|
|
141
|
|
|
if (!is_array($values) || empty($values)) { |
142
|
|
|
return; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$config = $form->get('value')->getConfig(); |
146
|
|
|
$choices = $config->getOption('choices'); |
147
|
|
|
$indexedChoices = $config->getAttribute('choice_list')->getChoices(); |
148
|
|
|
|
149
|
|
|
foreach ($values as $key => $value) { |
150
|
|
|
if (isset($indexedChoices[$value])) { |
151
|
|
|
unset($values[$key]); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$this->buildForm($form, array_merge($choices, $this->findChoices( |
156
|
|
|
$config->getOption('grid')->getDefinition(), |
157
|
|
|
$this->repositoryRegistry[$config->getOption('grid')->getDefinition()->getResource()->getName()], |
158
|
|
|
$values |
159
|
|
|
))); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|