Combined Random & Featured Photo

dilligaf
dilligaf's picture

Joined: 2003-02-20
Posts: 9
Posted: Thu, 2003-03-13 01:39

I combined the random photo block with Beckett's featured photo block.
For the random part I changed the link for the picture to reflect the album link not the picture link as I was getting to many $id=unknown errors.
Also add a little piece of code to give the total count of photos in the Gallery.
If you can make it better please do.
Change http://www.youraddress.com/ to the URL pointing to your Gallery and install the random block and featured block as per their instructions
<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class="pn-sub">Code:</font><HR></TD></TR><TR><TD><FONT class="pn-sub"><PRE>
<?php

/************************************************************************/
/* PHP-NUKE: Web Portal System */
/* =========================== */
/* */
/* Copyright (c) 2002 by Francisco Burzi */
/* http://phpnuke.org */
/* */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License. */
/************************************************************************/
/*
* Featured photo block for Gallery
* Beckett Madden-Woods (beckett@beckettmw.com)
*
* This is basically block-random.php from Gallery
* Original author and copyright:
*
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2001 Bharat Mediratta
*
* Find the original GNU GPL license at http://gallery.sourceforge.net/
*/
if (eregi("block-random.php",$PHP_SELF)) {
Header("Location: index.php");
die();
}

// Hack prevention.
if (!empty($HTTP_GET_VARS["GALLERY_BASEDIR"]) ||
!empty($HTTP_POST_VARS["GALLERY_BASEDIR"]) ||
!empty($HTTP_COOKIE_VARS["GALLERY_BASEDIR"])) {
$content= "Security violation";
exit;
}

$GALLERY_BASEDIR = "modules/Gallery/";

require($GALLERY_BASEDIR . "init.php");

$albumDB = new AlbumDB(FALSE);
$gallery->session->albumName = "";
$page = 1;

/* If there are albums in our list, display them in the table */
$numAlbums = $albumDB->numAlbums($gallery->user);
$numPhotos = $albumDB->getCachedNumPhotos($gallery->user);

if ($profile) {
$timer = time();
}

/* Initializing the seed */
srand ((double) microtime() * 1000000);

define(FEATURE_CACHE, $gallery->app->albumDir . "/featured-photo.cache");
define(CACHE_FILE, $gallery->app->albumDir . "/block-random.cache");
define(CACHE_EXPIRED, 86200);
//define(CACHE_EXPIRED, 20);

// Check the cache file to see if it's up to date
$rebuild = 1;
if (fs_file_exists(CACHE_FILE)) {
$stat = fs_stat(CACHE_FILE);
$mtime = $stat[9];
if (time() - $mtime < CACHE_EXPIRED) {
$rebuild = 0;
}
}

if ($rebuild) {
scanAlbums();
saveCache();
} else {
readCache();
}

$album = chooseAlbum();

if ($album) {
$index = choosePhoto($album);
}

if (isset($index)) {
$id = $album->getPhotoId($index);
$url = "http://www.youraddress.com/";
$url .= "modules.php?set_albumName=".$album->fields["name"];
$url .= "&amp;op=modload&amp;name=Gallery&amp;file=index&amp;include=view_album.php";
echo "<center><b>Random Photo</b><br> <br>"
."<a href="$url">"
//."<a href=" .makeAlbumUrl($album->fields["name"], $id) .">"
.$album->getThumbnailTag($index)
."</a></center>";
$caption = $album->getCaption($index);
if ($caption) {
echo "<br><center>$caption</center>";
}
$albumURL = "http://www.youraddress.com/";
$albumURL .= "modules.php?set_albumName=".$album->fields["name"];
$albumURL .= "&amp;op=modload&amp;name=Gallery&amp;file=index&amp;include=view_album.php";
$albumNO .= " " . pluralize($numPhotos, "photo", "no");
echo "<br><center>From: "
//."<a href=" .makeAlbumUrl(currentAlbum) .">"
."<a href="$albumURL">"
.$album->fields["title"]
."</a></center><hr size="5">";

} else {
$content = "No photo chosen.";
}

if ($set) {

if ($gallery->session->albumName &amp;&amp; isset($index)) {
if ($fd = fs_fopen(FEATURE_CACHE, "w")) {
fwrite($fd, $gallery->session->albumName . "/$index");
fclose($fd);
print "New featured photo saved.
</html>";
}
else {
print "<span class="error">Error: Could not open cache file"
. "</span>";
}
}
else {
print "<span class="error">Error: Invalid parameters.</span>";
}
print "<br /><br /><form>";
print "<input type="submit" value="Close" onclick='parent.close()'>";
print "</form>";
print "</html>";
exit;
}
else {
/* read in from cache file */
if ($fd = fs_fopen(FEATURE_CACHE, "r")) {
while ($line = fgets($fd, 4096)) {
list($albumName, $index) = explode("/", $line);
}
fclose($fd);
}

if ($albumName) {
$album = new Album();
$album->load($albumName);
}
if ($album &amp;&amp; $index) {
$id = $album->getPhotoId($index);
$PHOTO_URL = "http://www.youraddress.com/";
$PHOTO_URL .= "modules.php?set_albumName=".$album->fields["name"];
$PHOTO_URL .= "&amp;op=modload&amp;name=Gallery&amp;file=index&amp;include=view_album.php";
$IMG = "<a href="$PHOTO_URL">" . $album->getThumbnailTag($index) . "</a>";
$CAPTION = strtolower($album->getCaption($index));
$FROM = "From: <a href="$PHOTO_URL">" . $album->fields["title"] . "</a>";

print "<center><b>Featured Photo</b></center><br>";
print "<div id="photo">";
print "$IMG";
print "</div>";

print "<div id="caption">";
print "$CAPTION<br />";
print "$FROM";
print "</div>";
print "<hr><center><b>$albumNO online</b></center>";
}
else {
print "<div id="feature_caption">";
print "No featured photo.";
print "</div>";
}
}
/*
* --------------------------------------------------
* Support functions
* --------------------------------------------------
*/

function saveCache() {
global $cache;
if ($fd = fs_fopen(CACHE_FILE, "w")) {
foreach ($cache as $key => $val) {
fwrite($fd, "$key/$val");
}
fclose($fd);
}
}

function readCache()
{
global $cache;
if ($fd = fs_fopen(CACHE_FILE, "r")) {
while ($line = fgets($fd, 4096)) {
list($key, $val) = explode("/", trim($line));
$cache[$key] = $val;
}
fclose($fd);
}
}

function choosePhoto($album) {
global $cache;

$count = $cache[$album->fields["name"]];
if ($count == 0) {
// Shouldn't happen
return null;
} else if ($count == 1) {
$choose = 1;
} else {
$choose = rand(1, $count);
$wrap = 0;
if ($album->isHidden($choose)) {
$choose++;
if ($choose > $album->numPhotos(1)) {
$choose = 1;
$wrap++;

if ($wrap = 2) {
return null;
}
}
}
}

return $choose;
}

function chooseAlbum() {
global $cache;

/*
* The odds that an album will be selected is proportional
* to the number of (visible) items in the album.
*/

$total = 0;
foreach ($cache as $name => $count) {
if (!$choose) {
$choose = $name;
}

$total += $count;
if ($total != 0 &amp;&amp; ($total == 1 || rand(1, $total) <= $count)) {
$choose = $name;
}
}

if ($choose) {
$album = new Album();
$album->load($choose);
return $album;
} else {
return null;
}
}

function scanAlbums() {
global $cache;
global $gallery;

$cache = array();
$everybody = $gallery->userDB->getEverybody();
$albumDB = new AlbumDB();
foreach ($albumDB->albumList as $tmpAlbum) {
if ($everybody->canReadAlbum($tmpAlbum)) {
$seeHidden = $everybody->canWriteToAlbum($tmpAlbum);
$numPhotos = $tmpAlbum->numPhotos($seeHidden);
$name = $tmpAlbum->fields["name"];
if ($numPhotos > 0) {
$cache[$name] = $numPhotos;
}
}
}
}

?>
</TD></TR></TABLE><!-- BBCode End -->