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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace FSi\Bundle\AdminBundle\Admin\CRUD\Context\Request; |
13
|
|
|
|
14
|
|
|
use FSi\Bundle\AdminBundle\Admin\Context\Request\AbstractHandler; |
15
|
|
|
use FSi\Bundle\AdminBundle\Event\AdminEvent; |
16
|
|
|
use FSi\Bundle\AdminBundle\Event\ListEvent; |
17
|
|
|
use FSi\Bundle\AdminBundle\Event\ListEvents; |
18
|
|
|
use FSi\Bundle\AdminBundle\Exception\RequestHandlerException; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
|
22
|
|
|
class DataGridBindDataHandler extends AbstractHandler |
23
|
|
|
{ |
24
|
|
|
public function handleRequest(AdminEvent $event, Request $request): ?Response |
25
|
|
|
{ |
26
|
|
|
$event = $this->validateEvent($event); |
27
|
|
|
|
28
|
|
|
if ($request->isMethod(Request::METHOD_POST)) { |
29
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_PRE_BIND, $event); |
|
|
|
|
30
|
|
|
if ($event->hasResponse()) { |
31
|
|
|
return $event->getResponse(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$event->getDataGrid()->bindData($request); |
35
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_REQUEST_POST_BIND, $event); |
36
|
|
|
if ($event->hasResponse()) { |
37
|
|
|
return $event->getResponse(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$event->getElement()->saveDataGrid(); |
|
|
|
|
41
|
|
|
$event->getDataSource()->bindParameters($request); |
|
|
|
|
42
|
|
|
$event->getDataGrid()->setData($event->getDataSource()->getResult()); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->eventDispatcher->dispatch(ListEvents::LIST_RESPONSE_PRE_RENDER, $event); |
46
|
|
|
if ($event->hasResponse()) { |
47
|
|
|
return $event->getResponse(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function validateEvent(AdminEvent $event): ListEvent |
54
|
|
|
{ |
55
|
|
|
if (!$event instanceof ListEvent) { |
56
|
|
|
throw new RequestHandlerException(sprintf('%s require ListEvent', get_class($this))); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $event; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|