1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Resolver; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
7
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
8
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity; |
9
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Resolver\APIBusinessEntityResolver; |
10
|
|
|
use Victoire\Bundle\QueryBundle\Helper\QueryHelper; |
11
|
|
|
use Victoire\Bundle\WidgetBundle\Model\Widget; |
12
|
|
|
|
13
|
|
|
class BaseWidgetContentResolver |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var QueryHelper |
17
|
|
|
*/ |
18
|
|
|
protected $queryHelper; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
protected $entityManager; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var APIBusinessEntityResolver |
27
|
|
|
*/ |
28
|
|
|
protected $apiResolver; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the static content of the widget. |
32
|
|
|
* |
33
|
|
|
* @param Widget $widget |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getWidgetStaticContent(Widget $widget) |
38
|
|
|
{ |
39
|
|
|
$reflect = new \ReflectionClass($widget); |
40
|
|
|
$widgetProperties = $reflect->getProperties(); |
41
|
|
|
$parameters = ['widget' => $widget]; |
42
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
43
|
|
|
|
44
|
|
|
foreach ($widgetProperties as $property) { |
45
|
|
|
if (!$property->isStatic()) { |
46
|
|
|
$value = $accessor->getValue($widget, $property->getName()); |
47
|
|
|
$parameters[$property->getName()] = $value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $parameters; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the business entity content. |
56
|
|
|
* |
57
|
|
|
* @param Widget $widget |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function getWidgetBusinessEntityContent(Widget $widget) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$entity = $widget->getEntity(); |
64
|
|
|
$parameters = $this->getWidgetStaticContent($widget); |
65
|
|
|
|
66
|
|
|
$this->populateParametersWithWidgetFields($widget, $entity, $parameters); |
67
|
|
|
|
68
|
|
|
return $parameters; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the content of the widget by the entity linked to it. |
73
|
|
|
* |
74
|
|
|
* @param Widget $widget |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function getWidgetEntityContent(Widget $widget) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$entity = $widget->getEntity(); |
81
|
|
|
|
82
|
|
|
$parameters = $this->getWidgetStaticContent($widget); |
83
|
|
|
|
84
|
|
|
$this->populateParametersWithWidgetFields($widget, $entity, $parameters); |
85
|
|
|
|
86
|
|
|
return $parameters; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the content of the widget for the query mode. |
91
|
|
|
* |
92
|
|
|
* @param Widget $widget |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getWidgetQueryContent(Widget $widget) |
97
|
|
|
{ |
98
|
|
|
$parameters = $this->getWidgetStaticContent($widget); |
99
|
|
|
|
100
|
|
|
if (APIBusinessEntity::TYPE === $widget->getBusinessEntity()->getType()) { |
101
|
|
|
$entity = $this->apiResolver->getBusinessEntities($widget->getBusinessEntity()); |
102
|
|
|
} else { |
103
|
|
|
$entity = $this->getWidgetQueryBuilder($widget) |
104
|
|
|
->setMaxResults(1) |
105
|
|
|
->getQuery() |
106
|
|
|
->getOneOrNullResult(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->populateParametersWithWidgetFields($widget, $entity, $parameters); |
110
|
|
|
|
111
|
|
|
return $parameters; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the widget query result. |
116
|
|
|
* |
117
|
|
|
* @param Widget $widget The widget |
118
|
|
|
* |
119
|
|
|
* @return \Doctrine\ORM\QueryBuilder The list of entities |
120
|
|
|
*/ |
121
|
|
|
public function getWidgetQueryBuilder(Widget $widget) |
122
|
|
|
{ |
123
|
|
|
//get the base query |
124
|
|
|
$itemsQueryBuilder = $this->queryHelper->getQueryBuilder($widget, $this->entityManager); |
|
|
|
|
125
|
|
|
|
126
|
|
|
// Filter only visibleOnFront |
127
|
|
|
$itemsQueryBuilder->andWhere('main_item.visibleOnFront = true'); |
128
|
|
|
|
129
|
|
|
//add the query of the widget |
130
|
|
|
return $this->queryHelper->buildWithSubQuery($widget, $itemsQueryBuilder, $this->entityManager); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function populateParametersWithWidgetFields(Widget $widget, $entity, &$parameters) |
134
|
|
|
{ |
135
|
|
|
$fields = $widget->getFields(); |
136
|
|
|
//parse the field |
137
|
|
|
foreach ($fields as $widgetField => $field) { |
138
|
|
|
//get the value of the field |
139
|
|
|
if ($entity !== null) { |
140
|
|
|
$accessor = new PropertyAccessor(); |
141
|
|
|
$attributeValue = $accessor->getValue($entity, $field); |
142
|
|
|
} else { |
143
|
|
|
$attributeValue = $widget->getBusinessEntityName().' -> '.$field; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$parameters[$widgetField] = $attributeValue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$widget->setEntity($entity); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param QueryHelper $queryHelper |
154
|
|
|
*/ |
155
|
|
|
public function setQueryHelper(QueryHelper $queryHelper) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$this->queryHelper = $queryHelper; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param EntityManager $entityManager |
162
|
|
|
*/ |
163
|
|
|
public function setEntityManager(EntityManager $entityManager) |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$this->entityManager = $entityManager; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param APIBusinessEntityResolver $resolver |
170
|
|
|
*/ |
171
|
|
|
public function setApiBusinessEntityResolver(APIBusinessEntityResolver $resolver) |
172
|
|
|
{ |
173
|
|
|
$this->apiResolver = $resolver; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|