1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractFormValidRequestHandler; |
13
|
|
|
use FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement; |
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\CRUD\DeleteElement; |
15
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchEvent; |
16
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchEvents; |
17
|
|
|
use FSi\Bundle\AdminBundle\Event\BatchPreApplyEvent; |
18
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
19
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
20
|
|
|
use FSi\Bundle\AdminBundle\Message\FlashMessages; |
21
|
|
|
use LogicException; |
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
23
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
24
|
|
|
use Symfony\Component\HttpFoundation\Request; |
25
|
|
|
use Symfony\Component\Routing\RouterInterface; |
26
|
|
|
|
27
|
|
|
class BatchFormValidRequestHandler extends AbstractFormValidRequestHandler |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var FlashMessages |
31
|
|
|
*/ |
32
|
|
|
private $flashMessages; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
36
|
|
|
* @param RouterInterface $router |
37
|
|
|
* @param FlashMessages|null |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
EventDispatcherInterface $eventDispatcher, |
41
|
|
|
RouterInterface $router, |
42
|
|
|
FlashMessages $flashMessages |
43
|
|
|
) { |
44
|
|
|
parent::__construct($eventDispatcher, $router); |
45
|
|
|
$this->flashMessages = $flashMessages; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param FormEvent $event |
50
|
|
|
* @param Request $request |
51
|
|
|
*/ |
52
|
|
|
protected function action(FormEvent $event, Request $request) |
53
|
|
|
{ |
54
|
|
|
/** @var BatchElement $element */ |
55
|
|
|
$element = $event->getElement(); |
56
|
|
|
$objects = $this->getObjects($element, $request); |
57
|
|
|
|
58
|
|
|
if (empty($objects)) { |
59
|
|
|
$this->flashMessages->warning('messages.batch.no_elements'); |
60
|
|
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($objects as $object) { |
64
|
|
|
$preEvent = $this->eventDispatcher->dispatch( |
65
|
|
|
BatchEvents::BATCH_OBJECT_PRE_APPLY, |
66
|
|
|
new BatchPreApplyEvent($element, $request, $object) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if ($this->shouldSkip($preEvent)) { |
|
|
|
|
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$element->apply($object); |
74
|
|
|
|
75
|
|
|
$this->eventDispatcher->dispatch( |
76
|
|
|
BatchEvents::BATCH_OBJECT_POST_APPLY, |
77
|
|
|
new BatchEvent($element, $request, $object) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param BatchElement $element |
84
|
|
|
* @param Request $request |
85
|
|
|
* @return array |
86
|
|
|
* @throws RequestHandlerException |
87
|
|
|
*/ |
88
|
|
|
private function getObjects(BatchElement $element, Request $request) |
89
|
|
|
{ |
90
|
|
|
$objects = []; |
91
|
|
|
$indexes = $request->request->get('indexes', []); |
92
|
|
|
|
93
|
|
|
if (!count($indexes)) { |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($indexes as $index) { |
98
|
|
|
$object = $element->getDataIndexer()->getData($index); |
99
|
|
|
|
100
|
|
|
if (!isset($object)) { |
101
|
|
|
throw new RequestHandlerException(sprintf("Can't find object with id %s", $index)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$objects[] = $object; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $objects; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param FormEvent $event |
112
|
|
|
* @param Request $request |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function isValidPostRequest(FormEvent $event, Request $request) |
116
|
|
|
{ |
117
|
|
|
$element = $event->getElement(); |
118
|
|
|
if ($element instanceof DeleteElement |
119
|
|
|
&& $element->hasOption('allow_delete') |
120
|
|
|
&& !$element->getOption('allow_delete') |
121
|
|
|
) { |
122
|
|
|
throw new LogicException(sprintf( |
123
|
|
|
'Tried to delete objects through element "%s", which has option "allow_delete" set to false', |
124
|
|
|
get_class($element) |
125
|
|
|
)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return parent::isValidPostRequest($event, $request); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param FormEvent $event |
133
|
|
|
* @param Request $request |
134
|
|
|
* @return RedirectResponse |
135
|
|
|
*/ |
136
|
|
|
protected function getRedirectResponse(FormEvent $event, Request $request) |
137
|
|
|
{ |
138
|
|
|
if ($request->query->has('redirect_uri')) { |
139
|
|
|
return new RedirectResponse($request->query->get('redirect_uri')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return parent::getRedirectResponse($event, $request); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function getPreSaveEventName() |
149
|
|
|
{ |
150
|
|
|
return BatchEvents::BATCH_OBJECTS_PRE_APPLY; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
protected function getPostSaveEventName() |
157
|
|
|
{ |
158
|
|
|
return BatchEvents::BATCH_OBJECTS_POST_APPLY; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param BatchPreApplyEvent $event |
163
|
|
|
* @return boolean |
164
|
|
|
*/ |
165
|
|
|
private function shouldSkip($event) |
166
|
|
|
{ |
167
|
|
|
if (!($event instanceof BatchPreApplyEvent)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $event->shouldSkip(); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.