1<?php
2
3/**
4 * Provides functions to print a tag cloud/list of all image tags from an album (optionally including the subalbums) or the album tags including sub album tags.
5 * Or alternatively a tag cloud/list of all tags used by either Zenpage news articles or pages.
6 *
7 * Note: The optional counter prints the total number of the tag used, not just for the select items (as clicking on it will return all anyway.)
8 *
9 * @author Malte Müller (acrylian)
10 * @package plugins
11 * @subpackage tag-extras
12 */
13$plugin_description = gettext("Provides functions to print a tag cloud of all tags from a Zenphoto object.");
14$plugin_author = "Malte Müller (acrylian)";
15$plugin_category = gettext('Misc');
16
17/**
18 * Prints a tag cloud list of the tags in one album and optionally its subalbums. Returns FALSE if no value.
19 *
20 * @param string $albumname folder name of the album to get the tags from ($subalbums = true this is the base albums)- This value is mandatory.
21 * @param bool $subalbums TRUE if the tags of subalbum should be. FALSE is default
22 * @param string $mode "images" for image tags, "albums" for album tags."images" is default.
23 * @return array
24 */
25function getAllTagsFromAlbum($albumname, $subalbums = false, $mode = 'images') {
26	global $_zp_gallery;
27	$passwordcheck = '';
28	$imageWhere = '';
29	$tagWhere = "";
30	$albumname = sanitize($albumname);
31	if (empty($albumname)) {
32		return FALSE;
33	}
34	$albumobj = newAlbum($albumname);
35	if (!$albumobj->exists) {
36		return FALSE;
37	}
38	if (zp_loggedin()) {
39		$albumWhere = "WHERE `dynamic`=0";
40	} else {
41		$albumscheck = query_full_array("SELECT * FROM " . prefix('albums') . " ORDER BY title");
42		foreach ($albumscheck as $albumcheck) {
43			if (!checkAlbumPassword($albumcheck['folder'])) {
44				$albumpasswordcheck = " AND id != " . $albumcheck['id'];
45				$passwordcheck = $passwordcheck . $albumpasswordcheck;
46			}
47		}
48		$albumWhere = "WHERE `dynamic`=0 AND `show`=1" . $passwordcheck;
49	}
50	if ($subalbums) {
51		$albumWhere .= " AND `folder` LIKE " . db_quote(db_LIKE_escape($albumname) . "%");
52	} else {
53		$albumWhere .= " AND `folder` = " . db_quote($albumname);
54	}
55	$albumids = query_full_array("SELECT id, folder FROM " . prefix('albums') . $albumWhere);
56	switch ($mode) {
57		case "images":
58			if (count($albumids) == 0) {
59				return FALSE;
60			} else {
61				$imageWhere = " WHERE ";
62				$count = "";
63				foreach ($albumids as $albumid) {
64					$count++;
65					$imageWhere .= 'albumid=' . $albumid['id'];
66					if ($count != count($albumids))
67						$imageWhere .= " OR ";
68				}
69			}
70			$imageids = query_full_array("SELECT id, albumid FROM " . prefix('images') . $imageWhere);
71			// if the album has no direct images and $subalbums is set to false
72			if (count($imageids) == 0) {
73				return FALSE;
74			} else {
75				$count = "";
76				$tagWhere = " WHERE ";
77				foreach ($imageids as $imageid) {
78					$count++;
79					$tagWhere .= '(o.objectid =' . $imageid['id'] . " AND o.tagid = t.id AND o.type = 'images')";
80					if ($count != count($imageids))
81						$tagWhere .= " OR ";
82				}
83			}
84			if (empty($tagWhere)) {
85				return FALSE;
86			} else {
87				return query_full_array("SELECT DISTINCT t.name, t.id, (SELECT DISTINCT COUNT(*) FROM " . prefix('obj_to_tag') . " WHERE tagid = t.id AND type = 'images') AS count FROM  " . prefix('obj_to_tag') . " AS o," . prefix('tags') . " AS t" . $tagWhere . " ORDER BY t.name");
88			}
89			break;
90		case "albums":
91			$count = "";
92			if (count($albumids) == 0) {
93				return FALSE;
94			} else {
95				$tagWhere = " WHERE ";
96				foreach ($albumids as $albumid) {
97					$count++;
98					$tagWhere .= '(o.objectid =' . $albumid['id'] . " AND o.tagid = t.id AND o.type = 'albums')";
99					if ($count != count($albumids))
100						$tagWhere .= " OR ";
101				}
102			}
103			if (empty($tagWhere)) {
104				return FALSE;
105			} else {
106				return query_full_array("SELECT DISTINCT t.name, t.id, (SELECT DISTINCT COUNT(*) FROM " . prefix('obj_to_tag') . " WHERE tagid = t.id AND o.type = 'albums') AS count FROM " . prefix('obj_to_tag') . " AS o," . prefix('tags') . " AS t" . $tagWhere . " ORDER BY t.name");
107			}
108			break;
109	}
110}
111
112/**
113 * Gets all tags used by either all Zenpage news articles or pages.
114 * @param string $mode "news" for Zenpage news article tags, "pages" for Zenpage pages tags
115 *
116 */
117function getAllTagsFromZenpage($mode = 'news') {
118	global $_zp_gallery, $_zp_zenpage;
119	if (!extensionEnabled('zenpage')) {
120		return FALSE;
121	}
122	$passwordcheck = '';
123	$ids = array();
124	$where = '';
125	$tagWhere = "";
126	switch ($mode) {
127		case 'news':
128			if (zp_loggedin(ZENPAGE_NEWS_RIGHTS | ALL_NEWS_RIGHTS)) {
129				$published = 'all';
130			} else {
131				$published = 'published';
132			}
133			$type = 'news';
134			$items = $_zp_zenpage->getArticles(false, $published);
135			foreach ($items as $item) {
136				$obj = new ZenpageNews($item['titlelink']);
137				if ($obj->checkAccess()) {
138					$ids[] = $obj->getID();
139				}
140			}
141			break;
142		case 'pages':
143			$published = !zp_loggedin(ZENPAGE_NEWS_RIGHTS | ALL_NEWS_RIGHTS);
144			$type = 'pages';
145			$items = $_zp_zenpage->getPages($published);
146			foreach ($items as $item) {
147				$obj = new ZenpagePage($item['titlelink']);
148				if ($obj->checkAccess()) {
149					$ids[] = $obj->getID();
150				}
151			}
152			break;
153	}
154	$count = '';
155	if (count($ids) == 0) {
156		return FALSE;
157	} else {
158		$tagWhere = " WHERE ";
159		foreach ($ids as $id) {
160			$count++;
161			$tagWhere .= '(o.objectid =' . $id . " AND o.tagid = t.id AND o.type = '" . $type . "')";
162			if ($count != count($ids))
163				$tagWhere .= " OR ";
164		}
165	}
166	if (empty($tagWhere)) {
167		return FALSE;
168	} else {
169		return query_full_array("SELECT DISTINCT t.name, t.id, (SELECT DISTINCT COUNT(*) FROM " . prefix('obj_to_tag') . " WHERE tagid = t.id AND o.type = '" . $type . "') AS count FROM " . prefix('obj_to_tag') . " AS o," . prefix('tags') . " AS t" . $tagWhere . " ORDER BY t.name");
170	}
171}
172
173/**
174 * Prints a tag cloud list of the tags used by either all Zenpage news articles or pages.
175 *
176 * @param string $mode "news" for Zenpage news article tags, "pages" for Zenpage pages tags
177 * @param string $separator how to separate the entries
178 * @param string $class css classs to style the list
179 * @param integer $showcounter if the tag count should be shown (no counter if $mode = "all")
180 * @param bool $tagcloud if set to false a simple list without font size changes will be printed, set to true (default) prints a list as a tag cloud
181 * @param integere $size_min smallest font size the cloud should display
182 * @param integer $size_max largest font size the cloud should display
183 * @param integer $count_min the minimum count for a tag to appear in the output
184 * @param integer $count_max the floor count for setting the cloud font size to $size_max
185 */
186function printAllTagsFromZenpage($mode = 'news', $separator = '', $class = '', $showcounter = true, $tagcloud = true, $size_min = 1, $size_max = 5, $count_min = 1, $count_max = 50) {
187	$tags = getAllTagsFromZenpage($mode);
188	printAllTags($tags, $mode, $separator, $class, $showcounter, $tagcloud, $size_min, $size_max, $count_min, $count_max);
189}
190
191/**
192 * Prints a tag cloud list of the tags in one album and optionally its subalbums.
193 * Known limitation: If $mode is set to "all" there is no tag count and therefore no tag cloud but a simple list
194 *
195 * @param string $albumname folder name of the album to get the tags from ($subalbums = true this is the base albums)
196 * @param bool $subalbums TRUE if the tags of subalbum should be. FALSE is default
197 * @param string $mode "images" for image tags, "albums" for album tags, "all" for both mixed
198 * @param string $separator how to separate the entries
199 * @param string $class css classs to style the list
200 * @param integer $showcounter if the tag count should be shown (no counter if $mode = "all")
201 * @param bool $tagcloud if set to false a simple list without font size changes will be printed, set to true (default) prints a list as a tag cloud
202 * @param integere $size_min smallest font size the cloud should display
203 * @param integer $size_max largest font size the cloud should display
204 * @param integer $count_min the minimum count for a tag to appear in the output
205 * @param integer $count_max the floor count for setting the cloud font size to $size_max
206 */
207function printAllTagsFromAlbum($albumname = "", $subalbums = false, $mode = 'images', $separator = '', $class = '', $showcounter = true, $tagcloud = true, $size_min = 1, $size_max = 5, $count_min = 1, $count_max = 50) {
208	if ($mode == 'all') {
209		if (getAllTagsFromAlbum($albumname, $subalbums, 'albums') OR getAllTagsFromAlbum($albumname, $subalbums, 'images')) {
210			$showcounter = false;
211			$tags1 = getAllTagsFromAlbum($albumname, $subalbums, 'albums');
212			$tags2 = getAllTagsFromAlbum($albumname, $subalbums, 'images');
213			$tags = array_merge($tags1, $tags2);
214			$tags = getAllTagsFromAlbum_multi_unique($tags);
215		} else {
216			return FALSE;
217		}
218	} else {
219		if (getAllTagsFromAlbum($albumname, $subalbums, $mode)) {
220			$tags = getAllTagsFromAlbum($albumname, $subalbums, $mode);
221		} else {
222			return FALSE;
223		}
224	}
225	printAllTags($tags, $mode, $separator, $class, $showcounter, $tagcloud, $size_min, $size_max, $count_min, $count_max);
226}
227
228/**
229 * Prints a tag cloud list of the tags in one album and optionally its subalbums. Base function to printAllTagsFromAlbum().
230 *
231 * Note meant to be used standalone.
232 *
233 * @param array $tags array of tags with the fields count, id, and name (as passed by the specific printAllTagsFrom.... functions)
234 * @param string $mode "images" for image tags, "albums" for album tags, "all" for images and albums mixed or "news" for Zenpage news articles, "pages" for Zenpage pages
235 * @param string $separator how to separate the entries
236 * @param string $class css classs to style the list
237 * @param integer $showcounter if the tag count should be shown (no counter if $mode = "all")
238 * @param bool $tagcloud if set to false a simple list without font size changes will be printed, set to true (default) prints a list as a tag cloud
239 * @param integere $size_min smallest font size the cloud should display
240 * @param integer $size_max largest font size the cloud should display
241 * @param integer $count_min the minimum count for a tag to appear in the output
242 * @param integer $count_max the floor count for setting the cloud font size to $size_max
243 */
244function printAllTags($tags, $mode, $separator = '', $class = '', $showcounter = true, $tagcloud = true, $size_min = 1, $size_max = 5, $count_min = 1, $count_max = 50) {
245	if (!is_array($tags)) {
246		return FALSE;
247	}
248	$size_min = sanitize_numeric($size_min);
249	$size_max = sanitize_numeric($size_max);
250	$count_min = sanitize_numeric($count_min);
251	$count_max = sanitize_numeric($count_max);
252	$separator = sanitize($separator);
253	if (!empty($class))
254		$class = 'class="' . sanitize($class) . '"';
255	$counter = '';
256	echo "<ul " . $class . ">\n";
257	$loopcount = '';
258	$tagcount = count($tags);
259	foreach ($tags as $row) {
260		if ($row['count'] >= $count_min) {
261			$loopcount++;
262			$count = $row['count'];
263			$tid = $row['id'];
264			$tname = $row['name'];
265			$style = "";
266			if ($tagcloud OR $mode == 'all') {
267				$size = min(max(round(($size_max * ($count - $count_min)) / ($count_max - $count_min), 2), $size_min)
268								, $size_max);
269				$size = str_replace(',', '.', $size);
270				$style = " style=\"font-size:" . $size . "em;\"";
271			}
272			if ($showcounter) {
273				$counter = ' (' . $count . ')';
274			}
275			if ($loopcount == $tagcount)
276				$separator = '';
277			echo "<li><a class=\"tagLink\" href=\"" . html_encode(getSearchURL($tname, '', 'tags', 0)) . "\"" . $style . ">" . $tname . $counter . "</a>" . $separator . "</li>\n";
278		}
279	}
280	echo "</ul>\n";
281}
282
283/**
284 * Removes duplicate entries in multi dimensional array.
285 * From kenrbnsn at rbnsn dot com http://uk.php.net/manual/en/function.array-unique.php#57202
286 * @param array $array
287 * @return array
288 */
289function getAllTagsFromAlbum_multi_unique($array) {
290	foreach ($array as $k => $na)
291		$new[$k] = serialize($na);
292	$uniq = array_unique($new);
293	foreach ($uniq as $k => $ser)
294		$new1[$k] = getSerializedArray($ser);
295	return ($new1);
296}
297
298?>
299