1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetMapBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Orm\EntityManager; |
6
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
7
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
8
|
|
|
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder; |
9
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
10
|
|
|
use Victoire\Bundle\WidgetMapBundle\Helper\WidgetMapHelper; |
11
|
|
|
use Victoire\Bundle\WidgetMapBundle\Resolver\WidgetMapChildrenResolver; |
12
|
|
|
|
13
|
|
|
class WidgetMapManager |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
private $em; |
16
|
|
|
private $builder; |
17
|
|
|
private $resolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* WidgetMapManager constructor. |
21
|
|
|
* |
22
|
|
|
* @param EntityManager $em |
23
|
|
|
* @param WidgetMapBuilder $builder |
24
|
|
|
* @param WidgetMapChildrenResolver $resolver |
25
|
|
|
*/ |
26
|
|
|
public function __construct( |
27
|
|
|
EntityManager $em, |
|
|
|
|
28
|
|
|
WidgetMapBuilder $builder, |
29
|
|
|
WidgetMapChildrenResolver $resolver |
30
|
|
|
) { |
31
|
|
|
$this->em = $em; |
32
|
|
|
$this->builder = $builder; |
33
|
|
|
$this->resolver = $resolver; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* Insert a WidgetMap in a view at given position. |
38
|
|
|
* |
39
|
|
|
* @param string $slotId |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function insert(Widget $widget, View $view, $slotId, $position, $widgetReference) |
42
|
|
|
{ |
43
|
|
|
$quantum = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->findOneBy([ |
44
|
|
|
'view' => $view, |
45
|
|
|
'slot' => $slotId, |
46
|
|
|
'position' => $position, |
47
|
|
|
'parent' => $widgetReference, |
48
|
|
|
'action' => [ |
49
|
|
|
WidgetMap::ACTION_CREATE, |
50
|
|
|
WidgetMap::ACTION_OVERWRITE, |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
|
54
|
|
|
if ($quantum) { |
55
|
|
|
$widget->setWidgetMap($quantum); |
56
|
|
|
$view->addWidgetMap($quantum); |
57
|
|
|
} else { |
58
|
|
|
$parent = null; |
59
|
|
|
if ($widgetReference) { |
60
|
|
|
$parent = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find($widgetReference); |
61
|
|
|
} |
62
|
|
|
//create the new widget map |
63
|
|
|
$widgetMapEntry = new WidgetMap(); |
64
|
|
|
$widgetMapEntry->setAction(WidgetMap::ACTION_CREATE); |
65
|
|
|
$widgetMapEntry->setSlot($slotId); |
66
|
|
|
$widgetMapEntry->setPosition($position); |
67
|
|
|
$widgetMapEntry->setParent($parent); |
|
|
|
|
68
|
|
|
$widget->setWidgetMap($widgetMapEntry); |
69
|
|
|
|
70
|
|
|
$view->addWidgetMap($widgetMapEntry); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* moves a widget in a view. |
76
|
|
|
* |
77
|
|
|
* @param View $view |
78
|
|
|
* @param array $sortedWidget |
79
|
|
|
*/ |
80
|
|
|
public function move(View $view, $sortedWidget) |
81
|
|
|
{ |
82
|
|
|
/** @var WidgetMap $parentWidgetMap */ |
83
|
|
|
$parentWidgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['parentWidgetMap']); |
84
|
|
|
$position = $sortedWidget['position']; |
85
|
|
|
$slot = $sortedWidget['slot']; |
86
|
|
|
/** @var WidgetMap $widgetMap */ |
87
|
|
|
$widgetMap = $this->em->getRepository('VictoireWidgetMapBundle:WidgetMap')->find((int) $sortedWidget['widgetMap']); |
88
|
|
|
|
89
|
|
|
$originalParent = $widgetMap->getParent(); |
90
|
|
|
$originalPosition = $widgetMap->getPosition(); |
91
|
|
|
|
92
|
|
|
$children = $this->resolver->getChildren($widgetMap, $view); |
93
|
|
|
$beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null; |
94
|
|
|
$afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null; |
95
|
|
|
|
96
|
|
|
$parentWidgetMapChildren = $this->getChildrenByView($parentWidgetMap); |
97
|
|
|
|
98
|
|
|
$widgetMap = $this->moveWidgetMap($view, $widgetMap, $parentWidgetMap, $position, $slot); |
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition); |
101
|
|
|
|
102
|
|
|
foreach ($parentWidgetMapChildren['views'] as $_view) { |
103
|
|
|
if ($_view !== $view) { |
104
|
|
View Code Duplication |
if (isset($parentWidgetMapChildren['before'][$_view->getId()]) && $parentWidgetMapChildren['before'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) { |
|
|
|
|
105
|
|
|
$parentWidgetMapChildren['before'][$_view->getId()]->setParent($widgetMap); |
106
|
|
|
} |
107
|
|
View Code Duplication |
if (isset($parentWidgetMapChildren['after'][$_view->getId()]) && $parentWidgetMapChildren['after'][$_view->getId()]->getPosition() == $widgetMap->getPosition()) { |
|
|
|
|
108
|
|
|
$parentWidgetMapChildren['after'][$_view->getId()]->setParent($widgetMap); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Delete the widget from the view. |
116
|
|
|
* |
117
|
|
|
* @param View $view |
118
|
|
|
* @param Widget $widget |
119
|
|
|
* |
120
|
|
|
* @throws \Exception Widget map does not exists |
121
|
|
|
*/ |
122
|
|
|
public function delete(View $view, Widget $widget) |
123
|
|
|
{ |
124
|
|
|
$this->builder->build($view); |
125
|
|
|
|
126
|
|
|
$widgetMap = WidgetMapHelper::getWidgetMapByWidgetAndView($widget, $view); |
127
|
|
|
$slot = $widgetMap->getSlot(); |
|
|
|
|
128
|
|
|
|
129
|
|
|
$originalParent = $widgetMap->getParent(); |
|
|
|
|
130
|
|
|
$originalPosition = $widgetMap->getPosition(); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$children = $this->resolver->getChildren($widgetMap, $view); |
|
|
|
|
133
|
|
|
$beforeChild = !empty($children[WidgetMap::POSITION_BEFORE]) ? $children[WidgetMap::POSITION_BEFORE] : null; |
134
|
|
|
$afterChild = !empty($children[WidgetMap::POSITION_AFTER]) ? $children[WidgetMap::POSITION_AFTER] : null; |
135
|
|
|
|
136
|
|
|
//we remove the widget from the current view |
137
|
|
|
if ($widgetMap->getView() === $view) { |
|
|
|
|
138
|
|
|
// If the widgetMap has substitutes, delete them or transform them in create mode |
139
|
|
|
if (count($widgetMap->getAllSubstitutes()) > 0) { |
140
|
|
|
foreach ($widgetMap->getAllSubstitutes() as $substitute) { |
|
|
|
|
141
|
|
|
if ($substitute->getAction() === WidgetMap::ACTION_OVERWRITE) { |
142
|
|
|
$substitute->setAction(WidgetMap::ACTION_CREATE); |
143
|
|
|
$substitute->setReplaced(null); |
144
|
|
|
} else { |
145
|
|
|
$view->removeWidgetMap($widgetMap); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
//remove the widget map from the slot |
150
|
|
|
$view->removeWidgetMap($widgetMap); |
151
|
|
|
} else { |
152
|
|
|
//the widget is owned by another view (a parent) |
153
|
|
|
//so we add a new widget map that indicates we delete this widget |
154
|
|
|
$replaceWidgetMap = new WidgetMap(); |
155
|
|
|
$replaceWidgetMap->setAction(WidgetMap::ACTION_DELETE); |
156
|
|
|
$replaceWidgetMap->setWidget($widget); |
|
|
|
|
157
|
|
|
$replaceWidgetMap->setSlot($slot); |
158
|
|
|
$replaceWidgetMap->setReplaced($widgetMap); |
|
|
|
|
159
|
|
|
|
160
|
|
|
$view->addWidgetMap($replaceWidgetMap); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
//Move children for current WidgetMap View |
164
|
|
|
$this->moveChildren($view, $beforeChild, $afterChild, $originalParent, $originalPosition); |
165
|
|
|
|
166
|
|
|
//Move children WidgetMap for children from other View |
167
|
|
|
foreach ($widgetMap->getChildren() as $child) { |
|
|
|
|
168
|
|
|
$this->moveWidgetMap($child->getView(), $child, $originalParent, $originalPosition); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* the widget is owned by another view (a parent) |
174
|
|
|
* so we add a new widget map that indicates we delete this widget. |
175
|
|
|
* |
176
|
|
|
* @param View $view |
177
|
|
|
* @param WidgetMap $originalWidgetMap |
178
|
|
|
* @param Widget $widgetCopy |
179
|
|
|
* |
180
|
|
|
* @throws \Exception |
181
|
|
|
*/ |
182
|
|
|
public function overwrite(View $view, WidgetMap $originalWidgetMap, Widget $widgetCopy) |
183
|
|
|
{ |
184
|
|
|
$widgetMap = new WidgetMap(); |
185
|
|
|
$widgetMap->setAction(WidgetMap::ACTION_OVERWRITE); |
186
|
|
|
$widgetMap->setReplaced($originalWidgetMap); |
187
|
|
|
$widgetCopy->setWidgetMap($widgetMap); |
188
|
|
|
$widgetMap->setSlot($originalWidgetMap->getSlot()); |
189
|
|
|
$widgetMap->setPosition($originalWidgetMap->getPosition()); |
190
|
|
|
$widgetMap->setParent($originalWidgetMap->getParent()); |
191
|
|
|
|
192
|
|
|
$view->addWidgetMap($widgetMap); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
|
|
|
|
196
|
|
|
* If the moved widgetMap has someone at both his before and after, arbitrary move UP the before side |
197
|
|
|
* and find the first place after the before widgetMap hierarchy to place the after widgetMap. |
198
|
|
|
* |
199
|
|
|
* @param View $view |
|
|
|
|
200
|
|
|
* @param $beforeChild |
|
|
|
|
201
|
|
|
* @param $afterChild |
|
|
|
|
202
|
|
|
* @param $originalParent |
|
|
|
|
203
|
|
|
* @param $originalPosition |
|
|
|
|
204
|
|
|
*/ |
205
|
|
|
public function moveChildren(View $view, $beforeChild, $afterChild, $originalParent, $originalPosition) |
206
|
|
|
{ |
207
|
|
|
if ($beforeChild && $afterChild) { |
208
|
|
|
$this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition); |
209
|
|
|
|
210
|
|
|
$child = $beforeChild; |
211
|
|
|
while ($child->getChild(WidgetMap::POSITION_AFTER)) { |
212
|
|
|
$child = $child->getChild(WidgetMap::POSITION_AFTER); |
213
|
|
|
} |
214
|
|
|
if ($afterChild->getId() !== $child->getId()) { |
215
|
|
|
$this->moveWidgetMap($view, $afterChild, $child); |
216
|
|
|
} |
217
|
|
|
} elseif ($beforeChild) { |
218
|
|
|
$this->moveWidgetMap($view, $beforeChild, $originalParent, $originalPosition); |
219
|
|
|
} elseif ($afterChild) { |
220
|
|
|
$this->moveWidgetMap($view, $afterChild, $originalParent, $originalPosition); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Create a copy of a WidgetMap in "overwrite" mode and insert it in the given view. |
226
|
|
|
* |
227
|
|
|
* @param WidgetMap $widgetMap |
228
|
|
|
* @param View $view |
229
|
|
|
* |
230
|
|
|
* @throws \Exception |
231
|
|
|
* |
232
|
|
|
* @return WidgetMap |
233
|
|
|
*/ |
234
|
|
|
protected function cloneWidgetMap(WidgetMap $widgetMap, View $view) |
235
|
|
|
{ |
236
|
|
|
$originalWidgetMap = $widgetMap; |
237
|
|
|
$widgetMap = clone $widgetMap; |
238
|
|
|
$widgetMap->setId(null); |
239
|
|
|
$widgetMap->setAction(WidgetMap::ACTION_OVERWRITE); |
240
|
|
|
$widgetMap->setReplaced($originalWidgetMap); |
241
|
|
|
$widgetMap->setView($view); |
242
|
|
|
$view->addWidgetMap($widgetMap); |
243
|
|
|
$this->em->persist($widgetMap); |
244
|
|
|
|
245
|
|
|
return $widgetMap; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Move given WidgetMap as a child of given parent at given position and slot. |
250
|
|
|
* |
251
|
|
|
* @param View $view |
252
|
|
|
* @param WidgetMap $widgetMap |
253
|
|
|
* @param bool $parent |
254
|
|
|
* @param bool $position |
255
|
|
|
* @param bool $slot |
256
|
|
|
* |
257
|
|
|
* @return WidgetMap |
258
|
|
|
*/ |
259
|
|
|
protected function moveWidgetMap(View $view, WidgetMap $widgetMap, $parent = false, $position = false, $slot = false) |
260
|
|
|
{ |
261
|
|
|
if ($widgetMap->getView() !== $view) { |
262
|
|
|
$widgetMap = $this->cloneWidgetMap($widgetMap, $view); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
if ($parent !== false) { |
266
|
|
|
if ($oldParent = $widgetMap->getParent()) { |
267
|
|
|
$oldParent->removeChild($widgetMap); |
268
|
|
|
} |
269
|
|
|
$widgetMap->setParent($parent); |
|
|
|
|
270
|
|
|
if ($parent) { |
271
|
|
|
$parent->addChild($widgetMap); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
if ($position !== false) { |
275
|
|
|
$widgetMap->setPosition($position); |
|
|
|
|
276
|
|
|
} |
277
|
|
|
if ($slot !== false) { |
278
|
|
|
$widgetMap->setSlot($slot); |
|
|
|
|
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return $widgetMap; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Find return all the given WidgetMap children for each view where it's related. |
286
|
|
|
* |
287
|
|
|
* @param WidgetMap $widgetMap |
288
|
|
|
* |
289
|
|
|
* @return mixed |
290
|
|
|
*/ |
291
|
|
|
protected function getChildrenByView(WidgetMap $widgetMap) |
292
|
|
|
{ |
293
|
|
|
$beforeChilds = $widgetMap->getContextualChildren(WidgetMap::POSITION_BEFORE); |
294
|
|
|
$afterChilds = $widgetMap->getContextualChildren(WidgetMap::POSITION_AFTER); |
295
|
|
|
|
296
|
|
|
$childrenByView['views'] = []; |
|
|
|
|
297
|
|
|
$childrenByView['before'] = []; |
298
|
|
|
$childrenByView['after'] = []; |
299
|
|
|
foreach ($beforeChilds as $beforeChild) { |
300
|
|
|
$view = $beforeChild->getView(); |
301
|
|
|
$childrenByView['views'][] = $view; |
302
|
|
|
$childrenByView['before'][$view->getId()] = $beforeChild; |
303
|
|
|
} |
304
|
|
|
foreach ($afterChilds as $afterChild) { |
305
|
|
|
$view = $afterChild->getView(); |
306
|
|
|
$childrenByView['views'][] = $view; |
307
|
|
|
$childrenByView['after'][$view->getId()] = $afterChild; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return $childrenByView; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|