1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\WidgetBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\ORM\Mapping as ORM; |
7
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
8
|
|
|
use Victoire\Bundle\CoreBundle\Entity\EntityProxy; |
9
|
|
|
use Victoire\Bundle\CriteriaBundle\Entity\Criteria; |
10
|
|
|
use Victoire\Bundle\QueryBundle\Entity\QueryTrait; |
11
|
|
|
use Victoire\Bundle\QueryBundle\Entity\VictoireQueryInterface; |
12
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Traits\StyleTrait; |
13
|
|
|
use Victoire\Bundle\WidgetBundle\Model\Widget as BaseWidget; |
14
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Widget. |
18
|
|
|
* |
19
|
|
|
* @ORM\Table("vic_widget") |
20
|
|
|
* @ORM\Entity(repositoryClass="Victoire\Bundle\WidgetBundle\Repository\WidgetRepository") |
21
|
|
|
* @ORM\InheritanceType("JOINED") |
22
|
|
|
* @ORM\DiscriminatorColumn(name="type", type="string") |
23
|
|
|
*/ |
24
|
|
|
class Widget extends BaseWidget implements VictoireQueryInterface |
25
|
|
|
{ |
26
|
|
|
use StyleTrait; |
27
|
|
|
use QueryTrait; |
28
|
|
|
use TimestampableEntity; |
29
|
|
|
|
30
|
|
|
public function __construct() |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->childrenSlot = uniqid('', true); |
33
|
|
|
$this->criterias = new ArrayCollection(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
* |
39
|
|
|
* @ORM\Column(name="id", type="integer") |
40
|
|
|
* @ORM\Id |
41
|
|
|
* @ORM\GeneratedValue(strategy="AUTO") |
42
|
|
|
*/ |
43
|
|
|
protected $id; |
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
* |
48
|
|
|
* @ORM\Column(name="slot", type="string", length=255, nullable=true) |
49
|
|
|
*/ |
50
|
|
|
protected $slot; |
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
* |
55
|
|
|
* @ORM\Column(name="childrenSlot", type="string", length=100, nullable=true) |
56
|
|
|
*/ |
57
|
|
|
protected $childrenSlot; |
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
* |
62
|
|
|
* @ORM\Column(name="theme", type="string", length=255, nullable=true) |
63
|
|
|
*/ |
64
|
|
|
protected $theme; |
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
* |
69
|
|
|
* @ORM\Column(name="asynchronous", type="boolean", nullable=true) |
70
|
|
|
*/ |
71
|
|
|
protected $asynchronous; |
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string |
75
|
|
|
* |
76
|
|
|
* @ORM\Column(name="fields", type="text") |
77
|
|
|
*/ |
78
|
|
|
protected $fields = 'a:0:{}'; |
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
* |
83
|
|
|
* @ORM\Column(name="mode", type="string", length=255, nullable=false) |
84
|
|
|
*/ |
85
|
|
|
protected $mode = self::MODE_STATIC; |
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Auto simple mode: joined entity. |
89
|
|
|
* |
90
|
|
|
* @var EntityProxy |
91
|
|
|
* |
92
|
|
|
* @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\EntityProxy", inversedBy="widgets", cascade={"persist"}) |
93
|
|
|
* @ORM\JoinColumn(name="entityProxy_id", referencedColumnName="id", onDelete="CASCADE") |
94
|
|
|
*/ |
95
|
|
|
protected $entityProxy; |
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The entity linked to the widget. |
99
|
|
|
* |
100
|
|
|
* @var unknown |
101
|
|
|
*/ |
102
|
|
|
protected $entity; |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @deprecated Remove Doctrine mapping and property |
106
|
|
|
* |
107
|
|
|
* @var string |
108
|
|
|
* |
109
|
|
|
* @ORM\ManyToOne(targetEntity="\Victoire\Bundle\CoreBundle\Entity\View", inversedBy="widgets", cascade={"persist"}) |
110
|
|
|
* @ORM\JoinColumn(name="view_id", referencedColumnName="id", onDelete="CASCADE") |
111
|
|
|
*/ |
112
|
|
|
protected $view; |
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var WidgetMap |
116
|
|
|
* |
117
|
|
|
* @ORM\ManyToOne(targetEntity="\Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap", inversedBy="widgets") |
118
|
|
|
* @ORM\JoinColumn(name="widget_map_id", referencedColumnName="id", onDelete="SET NULL")) |
119
|
|
|
*/ |
120
|
|
|
protected $widgetMap; |
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var [Criteria] |
124
|
|
|
* |
125
|
|
|
* @ORM\OneToMany(targetEntity="\Victoire\Bundle\CriteriaBundle\Entity\Criteria", mappedBy="widget", cascade={"persist", "remove"}, orphanRemoval=true) |
126
|
|
|
*/ |
127
|
|
|
protected $criterias; |
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @var string |
131
|
|
|
* |
132
|
|
|
* @ORM\Column(name="quantum", type="string", length=255, nullable=true) |
133
|
|
|
*/ |
134
|
|
|
protected $quantum; |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function isAsynchronous() |
140
|
|
|
{ |
141
|
|
|
return $this->asynchronous; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $asynchronous |
146
|
|
|
*/ |
147
|
|
|
public function setAsynchronous($asynchronous) |
148
|
|
|
{ |
149
|
|
|
$this->asynchronous = $asynchronous; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the entity proxy. |
154
|
|
|
* |
155
|
|
|
* @param EntityProxy $entityProxy |
156
|
|
|
*/ |
157
|
|
|
public function setEntityProxy(EntityProxy $entityProxy) |
158
|
|
|
{ |
159
|
|
|
$this->entityProxy = $entityProxy; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the entity proxy. |
164
|
|
|
* |
165
|
|
|
* @return EntityProxy |
166
|
|
|
*/ |
167
|
|
|
public function getEntityProxy() |
168
|
|
|
{ |
169
|
|
|
return $this->entityProxy; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get businessEntityName. |
174
|
|
|
* |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getBusinessEntityName() |
178
|
|
|
{ |
179
|
|
|
if ($entityProxy = $this->getEntityProxy()) { |
180
|
|
|
return $entityProxy->getBusinessEntity()->getName(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $this->businessEntityName; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* to string. |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function __toString() |
192
|
|
|
{ |
193
|
|
|
return (string) $this->getId(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Get id. |
198
|
|
|
* |
199
|
|
|
* @return int |
200
|
|
|
*/ |
201
|
|
|
public function getId() |
202
|
|
|
{ |
203
|
|
|
return $this->id; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Set fields. |
208
|
|
|
* |
209
|
|
|
* @param string $fields |
210
|
|
|
* |
211
|
|
|
* @return Widget |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function setFields($fields) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$data = @unserialize($fields); |
216
|
|
|
if ($fields === 'b:0;' || $data !== false) { |
217
|
|
|
$this->fields = $fields; |
218
|
|
|
} else { |
219
|
|
|
$this->fields = serialize($fields); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get fields. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getFields() |
231
|
|
|
{ |
232
|
|
|
return unserialize($this->fields); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set The Id. |
237
|
|
|
* |
238
|
|
|
* @param int $id The id |
239
|
|
|
*/ |
240
|
|
|
public function setId($id) |
241
|
|
|
{ |
242
|
|
|
$this->id = $id; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Set slot. |
247
|
|
|
* |
248
|
|
|
* @param string $slot |
249
|
|
|
* |
250
|
|
|
* @return Widget |
251
|
|
|
*/ |
252
|
|
|
public function setSlot($slot) |
253
|
|
|
{ |
254
|
|
|
$this->slot = $slot; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Get slot. |
261
|
|
|
* |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function getSlot() |
265
|
|
|
{ |
266
|
|
|
return $this->slot; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Set theme. |
271
|
|
|
* |
272
|
|
|
* @param string $theme |
273
|
|
|
* |
274
|
|
|
* @return Widget |
275
|
|
|
*/ |
276
|
|
|
public function setTheme($theme) |
277
|
|
|
{ |
278
|
|
|
$this->theme = $theme; |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Get theme. |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public function getTheme() |
289
|
|
|
{ |
290
|
|
|
return $this->theme; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Get the type of the object. |
295
|
|
|
* |
296
|
|
|
* @return string The type |
297
|
|
|
*/ |
298
|
|
|
public function getType() |
299
|
|
|
{ |
300
|
|
|
return $this->guessType(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Guess the type of this by exploding and getting the last item. |
305
|
|
|
* |
306
|
|
|
* @return string The guessed type |
307
|
|
|
*/ |
308
|
|
|
protected function guessType() |
309
|
|
|
{ |
310
|
|
|
$type = explode('\\', get_class($this)); |
311
|
|
|
|
312
|
|
|
return strtolower(preg_replace('/Widget/', '', end($type))); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Set the mode. |
317
|
|
|
* |
318
|
|
|
* @param string $mode |
319
|
|
|
*/ |
320
|
|
|
public function setMode($mode) |
|
|
|
|
321
|
|
|
{ |
322
|
|
|
$this->mode = $mode; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Get the mode. |
327
|
|
|
* |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
|
|
public function getMode() |
331
|
|
|
{ |
332
|
|
|
return $this->mode; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Get the view id. |
337
|
|
|
* |
338
|
|
|
* @return int The view id |
339
|
|
|
*/ |
340
|
|
|
public function getViewId() |
341
|
|
|
{ |
342
|
|
|
$viewId = null; |
343
|
|
|
|
344
|
|
|
$view = $this->getView(); |
|
|
|
|
345
|
|
|
|
346
|
|
|
if ($view !== null) { |
347
|
|
|
$viewId = $view->getId(); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
return $viewId; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
|
public function getChildrenSlot() |
357
|
|
|
{ |
358
|
|
|
return $this->childrenSlot ?: $this->getId(); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param string $childrenSlot |
363
|
|
|
*/ |
364
|
|
|
public function setChildrenSlot($childrenSlot) |
365
|
|
|
{ |
366
|
|
|
$this->childrenSlot = $childrenSlot; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Set widgets. |
371
|
|
|
* |
372
|
|
|
* @param [WidgetMap] $widgetMap |
|
|
|
|
373
|
|
|
* |
374
|
|
|
* @return Widget |
375
|
|
|
*/ |
376
|
|
|
public function setWidgetMap(WidgetMap $widgetMap = null) |
377
|
|
|
{ |
378
|
|
|
if ($widgetMap) { |
379
|
|
|
$widgetMap->addWidget($this); |
380
|
|
|
} |
381
|
|
|
$this->widgetMap = $widgetMap; |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Get widgets. |
388
|
|
|
* |
389
|
|
|
* @return [WidgetMap] |
|
|
|
|
390
|
|
|
*/ |
391
|
|
|
public function getWidgetMap() |
392
|
|
|
{ |
393
|
|
|
return $this->widgetMap; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Set the entity. |
398
|
|
|
* |
399
|
|
|
* @param object $entity |
400
|
|
|
*/ |
401
|
|
|
public function setEntity($entity) |
402
|
|
|
{ |
403
|
|
|
$this->entity = $entity; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get the entity. |
408
|
|
|
* |
409
|
|
|
* @return number |
410
|
|
|
*/ |
411
|
|
|
public function getEntity() |
412
|
|
|
{ |
413
|
|
|
//if there is no entity |
414
|
|
|
if ($this->entity === null) { |
415
|
|
|
//we try to get one from the proxy |
416
|
|
|
$entityProxy = $this->getEntityProxy(); |
417
|
|
|
|
418
|
|
|
//if there is a proxy |
419
|
|
|
if ($entityProxy !== null) { |
420
|
|
|
$this->entity = $entityProxy->getEntity(); |
421
|
|
|
} |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
return $this->entity; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Clone a widget. |
429
|
|
|
*/ |
430
|
|
|
public function __clone() |
431
|
|
|
{ |
432
|
|
|
// if there is a proxy |
433
|
|
|
if ($this->entityProxy) { |
434
|
|
|
// we clone this one |
435
|
|
|
$this->entityProxy = clone $this->entityProxy; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
// This check should be in the __constructor, but Doctrine does not use __constructor to |
439
|
|
|
// instanciate entites but __clone method. |
440
|
|
|
if (property_exists(get_called_class(), 'widget')) { |
441
|
|
|
throw new \Exception(sprintf('A property $widget was found in %s object. |
442
|
|
|
The $widget property is reserved for Victoire. |
443
|
|
|
You should chose a different property name.', get_called_class())); |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @deprecated |
449
|
|
|
* Get view |
450
|
|
|
* |
451
|
|
|
* @return string |
452
|
|
|
*/ |
453
|
|
|
public function getView() |
454
|
|
|
{ |
455
|
|
|
return $this->view; |
|
|
|
|
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
/** |
459
|
|
|
* @return [Criteria] |
|
|
|
|
460
|
|
|
*/ |
461
|
|
|
public function getCriterias() |
462
|
|
|
{ |
463
|
|
|
return $this->criterias; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* @param [Criteria] $criterias |
|
|
|
|
468
|
|
|
*/ |
469
|
|
|
public function setCriterias($criterias) |
470
|
|
|
{ |
471
|
|
|
$this->criterias = $criterias; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @param Criteria $criteria |
476
|
|
|
*/ |
477
|
|
|
public function addCriteria($criteria) |
478
|
|
|
{ |
479
|
|
|
$criteria->setWidget($this); |
480
|
|
|
$this->criterias[] = $criteria; |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @param Criteria $criteria |
485
|
|
|
*/ |
486
|
|
|
public function removeCriteria(Criteria $criteria) |
487
|
|
|
{ |
488
|
|
|
$criteria->setWidget(null); |
|
|
|
|
489
|
|
|
$this->criterias->removeElement($criteria); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @param Criteria $criteria |
494
|
|
|
* |
495
|
|
|
* @return bool |
496
|
|
|
*/ |
497
|
|
|
public function hasCriteria(Criteria $criteria) |
498
|
|
|
{ |
499
|
|
|
return $this->criterias->contains($criteria); |
500
|
|
|
} |
501
|
|
|
|
502
|
|
|
/** |
503
|
|
|
* @param string $criteriaAlias |
504
|
|
|
* |
505
|
|
|
* @return bool |
506
|
|
|
*/ |
507
|
|
|
public function hasCriteriaNamed($criteriaAlias) |
508
|
|
|
{ |
509
|
|
|
return $this->criterias->exists(function ($key, $element) use ($criteriaAlias) { |
510
|
|
|
return $criteriaAlias === $element->getName(); |
511
|
|
|
}); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* @return string |
516
|
|
|
*/ |
517
|
|
|
public function getQuantum() |
518
|
|
|
{ |
519
|
|
|
return $this->quantum; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @param string $quantum |
524
|
|
|
*/ |
525
|
|
|
public function setQuantum($quantum) |
526
|
|
|
{ |
527
|
|
|
$this->quantum = $quantum; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
/** |
531
|
|
|
* Generate the CacheId, insert params that can invalid the cache. |
532
|
|
|
* |
533
|
|
|
* @return string |
534
|
|
|
*/ |
535
|
|
|
public function generateCacheId() |
536
|
|
|
{ |
537
|
|
|
if (!$this->getCurrentView()) { |
538
|
|
|
throw new \Exception(sprintf('Cannot generate an hash for widget %s if currentView is not defined.', |
539
|
|
|
$this->getId() |
540
|
|
|
)); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
return sprintf('%s-%s-%s', |
544
|
|
|
$this->getId(), |
545
|
|
|
$this->getUpdatedAt()->getTimestamp(), |
546
|
|
|
$this->getCurrentView()->getReference()->getId() |
547
|
|
|
); |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* This method parse the widget criterias to discover in which locale this widget is used. |
552
|
|
|
* |
553
|
|
|
* @param $defaultLocale |
|
|
|
|
554
|
|
|
* |
555
|
|
|
* @return string|null |
556
|
|
|
*/ |
557
|
|
|
public function guessLocale() |
558
|
|
|
{ |
559
|
|
|
if ($this->hasCriteriaNamed('locale')) { |
560
|
|
|
foreach ($this->getCriterias() as $criteria) { |
561
|
|
|
if ($criteria->getName() === 'locale') { |
562
|
|
|
return $criteria->getValue(); |
563
|
|
|
} |
564
|
|
|
} |
565
|
|
|
} |
566
|
|
|
} |
567
|
|
|
} |
568
|
|
|
|