1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
12
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity; |
13
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper; |
14
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\VirtualBusinessPage; |
15
|
|
|
use Victoire\Bundle\BusinessPageBundle\Transformer\VirtualToBusinessPageTransformer; |
16
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
17
|
|
|
use Victoire\Bundle\CoreBundle\Event\WidgetFlushedEvent; |
18
|
|
|
use Victoire\Bundle\CoreBundle\VictoireCmsEvents; |
19
|
|
|
use Victoire\Bundle\FormBundle\Helper\FormErrorHelper; |
20
|
|
|
use Victoire\Bundle\PageBundle\Helper\PageHelper; |
21
|
|
|
use Victoire\Bundle\WidgetBundle\Builder\WidgetFormBuilder; |
22
|
|
|
use Victoire\Bundle\WidgetBundle\Helper\WidgetHelper; |
23
|
|
|
use Victoire\Bundle\WidgetBundle\Renderer\WidgetRenderer; |
24
|
|
|
use Victoire\Bundle\WidgetBundle\Resolver\WidgetContentResolver; |
25
|
|
|
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder; |
26
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
27
|
|
|
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper; |
28
|
|
|
use Victoire\Bundle\WidgetMapBundle\Manager\WidgetMapManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This manager handles crud operations on a Widget. |
32
|
|
|
*/ |
33
|
|
|
class WidgetManager |
34
|
|
|
{ |
35
|
|
|
protected $widgetFormBuilder; |
36
|
|
|
protected $widgetHelper; |
37
|
|
|
protected $widgetContentResolver; |
38
|
|
|
protected $widgetRenderer; |
39
|
|
|
protected $eventDispatcher; |
40
|
|
|
protected $entityManager; |
41
|
|
|
protected $formErrorHelper; // @victoire_form.error_helper |
42
|
|
|
protected $request; // @request |
43
|
|
|
protected $widgetMapManager; |
44
|
|
|
protected $businessEntityHelper; |
45
|
|
|
protected $templating; |
46
|
|
|
protected $pageHelper; |
47
|
|
|
protected $slots; // %victoire_core.slots% |
48
|
|
|
protected $virtualToBpTransformer; |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* construct. |
52
|
|
|
* |
53
|
|
|
* @param WidgetHelper $widgetHelper |
|
|
|
|
54
|
|
|
* @param WidgetFormBuilder $widgetFormBuilder |
|
|
|
|
55
|
|
|
* @param WidgetContentResolver $widgetContentResolver |
|
|
|
|
56
|
|
|
* @param WidgetRenderer $widgetRenderer |
|
|
|
|
57
|
|
|
* @param EventDispatcherInterface $eventDispatcher, |
|
|
|
|
58
|
|
|
* @param EntityManager $entityManager |
|
|
|
|
59
|
|
|
* @param FormErrorHelper $formErrorHelper |
|
|
|
|
60
|
|
|
* @param Request $request |
|
|
|
|
61
|
|
|
* @param WidgetMapManager $widgetMapManager |
|
|
|
|
62
|
|
|
* @param WidgetMapBuilder $widgetMapBuilder |
|
|
|
|
63
|
|
|
* @param EngineInterface $templating |
|
|
|
|
64
|
|
|
* @param PageHelper $pageHelper |
|
|
|
|
65
|
|
|
* @param array $slots |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
WidgetHelper $widgetHelper, |
69
|
|
|
WidgetFormBuilder $widgetFormBuilder, |
70
|
|
|
WidgetContentResolver $widgetContentResolver, |
71
|
|
|
WidgetRenderer $widgetRenderer, |
72
|
|
|
EventDispatcherInterface $eventDispatcher, |
73
|
|
|
EntityManager $entityManager, |
|
|
|
|
74
|
|
|
FormErrorHelper $formErrorHelper, |
75
|
|
|
Request $request, |
|
|
|
|
76
|
|
|
WidgetMapManager $widgetMapManager, |
77
|
|
|
WidgetMapBuilder $widgetMapBuilder, |
78
|
|
|
BusinessEntityHelper $businessEntityHelper, |
79
|
|
|
EngineInterface $templating, |
80
|
|
|
PageHelper $pageHelper, |
81
|
|
|
$slots, |
82
|
|
|
VirtualToBusinessPageTransformer $virtualToBpTransformer |
83
|
|
|
) { |
84
|
|
|
$this->widgetFormBuilder = $widgetFormBuilder; |
85
|
|
|
$this->widgetHelper = $widgetHelper; |
86
|
|
|
$this->widgetContentResolver = $widgetContentResolver; |
87
|
|
|
$this->widgetRenderer = $widgetRenderer; |
88
|
|
|
$this->eventDispatcher = $eventDispatcher; |
89
|
|
|
$this->entityManager = $entityManager; |
90
|
|
|
$this->formErrorHelper = $formErrorHelper; |
91
|
|
|
$this->request = $request; |
92
|
|
|
$this->widgetMapManager = $widgetMapManager; |
93
|
|
|
$this->widgetMapBuilder = $widgetMapBuilder; |
|
|
|
|
94
|
|
|
$this->businessEntityHelper = $businessEntityHelper; |
95
|
|
|
$this->templating = $templating; |
96
|
|
|
$this->pageHelper = $pageHelper; |
97
|
|
|
$this->slots = $slots; |
98
|
|
|
$this->virtualToBpTransformer = $virtualToBpTransformer; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
|
|
|
|
102
|
|
|
* new widget. |
103
|
|
|
* |
104
|
|
|
* @param string $type |
|
|
|
|
105
|
|
|
* @param string $slot |
|
|
|
|
106
|
|
|
* @param View $view |
|
|
|
|
107
|
|
|
* @param int $position |
|
|
|
|
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function newWidget($mode, $type, $slot, $view, $position, $parentWidgetMap, $quantum) |
112
|
|
|
{ |
113
|
|
|
$quantum = $this->widgetFormBuilder->convertToString($quantum); |
114
|
|
|
|
115
|
|
|
$widget = $this->widgetHelper->newWidgetInstance($type, $mode); |
116
|
|
|
$widgets = [$widget]; |
117
|
|
|
|
118
|
|
|
/** @var BusinessEntity[] $classes */ |
119
|
|
|
$classes = $this->businessEntityHelper->getAvailableForWidget($this->widgetHelper->getWidgetName($widget)); |
120
|
|
|
|
121
|
|
|
$forms = $this->widgetFormBuilder->renderNewQuantumForms($slot, $view, $widgets, $widget, $classes, $position, $parentWidgetMap, $quantum); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return [ |
124
|
|
|
'widget' => $widget, |
125
|
|
|
'html' => $this->templating->render( |
126
|
|
|
'VictoireCoreBundle:Widget:Form/new.html.twig', |
127
|
|
|
[ |
128
|
|
|
'id' => time(), |
129
|
|
|
'view' => $view, |
130
|
|
|
'slot' => $slot, |
131
|
|
|
'position' => $position, |
132
|
|
|
'parentWidgetMap' => $parentWidgetMap, |
133
|
|
|
'classes' => $classes, |
134
|
|
|
'widgets' => $widgets, |
135
|
|
|
'widget' => $widget, |
136
|
|
|
'forms' => $forms, |
137
|
|
|
'quantum' => $quantum, |
138
|
|
|
] |
139
|
|
|
), |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
|
|
|
|
144
|
|
|
* Create a widget. |
145
|
|
|
* |
146
|
|
|
* @param string $mode |
147
|
|
|
* @param string $type |
148
|
|
|
* @param string $slotId |
149
|
|
|
* @param View $view |
150
|
|
|
* @param string $entity |
151
|
|
|
* @param string $type |
|
|
|
|
152
|
|
|
* |
153
|
|
|
* @throws \Exception |
154
|
|
|
* |
155
|
|
|
* @return Template |
156
|
|
|
*/ |
157
|
|
|
public function createWidget($mode, $type, $slotId, View $view, $entity, $position, $widgetReference, $quantum) |
158
|
|
|
{ |
159
|
|
|
//services |
160
|
|
|
$formErrorHelper = $this->formErrorHelper; |
161
|
|
|
$request = $this->request; |
162
|
|
|
|
163
|
|
|
if ($view instanceof VirtualBusinessPage) { |
164
|
|
|
$this->virtualToBpTransformer->transform($view); |
165
|
|
|
} |
166
|
|
|
//create a new widget |
167
|
|
|
$widget = $this->widgetHelper->newWidgetInstance($type, $mode); |
168
|
|
|
|
169
|
|
|
$businessEntity = $this->entityManager->getRepository('VictoireBusinessEntityBundle:BusinessEntity')->findOneBy(['name' => $entity]); |
170
|
|
|
$form = $this->widgetFormBuilder->callBuildFormSwitchParameters($widget, $view, $businessEntity, $position, $widgetReference, $slotId, $quantum); |
|
|
|
|
171
|
|
|
|
172
|
|
|
$noValidate = $request->query->get('novalidate', false); |
173
|
|
|
|
174
|
|
|
$form->handleRequest($request); |
175
|
|
|
if ($noValidate === false && $form->isValid()) { |
176
|
|
|
if (!$view->getId()) { |
177
|
|
|
//create a view for the business entity instance if we are currently on a virtual one |
178
|
|
|
$this->entityManager->persist($view); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
//get the widget from the form |
182
|
|
|
$widget = $form->getData(); |
183
|
|
|
|
184
|
|
|
//persist the widget |
185
|
|
|
$this->entityManager->persist($widget); |
186
|
|
|
$this->entityManager->flush(); |
187
|
|
|
|
188
|
|
|
$this->widgetMapManager->insert($widget, $view, $slotId, $position, $widgetReference); |
189
|
|
|
|
190
|
|
|
$this->entityManager->persist($view); |
191
|
|
|
$this->entityManager->flush(); |
192
|
|
|
|
193
|
|
|
$widget->setCurrentView($view); |
194
|
|
|
|
195
|
|
|
$event = new WidgetFlushedEvent($widget); |
196
|
|
|
$this->eventDispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_FLUSH, $event); |
197
|
|
|
$this->eventDispatcher->dispatch(VictoireCmsEvents::WIDGET_POST_FLUSH.'_'.strtoupper($type), $event); |
198
|
|
|
|
199
|
|
|
$this->widgetMapBuilder->build($view); |
200
|
|
|
|
201
|
|
|
//get the html for the widget |
202
|
|
|
$htmlWidget = $this->widgetRenderer->renderContainer($widget, $view); |
203
|
|
|
|
204
|
|
|
$response = [ |
205
|
|
|
'success' => true, |
206
|
|
|
'widgetId' => $widget->getId(), |
207
|
|
|
'html' => $htmlWidget, |
208
|
|
|
]; |
209
|
|
|
} else { |
210
|
|
|
//get the errors as a string |
211
|
|
|
$response = [ |
212
|
|
|
'success' => false, |
213
|
|
|
'message' => $noValidate === false ? $formErrorHelper->getRecursiveReadableErrors($form) : null, |
214
|
|
|
'html' => $this->widgetFormBuilder->renderNewForm($form, $widget, $slotId, $view, $quantum, $entity), |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $response; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
|
|
|
|
222
|
|
|
* edit a widget. |
223
|
|
|
* |
224
|
|
|
* @param Request $request |
225
|
|
|
* @param Widget $widget |
226
|
|
|
* @param View $currentView |
227
|
|
|
* @param string $businessEntityName The entity name is used to know which form to submit |
|
|
|
|
228
|
|
|
* |
229
|
|
|
* @return template |
230
|
|
|
*/ |
231
|
|
|
public function editWidget(Request $request, Widget $widget, View $currentView, $quantum = null, $businessEntityName = null, $widgetMode = Widget::MODE_STATIC) |
232
|
|
|
{ |
233
|
|
|
/** @var BusinessEntity[] $classes */ |
234
|
|
|
$classes = $this->businessEntityHelper->getAvailableForWidget($this->widgetHelper->getWidgetName($widget)); |
|
|
|
|
235
|
|
|
|
236
|
|
|
//the id of the edited widget |
237
|
|
|
//a new widget might be created in the case of a legacy |
238
|
|
|
$initialWidgetId = $widget->getId(); |
239
|
|
|
|
240
|
|
|
//the type of method used |
241
|
|
|
$requestMethod = $request->getMethod(); |
242
|
|
|
|
243
|
|
|
//if the form is posted |
244
|
|
|
if ($requestMethod === 'POST') { |
245
|
|
|
//the widget view |
246
|
|
|
$widgetView = $widget->getWidgetMap()->getView(); |
247
|
|
|
|
248
|
|
|
//we only copy the widget if the view of the widget is not the current view |
249
|
|
|
if ($widgetView !== $currentView) { |
250
|
|
|
$widget = $this->overwriteWidget($currentView, $widget); |
251
|
|
|
} |
252
|
|
|
$form = $this->widgetFormBuilder->buildForm($widget, $currentView, $businessEntityName, $widgetMode, null, null, null, $quantum); |
|
|
|
|
253
|
|
|
|
254
|
|
|
$noValidate = $request->query->get('novalidate', false); |
255
|
|
|
$form->handleRequest($request); |
256
|
|
|
if ($noValidate === false && $form->isValid()) { |
|
|
|
|
257
|
|
|
|
258
|
|
|
//force cache invalidation |
259
|
|
|
$widget->setUpdatedAt(new \DateTime()); |
260
|
|
|
$this->entityManager->persist($widget); |
261
|
|
|
|
262
|
|
|
$this->entityManager->persist($currentView); |
263
|
|
|
$this->entityManager->flush(); |
264
|
|
|
|
265
|
|
|
$response = [ |
266
|
|
|
'view' => $currentView, |
267
|
|
|
'success' => true, |
268
|
|
|
'html' => $this->widgetRenderer->render($widget, $currentView), |
|
|
|
|
269
|
|
|
'widgetId' => $initialWidgetId, |
270
|
|
|
'slot' => $widget->getWidgetMap()->getSlot(), |
271
|
|
|
'viewCssHash' => $currentView->getCssHash(), |
272
|
|
|
]; |
273
|
|
|
} else { |
274
|
|
|
$formErrorHelper = $this->formErrorHelper; |
275
|
|
|
//Return a message for developer in console and form view in order to refresh view and show form errors |
276
|
|
|
$response = [ |
277
|
|
|
'success' => false, |
278
|
|
|
'widgetId' => $initialWidgetId, |
279
|
|
|
'slot' => $widget->getWidgetMap()->getSlot(), |
280
|
|
|
'message' => $noValidate === false ? $formErrorHelper->getRecursiveReadableErrors($form) : null, |
281
|
|
|
'html' => $this->widgetFormBuilder->renderForm($form, $widget, $businessEntityName), |
|
|
|
|
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
} else { |
285
|
|
|
$widgets = $widget->getWidgetMap()->getWidgets(); |
286
|
|
|
$forms = $this->widgetFormBuilder->renderNewQuantumForms($widget->getSlot(), $currentView, $widgets, $widget, $classes); |
287
|
|
|
|
288
|
|
|
$response = [ |
289
|
|
|
'success' => true, |
290
|
|
|
'html' => $this->templating->render( |
291
|
|
|
'VictoireCoreBundle:Widget:Form/edit.html.twig', |
292
|
|
|
[ |
293
|
|
|
'view' => $currentView, |
294
|
|
|
'slot' => $widget->getWidgetMap()->getSlot(), |
295
|
|
|
'position' => $widget->getWidgetMap()->getPosition(), |
296
|
|
|
'parentWidgetMap' => $widget->getWidgetMap()->getParent() ? $widget->getWidgetMap()->getParent()->getId() : null, |
297
|
|
|
'classes' => $classes, |
298
|
|
|
'forms' => $forms, |
299
|
|
|
'widgets' => $widgets, |
300
|
|
|
'widget' => $widget, |
301
|
|
|
] |
302
|
|
|
), |
303
|
|
|
]; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $response; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
|
|
|
|
310
|
|
|
* Remove a widget. |
311
|
|
|
* |
312
|
|
|
* @param Widget $widget |
313
|
|
|
* |
314
|
|
|
* @return array The parameter for the view |
315
|
|
|
*/ |
316
|
|
|
public function deleteWidget(Widget $widget, View $view) |
317
|
|
|
{ |
318
|
|
|
//Used to update view in callback (we do it before delete it else it'll not exists anymore) |
319
|
|
|
$widgetId = $widget->getId(); |
320
|
|
|
//we update the widget map of the view |
321
|
|
|
$this->widgetMapBuilder->build($view); |
322
|
|
|
$widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view); |
|
|
|
|
323
|
|
|
//the widget is removed only if the current view is the view of the widget |
324
|
|
|
if ($widgetMap->getView() == $view && $widgetMap->getAction() != WidgetMap::ACTION_DELETE) { |
|
|
|
|
325
|
|
|
//we remove the widget |
326
|
|
|
$this->entityManager->remove($widget); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
//we update the view |
330
|
|
|
$this->entityManager->persist($view); |
331
|
|
|
//update the view deleting the widget |
332
|
|
|
$this->widgetMapManager->delete($view, $widget); |
|
|
|
|
333
|
|
|
|
334
|
|
|
$this->entityManager->flush(); |
335
|
|
|
|
336
|
|
|
return [ |
337
|
|
|
'success' => true, |
338
|
|
|
'widgetId' => $widgetId, |
339
|
|
|
'viewCssHash' => $view->getCssHash(), |
340
|
|
|
]; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Overwrite the widget for the current view because the widget is not linked to the current view, a copy is created. |
345
|
|
|
* |
346
|
|
|
* @param View $view |
347
|
|
|
* @param Widget $widget |
348
|
|
|
* |
349
|
|
|
* @throws \Exception The slot does not exists |
350
|
|
|
* |
351
|
|
|
* @return Widget The widget |
352
|
|
|
*/ |
353
|
|
|
public function overwriteWidget(View $view, Widget $widget) |
354
|
|
|
{ |
355
|
|
|
$widgetCopy = $this->cloneEntity($widget); |
|
|
|
|
356
|
|
|
$originalWidgetMap = $widget->getWidgetMap(); |
357
|
|
|
$this->widgetMapManager->overwrite($view, $originalWidgetMap, $widgetCopy); |
|
|
|
|
358
|
|
|
|
359
|
|
|
return $widgetCopy; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param Widget $entity |
364
|
|
|
*/ |
|
|
|
|
365
|
|
|
public function cloneEntity(Widget $entity) |
366
|
|
|
{ |
367
|
|
|
$entityCopy = clone $entity; |
368
|
|
|
$entityCopy->setWidgetMap(null); |
369
|
|
|
//Look for on_to_many relations, if found, duplicate related entities. |
370
|
|
|
//It is necessary for 'list' widgets, this algo duplicates and persists list items. |
371
|
|
|
$associations = $this->entityManager->getClassMetadata(get_class($entityCopy))->getAssociationMappings(); |
372
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
373
|
|
|
foreach ($associations as $name => $values) { |
374
|
|
|
if ($values['type'] === ClassMetadataInfo::ONE_TO_MANY) { |
375
|
|
|
$relatedEntities = $accessor->getValue($entityCopy, $values['fieldName']); |
376
|
|
|
$relatedEntitiesCopies = []; |
377
|
|
|
foreach ($relatedEntities as $relatedEntity) { |
378
|
|
|
$relatedEntityCopy = clone $relatedEntity; |
379
|
|
|
$this->entityManager->persist($relatedEntity); |
380
|
|
|
$relatedEntitiesCopies[] = $relatedEntityCopy; |
381
|
|
|
} |
382
|
|
|
$accessor->setValue($entityCopy, $name, $relatedEntitiesCopies); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
//Clone OneToOne relation objects |
386
|
|
|
if ($values['type'] === ClassMetadataInfo::ONE_TO_ONE) { |
387
|
|
|
$relatedEntity = $accessor->getValue($entityCopy, $values['fieldName']); |
388
|
|
|
if ($relatedEntity) { |
389
|
|
|
$relatedEntityCopy = clone $relatedEntity; |
390
|
|
|
$this->entityManager->persist($relatedEntity); |
391
|
|
|
$accessor->setValue($entityCopy, $name, $relatedEntityCopy); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
$this->entityManager->persist($entityCopy); |
|
|
|
|
397
|
|
|
|
398
|
|
|
return $entityCopy; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|