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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
<?php
/**
* Tidypics Album class
*
* @package TidypicsAlbum
* @author Cash Costello
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2
*/
class TidypicsAlbum extends ElggObject {
/**
* Sets the internal attributes
*/
protected function initializeAttributes() {
parent::initializeAttributes();
$this->attributes['subtype'] = "album";
}
/**
* Constructor
* @param mixed $guid
*/
public function __construct($guid = null) {
parent::__construct($guid);
}
/**
* Save an album
*
* @return bool
*/
public function save() {
if (!isset($this->new_album)) {
$this->new_album = true;
}
if (!parent::save()) {
return false;
}
mkdir(tp_get_img_dir() . $this->guid, 0755, true);
elgg_trigger_event('create', 'album', $this);
return true;
}
/**
* Delete album
*
* @return bool
*/
public function delete() {
$this->deleteImages();
$this->deleteAlbumDir();
return parent::delete();
}
/**
* Get the title of the photo album
*
* @return string
*/
public function getTitle() {
return $this->title;
}
/**
* Get the URL for this album
*
* @return string
*/
public function getURL() {
$title = elgg_get_friendly_title($this->getTitle());
$url = "photos/album/$this->guid/$title";
return elgg_normalize_url($url);
}
/**
* Get an array of image objects
*
* @param int $limit
* @param int $offset
* @return array
*/
public function getImages($limit, $offset = 0) {
$imageList = $this->getImageList();
if ($offset > count($imageList)) {
return array();
}
$imageList = array_slice($imageList, $offset, $limit);
$images = array();
foreach ($imageList as $guid) {
$images[] = get_entity($guid);
}
return $images;
}
/**
* View a list of images
*
* @param int $limit
* @param int $offset
* @return string
*/
public function viewImages($limit, $offset = 0) {
$images = $this->getImages($limit, $offset);
if (count($images) == 0) {
return '';
}
$count = $this->getSize();
return elgg_view_entity_list($images, $count, $offset, $limit, false, false, true);
}
public function getCoverImage() {
return get_entity($this->getCoverImageGuid());
}
/**
* Get the GUID of the album cover
*
* @return int
*/
public function getCoverImageGuid() {
if ($this->getSize() == 0) {
return 0;
}
$guid = $this->cover;
$imageList = $this->getImageList();
if (!in_array($guid, $imageList)) {
// select random photo to be cover
$index = array_rand($imageList, 1);
$guid = $imageList[$index];
$this->cover = $guid;
}
return $guid;
}
/**
* Set the GUID for the album cover
*
* @param int $guid
* @return bool
*/
public function setCoverImageGuid($guid) {
$imageList = $this->getImageList();
if (!in_array($guid, $imageList)) {
return false;
}
$this->cover = $guid;
return true;
}
/**
* Get the number of photos in the album
*
* @return int
*/
public function getSize() {
return count($this->getImageList());
}
/**
* Returns an order list of image guids
*
* @return array
*/
public function getImageList() {
$listString = $this->orderedImages;
if (!$listString) {
return array();
}
$list = unserialize($listString);
return $list;
}
/**
* Sets the album image order
*
* @param array $list An indexed array of image guids
*/
public function setImageList($list) {
$listString = serialize($list);
$this->orderedImages = $listString;
}
/**
* Add new images to the front of the image list
*
* @param array $list An indexed array of image guids
*/
public function prependImageList($list) {
$currentList = $this->getImageList();
$list = array_merge($list, $currentList);
$this->setImageList($list);
}
/**
* Get the previous image in the album
*
* @param int $guid GUID of the current image
* @return TidypicsImage
*/
public function getPreviousImage($guid) {
$imageList = $this->getImageList();
$key = array_search($guid, $imageList);
if ($key === FALSE) {
return null;
}
$key--;
if ($key < 0) {
return get_entity(end($imageList));
}
return get_entity($imageList[$key]);
}
/**
* Get the next image in the album
*
* @param int $guid GUID of the current image
* @return TidypicsImage
*/
public function getNextImage($guid) {
$imageList = $this->getImageList();
$key = array_search($guid, $imageList);
if ($key === FALSE) {
return null;
}
$key++;
if ($key >= count($imageList)) {
return get_entity($imageList[0]);
}
return get_entity($imageList[$key]);
}
public function getIndex($guid) {
return array_search($guid, $this->getImageList()) + 1;
}
/**
* Remove an image from the album list
*
* @param int $imageGuid
* @return bool
*/
public function removeImage($imageGuid) {
$imageList = $this->getImageList();
$key = array_search($imageGuid, $imageList);
if ($key === false) {
return false;
}
unset($imageList[$key]);
$this->setImageList($imageList);
return true;
}
/**
* Delete all the images in this album
*/
protected function deleteImages() {
// get all the images from this album as long as less than 999 images
$images = elgg_get_entities(array(
"type=" => "object",
"subtype" => "image",
"container_guid" => $this->guid,
"limit" => ELGG_ENTITIES_NO_VALUE,
));
if ($images) {
foreach ($images as $image) {
if ($image) {
$image->delete();
}
}
}
}
/**
* Delete the album directory on disk
*/
protected function deleteAlbumDir() {
$tmpfile = new ElggFile();
$tmpfile->setFilename('image/' . $this->guid . '/._tmp_del_tidypics_album_');
$tmpfile->subtype = 'image';
$tmpfile->owner_guid = $this->owner_guid;
$tmpfile->container_guid = $this->guid;
$tmpfile->open("write");
$tmpfile->write('');
$tmpfile->close();
$tmpfile->save();
$albumdir = eregi_replace('/._tmp_del_tidypics_album_', '', $tmpfile->getFilenameOnFilestore());
$tmpfile->delete();
if (is_dir($albumdir)) {
rmdir($albumdir);
}
}
}
|