aboutsummaryrefslogtreecommitdiff
path: root/lib/album.php
blob: 15450049aa255d61f41e9561878eefb03477c796 (plain)
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
<?php
/**
 * Tidypics Album class
 *
 * @package TidypicsAlbum
 */


class TidypicsAlbum extends ElggObject {
	protected function initialise_attributes() {
		parent::initialise_attributes();

		$this->attributes['subtype'] = "album";
	}

	public function __construct($guid = null) {
		parent::__construct($guid);
	}

	/**
	 * 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);
	}

	/**
	 * 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 GUID of the image before the current one
	 *
	 * @param int $currentGuid
	 * @return int
	 */
	public function getPreviousImageGuid($currentGuid) {
		$imageList = $this->getImageList();
		$key = array_search($currentGuid, $imageList);
		if ($key === FALSE) {
			return 0;
		}
		$key--;
		if ($key < 0) {
			return 0;
		}
		return $imageList[$key];
	}

	/**
	 * Get the GUID of the image after the current one
	 *
	 * @param int $currentGuid
	 * @return int
	 */
	public function getNextImageGuid($currentGuid) {
		$imageList = $this->getImageList();
		$key = array_search($currentGuid, $imageList);
		if ($key === FALSE) {
			return 0;
		}
		$key++;
		if ($key >= count($imageList)) {
			return 0;
		}
		return $imageList[$key];
	}

	/**
	 * 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;
	}
}