-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGallery.php
250 lines (217 loc) · 6.42 KB
/
Gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
class RM_Gallery
extends
RM_Entity
implements
RM_Interface_Hideable,
RM_Interface_Deletable {
const CACHE_NAME = 'galleries';
const TABLE_NAME = 'galleries';
protected static $_properties = array(
'idGallery' => array(
'id' => true,
'type' => 'int'
),
'galleryStatus' => array(
'default' => self::STATUS_SHOW,
'type' => 'int'
)
);
private $_isPhotosLoaded = false;
/**
* @var RM_Gallery_Photo[]
*/
private $_photos = array();
/**
* @var RM_Gallery_Photo
*/
private $_poster;
/**
* @var RM_Entity_Worker_Data
*/
protected $_rmGalleryDataWorker;
/**
* @return RM_Gallery
*/
public static function create() {
$gallery = new static( new stdClass() );
return $gallery;
}
public function __construct($data) {
$this->_rmGalleryDataWorker = new RM_Entity_Worker_Data(get_class(), $data);
$this->_cacheWorker = new RM_Entity_Worker_Cache(get_class());
}
public function destroy() {
if ($this->_poster) $this->_poster->destroy();
foreach ($this->_photos as &$photo) $photo->destroy();
$this->_poster = null;
$this->_isPhotosLoaded = false;
$this->_photos = [];
parent::destroy();
}
public function getId() {
return $this->getIdGallery();
}
public function getIdGallery() {
return $this->_rmGalleryDataWorker->getValue('idGallery');
}
public function setStatus($statusGallery) {
$statusGallery = (int)$statusGallery;
$this->_rmGalleryDataWorker->setValue('galleryStatus', $statusGallery);
}
public function getStatus() {
return $this->_rmGalleryDataWorker->getValue('galleryStatus');
}
public function getMaxPosition() {
$max = 0;
foreach ($this->getPhotos() as $photo) {
if ($max < $photo->getPosition()) {
$max = $photo->getPosition();
}
}
return $max;
}
public function addPhoto(RM_Photo $photo) {
if ( !$this->_isPhotoAdded($photo) ) {
$galleryPhotoClassName = $this->__getGalleryPhotoClassName();
$this->_photos[] = $galleryPhotoClassName::createGalleryPhoto(
$this->getId(),
($this->getMaxPosition() + 1),
$photo
);
$this->__refreshCache();
}
$photo->destroy();
}
/**
* @param int $id
* @throws Exception
* @return RM_Gallery_Photo
*/
public function getPhotoById($id) {
$id = (int)$id;
foreach ($this->getPhotos() as $photo) {
if ($photo->getIdPhoto() === $id) {
return $photo;
}
}
throw new Exception('Photo not found');
}
public function savePhotos() {
foreach ($this->getPhotos() as $photo) {
$photo->save();
}
}
/**
* TODO optimization
* @return int
*/
public function getPhotosCount() {
return sizeof( $this->getPhotos() );
}
/**
* @return RM_Gallery_Photo[]
*/
public function getPhotos() {
if (!$this->_isPhotosLoaded) {
$galleryPhotoClassName = $this->__getGalleryPhotoClassName();
$this->_photos = $galleryPhotoClassName::getGalleryPhotos(
$this->getId(),
new RM_Query_Limits(0)
);
$this->_isPhotosLoaded = true;
}
return $this->_photos;
}
/**
* @return RM_Gallery_Photo|null
*/
public function getPosterPhoto() {
if (!($this->_poster instanceof RM_Photo)) {
$galleryPhotoClassName = $this->__getGalleryPhotoClassName();
$photos = $galleryPhotoClassName::getGalleryPhotos(
$this->getId(),
new RM_Query_Limits(1)
);
if (sizeof($photos) > 0) {
$this->_poster = $photos[0];
}
}
return $this->_poster;
}
public function _reload() {
$this->_isPhotosLoaded = false;
$this->_poster = null;
$this->getPhotos();
}
public function save() {
if ($this->_rmGalleryDataWorker->save() && static::AUTO_CACHE) {
$this->__refreshCache();
}
$this->_savePhotos();
return $this;
}
public function updatePositions() {
$photos = array();
$i = 0;
foreach ($this->getPhotos() as $photo) {
if ($photo->getPosition() !== $i) {
$photo->setPosition($i);
$photo->save();
}
$photos[ $i ] = $photo;
++$i;
}
$this->__refreshCache();
}
public function __cachePrepare() {
$this->_reload();
$this->getPosterPhoto();
}
public function isShow() {
return $this->getStatus() === self::STATUS_SHOW;
}
public function show() {
$this->setStatus( self::STATUS_SHOW );
$this->save();
}
public function hide() {
$this->setStatus( self::STATUS_HIDE );
$this->save();
}
public function hasOwner() {
$photo = $this->getPosterPhoto();
return $photo instanceof RM_Photo && $photo->getIdUser();
}
public function belongsTo(RM_User_Interface $user) {
$photo = $this->getPosterPhoto();
return $photo instanceof RM_Photo ? $photo->getIdUser() == $user->getId() : null;
}
public function remove() {
$this->setStatus( self::STATUS_DELETED );
$this->save();
$this->__cleanCache();
}
private function _isPhotoAdded(RM_Photo $addPhoto) {
foreach ($this->getPhotos() as $photo) {
if ($addPhoto->getId() === $photo->getId()) {
return true;
}
}
return false;
}
private function _savePhotos() {
foreach ($this->getPhotos() as $photo) {
if ($photo->getIdGallery() != $this->getId()) {
$photo->setIdGallery( $this->getId() );
$photo->save();
}
}
}
/**
* @return RM_Gallery_Photo
*/
protected function __getGalleryPhotoClassName() {
return 'RM_Gallery_Photo';
}
}