1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetMapBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use Symfony\Bridge\Monolog\Logger; |
6
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
7
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
8
|
|
|
|
9
|
|
|
class WidgetMapChildrenResolver |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private $logger; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* WidgetMapChildrenResolver constructor. |
15
|
|
|
* |
16
|
|
|
* @param Logger $logger |
17
|
|
|
*/ |
18
|
|
|
public function __construct(Logger $logger) |
19
|
|
|
{ |
20
|
|
|
$this->logger = $logger; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* Return "after" and "before" children, |
25
|
|
|
* based on contextual View and its Templates. |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function getChildren(WidgetMap $widgetMap, View $view = null) |
30
|
|
|
{ |
31
|
|
|
$positions = [WidgetMap::POSITION_BEFORE, WidgetMap::POSITION_AFTER]; |
32
|
|
|
$children = []; |
33
|
|
|
foreach ($positions as $position) { |
34
|
|
|
$matchingChildren = []; |
35
|
|
|
|
36
|
|
|
//Position is null by default |
37
|
|
|
$children[$position] = null; |
38
|
|
|
|
39
|
|
|
//Pass through all current WidgetMap children for a given position |
40
|
|
View Code Duplication |
foreach ($widgetMap->getContextualChildren($position) as $_child) { |
|
|
|
|
41
|
|
|
//If child don't have a substitute for this View and Templates, this is the one |
42
|
|
|
if (null === $_child->getSubstituteForView($view)) { |
|
|
|
|
43
|
|
|
$children[$position] = $_child; |
44
|
|
|
$matchingChildren[] = $_child->getId(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//If children has not been found for this position |
49
|
|
|
//and current WidgetMap is a substitute |
50
|
|
|
if (!$children[$position] && $widgetMap->getReplaced()) { |
51
|
|
|
//Pass through all replaced WidgetMap children for a given position |
52
|
|
View Code Duplication |
foreach ($widgetMap->getReplaced()->getContextualChildren($position) as $_child) { |
|
|
|
|
53
|
|
|
//If child don't have a substitute for this View and Templates, this is the one |
54
|
|
|
if (null === $_child->getSubstituteForView($view)) { |
|
|
|
|
55
|
|
|
$children[$position] = $_child; |
56
|
|
|
$matchingChildren[] = $_child->getId(); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$matchingChildren = array_unique($matchingChildren); |
62
|
|
|
if (count($matchingChildren) > 1) { |
63
|
|
|
$this->logger->critical(sprintf( |
64
|
|
|
'Conflict found between WidgetMaps %s for View %s', |
65
|
|
|
implode(', ', $matchingChildren), |
66
|
|
|
$view->getId() |
|
|
|
|
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $children; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
|
|
|
|
75
|
|
|
* @param $position |
|
|
|
|
76
|
|
|
* @param View|null $view |
|
|
|
|
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function hasChildren(WidgetMap $widgetMap, $position, View $view = null) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->getChildren($widgetMap, $view) as $child) { |
83
|
|
|
if ($child && $child->getPosition() === $position) { |
84
|
|
|
return true; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|