|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BusinessEntityBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use Doctrine\Common\EventSubscriber; |
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
9
|
|
|
use Doctrine\ORM\Event\PostFlushEventArgs; |
|
10
|
|
|
use Doctrine\ORM\UnitOfWork; |
|
11
|
|
|
use Knp\DoctrineBehaviors\Model\Translatable\Translation; |
|
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
13
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity; |
|
14
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper; |
|
15
|
|
|
use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder; |
|
16
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; |
|
17
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; |
|
18
|
|
|
use Victoire\Bundle\BusinessPageBundle\Helper\BusinessPageHelper; |
|
19
|
|
|
use Victoire\Bundle\BusinessPageBundle\Repository\BusinessPageRepository; |
|
20
|
|
|
use Victoire\Bundle\I18nBundle\Entity\ViewTranslation; |
|
21
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Event\ViewReferenceEvent; |
|
22
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReferenceEvents; |
|
23
|
|
|
|
|
24
|
|
|
class BusinessEntitySubscriber implements EventSubscriber |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
protected $businessPageBuilder; |
|
27
|
|
|
protected $dispatcher; |
|
28
|
|
|
protected $businessEntityHelper; |
|
29
|
|
|
protected $flushedBusinessEntities; |
|
30
|
|
|
protected $businessPageHelper; |
|
31
|
|
|
protected $flushedBusinessTemplates; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param BusinessPageBuilder $businessPageBuilder |
|
35
|
|
|
* @param BusinessEntityHelper $businessEntityHelper |
|
36
|
|
|
* @param BusinessPageHelper $businessPageHelper |
|
37
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( |
|
40
|
|
|
BusinessPageBuilder $businessPageBuilder, |
|
41
|
|
|
BusinessEntityHelper $businessEntityHelper, |
|
42
|
|
|
BusinessPageHelper $businessPageHelper, |
|
43
|
|
|
EventDispatcherInterface $dispatcher |
|
44
|
|
|
) { |
|
45
|
|
|
$this->businessPageBuilder = $businessPageBuilder; |
|
46
|
|
|
$this->businessEntityHelper = $businessEntityHelper; |
|
47
|
|
|
$this->businessPageHelper = $businessPageHelper; |
|
48
|
|
|
$this->dispatcher = $dispatcher; |
|
49
|
|
|
$this->flushedBusinessEntities = new ArrayCollection(); |
|
50
|
|
|
$this->flushedBusinessTemplates = new ArrayCollection(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* bind to LoadClassMetadata method. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string[] The subscribed events |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getSubscribedEvents() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
'postUpdate', |
|
62
|
|
|
'postPersist', |
|
63
|
|
|
'preRemove', |
|
64
|
|
|
'postFlush', |
|
65
|
|
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
70
|
|
|
*/ |
|
71
|
|
|
public function postUpdate(LifecycleEventArgs $eventArgs) |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var EntityManager $entityManager */ |
|
74
|
|
|
$entityManager = $eventArgs->getEntityManager(); |
|
75
|
|
|
/** @var UnitOfWork $uow */ |
|
76
|
|
|
$uow = $entityManager->getUnitOfWork(); |
|
77
|
|
|
|
|
78
|
|
|
foreach ($uow->getScheduledEntityInsertions() as $entity) { |
|
79
|
|
|
$businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
|
|
|
|
|
|
80
|
|
|
if ($businessEntity) { |
|
81
|
|
|
$this->updateBusinessPages( |
|
82
|
|
|
$entity, |
|
83
|
|
|
$businessEntity, |
|
84
|
|
|
$entityManager, |
|
85
|
|
|
$uow->getScheduledEntityDeletions() |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
$this->updateViewReference($eventArgs); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
94
|
|
|
*/ |
|
95
|
|
|
public function postPersist(LifecycleEventArgs $eventArgs) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->updateViewReference($eventArgs); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
|
|
|
|
|
101
|
|
|
* get BusinessTemplate concerned by this entity (if so) |
|
102
|
|
|
* then get BusinessPages |
|
103
|
|
|
* for each BusinessPage, update its slug according to the new slug (if so). |
|
104
|
|
|
* |
|
105
|
|
|
* @param $entity |
|
|
|
|
|
|
106
|
|
|
* @param BusinessEntity $businessEntity |
|
107
|
|
|
* @param EntityManager $entityManager |
|
108
|
|
|
* @param array $deletions |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \Exception |
|
111
|
|
|
* |
|
112
|
|
|
* @internal param LifecycleEventArgs $eventArgs |
|
113
|
|
|
*/ |
|
114
|
|
|
public function updateBusinessPages($entity, BusinessEntity $businessEntity, EntityManager $entityManager, $deletions) |
|
115
|
|
|
{ |
|
116
|
|
|
$businessTemplates = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
|
117
|
|
|
foreach ($businessTemplates as $businessTemplate) { |
|
118
|
|
|
// Generate a viewReference for each BT translation |
|
119
|
|
|
/** @var ViewTranslation $translation */ |
|
120
|
|
|
foreach ($businessTemplate->getTranslations() as $translation) { |
|
121
|
|
|
$businessTemplate->setCurrentLocale($translation->getLocale()); |
|
122
|
|
|
if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $entityManager)) { |
|
123
|
|
|
/** @var BusinessPageRepository $bepRepo */ |
|
124
|
|
|
$bepRepo = $entityManager->getRepository('VictoireBusinessPageBundle:BusinessPage'); |
|
125
|
|
|
$virtualBusinessPage = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
126
|
|
|
$businessTemplate, |
|
127
|
|
|
$entity, |
|
128
|
|
|
$entityManager |
|
129
|
|
|
); |
|
130
|
|
|
// Get the BusinessPage if exists for the given entity |
|
131
|
|
|
/** @var BusinessPage $businessPage */ |
|
132
|
|
|
$businessPage = $bepRepo->findPageByBusinessEntityAndPattern( |
|
133
|
|
|
$businessTemplate, |
|
134
|
|
|
$entity, |
|
135
|
|
|
$businessEntity |
|
136
|
|
|
); |
|
137
|
|
|
$businessPage->setCurrentLocale($translation->getLocale()); |
|
138
|
|
|
// If there is diff between persisted BEP and computed, persist the change |
|
139
|
|
|
$scheduledForRemove = false; |
|
140
|
|
|
foreach ($deletions as $deletion) { |
|
141
|
|
|
if (get_class($deletion) == get_class($businessPage) |
|
142
|
|
|
&& $deletion->getId() === $businessPage->getId() |
|
143
|
|
|
) { |
|
144
|
|
|
$scheduledForRemove = true; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if ($businessPage && !$scheduledForRemove) { |
|
149
|
|
|
$businessPage->setName($virtualBusinessPage->getName()); |
|
150
|
|
|
$businessPage->setSlug($virtualBusinessPage->getSlug()); |
|
151
|
|
|
|
|
152
|
|
|
$entityManager->persist($businessPage); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
$entityManager->flush(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Iterate over inserted BusinessEntities and BusinessTemplates catched by postPersist |
|
162
|
|
|
* and dispatch event to generate the needed ViewReferences. |
|
163
|
|
|
* |
|
164
|
|
|
* @param PostFlushEventArgs $eventArgs |
|
165
|
|
|
* |
|
166
|
|
|
* @throws \Exception |
|
167
|
|
|
*/ |
|
168
|
|
|
public function postFlush(PostFlushEventArgs $eventArgs) |
|
169
|
|
|
{ |
|
170
|
|
|
$em = $eventArgs->getEntityManager(); |
|
171
|
|
|
foreach ($this->flushedBusinessEntities as $entity) { |
|
172
|
|
|
$businessEntity = $this->businessEntityHelper->findByEntityInstance($entity); |
|
173
|
|
|
//find all BT that can represent the businessEntity |
|
174
|
|
|
$businessTemplates = $em->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
|
175
|
|
View Code Duplication |
foreach ($businessTemplates as $businessTemplate) { |
|
|
|
|
|
|
176
|
|
|
// Generate a viewReference for each BT translation |
|
177
|
|
|
foreach ($businessTemplate->getTranslations() as $translation) { |
|
178
|
|
|
$businessTemplate->setCurrentLocale($translation->getLocale()); |
|
179
|
|
|
|
|
180
|
|
|
if ($page = $em->getRepository( |
|
181
|
|
|
'Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage' |
|
182
|
|
|
)->findPageByBusinessEntityAndPattern($businessTemplate, $entity, $businessEntity) |
|
183
|
|
|
) { |
|
184
|
|
|
//if it's a BP we update the BP |
|
185
|
|
|
$this->businessPageBuilder->updatePageParametersByEntity($page, $entity); |
|
186
|
|
|
} else { |
|
187
|
|
|
$page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
188
|
|
|
$businessTemplate, |
|
189
|
|
|
$entity, |
|
190
|
|
|
$em |
|
191
|
|
|
); |
|
192
|
|
|
} |
|
193
|
|
|
if ($this->businessPageHelper->isEntityAllowed($businessTemplate, $entity, $em)) { |
|
194
|
|
|
//update the reference |
|
195
|
|
|
$event = new ViewReferenceEvent($page); |
|
196
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
foreach ($this->flushedBusinessTemplates as $entity) { |
|
203
|
|
|
$em = $eventArgs->getEntityManager(); |
|
204
|
|
|
$businessEntityId = $entity->getBusinessEntityName(); |
|
205
|
|
|
$businessEntity = $eventArgs->getEntityManager()->getRepository('VictoireBusinessEntityBundle:BusinessEntity')->findOneBy(['name' => $businessEntityId]); |
|
206
|
|
|
//find all entities |
|
207
|
|
|
$entities = $this->businessPageHelper->getEntitiesAllowed($entity, $em); |
|
208
|
|
|
// Generate a viewReference for each BT translation |
|
209
|
|
View Code Duplication |
foreach ($entity->getTranslations() as $translation) { |
|
|
|
|
|
|
210
|
|
|
$entity->setCurrentLocale($translation->getLocale()); |
|
211
|
|
|
foreach ($entities as $be) { |
|
|
|
|
|
|
212
|
|
|
if ($this->businessPageHelper->isEntityAllowed($entity, $be, $em)) { |
|
213
|
|
|
if ($page = $em->getRepository( |
|
214
|
|
|
'Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage' |
|
215
|
|
|
)->findPageByBusinessEntityAndPattern($entity, $be, $businessEntity) |
|
216
|
|
|
) { |
|
217
|
|
|
//rebuild page if its a BP |
|
218
|
|
|
$this->businessPageBuilder->updatePageParametersByEntity($page, $be); |
|
219
|
|
|
} else { |
|
220
|
|
|
$page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
221
|
|
|
$entity, |
|
222
|
|
|
$be, |
|
223
|
|
|
$em |
|
224
|
|
|
); |
|
225
|
|
|
} |
|
226
|
|
|
// update reference |
|
227
|
|
|
$event = new ViewReferenceEvent($page); |
|
228
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
$this->flushedBusinessEntities->clear(); |
|
234
|
|
|
$this->flushedBusinessTemplates->clear(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* This method throw an event if needed for a view related to a businessEntity. |
|
239
|
|
|
* |
|
240
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
241
|
|
|
* |
|
242
|
|
|
* @throws \Exception |
|
243
|
|
|
*/ |
|
244
|
|
|
private function updateViewReference(LifecycleEventArgs $eventArgs) |
|
245
|
|
|
{ |
|
246
|
|
|
$entity = $eventArgs->getEntity(); |
|
247
|
|
|
|
|
248
|
|
|
//if entity is a translation, get its translatable entity |
|
249
|
|
|
if (in_array(Translation::class, class_uses($entity)) && null !== $entity->getTranslatable()) { |
|
250
|
|
|
$entity = $entity->getTranslatable(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
//if it's a businessEntity we need to rebuild virtuals (BPs are rebuild in businessEntitySubscriber) |
|
254
|
|
|
if ($businessEntity = $this->businessEntityHelper->findByEntityInstance($entity)) { |
|
|
|
|
|
|
255
|
|
|
$this->flushedBusinessEntities->add($entity); |
|
256
|
|
|
} |
|
257
|
|
|
//if it a businessTemplate we have to rebuild virtuals or update BP |
|
258
|
|
|
if ($entity instanceof BusinessTemplate) { |
|
259
|
|
|
$this->flushedBusinessTemplates->add($entity); |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* @param LifecycleEventArgs $eventArgs |
|
265
|
|
|
* |
|
266
|
|
|
* @throws \Exception |
|
267
|
|
|
*/ |
|
268
|
|
|
public function preRemove(LifecycleEventArgs $eventArgs) |
|
|
|
|
|
|
269
|
|
|
{ |
|
270
|
|
|
$entity = $eventArgs->getEntity(); |
|
271
|
|
|
|
|
272
|
|
|
//if we remove a BP we need to replace by a VBP ref |
|
273
|
|
View Code Duplication |
if ($entity instanceof BusinessPage) { |
|
|
|
|
|
|
274
|
|
|
//remove BP ref |
|
275
|
|
|
$event = new ViewReferenceEvent($entity); |
|
276
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
277
|
|
|
$em = $eventArgs->getEntityManager(); |
|
278
|
|
|
$businessTemplate = $entity->getTemplate(); |
|
279
|
|
|
$page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
280
|
|
|
$businessTemplate, |
|
|
|
|
|
|
281
|
|
|
$entity->getEntity(), |
|
282
|
|
|
$em |
|
283
|
|
|
); |
|
284
|
|
|
//create VBP ref |
|
285
|
|
|
//TODO :: dont rebuild if businessEntity or businessTemplate doesn't exist |
|
286
|
|
|
$event = new ViewReferenceEvent($page); |
|
287
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::UPDATE_VIEW_REFERENCE, $event); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
//if it's a businessEntity, we need to remove all BP and VBP ref |
|
291
|
|
|
if ($businessEntity = $this->businessEntityHelper->findByEntityInstance($entity)) { |
|
292
|
|
|
$em = $eventArgs->getEntityManager(); |
|
293
|
|
|
$businessTemplates = $em->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findPagePatternByBusinessEntity($businessEntity); |
|
294
|
|
|
foreach ($businessTemplates as $businessTemplate) { |
|
|
|
|
|
|
295
|
|
|
|
|
296
|
|
|
// Generate a viewReference for each BT translation |
|
297
|
|
|
foreach ($businessTemplate->getTranslations() as $translation) { |
|
298
|
|
|
$businessTemplate->setCurrentLocale($translation->getLocale()); |
|
299
|
|
View Code Duplication |
if ($page = $em->getRepository('Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage')->findPageByBusinessEntityAndPattern($businessTemplate, $entity, $businessEntity)) { |
|
|
|
|
|
|
300
|
|
|
$event = new ViewReferenceEvent($page); |
|
301
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
302
|
|
|
} else { |
|
303
|
|
|
$page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
304
|
|
|
$businessTemplate, |
|
305
|
|
|
$entity, |
|
306
|
|
|
$em |
|
307
|
|
|
); |
|
308
|
|
|
$event = new ViewReferenceEvent($page); |
|
309
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
//if we remove a businessTemplate remove all VBT ref (BP cascade remove) |
|
315
|
|
|
if ($entity instanceof BusinessTemplate) { |
|
316
|
|
|
$em = $eventArgs->getEntityManager(); |
|
317
|
|
|
$entities = $this->businessPageHelper->getEntitiesAllowed($entity, $em); |
|
318
|
|
|
|
|
319
|
|
|
// Generate a viewReference for each BT translation |
|
320
|
|
|
foreach ($entity->getTranslations() as $translation) { |
|
321
|
|
|
$entity->setCurrentLocale($translation->getLocale()); |
|
322
|
|
|
foreach ($entities as $be) { |
|
|
|
|
|
|
323
|
|
|
$page = $this->businessPageBuilder->generateEntityPageFromTemplate( |
|
324
|
|
|
$entity, |
|
325
|
|
|
$be, |
|
326
|
|
|
$em |
|
327
|
|
|
); |
|
328
|
|
|
$event = new ViewReferenceEvent($page); |
|
329
|
|
|
$this->dispatcher->dispatch(ViewReferenceEvents::REMOVE_VIEW_REFERENCE, $event); |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|