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:  */
 13: $plugin_description = gettext("Functions that provide various statistics about images and albums in the gallery.");
 14: $plugin_author = "Malte Müller (acrylian), Stephen Billard (sbillard)";
 15: 
 16: require_once(dirname(dirname(__FILE__)) . '/template-functions.php');
 17: 
 18: /**
 19:  * Returns a array of album objects of album statistic accordingly to $option
 20:  *
 21:  * @param int $number the number of albums to get
 22:  * @param string $option
 23:  *      "popular" for the most popular albums,
 24:  *      "latest" for the latest uploaded by id (Discovery)
 25:  *      "latest-date" for the latest by date
 26:  *      "latest-mtime" for the latest by mtime
 27:  *      "latest-publishdate" for the latest by publishdate
 28:  *    "mostrated" for the most voted,
 29:  *      "toprated" for the best voted
 30:  *      "latestupdated" for the latest updated
 31:  *      "random" for random order (yes, strictly no statistical order...)
 32:  * @param string $albumfolder The name of an album to get only the statistc for its direct subalbums
 33:  * @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)
 34:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
 35:  * @return array
 36:  */
 37: function getAlbumStatistic($number = 5, $option, $albumfolder = '', $threshold = 0, $sortdirection = 'desc', $collection = false) {
 38:   global $_zp_gallery;
 39:   if ($albumfolder) {
 40:     $obj = newAlbum($albumfolder);
 41:     $albumWhere = ' WHERE parentid = ' . $obj->getID();
 42:     if ($collection) {
 43:       $albumWhere = '';
 44:       $ids = getAllSubAlbumIDs($albumfolder);
 45:       if (!empty($ids)) {
 46:         foreach ($ids as $id) {
 47:           $getids[] = $id['id'];
 48:         }
 49:         $getids = implode(', ', $getids);
 50:         $albumWhere = ' WHERE id IN (' . $getids . ')';
 51:       } else {
 52:         $albumWhere = ' WHERE parentid = ' . $obj->getID();
 53:       }
 54:     }
 55:   } else {
 56:     $obj = $_zp_gallery;
 57:     $albumWhere = '';
 58:   }
 59:   switch (strtolower($sortdirection)) {
 60:     case 'desc':
 61:     default:
 62:       $sortdir = 'DESC';
 63:       break;
 64:     case 'asc':
 65:       $sortdir = 'ASC';
 66:       break;
 67:   }
 68:   if (($option == 'toprated' || $option == 'mostrated' || $option == 'popular') && $threshold > 0) {
 69:     if (empty($albumWhere)) {
 70:       $albumWhere = ' WHERE ';
 71:     } else {
 72:       $albumWhere .= ' AND ';
 73:     }
 74:     if (($option == 'toprated' || $option == 'mostrated') && $threshold > 0) {
 75:       $albumWhere .= 'total_votes >= ' . $threshold;
 76:     }
 77:     if ($option == 'popular' && $threshold > 0) {
 78:       $albumWhere .= 'hitcounter >= ' . $threshold;
 79:     }
 80:   }
 81:   switch ($option) {
 82:     case "popular":
 83:       $sortorder = "hitcounter";
 84:       break;
 85:     default:
 86:     case "latest":
 87:       $sortorder = "id";
 88:       break;
 89:     case "latest-mtime":
 90:       $sortorder = "mtime";
 91:       break;
 92:     case "latest-date":
 93:       $sortorder = "date";
 94:       break;
 95:     case "latest-publishdate":
 96:       $sortorder = "IFNULL(publishdate,date)";
 97:       break;
 98:     case "mostrated":
 99:       $sortorder = "total_votes";
100:       break;
101:     case "toprated":
102:       $sortorder = "(total_value/total_votes) DESC, total_value";
103:       break;
104:     case "latestupdated":
105:       $sortorder = 'updateddate';
106:       break;
107:     case "random":
108:       $sortorder = "RAND()";
109:       break;
110:   }
111:   $albumArray = array();
112:   if ($obj->table == 'albums' && $obj->isDynamic()) {
113:     $albums = $obj->getAlbums(0, $sortorder, $sortdir);
114:     foreach ($albums as $album) {
115:       $album = newAlbum($album);
116:       if ($album->checkAccess() && ($album->getShow() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
117:         $albumArray[] = $album;
118:         if (count($albumArray) >= $number) { // got enough
119:           break;
120:         }
121:       }
122:     }
123:   } else {
124:     $result = query("SELECT id, title, folder, thumb FROM " . prefix('albums') . $albumWhere . " ORDER BY " . $sortorder . " " . $sortdir);
125:     while ($row = db_fetch_assoc($result)) {
126:       $album = newAlbum($row['folder'], true, true);
127:       if ($album->exists && $album->checkAccess() && ($album->getShow() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
128:         //actually we only use "folder" but keep for backward compatibility in case someone uses those for now …
129:         $albumArray[] = $album;
130:         if (count($albumArray) >= $number) { // got enough
131:           break;
132:         }
133:       }
134:     }
135:     db_free_result($result);
136:   }
137:   return $albumArray;
138: }
139: 
140: /**
141:  * Prints album statistic according to $option as an unordered HTML list
142:  * A css id is attached by default named '$option_album'
143:  *
144:  * @param string $number the number of albums to get
145:  * @param string $option
146:  *      "popular" for the most popular albums,
147:  *      "latest" for the latest uploaded by id (Discovery)
148:  *      "latest-date" for the latest by date
149:  *      "latest-mtime" for the latest by mtime
150:  *      "latest-publishdate" for the latest by publishdate
151:  *    "mostrated" for the most voted,
152:  *      "toprated" for the best voted
153:  *      "latestupdated" for the latest updated
154:  *      "random" for random order (yes, strictly no statistical order...)
155:  * @param bool $showtitle if the album title should be shown
156:  * @param bool $showdate if the album date should be shown
157:  * @param bool $showdesc if the album description should be shown
158:  * @param integer $desclength the length of the description to be shown
159:  * @param string $showstatistic
160:  *      "hitcounter" for showing the hitcounter (views),
161:  *      "rating" for rating,
162:  *      "rating+hitcounter" for both.
163:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
164:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
165:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
166:  * @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)
167:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
168:  */
169: function 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) {
170:     $albums = getAlbumStatistic($number, $option, $albumfolder, $threshold, $collection);
171:     echo "\n<div id=\"" . $option . "_album\">\n";
172:     echo "<ul>";
173:     foreach ($albums as $album) {
174:         printAlbumStatisticItem($album, $option, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $firstimglink);
175:     }
176:     echo "</ul></div>\n";
177: }
178: 
179: /**
180:  * A helper function that only prints a item of the loop within printAlbumStatistic()
181:  * Not for standalone use.
182:  *
183:  * @param array $album the array with album objects that getAlbumsStatistic() submitted
184:  * @param string $option
185:  *      "popular" for the most popular albums,
186:  *      "latest" for the latest uploaded by id (Discovery)
187:  *      "latest-date" for the latest by date
188:  *      "latest-mtime" for the latest by mtime
189:  *      "latest-publishdate" for the latest by publishdate
190:  *    "mostrated" for the most voted,
191:  *      "toprated" for the best voted
192:  *      "latestupdated" for the latest updated
193:  *      "random" for random order (yes, strictly no statistical order...)
194:  * @param bool $showtitle if the album title should be shown
195:  * @param bool $showdate if the album date should be shown
196:  * @param bool $showdesc if the album description should be shown
197:  * @param integer $desclength the length of the description to be shown
198:  * @param string $showstatistic
199:  *      "hitcounter" for showing the hitcounter (views),
200:  *      "rating" for rating,
201:  *      "rating+hitcounter" for both.
202:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
203:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
204:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
205:  * @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
206:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
207:  */
208: function printAlbumStatisticItem($album, $option, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $firstimglink = false) {
209:     global $_zp_gallery;
210:     $twidth = $width;
211:     $theight = $height;
212:     if (is_null($crop) && is_null($width) && is_null($height)) {
213:         $crop = 2;
214:     } else {
215:         if (is_null($width))
216:             $width = 85;
217:         if (is_null($height))
218:             $height = 85;
219:         if (is_null($crop)) {
220:             $crop = 1;
221:         } else {
222:             $crop = (int) $crop && true;
223:         }
224:     }
225:     $tempalbum = $album;
226:     if ($firstimglink && $tempimage = $tempalbum->getImage(0)) {
227:         $albumpath = $tempimage->getLink();
228:     } else {
229:         $albumpath = $tempalbum->getLink();
230:     }
231:     echo "<li><a href=\"" . $albumpath . "\" title=\"" . html_encode($tempalbum->getTitle()) . "\">\n";
232:     $albumthumb = $tempalbum->getAlbumThumbImage();
233:     switch ($crop) {
234:         case 0:
235:             $sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $albumthumb);
236:             echo '<img src="' . html_encode(pathurlencode($albumthumb->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($albumthumb->getTitle()) . '" /></a>' . "\n";
237:             break;
238:         case 1;
239:     if(isImagePhoto($albumthumb)) {
240:       $sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $albumthumb);
241:     } else {
242:       $sizes[0] = $width;
243:       $sizes[1] = $height;
244:     }
245:             echo '<img src="' . html_encode(pathurlencode($albumthumb->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($albumthumb->getTitle()) . '" /></a>' . "\n";
246:             break;
247:         case 2:
248:             $sizes = getSizeDefaultThumb($albumthumb);
249:             echo '<img src="' . html_encode(pathurlencode($albumthumb->getThumb())) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($albumthumb->getTitle()) . '" /></a>' . "\n";
250:             break;
251:     }
252:     if ($showtitle) {
253:         echo "<h3><a href=\"" . $albumpath . "\" title=\"" . html_encode($tempalbum->getTitle()) . "\">\n";
254:         echo $tempalbum->getTitle() . "</a></h3>\n";
255:     }
256:     if ($showdate) {
257:         if ($option === "latestupdated") {
258:             $filechangedate = strtotime($tempalbum->getUpdatedDate());
259:             echo "<p>" . sprintf(gettext("Last update: %s"), zpFormattedDate(DATE_FORMAT, $filechangedate)) . "</p>";
260:             $latestimage = query_single_row("SELECT mtime FROM " . prefix('images') . " WHERE albumid = " . $tempalbum->getID() . " AND `show` = 1 ORDER BY id DESC");
261:             if ($latestimage) {
262:                 $count = db_count('images', "WHERE albumid = " . $tempalbum->getID() . " AND mtime = " . $latestimage['mtime']);
263:                 if ($count <= 1) {
264:                     $image = gettext("image");
265:                 } else {
266:                     $image = gettext("images");
267:                 }
268:                 echo "<span>" . sprintf(gettext('%1$u new %2$s'), $count, $image) . "</span>";
269:             }
270:         } else {
271:             echo "<p>" . zpFormattedDate(DATE_FORMAT, strtotime($tempalbum->getDateTime())) . "</p>";
272:         }
273:     }
274:     if ($showstatistic === "rating" OR $showstatistic === "rating+hitcounter") {
275:         $votes = $tempalbum->get("total_votes");
276:         $value = $tempalbum->get("total_value");
277:         if ($votes != 0) {
278:             $rating = round($value / $votes, 1);
279:         }
280:         echo "<p>" . sprintf(gettext('Rating: %1$u (Votes: %2$u)'), $rating, $tempalbum->get("total_votes")) . "</p>";
281:     }
282:     if ($showstatistic === "hitcounter" OR $showstatistic === "rating+hitcounter") {
283:         $hitcounter = $tempalbum->getHitcounter();
284:         if (empty($hitcounter)) {
285:             $hitcounter = "0";
286:         }
287:         echo "<p>" . sprintf(gettext("Views: %u"), $hitcounter) . "</p>";
288:     }
289:     if ($showdesc) {
290:         echo shortenContent($tempalbum->getDesc(), $desclength, ' (...)');
291:     }
292:     echo "</li>";
293: }
294: 
295: /**
296:  * Prints the most popular albums
297:  *
298:  * @param string $number the number of albums to get
299:  * @param bool $showtitle if the album title should be shown
300:  * @param bool $showdate if the album date should be shown
301:  * @param bool $showdesc if the album description should be shown
302:  * @param integer $desclength the length of the description to be shown
303:  * @param string $showstatistic
304:  *      "hitcounter" for showing the hitcounter (views),
305:  *      "rating" for rating,
306:  *      "rating+hitcounter" for both.
307:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
308:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
309:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
310:  * @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
311:  * @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)
312:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
313:  */
314: function printPopularAlbums($number = 5, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = 'hitcounter', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $threshold = 0, $collection = false) {
315:     printAlbumStatistic($number, "popular", $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $albumfolder, $firstimglink, $threshold, $collection);
316: }
317: 
318: /**
319:  * Prints the latest albums
320:  *
321:  * @param string $number the number of albums to get
322:  * @param bool $showtitle if the album title should be shown
323:  * @param bool $showdate if the album date should be shown
324:  * @param bool $showdesc if the album description should be shown
325:  * @param integer $desclength the length of the description to be shown
326:  * @param string $showstatistic
327:  *      "hitcounter" for showing the hitcounter (views),
328:  *      "rating" for rating,
329:  *      "rating+hitcounter" for both.
330:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
331:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
332:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
333:  * @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
334:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
335:  */
336: function printLatestAlbums($number = 5, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $collection = false) {
337:     printAlbumStatistic($number, "latest", $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $albumfolder, $firstimglink, $collection);
338: }
339: 
340: /**
341:  * Prints the most rated albums
342:  *
343:  * @param string $number the number of albums to get
344:  * @param bool $showtitle if the album title should be shown
345:  * @param bool $showdate if the album date should be shown
346:  * @param bool $showdesc if the album description should be shown
347:  * @param integer $desclength the length of the description to be shown
348:  * @param string $showstatistic
349:  *      "hitcounter" for showing the hitcounter (views),
350:  *      "rating" for rating,
351:  *      "rating+hitcounter" for both.
352:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
353:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
354:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
355:  * @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
356:  * @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)
357:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
358:  */
359: function printMostRatedAlbums($number = 5, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $threshold = 0, $collection = false) {
360:     printAlbumStatistic($number, "mostrated", $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $albumfolder, $firstimglink, $threshold, $collection);
361: }
362: 
363: /**
364:  * Prints the top voted albums
365:  *
366:  * @param string $number the number of albums to get
367:  * @param bool $showtitle if the album title should be shown
368:  * @param bool $showdate if the album date should be shown
369:  * @param bool $showdesc if the album description should be shown
370:  * @param integer $desclength the length of the description to be shown
371:  * @param string $showstatistic
372:  *      "hitcounter" for showing the hitcounter (views),
373:  *      "rating" for rating,
374:  *      "rating+hitcounter" for both.
375:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
376:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
377:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
378:  * @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
379:  * @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)
380:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
381:  */
382: function printTopRatedAlbums($number = 5, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $threshold = 0, $collection = false) {
383:     printAlbumStatistic($number, "toprated", $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $albumfolder, $firstimglink, $threshold, $collection);
384: }
385: 
386: /**
387:  * Prints the latest updated albums
388:  *
389:  * @param string $number the number of albums to get
390:  * @param bool $showtitle if the album title should be shown
391:  * @param bool $showdate if the album date should be shown
392:  * @param bool $showdesc if the album description should be shown
393:  * @param integer $desclength the length of the description to be shown
394:  * @param string $showstatistic
395:  *      "hitcounter" for showing the hitcounter (views),
396:  *      "rating" for rating,
397:  *      "rating+hitcounter" for both.
398:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
399:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
400:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
401:  * @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
402:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics to include all subalbum levels
403:  */
404: function printLatestUpdatedAlbums($number = 5, $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $albumfolder = '', $firstimglink = false, $collection = false) {
405:     printAlbumStatistic($number, "latestupdated", $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $albumfolder, $firstimglink, $collection);
406: }
407: 
408: /**
409:  * Returns a array of image objects of image statistic according to $option
410:  *
411:  * @param string $number the number of images to get
412:  * @param string $option "popular" for the most popular images,
413:  *      "popular" for the most popular albums,
414:  *      "latest" for the latest uploaded by id (Discovery)
415:  *      "latest-date" for the latest by date
416:  *      "latest-mtime" for the latest by mtime
417:  *      "latest-publishdate" for the latest by publishdate
418:  *    "mostrated" for the most voted,
419:  *      "toprated" for the best voted
420:  *      "latestupdated" for the latest updated
421:  *      "random" for random order (yes, strictly no statistical order...)
422:  * @param string $albumfolder foldername of an specific album
423:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
424:  * @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)
425:  * @return string
426:  */
427: function getImageStatistic($number, $option, $albumfolder = '', $collection = false, $threshold = 0, $sortdirection = 'desc') {
428:   global $_zp_gallery;
429:   if ($albumfolder) {
430:     $obj = newAlbum($albumfolder);
431:     $albumWhere = ' AND albums.id = ' . $obj->getID();
432:     if ($collection) {
433:       $albumWhere = '';
434:       $ids = getAllSubAlbumIDs($albumfolder);
435:       if (!empty($ids)) {
436:         foreach ($ids as $id) {
437:           $getids[] = $id['id'];
438:         }
439:         $getids = implode(', ', $getids);
440:         $albumWhere = ' AND albums.id IN (' . $getids . ')';
441:       }
442:     } else {
443:       $albumWhere = ' AND albums.id = ' . $obj->getID();
444:     }
445:   } else {
446:     $obj = $_zp_gallery;
447:     $albumWhere = '';
448:   }
449:   if (($option == 'toprated' || $option == 'mostrated') && $threshold > 0) {
450:     $albumWhere .= 'AND images.total_votes >= ' . $threshold;
451:   }
452:   if ($option == 'popular' && $threshold > 0) {
453:     $albumWhere .= 'AND images.hitcounter >= ' . $threshold;
454:   }
455:   switch (strtolower($sortdirection)) {
456:     case 'desc':
457:     default:
458:       $sortdir = 'DESC';
459:       break;
460:     case 'asc':
461:       $sortdir = 'ASC';
462:       break;
463:   }
464:   switch ($option) {
465:     case "popular":
466:       $sortorder = "images.hitcounter";
467:       break;
468:     case "latest-date":
469:       $sortorder = "images.date";
470:       break;
471:     case "latest-mtime":
472:       $sortorder = "images.mtime";
473:       break;
474:     default:
475:     case "latest":
476:       $sortorder = "images.id";
477:       break;
478:     case "latest-publishdate":
479:       $sortorder = "IFNULL(images.publishdate,images.date)";
480:       break;
481:     case "mostrated":
482:       $sortorder = "images.total_votes";
483:       break;
484:     case "toprated":
485:       $sortorder = "(images.total_value/images.total_votes) DESC, images.total_value";
486:       break;
487:     case "random":
488:       $sortorder = "RAND()";
489:       break;
490:   }
491:   $imageArray = array();
492:   if (!empty($albumfolder) && $obj->isDynamic()) {
493:     $sorttype = str_replace('images.', '', $sortorder);
494:     $images = $obj->getImages(0, 0, $sorttype, $sortdir);
495:     foreach ($images as $image) {
496:       $image = newImage($obj, $image);
497:       if ($image->exists && $image->checkAccess() && ($image->getShow() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
498:         $imageArray[] = $image;
499:         if (count($imageArray) >= $number) { // got enough
500:           break;
501:         }
502:       }
503:     }
504:   } else {
505:     $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);
506:     while ($row = db_fetch_assoc($result)) {
507:       $image = newImage(NULL, $row, true);
508:       if ($image->exists && $image->checkAccess() && ($image->getShow() || zp_loggedin(VIEW_UNPUBLISHED_RIGHTS))) {
509:         $imageArray[] = $image;
510:         if (count($imageArray) >= $number) { // got enough
511:           break;
512:         }
513:       }
514:     }
515:     db_free_result($result);
516:   }
517:   return $imageArray;
518: }
519: 
520: /**
521:  * Prints image statistic according to $option as an unordered HTML list
522:  * A css id is attached by default named accordingly'$option'
523:  *
524:  * @param string $number the number of albums to get
525:  * @param string $option "popular" for the most popular images,
526:  *      "popular" for the most popular albums,
527:  *      "latest" for the latest uploaded by id (Discovery)
528:  *      "latest-date" for the latest by date
529:  *      "latest-mtime" for the latest by mtime
530:  *      "latest-publishdate" for the latest by publishdate
531:  *    "mostrated" for the most voted,
532:  *      "toprated" for the best voted
533:  *      "latestupdated" for the latest updated
534:  *      "random" for random order (yes, strictly no statistical order...)
535:  * @param string $albumfolder foldername of an specific album
536:  * @param bool $showtitle if the image title should be shown
537:  * @param bool $showdate if the image date should be shown
538:  * @param bool $showdesc if the image description should be shown
539:  * @param integer $desclength the length of the description to be shown
540:  * @param string $showstatistic "hitcounter" for showing the hitcounter (views),
541:  *      "rating" for rating,
542:  *      "rating+hitcounter" for both.
543:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
544:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
545:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
546:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
547:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
548:  * @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)
549:  * @return string
550:  */
551: function 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) {
552:     $images = getImageStatistic($number, $option, $albumfolder, $collection, $threshold);
553:     if (is_null($crop) && is_null($width) && is_null($height)) {
554:         $crop = 2;
555:     } else {
556:         if (is_null($width))
557:             $width = 85;
558:         if (is_null($height))
559:             $height = 85;
560:         if (is_null($crop)) {
561:             $crop = 1;
562:         } else {
563:             $crop = (int) $crop && true;
564:         }
565:     }
566:     echo "\n<div id=\"$option\">\n";
567:     echo "<ul>";
568:     foreach ($images as $image) {
569:         if ($fullimagelink) {
570:             $imagelink = $image->getFullImageURL();
571:         } else {
572:             $imagelink = $image->getLink();
573:         }
574:         echo '<li><a href="' . html_encode($imagelink) . '" title="' . html_encode($image->getTitle()) . "\">\n";
575:         switch ($crop) {
576:             case 0:
577:                 $sizes = getSizeCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, $image);
578:                 echo '<img src="' . html_encode(pathurlencode($image->getCustomImage($width, NULL, NULL, NULL, NULL, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" /></a>\n";
579:                 break;
580:             case 1:
581:                 $sizes = getSizeCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, $image);
582:                 echo '<img src="' . html_encode(pathurlencode($image->getCustomImage(NULL, $width, $height, $width, $height, NULL, NULL, TRUE))) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" width=\"" . $width . "\" height=\"" . $height . "\" /></a>\n";
583:                 break;
584:             case 2:
585:                 $sizes = getSizeDefaultThumb($image);
586:                 echo '<img src="' . html_encode(pathurlencode($image->getThumb())) . '" width="' . $sizes[0] . '" height="' . $sizes[1] . '" alt="' . html_encode($image->getTitle()) . "\" /></a>\n<br />";
587:                 break;
588:         }
589:         if ($showtitle) {
590:             echo '<h3><a href="' . html_encode($image->getLink()) . '" title="' . html_encode($image->getTitle()) . "\">\n";
591:             echo $image->getTitle() . "</a></h3>\n";
592:         }
593:         if ($showdate) {
594:             echo "<p>" . zpFormattedDate(DATE_FORMAT, strtotime($image->getDateTime())) . "</p>";
595:         }
596:         if ($showstatistic === "rating" OR $showstatistic === "rating+hitcounter") {
597:             $votes = $image->get("total_votes");
598:             $value = $image->get("total_value");
599:             if ($votes != 0) {
600:                 $rating = round($value / $votes, 1);
601:             }
602:             echo "<p>" . sprintf(gettext('Rating: %1$u (Votes: %2$u)'), $rating, $votes) . "</p>";
603:         }
604:         if ($showstatistic === "hitcounter" OR $showstatistic === "rating+hitcounter") {
605:             $hitcounter = $image->getHitcounter();
606:             if (empty($hitcounter)) {
607:                 $hitcounter = "0";
608:             }
609:             echo "<p>" . sprintf(gettext("Views: %u"), $hitcounter) . "</p>";
610:         }
611:         if ($showdesc) {
612:             echo shortenContent($image->getDesc(), $desclength, ' (...)');
613:         }
614:         echo "</li>";
615:     }
616:     echo "</ul></div>\n";
617: }
618: 
619: /**
620:  * Prints the most popular images
621:  *
622:  * @param string $number the number of images to get
623:  * @param string $albumfolder folder of an specific album
624:  * @param bool $showtitle if the image title should be shown
625:  * @param bool $showdate if the image date should be shown
626:  * @param bool $showdesc if the image description should be shown
627:  * @param integer $desclength the length of the description to be shown
628:  * @param string $showstatistic
629:  *      "hitcounter" for showing the hitcounter (views),
630:  *      "rating" for rating,
631:  *      "rating+hitcounter" for both.
632:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
633:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
634:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
635:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
636:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
637:  * @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)
638:  */
639: function printPopularImages($number = 5, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false, $threshold = 0) {
640:     printImageStatistic($number, "popular", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink, $threshold);
641: }
642: 
643: /**
644:  * Prints the n top rated images
645:  *
646:  * @param int $number The number if images desired
647:  * @param string $albumfolder folder of an specific album
648:  * @param bool $showtitle if the image title should be shown
649:  * @param bool $showdate if the image date should be shown
650:  * @param bool $showdesc if the image description should be shown
651:  * @param integer $desclength the length of the description to be shown
652:  * @param string $showstatistic
653:  *      "hitcounter" for showing the hitcounter (views),
654:  *      "rating" for rating,
655:  *      "rating+hitcounter" for both.
656:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
657:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
658:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
659:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
660:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
661:  * @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)
662:  */
663: function printTopRatedImages($number = 5, $albumfolder = "", $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false, $threshold = 0) {
664:     printImageStatistic($number, "toprated", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink, $threshold);
665: }
666: 
667: /**
668:  * Prints the n most rated images
669:  *
670:  * @param int $number The number if images desired
671:  * @param string $albumfolder folder of an specific album
672:  * @param bool $showtitle if the image title should be shown
673:  * @param bool $showdate if the image date should be shown
674:  * @param bool $showdesc if the image description should be shown
675:  * @param integer $desclength the length of the description to be shown
676:  * @param string $showstatistic
677:  *      "hitcounter" for showing the hitcounter (views),
678:  *      "rating" for rating,
679:  *      "rating+hitcounter" for both.
680:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
681:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
682:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
683:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
684:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
685:  * @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)
686:  */
687: function printMostRatedImages($number = 5, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false, $threshold = 0) {
688:     printImageStatistic($number, "mostrated", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink, $threshold);
689: }
690: 
691: /**
692:  * Prints the latest images by ID (the order zenphoto recognized the images on the filesystem)
693:  *
694:  * @param string $number the number of images to get
695:  * @param string $albumfolder folder of an specific album
696:  * @param bool $showtitle if the image title should be shown
697:  * @param bool $showdate if the image date should be shown
698:  * @param bool $showdesc if the image description should be shown
699:  * @param integer $desclength the length of the description to be shown
700:  * @param string $showstatistic
701:  *      "hitcounter" for showing the hitcounter (views),
702:  *      "rating" for rating,
703:  *      "rating+hitcounter" for both.
704:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
705:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
706:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
707:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
708:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
709:  */
710: function printLatestImages($number = 5, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false) {
711:     printImageStatistic($number, "latest", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink);
712: }
713: 
714: /**
715:  * Prints the latest images by date order (date taken order)
716:  *
717:  * @param string $number the number of images to get
718:  * @param string $albumfolder folder of an specific album
719:  * @param bool $showtitle if the image title should be shown
720:  * @param bool $showdate if the image date should be shown
721:  * @param bool $showdesc if the image description should be shown
722:  * @param integer $desclength the length of the description to be shown
723:  * @param string $showstatistic
724:  *      "hitcounter" for showing the hitcounter (views),
725:  *      "rating" for rating,
726:  *      "rating+hitcounter" for both.
727:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
728:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
729:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
730:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
731:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
732:  */
733: function printLatestImagesByDate($number = 5, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false) {
734:     printImageStatistic($number, "latest-date", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink);
735: }
736: 
737: /**
738:  * Prints the latest images by mtime order (date uploaded order)
739:  *
740:  * @param string $number the number of images to get
741:  * @param string $albumfolder folder of an specific album
742:  * @param bool $showtitle if the image title should be shown
743:  * @param bool $showdate if the image date should be shown
744:  * @param bool $showdesc if the image description should be shown
745:  * @param integer $desclength the length of the description to be shown
746:  * @param string $showstatistic
747:  *      "hitcounter" for showing the hitcounter (views),
748:  *      "rating" for rating,
749:  *  "rating+hitcounter" for both.
750:  * @param integer $width the width/cropwidth of the thumb if crop=true else $width is longest size. (Default 85px)
751:  * @param integer $height the height/cropheight of the thumb if crop=true else not used.  (Default 85px)
752:  * @param bool $crop 'true' (default) if the thumb should be cropped, 'false' if not
753:  * @param bool $collection only if $albumfolder is set: true if you want to get statistics from this album and all of its subalbums
754:  * @param bool $fullimagelink 'false' (default) for the image page link , 'true' for the unprotected full image link (to use Colorbox for example)
755:  */
756: function printLatestImagesByMtime($number = 5, $albumfolder = '', $showtitle = false, $showdate = false, $showdesc = false, $desclength = 40, $showstatistic = '', $width = NULL, $height = NULL, $crop = NULL, $collection = false, $fullimagelink = false) {
757:     printImageStatistic($number, "latest-mtime", $albumfolder, $showtitle, $showdate, $showdesc, $desclength, $showstatistic, $width, $height, $crop, $collection, $fullimagelink);
758: }
759: 
760: /**
761:  * 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()
762:  * Returns true or false.
763:  *
764:  * @param string $mode What to check "image" or "album".
765:  * @param integer $timerange The time range the item should be considered new. Default is 604800 (unix time seconds = ca. 7 days)
766:  * @return bool
767:  */
768: function checkIfNew($mode = "image", $timerange = 604800) {
769:     $currentdate = date("U");
770:     switch ($mode) {
771:         case "image":
772:             $itemdate = getImageDate("%s");
773:             break;
774:         case "album":
775:             $itemdate = getAlbumDate("%s");
776:             break;
777:     }
778:     $newcheck = $currentdate - $itemdate;
779:     if ($newcheck < $timerange) {
780:         return TRUE;
781:     } else {
782:         return FALSE;
783:     }
784: }
785: 
786: /**
787:  * Gets the number of all subalbums of all subalbum levels of either the current album or $albumobj
788:  *
789:  * @param object $albumobj Optional album object to check
790:  * @param string $pre Optional text you want to print before the number
791:  * @return bool
792:  */
793: function getNumAllSubalbums($albumobj, $pre = '') {
794:     global $_zp_gallery, $_zp_current_album;
795:     if (is_null($albumobj)) {
796:         $albumobj = $_zp_current_album;
797:     }
798:     $count = '';
799:     $albums = getAllAlbums($_zp_current_album);
800:     if (count($albums) != 0) {
801:         $count = '';
802:         foreach ($albums as $album) {
803:             $count++;
804:         }
805:         return $pre . $count;
806:     } else {
807:         return false;
808:     }
809: }
810: ?>