1<?php
2
3/**
4 * Support functions for "statistics" about images and albums.
5 *
6 * Supports such statistics as "most popular", "latest", "top rated", etc.
7 *
8 * <b>CAUTION:</b> The way to get a specific album has changed. You now have to pass the foldername of an album instead the album title.
9 *
10 * @author Malte Müller (acrylian), Stephen Billard (sbillard), gjr
11 * @package plugins
12 * @subpackage image-album-statistiscs
13 */
14$plugin_description = gettext("Functions that provide various statistics about images and albums in the gallery.");
15$plugin_author = "Malte Müller (acrylian), Stephen Billard (sbillard), gjr";
16$plugin_category = gettext('Statistics');
17
18require_once(dirname(dirname(__FILE__)) . '/template-functions.php');
19
20/**
21 * Returns a array of album objects of album statistic accordingly to $option
22 *
23 * @param int $number the number of albums to get
24 * @param string $option
25 * 		"popular" for the most popular albums,
26 * 		"latest" for the latest uploaded by id (Discovery)
27 * 		"latest-date" for the latest by date
28 * 		"latest-mtime" for the latest by mtime
29 *   	"latest-publishdate" for the latest by publishdate
30 *    "mostrated" for the most voted,
31 * 		"toprated" for the best voted
32 * 		"latestupdated" for the latest updated
33 * 		"random" for random order (yes, strictly no statistical order...)
34 * @param string $albumfolder The name of an album to get only the statistc for its direct subalbums
35 * @param integer $threshold the minimum number of ratings (for rating options) or hits (for popular option) an album must have to be included in the list. (Default 0)
36 * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
37 * @return array
38 */
39function getAlbumStatistic($number = 5, $option, $albumfolder = '', $threshold = 0, $sortdirection = 'desc', $collection = false) {
40  global $_zp_gallery;
41  if ($albumfolder) {
42    $obj = newAlbum($albumfolder);
43    $albumWhere = ' WHERE parentid = ' . $obj->getID();
44    if ($collection) {
45      $albumWhere = '';
46      $ids = getAllSubAlbumIDs($albumfolder);
47      if (!empty($ids)) {
48        foreach ($ids as $id) {
49          $getids[] = $id['id'];
50        }
51        $getids = implode(', ', $getids);
52        $albumWhere = ' WHERE id IN (' . $getids . ')';
53      } else {
54        $albumWhere = ' WHERE parentid = ' . $obj->getID();
55      }
56    }
57  } else {
58    $obj = $_zp_gallery;
59    $albumWhere = '';
60  }
61  switch (strtolower($sortdirection)) {
62    case 'desc':
63    default:
64      $sortdir = 'DESC';
65      break;
66    case 'asc':
67      $sortdir = 'ASC';
68      break;
69  }
70  if (($option == 'toprated' || $option == 'mostrated' || $option == 'popular') && $threshold > 0) {
71    if (empty($albumWhere)) {
72      $albumWhere = ' WHERE ';
73    } else {
74      $albumWhere .= ' AND ';
75    }
76    if (($option == 'toprated' || $option == 'mostrated') && $threshold > 0) {
77      $albumWhere .= 'total_votes >= ' . $threshold;
78    }
79    if ($option == 'popular' && $threshold > 0) {
80      $albumWhere .= 'hitcounter >= ' . $threshold;
81    }
82  }
83  switch ($option) {
84    case "popular":
85      $sortorder = "hitcounter";
86      break;
87    default:
88    case "latest":
89      $sortorder = "id";
90      break;
91    case "latest-mtime":
92      $sortorder = "mtime";
93      break;
94    case "latest-date":
95      $sortorder = "date";
96      break;
97    case "latest-publishdate":
98      $sortorder = "IFNULL(publishdate,date)";
99      break;
100    case "mostrated":
101      $sortorder = "total_votes";
102      break;
103    case "toprated":
104      $sortorder = "(total_value/total_votes) DESC, total_value";
105      break;
106    case "latestupdated":
107      $sortorder = 'updateddate';
108      break;
109    case "random":
110      $sortorder = "RAND()";
111      break;
112  }
113  $albumArray = array();
114  if ($obj->table == 'albums' && $obj->isDynamic()) {
115    $albums = $obj->getAlbums(0, $sortorder, $sortdir);
116    foreach ($albums as $album) {
117      $album = newAlbum($album);
118      if ($album->checkAccess() && ($album->isPublic() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
119        $albumArray[] = $album;
120        if (count($albumArray) >= $number) { // got enough
121          break;
122        }
123      }
124    }
125  } else {
126    $result = query("SELECT id, title, folder, thumb FROM " . prefix('albums') . $albumWhere . " ORDER BY " . $sortorder . " " . $sortdir);
127    while ($row = db_fetch_assoc($result)) {
128      $album = newAlbum($row['folder'], true, true);
129      if ($album->exists && $album->checkAccess() && ($album->isPublic() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
130        //actually we only use "folder" but keep for backward compatibility in case someone uses those for now …
131        $albumArray[] = $album;
132        if (count($albumArray) >= $number) { // got enough
133          break;
134        }
135      }
136    }
137    db_free_result($result);
138  }
139  return $albumArray;
140}
141
142/**
143 * Prints album statistic according to $option as an unordered HTML list
144 * A css id is attached by default named '$option_album'
145 *
146 * @param string $number the number of albums to get
147 * @param string $option
148 * 		"popular" for the most popular albums,
149 * 		"latest" for the latest uploaded by id (Discovery)
150 * 		"latest-date" for the latest by date
151 * 		"latest-mtime" for the latest by mtime
152 *   	"latest-publishdate" for the latest by publishdate
153 *    "mostrated" for the most voted,
154 * 		"toprated" for the best voted
155 * 		"latestupdated" for the latest updated
156 * 		"random" for random order (yes, strictly no statistical order...)
157 * @param bool $showtitle if the album title should be shown
158 * @param bool $showdate if the album date should be shown
159 * @param bool $showdesc if the album description should be shown
160 * @param integer $desclength the length of the description to be shown
161 * @param string $showstatistic
162 * 		"hitcounter" for showing the hitcounter (views),
163 * 		"rating" for rating,
164 * 		"rating+hitcounter" for both.
165 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
166 * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
167 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
168 * @param integer $threshold the minimum number of ratings (for rating options) or hits (for popular option) an album must have to be included in the list. (Default 0)
169 * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
170 */
171function printAlbumStatistic($number, $option, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $threshold = 0, $collection = false) {
172	$albums = getAlbumStatistic($number, $option, $albumfolder, $threshold, $collection);
173	echo "\n<div id=\"" . $option . "_album\">\n";
174	echo "<ul>";
175	foreach ($albums as $album) {
176		printAlbumStatisticItem($album, $option, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $firstimglink);
177	}
178	echo "</ul></div>\n";
179}
180
181/**
182 * A helper function that only prints a item of the loop within printAlbumStatistic()
183 * Not for standalone use.
184 *
185 * @param array $album the array with album objects that getAlbumsStatistic() submitted
186 * @param string $option
187 * 		"popular" for the most popular albums,
188 * 		"latest" for the latest uploaded by id (Discovery)
189 * 		"latest-date" for the latest by date
190 * 		"latest-mtime" for the latest by mtime
191 *   	"latest-publishdate" for the latest by publishdate
192 *    "mostrated" for the most voted,
193 * 		"toprated" for the best voted
194 * 		"latestupdated" for the latest updated
195 * 		"random" for random order (yes, strictly no statistical order...)
196 * @param bool $showtitle if the album title should be shown
197 * @param bool $showdate if the album date should be shown
198 * @param bool $showdesc if the album description should be shown
199 * @param integer $desclength the length of the description to be shown
200 * @param string $showstatistic
201 * 		"hitcounter" for showing the hitcounter (views),
202 * 		"rating" for rating,
203 * 		"rating+hitcounter" for both.
204 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
205 * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
206 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
207 * @param bool $firstimglink 'false' (default) if the album thumb link should lead to the album page, 'true' if to the first image of theh album if the album itself has images
208 * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
209 */
210function printAlbumStatisticItem($album, $option, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $firstimglink = false) {
211	global $_zp_gallery;
212	$twidth = $width;
213	$theight = $height;
214	if (is_null($crop) && is_null($width) && is_null($height)) {
215		$crop = 2;
216	} else {
217		if (is_null($width))
218			$width = 85;
219		if (is_null($height))
220			$height = 85;
221		if (is_null($crop)) {
222			$crop = 1;
223		} else {
224			$crop = (int) $crop && true;
225		}
226	}
227	$tempalbum = $album;
228	if ($firstimglink && $tempimage = $tempalbum->getImage(0)) {
229		$albumpath = $tempimage->getLink();
230	} else {
231		$albumpath = $tempalbum->getLink();
232	}
233	echo "<li><a href=\"" . $albumpath . "\" title=\"" . html_encode($tempalbum->getTitle()) . "\">\n";
234	$albumthumb = $tempalbum->getAlbumThumbImage();
235	$attr = array(
236					'src' => '',
237					'width' => '',
238					'height' => '',
239					'alt' => html_encode($albumthumb->getTitle()),
240					'title' => html_encode($albumthumb->getTitle()),
241					'loading' => 'lazy'
242			);
243	switch ($crop) {
244		case 0:
245			$sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $albumthumb, 'thumb');
246			$attr['src'] = html_pathurlencode($albumthumb->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE));
247			$attr['width'] = $sizes[0];
248			$attr['height'] = $sizes[1];
249			$attr_filtered = zp_apply_filter('custom_album_thumb_attr', $attr, $albumthumb);
250			$attributes = generateAttributesFromArray($attr_filtered);
251			$html = '<img' . $attributes . ' /></a>' . "\n";
252			echo zp_apply_filter('custom_album_thumb_html', $html, $albumthumb);
253			break;
254		case 1;
255			$sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $albumthumb, 'thumb');
256			$attr['src'] = html_pathurlencode($albumthumb->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE));
257			$attr['width'] = $sizes[0];
258			$attr['height'] = $sizes[1];
259			$attr_filtered = zp_apply_filter('custom_album_thumb_attr', $attr, $albumthumb);
260			$attributes = generateAttributesFromArray($attr_filtered);
261			$html = '<img' . $attributes . '  /></a>' . "\n";
262			echo zp_apply_filter('custom_album_thumb_html', $html, $albumthumb);
263			break;
264		case 2:
265			$sizes = getSizeDefaultThumb($albumthumb);
266			$attr['src'] = html_pathurlencode($albumthumb->getThumb());
267			$attr['width'] = $sizes[0];
268			$attr['height'] = $sizes[1];
269			$attr_filtered = zp_apply_filter('standard_album_thumb_attr', $attr, $albumthumb);
270			$attributes = generateAttributesFromArray($attr_filtered);
271			$html = '<img' .$attributes .' /></a>' . "\n";
272			echo zp_apply_filter('standard_album_thumb_html', $html, $albumthumb);
273			break;
274	}
275	if ($showtitle) {
276		echo "<h3><a href=\"" . $albumpath . "\" title=\"" . html_encode($tempalbum->getTitle()) . "\">\n";
277		echo $tempalbum->getTitle() . "</a></h3>\n";
278	}
279	if ($showdate) {
280		if ($option === "latestupdated") {
281			$filechangedate = strtotime($tempalbum->getUpdatedDate());
282			echo "<p>" . sprintf(gettext("Last update: %s"), zpFormattedDate(DATE_FORMAT, $filechangedate)) . "</p>";
283			$latestimage = query_single_row("SELECT mtime FROM " . prefix('images') . " WHERE albumid = " . $tempalbum->getID() . " AND `show` = 1 ORDER BY id DESC");
284			if ($latestimage) {
285				$count = db_count('images', "WHERE albumid = " . $tempalbum->getID() . " AND mtime = " . $latestimage['mtime']);
286				if ($count <= 1) {
287					$image = gettext("image");
288				} else {
289					$image = gettext("images");
290				}
291				echo "<span>" . sprintf(gettext('%1$u new %2$s'), $count, $image) . "</span>";
292			}
293		} else {
294			echo "<p>" . zpFormattedDate(DATE_FORMAT, strtotime($tempalbum->getDateTime())) . "</p>";
295		}
296	}
297	if ($showstatistic === "rating" OR $showstatistic === "rating+hitcounter") {
298		$votes = $tempalbum->get("total_votes");
299		$value = $tempalbum->get("total_value");
300		if ($votes != 0) {
301			$rating = round($value / $votes, 1);
302		}
303		echo "<p>" . sprintf(gettext('Rating: %1$u (Votes: %2$u)'), $rating, $tempalbum->get("total_votes")) . "</p>";
304	}
305	if ($showstatistic === "hitcounter" OR $showstatistic === "rating+hitcounter") {
306		$hitcounter = $tempalbum->getHitcounter();
307		if (empty($hitcounter)) {
308			$hitcounter = "0";
309		}
310		echo "<p>" . sprintf(gettext("Views: %u"), $hitcounter) . "</p>";
311	}
312	if ($showdesc) {
313		echo shortenContent($tempalbum->getDesc(), $desclength, ' (...)');
314	}
315	echo "</li>";
316}
317
318/**
319 * Returns a array of image objects of image statistic according to $option
320 *
321 * @param string $number the number of images to get
322 * @param string $option "popular" for the most popular images,
323 * 		"popular" for the most popular albums,
324 * 		"latest" for the latest uploaded by id (Discovery)
325 * 		"latest-date" for the latest by date
326 * 		"latest-mtime" for the latest by mtime
327 *   	"latest-publishdate" for the latest by publishdate
328 *    "mostrated" for the most voted,
329 * 		"toprated" for the best voted
330 * 		"latestupdated" for the latest updated
331 * 		"random" for random order (yes, strictly no statistical order...)
332 * @param string $albumfolder foldername of an specific album
333 * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
334 * @param integer $threshold the minimum number of ratings (for rating options) or hits (for popular option) an image must have to be included in the list. (Default 0)
335 * @return string
336 */
337function getImageStatistic($number, $option, $albumfolder = '', $collection = false, $threshold = 0, $sortdirection = 'desc') {
338  global $_zp_gallery;
339  if ($albumfolder) {
340    $obj = newAlbum($albumfolder);
341    $albumWhere = ' AND albums.id = ' . $obj->getID();
342    if ($collection) {
343      $albumWhere = '';
344      $ids = getAllSubAlbumIDs($albumfolder);
345      if (!empty($ids)) {
346        foreach ($ids as $id) {
347          $getids[] = $id['id'];
348        }
349        $getids = implode(', ', $getids);
350        $albumWhere = ' AND albums.id IN (' . $getids . ')';
351      }
352    } else {
353      $albumWhere = ' AND albums.id = ' . $obj->getID();
354    }
355  } else {
356    $obj = $_zp_gallery;
357    $albumWhere = '';
358  }
359  if (($option == 'toprated' || $option == 'mostrated') && $threshold > 0) {
360    $albumWhere .= 'AND images.total_votes >= ' . $threshold;
361  }
362  if ($option == 'popular' && $threshold > 0) {
363    $albumWhere .= 'AND images.hitcounter >= ' . $threshold;
364  }
365  switch (strtolower($sortdirection)) {
366    case 'desc':
367    default:
368      $sortdir = 'DESC';
369      break;
370    case 'asc':
371      $sortdir = 'ASC';
372      break;
373  }
374  switch ($option) {
375    case "popular":
376      $sortorder = "images.hitcounter";
377      break;
378    case "latest-date":
379      $sortorder = "images.date";
380      break;
381    case "latest-mtime":
382      $sortorder = "images.mtime";
383      break;
384    default:
385    case "latest":
386      $sortorder = "images.id";
387      break;
388    case "latest-publishdate":
389      $sortorder = "IFNULL(images.publishdate,images.date)";
390      break;
391    case "mostrated":
392      $sortorder = "images.total_votes";
393      break;
394    case "toprated":
395      $sortorder = "(images.total_value/images.total_votes) DESC, images.total_value";
396      break;
397    case "random":
398      $sortorder = "RAND()";
399      break;
400  }
401  $imageArray = array();
402  if (!empty($albumfolder) && $obj->isDynamic()) {
403    $sorttype = str_replace('images.', '', $sortorder);
404    $images = $obj->getImages(0, 0, $sorttype, $sortdir);
405    foreach ($images as $image) {
406      $image = newImage($obj, $image);
407      if ($image->exists && $image->checkAccess() && ($image->isPublic() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
408        $imageArray[] = $image;
409        if (count($imageArray) >= $number) { // got enough
410          break;
411        }
412      }
413    }
414  } else {
415    $result = query("SELECT images.filename AS filename, albums.folder AS folder FROM " . prefix('images') . " AS images, " . prefix('albums') . " AS albums " . "WHERE (images.albumid = albums.id) " . $albumWhere . " ORDER BY " . $sortorder . " " . $sortdir);
416    while ($row = db_fetch_assoc($result)) {
417      $image = newImage(NULL, $row, true);
418      if ($image->exists && $image->checkAccess() && ($image->isPublic() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
419        $imageArray[] = $image;
420        if (count($imageArray) >= $number) { // got enough
421          break;
422        }
423      }
424    }
425    db_free_result($result);
426  }
427  return $imageArray;
428}
429
430/**
431 * Prints image statistic according to $option as an unordered HTML list
432 * A css id is attached by default named accordingly'$option'
433 *
434 * @param string $number the number of albums to get
435 * @param string $option "popular" for the most popular images,
436 * 		"popular" for the most popular albums,
437 * 		"latest" for the latest uploaded by id (Discovery)
438 * 		"latest-date" for the latest by date
439 * 		"latest-mtime" for the latest by mtime
440 *   	"latest-publishdate" for the latest by publishdate
441 *    "mostrated" for the most voted,
442 * 		"toprated" for the best voted
443 * 		"latestupdated" for the latest updated
444 * 		"random" for random order (yes, strictly no statistical order...)
445 * @param string $albumfolder foldername of an specific album
446 * @param bool $showtitle if the image title should be shown
447 * @param bool $showdate if the image date should be shown
448 * @param bool $showdesc if the image description should be shown
449 * @param integer $desclength the length of the description to be shown
450 * @param string $showstatistic "hitcounter" for showing the hitcounter (views),
451 * 		"rating" for rating,
452 * 		"rating+hitcounter" for both.
453 * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
454 * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
455 * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
456 * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
457 * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
458 * @param integer $threshold the minimum number of ratings (for rating options) or hits (for popular option) an image must have to be included in the list. (Default 0)
459 * @return string
460 */
461function printImageStatistic($number, $option, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false, $threshold = 0) {
462	$images = getImageStatistic($number, $option, $albumfolder, $collection, $threshold);
463	if (is_null($crop) && is_null($width) && is_null($height)) {
464		$crop = 2;
465	} else {
466		if (is_null($width))
467			$width = 85;
468		if (is_null($height))
469			$height = 85;
470		if (is_null($crop)) {
471			$crop = 1;
472		} else {
473			$crop = (int) $crop && true;
474		}
475	}
476	echo "\n<div id=\"$option\">\n";
477	echo "<ul>";
478	foreach ($images as $image) {
479		if ($fullimagelink) {
480			$imagelink = $image->getFullImageURL();
481		} else {
482			$imagelink = $image->getLink();
483		}
484		echo '<li><a href="' . html_encode($imagelink) . '" title="' . html_encode($image->getTitle()) . "\">\n";
485		$attr = array(
486					'src' => '',
487					'width' => '',
488					'height' => '',
489					'alt' => html_encode($image->getTitle()),
490					'title' => html_encode($image->getTitle()),
491					'loading' => 'lazy'
492				);
493		switch ($crop) {
494			case 0:
495				$sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $image, 'thumb');
496				$attr['src'] = html_pathurlencode($image->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE));
497				$attr['width'] = $sizes[0];
498				$attr['height'] =  $sizes[1];
499				$attr_filtered = zp_apply_filter('custom_image_attr', $attr, $image);
500				$attributes = generateAttributesFromArray($attr_filtered);
501				$html = '<img' . $attributes . '" /></a>' . "\n";
502				echo zp_apply_filter('custom_image_html', $html, true, $image);
503				break;
504			case 1:
505				$sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $image, 'thumb');
506				$attr['src'] = html_pathurlencode($image->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE));
507				$attr['width'] = $sizes[0];
508				$attr['height'] =  $sizes[1];
509				$attr_filtered = zp_apply_filter('custom_image_attr', $attr, $image);
510				$attributes = generateAttributesFromArray($attr_filtered);
511				$html = '<img' . $attributes . ' /></a>' . "\n";
512				echo zp_apply_filter('custom_image_html', $html, true, $image);
513				break;
514			case 2:
515				$sizes = getSizeDefaultThumb($image);
516				$attr['src'] = html_pathurlencode($image->getThumb());
517				$attr['width'] = $sizes[0];
518				$attr['height'] =  $sizes[1];
519				$attr_filtered = zp_apply_filter('standard_image_thumb_attr', $attr, $image);
520				$attributes = generateAttributesFromArray($attr_filtered);
521				$html = '<img' . $attributes . ' /></a>' . "\n";
522				echo zp_apply_filter('standard_image_thumb_html', $html, $image);
523				break;
524		}
525		if ($showtitle) {
526			echo '<h3><a href="' . html_encode($image->getLink()) . '" title="' . html_encode($image->getTitle()) . "\">\n";
527			echo $image->getTitle() . "</a></h3>\n";
528		}
529		if ($showdate) {
530			echo "<p>" . zpFormattedDate(DATE_FORMAT, strtotime($image->getDateTime())) . "</p>";
531		}
532		if ($showstatistic === "rating" OR $showstatistic === "rating+hitcounter") {
533			$votes = $image->get("total_votes");
534			$value = $image->get("total_value");
535			if ($votes != 0) {
536				$rating = round($value / $votes, 1);
537			}
538			echo "<p>" . sprintf(gettext('Rating: %1$u (Votes: %2$u)'), $rating, $votes) . "</p>";
539		}
540		if ($showstatistic === "hitcounter" OR $showstatistic === "rating+hitcounter") {
541			$hitcounter = $image->getHitcounter();
542			if (empty($hitcounter)) {
543				$hitcounter = "0";
544			}
545			echo "<p>" . sprintf(gettext("Views: %u"), $hitcounter) . "</p>";
546		}
547		if ($showdesc) {
548			echo shortenContent($image->getDesc(), $desclength, ' (...)');
549		}
550		echo "</li>";
551	}
552	echo "</ul></div>\n";
553}
554
555/**
556 * A little helper function that checks if an image or album is to be considered 'new' within the time range set in relation to getImageDate()/getAlbumDate()
557 * Returns true or false.
558 *
559 * @param string $mode What to check "image" or "album".
560 * @param integer $timerange The time range the item should be considered new. Default is 604800 (unix time seconds = ca. 7 days)
561 * @return bool
562 */
563function checkIfNew($mode = "image", $timerange = 604800) {
564	$currentdate = date("U");
565	switch ($mode) {
566		case "image":
567			$itemdate = getImageDate("%s");
568			break;
569		case "album":
570			$itemdate = getAlbumDate("%s");
571			break;
572	}
573	$newcheck = $currentdate - $itemdate;
574	if ($newcheck < $timerange) {
575		return TRUE;
576	} else {
577		return FALSE;
578	}
579}
580
581/**
582 * Gets the number of all subalbums of all subalbum levels of either the current album or $albumobj
583 *
584 * @param object $albumobj Optional album object to check
585 * @param string $pre Optional text you want to print before the number
586 * @return bool
587 */
588function getNumAllSubalbums($albumobj, $pre = '') {
589	global $_zp_gallery, $_zp_current_album;
590	if (is_null($albumobj)) {
591		$albumobj = $_zp_current_album;
592	}
593	$albums = $_zp_gallery->getAllAlbums($albumobj, LIST_RIGHTS, false);
594	$count = count($albums);
595	if ($count != 0) {
596		return $pre . $count;
597	} else {
598		return false;
599	}
600}
601
602/**
603 * Returns an randomly selected image objecet from the gallery per day.
604 * Returns null on failure
605 *
606 * @since Zenphoto 1.5.8
607 *
608 * @param string $albumfolder foldername of an specific album. If not set from all images in the gallery
609 * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
610 *
611 * @return object|nill
612 */
613function getPictureOfTheDay($albumfolder = '', $collection = false) {
614	global $_zp_gallery;
615	$potd = getSerializedArray(getOption('picture_of_the_day'));
616	if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
617		$album = newAlbum($potd['folder'], true, true);
618		if ($album->exists) {
619			$image = newImage($album, $potd['filename'], true);
620			if ($image->exists) {
621				return $image;
622			}
623		}
624	}
625	$randomimage = getImageStatistic(1, 'random', $albumfolder, $collection);
626	if ($randomimage) {
627		$potd = array('day' => time(), 'folder' => $randomimage[0]->getAlbumName(), 'filename' => $randomimage[0]->getFileName());
628		setThemeOption('picture_of_the_day', serialize($potd), NULL, $_zp_gallery->getCurrentTheme());
629		return $randomimage[0];
630	}
631	return NULL;
632}
633