1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BusinessPageBundle\Helper; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\View; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\EntityRepository; |
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
9
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Entity\APIBusinessEntity; |
10
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Resolver\APIBusinessEntityResolver; |
11
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Converter\ParameterConverter; |
12
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntity; |
13
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntityInterface; |
14
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessEntityRepository; |
15
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty; |
16
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Helper\BusinessEntityHelper; |
17
|
|
|
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate; |
18
|
|
|
use Victoire\Bundle\CoreBundle\Helper\UrlBuilder; |
19
|
|
|
use Victoire\Bundle\ORMBusinessEntityBundle\Entity\ORMBusinessEntity; |
20
|
|
|
use Victoire\Bundle\QueryBundle\Helper\QueryHelper; |
21
|
|
|
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository; |
22
|
|
|
use Victoire\Bundle\ViewReferenceBundle\ViewReference\BusinessPageReference; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The business entity page pattern helper |
26
|
|
|
* ref: victoire_business_page.business_page_helper. |
27
|
|
|
*/ |
28
|
|
|
class BusinessPageHelper |
29
|
|
|
{ |
30
|
|
|
protected $queryHelper = null; |
31
|
|
|
protected $viewReferenceRepository = null; |
32
|
|
|
protected $businessEntityHelper = null; |
33
|
|
|
protected $parameterConverter = null; |
34
|
|
|
protected $urlBuilder = null; |
35
|
|
|
/** |
36
|
|
|
* @var APIBusinessEntityResolver |
37
|
|
|
*/ |
38
|
|
|
private $apiBusinessEntityResolver; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param QueryHelper $queryHelper |
42
|
|
|
* @param ViewReferenceRepository $viewReferenceRepository |
43
|
|
|
* @param EntityRepository $businessEntityRepository |
44
|
|
|
* @param BusinessEntityHelper $businessEntityHelper |
45
|
|
|
* @param ParameterConverter $parameterConverter |
46
|
|
|
* @param UrlBuilder $urlBuilder |
47
|
|
|
* @param APIBusinessEntityResolver $apiBusinessEntityResolver |
48
|
|
|
*/ |
49
|
|
|
public function __construct(QueryHelper $queryHelper, ViewReferenceRepository $viewReferenceRepository, EntityRepository $businessEntityRepository, BusinessEntityHelper $businessEntityHelper, ParameterConverter $parameterConverter, UrlBuilder $urlBuilder, APIBusinessEntityResolver $apiBusinessEntityResolver) |
50
|
|
|
{ |
51
|
|
|
$this->queryHelper = $queryHelper; |
52
|
|
|
$this->viewReferenceRepository = $viewReferenceRepository; |
53
|
|
|
$this->businessEntityRepository = $businessEntityRepository; |
|
|
|
|
54
|
|
|
$this->businessEntityHelper = $businessEntityHelper; |
55
|
|
|
$this->parameterConverter = $parameterConverter; |
56
|
|
|
$this->urlBuilder = $urlBuilder; |
57
|
|
|
$this->apiBusinessEntityResolver = $apiBusinessEntityResolver; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Is the entity allowed for the business entity page. |
62
|
|
|
* |
63
|
|
|
* @param BusinessTemplate $businessTemplate |
64
|
|
|
* @param object|null $entity |
65
|
|
|
* @param EntityManager $em |
66
|
|
|
* |
67
|
|
|
* @throws \Exception |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function isEntityAllowed(BusinessTemplate $businessTemplate, $entity, EntityManager $em = null) |
72
|
|
|
{ |
73
|
|
|
if ($businessTemplate->getBusinessEntity()->getType() === APIBusinessEntity::TYPE) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
$allowed = true; |
77
|
|
|
|
78
|
|
|
//test that an entity is given |
79
|
|
|
if ($entity === null) { |
80
|
|
|
throw new \Exception('The entity is required.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$queryHelper = $this->queryHelper; |
84
|
|
|
|
85
|
|
|
//the page id |
86
|
|
|
$entityId = $entity->getId(); |
87
|
|
|
|
88
|
|
|
//the base of the query |
89
|
|
|
$baseQuery = $queryHelper->getQueryBuilder($businessTemplate, $em); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$baseQuery->andWhere('main_item.id = '.$entityId); |
92
|
|
|
|
93
|
|
|
//filter with the query of the page |
94
|
|
|
$items = $queryHelper->buildWithSubQuery($businessTemplate, $baseQuery, $em) |
|
|
|
|
95
|
|
|
->getQuery()->getResult(); |
96
|
|
|
|
97
|
|
|
//only one page can be found because we filter on the |
98
|
|
|
if (count($items) > 1) { |
99
|
|
|
throw new \Exception('More than 1 item was found, there should not be more than 1 item with this query.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (count($items) === 0) { |
103
|
|
|
$allowed = false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $allowed; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the list of entities allowed for the BusinessTemplate page. |
111
|
|
|
* |
112
|
|
|
* @param BusinessTemplate $businessTemplate |
113
|
|
|
* @param EntityManager $em |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getEntitiesAllowed(BusinessTemplate $businessTemplate, EntityManager $em) |
118
|
|
|
{ |
119
|
|
|
$businessEntity = $businessTemplate->getBusinessEntity(); |
120
|
|
|
if ($businessEntity->getType() === ORMBusinessEntity::TYPE) { |
121
|
|
|
return $this->getEntitiesAllowedQueryBuilder($businessTemplate, $em) |
122
|
|
|
->getQuery() |
123
|
|
|
->getResult(); |
124
|
|
|
} |
125
|
|
|
if ($businessEntity->getType() === APIBusinessEntity::TYPE) { |
126
|
|
|
return $this->apiBusinessEntityResolver->getBusinessEntities($businessEntity); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the list of entities allowed for the BusinessTemplate page. |
132
|
|
|
* |
133
|
|
|
* @param BusinessTemplate $businessTemplate |
134
|
|
|
* @param EntityManager $em |
135
|
|
|
* |
136
|
|
|
* @throws \Exception |
137
|
|
|
* |
138
|
|
|
* @return QueryBuilder |
139
|
|
|
*/ |
140
|
|
|
public function getEntitiesAllowedQueryBuilder(BusinessTemplate $businessTemplate, EntityManager $em) |
141
|
|
|
{ |
142
|
|
|
//the base of the query |
143
|
|
|
$baseQuery = $this->queryHelper->getQueryBuilder($businessTemplate, $em); |
144
|
|
|
|
145
|
|
|
// add this fake condition to ensure that there is always a "where" clause. |
146
|
|
|
// In query mode, usage of "AND" will be always valid instead of "WHERE" |
147
|
|
|
$baseQuery->andWhere('1 = 1'); |
148
|
|
|
|
149
|
|
|
//filter with the query of the page |
150
|
|
|
return $this->queryHelper->buildWithSubQuery($businessTemplate, $baseQuery, $em); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the list of business properties usable for the url. |
155
|
|
|
* |
156
|
|
|
* @param BusinessEntity $businessEntity |
157
|
|
|
* |
158
|
|
|
* @return BusinessProperty[] The list of business properties |
159
|
|
|
*/ |
160
|
|
View Code Duplication |
public function getBusinessProperties(BusinessEntity $businessEntity) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
//the business properties usable in a url |
163
|
|
|
$businessProperties = $businessEntity->getBusinessPropertiesByType('businessParameter'); |
164
|
|
|
|
165
|
|
|
//the business properties usable in a url |
166
|
|
|
$seoBusinessProps = $businessEntity->getBusinessPropertiesByType('seoable'); |
167
|
|
|
|
168
|
|
|
//the business properties are the identifier and the seoables properties |
169
|
|
|
$businessProperties = array_merge($businessProperties->toArray(), $seoBusinessProps->toArray()); |
170
|
|
|
|
171
|
|
|
return $businessProperties; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the position of the identifier in the url of a business entity page pattern. |
176
|
|
|
* |
177
|
|
|
* @param BusinessTemplate $businessTemplate |
178
|
|
|
* |
179
|
|
|
* @return array The position |
180
|
|
|
*/ |
181
|
|
|
public function getIdentifierPositionInUrl(BusinessTemplate $businessTemplate) |
182
|
|
|
{ |
183
|
|
|
$position = null; |
184
|
|
|
|
185
|
|
|
$url = $businessTemplate->getUrl(); |
|
|
|
|
186
|
|
|
|
187
|
|
|
// split on the / character |
188
|
|
|
$keywords = preg_split("/\//", $url); |
189
|
|
|
// preg_match_all('/\{\%\s*([^\%\}]*)\s*\%\}|\{\{\s*([^\}\}]*)\s*\}\}/i', $url, $matches); |
|
|
|
|
190
|
|
|
|
191
|
|
|
//the business property link to the page |
192
|
|
|
$businessEntityId = $businessTemplate->getBusinessEntityName(); |
193
|
|
|
|
194
|
|
|
$businessEntity = $this->businessEntityRepository->findBy(['name' => $businessEntityId]); |
195
|
|
|
|
196
|
|
|
//the business properties usable in a url |
197
|
|
|
$businessProperties = $businessEntity->getBusinessPropertiesByType('businessParameter'); |
198
|
|
|
|
199
|
|
|
//we parse the words of the url |
200
|
|
|
foreach ($keywords as $index => $keyword) { |
201
|
|
|
foreach ($businessProperties as $businessProperty) { |
202
|
|
|
$entityProperty = $businessProperty->getEntityProperty(); |
203
|
|
|
$searchWord = '{{item.'.$entityProperty.'}}'; |
204
|
|
|
|
205
|
|
|
if ($searchWord === $keyword) { |
206
|
|
|
//the array start at index 0 but we want the position to start at 1 |
207
|
|
|
$position = [ |
208
|
|
|
'position' => $index + 1, |
209
|
|
|
'businessProperty' => $businessProperty, |
210
|
|
|
]; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $position; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Guess the best pattern to represent given reflectionClass. |
220
|
|
|
* |
221
|
|
|
* @param \ReflectionClass $refClass |
|
|
|
|
222
|
|
|
* @param int $entityId |
|
|
|
|
223
|
|
|
* @param EntityManager $em |
224
|
|
|
* @param string $originalRefClassName When digging into parentClass, we do not have to forget originalClass to be able to get reference after all |
225
|
|
|
* |
226
|
|
|
* @throws \Exception |
227
|
|
|
* |
228
|
|
|
* @return View |
229
|
|
|
*/ |
230
|
|
|
public function guessBestPatternIdForEntity($entity, $em, $originalRefClassName = null) |
231
|
|
|
{ |
232
|
|
|
$entityId = $entity->getId(); |
233
|
|
|
$templateId = null; |
234
|
|
|
$viewReference = null; |
235
|
|
|
$businessEntity = null; |
236
|
|
|
$refClass = null; |
237
|
|
|
if (is_array($entity) && array_key_exists('_businessEntity', $entity)) { |
238
|
|
|
$businessEntity = $entity['_businessEntity']; |
239
|
|
|
} elseif ($entity instanceof BusinessEntityInterface) { |
240
|
|
|
$refClass = new \ReflectionClass($entity); |
241
|
|
|
$refClassName = $em->getClassMetadata($refClass->name)->name; |
242
|
|
|
|
243
|
|
|
if (!$originalRefClassName) { |
|
|
|
|
244
|
|
|
$originalRefClassName = $refClassName; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$businessEntity = $this->businessEntityHelper->findByEntityClassname($refClassName); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if ($businessEntity) { |
251
|
|
|
$parameters = [ |
252
|
|
|
'entityId' => $entityId, |
253
|
|
|
'businessEntity' => $businessEntity->getId(), |
254
|
|
|
]; |
255
|
|
|
$viewReference = $this->viewReferenceRepository->getOneReferenceByParameters($parameters); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if (!$viewReference) { |
259
|
|
|
if ($refClass && $parentRefClass = $refClass->getParentClass()) { |
260
|
|
|
$templateId = $this->guessBestPatternIdForEntity($parentRefClass, $em, $originalRefClassName); |
261
|
|
|
} else { |
262
|
|
|
throw new \Exception(sprintf('Cannot find a BusinessTemplate that can display the requested BusinessEntity ("%s", "%s".)', $refClassName, $entityId)); |
263
|
|
|
} |
264
|
|
|
} elseif ($viewReference instanceof BusinessPageReference) { |
265
|
|
|
$templateId = $viewReference->getTemplateId(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $templateId; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: