1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker; |
6
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Resolver\BusinessEntityResolver; |
7
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage; |
8
|
|
|
use Victoire\Bundle\CoreBundle\Helper\CurrentViewHelper; |
9
|
|
|
use Victoire\Bundle\CriteriaBundle\Chain\DataSourceChain; |
10
|
|
|
use Victoire\Bundle\CriteriaBundle\Entity\Criteria; |
11
|
|
|
use Victoire\Bundle\WidgetBundle\Model\Widget; |
12
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
13
|
|
|
|
14
|
|
|
class WidgetResolver |
15
|
|
|
{ |
16
|
|
|
const OPERAND_EQUAL = 'equal'; |
17
|
|
|
const OPERAND_TRUE = 'true'; |
18
|
|
|
const OPERAND_FALSE = 'false'; |
19
|
|
|
const OPERAND_IN = 'in'; |
20
|
|
|
const IS_GRANTED = 'is_granted'; |
21
|
|
|
const IS_NOT_GRANTED = 'is_not_granted'; |
22
|
|
|
|
23
|
|
|
private $dataSourceChain; |
24
|
|
|
private $authorizationChecker; |
25
|
|
|
private $currentViewHelper; |
26
|
|
|
private $businessEntityResolver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* WidgetResolver constructor. |
30
|
|
|
* |
31
|
|
|
* @param DataSourceChain $dataSourceChain |
32
|
|
|
* @param AuthorizationChecker $authorizationChecker |
33
|
|
|
* @param CurrentViewHelper $currentViewHelper |
34
|
|
|
* @param BusinessEntityResolver $businessEntityResolver |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
DataSourceChain $dataSourceChain, |
38
|
|
|
AuthorizationChecker $authorizationChecker, |
39
|
|
|
CurrentViewHelper $currentViewHelper, |
40
|
|
|
BusinessEntityResolver $businessEntityResolver |
41
|
|
|
) { |
42
|
|
|
$this->dataSourceChain = $dataSourceChain; |
43
|
|
|
$this->authorizationChecker = $authorizationChecker; |
44
|
|
|
$this->currentViewHelper = $currentViewHelper; |
45
|
|
|
$this->businessEntityResolver = $businessEntityResolver; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function resolve(WidgetMap $widgetMap) |
49
|
|
|
{ |
50
|
|
|
//TODO: orderize it |
51
|
|
|
$widgets = $widgetMap->getWidgets(); |
52
|
|
|
// if the widgetmap is linked to no widgets, it seems that it is an overwrite of the position so keep the replaced widgets for display |
53
|
|
|
|
54
|
|
|
if ($widgetMap->getReplaced() && count($widgets) === 0) { |
55
|
|
|
$widgets = $widgetMap->getReplaced()->getWidgets(); |
56
|
|
|
} |
57
|
|
|
/* @var \Victoire\Bundle\WidgetBundle\Entity\Widget $widget */ |
58
|
|
|
foreach ($widgets as $_widget) { |
59
|
|
|
|
60
|
|
|
/** @var Criteria $criteria */ |
61
|
|
|
foreach ($_widget->getCriterias() as $criteria) { |
62
|
|
|
$value = $this->dataSourceChain->getData($criteria->getName()); |
63
|
|
|
if (!$this->assert($value(), $criteria->getOperator(), $criteria->getValue())) { |
64
|
|
|
continue 2; //try with break |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($_widget instanceof Widget && $proxy = $_widget->getEntityProxy()) { |
69
|
|
|
$_widget->setEntity($this->businessEntityResolver->getBusinessEntity($proxy)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $_widget; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function assert($value, $operator, $expected) |
77
|
|
|
{ |
78
|
|
|
$businessEntity = null; |
79
|
|
|
if ($this->currentViewHelper->getCurrentView() instanceof BusinessPage) { |
80
|
|
|
$businessEntity = $this->currentViewHelper->getCurrentView()->getEntity(); |
81
|
|
|
} |
82
|
|
|
$result = false; |
83
|
|
|
switch ($operator) { |
84
|
|
|
case self::OPERAND_EQUAL: |
85
|
|
|
$result = $value === $expected; |
86
|
|
|
break; |
87
|
|
|
case self::OPERAND_TRUE: |
88
|
|
|
$result = $value == true; |
89
|
|
|
break; |
90
|
|
|
case self::OPERAND_FALSE: |
91
|
|
|
$result = $value == false; |
92
|
|
|
break; |
93
|
|
|
case self::OPERAND_IN: |
94
|
|
|
$result = in_array($value, unserialize($expected)); |
95
|
|
|
break; |
96
|
|
|
case self::IS_GRANTED: |
97
|
|
|
case self::IS_NOT_GRANTED: |
98
|
|
|
if (!$this->authorizationChecker->isGranted('ROLE_VICTOIRE')) { |
99
|
|
|
$granted = $this->authorizationChecker->isGranted($expected, $businessEntity); |
100
|
|
|
if ($operator === self::IS_GRANTED) { |
101
|
|
|
$result = $granted; |
102
|
|
|
} else { |
103
|
|
|
$result = (false === $granted); |
104
|
|
|
} |
105
|
|
|
} else { |
106
|
|
|
$result = true; |
107
|
|
|
} |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|