|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Doctrine\ORM\EntityRepository; |
|
8
|
|
|
use LAG\AdminBundle\Admin\AdminInterface; |
|
9
|
|
|
use LAG\AdminBundle\DataProvider\DataProviderInterface; |
|
10
|
|
|
use LAG\AdminBundle\Event\Events; |
|
11
|
|
|
use LAG\AdminBundle\Event\DoctrineOrmFilterEvent; |
|
12
|
|
|
use LAG\AdminBundle\Exception\Exception; |
|
13
|
|
|
use Pagerfanta\Adapter\DoctrineORMAdapter; |
|
14
|
|
|
use Pagerfanta\Pagerfanta; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
17
|
|
|
|
|
18
|
|
|
class ORMDataProvider implements DataProviderInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityManagerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $entityManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var EventDispatcherInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $eventDispatcher; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RequestStack |
|
32
|
|
|
*/ |
|
33
|
|
|
private $requestStack; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* DoctrineORMDataProvider constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param EntityManagerInterface $entityManager |
|
39
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
40
|
|
|
* @param RequestStack $requestStack |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
EntityManagerInterface $entityManager, |
|
44
|
|
|
EventDispatcherInterface $eventDispatcher, |
|
45
|
|
|
RequestStack $requestStack |
|
46
|
|
|
) { |
|
47
|
|
|
$this->entityManager = $entityManager; |
|
48
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
49
|
|
|
$this->requestStack = $requestStack; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Load a collection of entities. |
|
54
|
|
|
* |
|
55
|
|
|
* @param AdminInterface $admin |
|
56
|
|
|
* @param array $filters |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCollection(AdminInterface $admin, array $filters = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$adminConfiguration = $admin->getConfiguration(); |
|
63
|
|
|
$actionConfiguration = $admin->getAction()->getConfiguration(); |
|
64
|
|
|
|
|
65
|
|
|
// Create a query builder for the configured entity class |
|
66
|
|
|
$queryBuilder = $this |
|
67
|
|
|
->getRepository($adminConfiguration->getParameter('entity')) |
|
68
|
|
|
->createQueryBuilder('entity') |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
// Dispatch an event to allow filter modification on the query builder |
|
72
|
|
|
$event = new DoctrineOrmFilterEvent($queryBuilder, $admin, $filters); |
|
73
|
|
|
$this->eventDispatcher->dispatch(Events::DOCTRINE_ORM_FILTER, $event); |
|
74
|
|
|
|
|
75
|
|
|
if ('pagerfanta' === $actionConfiguration->getParameter('pager')) { |
|
76
|
|
|
$pageParameter = $actionConfiguration->getParameter('page_parameter'); |
|
77
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
78
|
|
|
$page = (int)$request->get($pageParameter, 1); |
|
79
|
|
|
|
|
80
|
|
|
$adapter = new DoctrineORMAdapter($queryBuilder); |
|
81
|
|
|
$pager = new Pagerfanta($adapter); |
|
82
|
|
|
$pager->setCurrentPage($page); |
|
83
|
|
|
$pager->setMaxPerPage($actionConfiguration->getParameter('max_per_page')); |
|
84
|
|
|
$entities = $pager; |
|
85
|
|
|
} else { |
|
86
|
|
|
$entities = $queryBuilder->getQuery()->getResult(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $entities; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @inheritdoc |
|
94
|
|
|
*/ |
|
95
|
|
|
public function get(AdminInterface $admin, string $identifier) |
|
96
|
|
|
{ |
|
97
|
|
|
$class = $admin->getConfiguration()->getParameter('entity'); |
|
98
|
|
|
$item = $this |
|
99
|
|
|
->getRepository($class) |
|
100
|
|
|
->find($identifier) |
|
101
|
|
|
; |
|
102
|
|
|
|
|
103
|
|
|
if (null === $item) { |
|
104
|
|
|
throw new Exception(sprintf( |
|
105
|
|
|
'Item of class "%s" with identifier "%s" not found.', |
|
106
|
|
|
$class, |
|
107
|
|
|
$identifier |
|
108
|
|
|
)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $item; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritdoc |
|
116
|
|
|
*/ |
|
117
|
|
|
public function save(AdminInterface $admin): void |
|
118
|
|
|
{ |
|
119
|
|
|
$this->entityManager->persist($admin->getEntities()->first()); |
|
120
|
|
|
$this->entityManager->flush(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
|
|
public function create(AdminInterface $admin) |
|
127
|
|
|
{ |
|
128
|
|
|
$class = $admin->getConfiguration()->getParameter('entity'); |
|
129
|
|
|
|
|
130
|
|
|
return new $class(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @inheritdoc |
|
135
|
|
|
*/ |
|
136
|
|
|
public function delete(AdminInterface $admin): void |
|
137
|
|
|
{ |
|
138
|
|
|
if ($admin->getEntities()->isEmpty()) { |
|
139
|
|
|
throw new Exception('The admin "'.$admin->getName().'" has no loaded entity'); |
|
140
|
|
|
} |
|
141
|
|
|
$this->entityManager->remove($admin->getEntities()->first()); |
|
142
|
|
|
$this->entityManager->flush(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $entityClass |
|
147
|
|
|
* |
|
148
|
|
|
* @return ObjectRepository|EntityRepository |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getRepository(string $entityClass) |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->entityManager->getRepository($entityClass); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|