1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\PHPCR; |
13
|
|
|
|
14
|
|
|
use Sonata\MediaBundle\Model\Gallery; |
15
|
|
|
use Sonata\MediaBundle\Model\GalleryItemInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Bundle\MediaBundle\Document\BaseGallery. |
19
|
|
|
*/ |
20
|
|
|
abstract class BaseGallery extends Gallery |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $uuid; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->galleryItems = new \Doctrine\Common\Collections\ArrayCollection(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Get universal unique id. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
|
|
public function getUuid() |
41
|
|
|
{ |
42
|
|
|
return $this->uuid; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function addGalleryItem(GalleryItemInterface $galleryItem) |
49
|
|
|
{ |
50
|
|
|
$galleryItem->setGallery($this); |
51
|
|
|
|
52
|
|
|
// set nodename of GalleryItem |
53
|
|
|
if (!$galleryItem->getNodename()) { |
|
|
|
|
54
|
|
|
$galleryItem->setNodename( |
|
|
|
|
55
|
|
|
'media'.($this->galleryItems->count() + 1) |
|
|
|
|
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->galleryItems->set($galleryItem->getNodename(), $galleryItem); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Pre persist method. |
64
|
|
|
*/ |
65
|
|
|
public function prePersist() |
66
|
|
|
{ |
67
|
|
|
$this->createdAt = new \DateTime(); |
68
|
|
|
$this->updatedAt = new \DateTime(); |
69
|
|
|
|
70
|
|
|
$this->reorderGalleryItems(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Pre Update method. |
75
|
|
|
*/ |
76
|
|
|
public function preUpdate() |
77
|
|
|
{ |
78
|
|
|
$this->updatedAt = new \DateTime(); |
79
|
|
|
|
80
|
|
|
$this->reorderGalleryItems(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Reorders gallery items based on their position. |
85
|
|
|
*/ |
86
|
|
|
public function reorderGalleryItems() |
87
|
|
|
{ |
88
|
|
|
if ($this->getGalleryItems() && $this->getGalleryItems() instanceof \IteratorAggregate) { |
89
|
|
|
|
90
|
|
|
// reorder |
91
|
|
|
$iterator = $this->getGalleryItems()->getIterator(); |
92
|
|
|
|
93
|
|
|
$iterator->uasort(function ($a, $b) { |
94
|
|
|
if ($a->getPosition() === $b->getPosition()) { |
95
|
|
|
return 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $a->getPosition() > $b->getPosition() ? 1 : -1; |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
$this->setGalleryItems($iterator); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..