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\BatchEvents; |
16
|
|
|
use FSi\Bundle\AdminBundle\Event\FormEvent; |
17
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
18
|
|
|
use LogicException; |
19
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
|
22
|
|
|
class BatchFormValidRequestHandler extends AbstractFormValidRequestHandler |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
26
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
27
|
|
|
*/ |
28
|
|
|
protected function action(FormEvent $event, Request $request) |
29
|
|
|
{ |
30
|
|
|
/** @var \FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement $element */ |
31
|
|
|
$element = $event->getElement(); |
32
|
|
|
$objects = $this->getObjects($element, $request); |
33
|
|
|
|
34
|
|
|
foreach ($objects as $object) { |
35
|
|
|
$element->apply($object); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \FSi\Bundle\AdminBundle\Admin\CRUD\BatchElement $element |
41
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
42
|
|
|
* @return array |
43
|
|
|
* @throws \FSi\Bundle\AdminBundle\Exception\ContextBuilderException |
44
|
|
|
*/ |
45
|
|
|
private function getObjects(BatchElement $element, Request $request) |
46
|
|
|
{ |
47
|
|
|
$objects = array(); |
48
|
|
|
$indexes = $request->request->get('indexes', array()); |
49
|
|
|
|
50
|
|
|
if (!count($indexes)) { |
51
|
|
|
throw new RequestHandlerException('There must be at least one object to execute batch action'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
foreach ($indexes as $index) { |
55
|
|
|
$object = $element->getDataIndexer()->getData($index); |
56
|
|
|
|
57
|
|
|
if (!isset($object)) { |
58
|
|
|
throw new RequestHandlerException(sprintf("Can't find object with id %s", $index)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$objects[] = $object; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $objects; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param \FSi\Bundle\AdminBundle\Event\FormEvent $event |
69
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
|
|
protected function isValidPostRequest(FormEvent $event, Request $request) |
73
|
|
|
{ |
74
|
|
|
$element = $event->getElement(); |
75
|
|
|
if ($element instanceof DeleteElement |
76
|
|
|
&& $element->hasOption('allow_delete') |
77
|
|
|
&& !$element->getOption('allow_delete') |
78
|
|
|
) { |
79
|
|
|
throw new LogicException(sprintf( |
80
|
|
|
'Tried to delete objects through element "%s", which has option "allow_delete" set to false', |
81
|
|
|
get_class($element) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return parent::isValidPostRequest($event, $request); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param FormEvent $event |
90
|
|
|
* @param Request $request |
91
|
|
|
* @return RedirectResponse |
92
|
|
|
*/ |
93
|
|
|
protected function getRedirectResponse(FormEvent $event, Request $request) |
94
|
|
|
{ |
95
|
|
|
if ($request->query->has('redirect_uri')) { |
96
|
|
|
return new RedirectResponse($request->query->get('redirect_uri')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return parent::getRedirectResponse($event, $request); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
protected function getPreSaveEventName() |
106
|
|
|
{ |
107
|
|
|
return BatchEvents::BATCH_OBJECTS_PRE_APPLY; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected function getPostSaveEventName() |
114
|
|
|
{ |
115
|
|
|
return BatchEvents::BATCH_OBJECTS_POST_APPLY; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|