Quote:
>>
One more thing, this block WILL NOT work in conjunction with the Random Photo Block. I haven't figured out how to display them both at once yet. You'll get an error. If ANYONE can figure out how to eliminate this error and have both blocks displayed at the same time, please email me at
perfectcell@perfectcell.org
Let me know how it works :grin:<<
Yes it will work with the random photo block. I just got it going combined with the random photo and Beckett's featured photo into one large block.
It can be seen working at <!-- BBCode Start --><A HREF="http://www.dilligaf.to/hobbies" TARGET="_blank">http://www.dilligaf.to/hobbies</A><!-- BBCode End -->.
Here is the complete code. Just make sure to change YOUR_DOMAIN and YOUR_GALLERY_DIRECTORY to their proper values. And add last-updated.cache to your album directory.
<!-- 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/YOUR_GALLERY_DIRECTORY/";
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(CACHEUP_FILE, $gallery->app->albumDir . "/last-updated.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();
scanAlbumsup();
saveCacheup();
readCacheup();
} else {
readCache();
readCacheup();
}
$album = chooseAlbum();
if ($album) {
$index = choosePhoto($album);
}
if (isset($index)) {
$id = $album->getPhotoId($index);
$url = "http://www.YOUR_DOMAIN.com/";
$url .= "modules.php?set_albumName=".$album->fields["name"];
$url .= "&op=modload&name=YOUR_GALLERY_DIRECTORY&file=index&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.YOUR_DOMAIN.com/";
$albumURL .= "modules.php?set_albumName=".$album->fields["name"];
$albumURL .= "&op=modload&name=YOUR_GALLERY_DIRECTORY&file=index&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 && 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 && $index) {
$id = $album->getPhotoId($index);
$PHOTO_URL = "http://www.YOUR_DOMAIN.com/";
$PHOTO_URL .= "modules.php?set_albumName=".$album->fields["name"];
$PHOTO_URL .= "&op=modload&name=YOUR_GALLERY_DIRECTORY&file=index&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>Last 5 Updated Galleries</b><br><br></center>
";
}
else {
print "<div id="feature_caption">
";
print "No featured photo.
";
print "</div>
";
}
}
/*
* this code displays the last 5 updated albums
* according to the last-updated.cache file - may be a few hours etc out of date
*/
global $cacheName;
global $cacheDate;
global $cacheRealName;
global $albumName;
global $gallery;
$albumDB = new AlbumDB();
$gallery->session->albumName = "";
$numAlbums = $albumDB->numAlbums($gallery->user);
$numPhotos = $albumDB->numPhotos($gallery->user);
// for loop reads the data from the arrays and renders it on screen
for ($i = 0; $i <= 4; $i++) {
$albumupURL = "http://www.YOUR_DOMAIN.com/";
$albumupURL .= "modules.php?set_albumName=" . $cacheName[$i];
$albumupURL .= "&op=modload&name=YOUR_GALLERY_DIRECTORY&file=index&include=view_album.php";
print "<a href=" . $albumupURL . ">" . $cacheRealName[$i] . "</a>"
."<br><font class="tiny"> updated: " . $cacheDate[$i] . "<br><br></font>";
}
print "<hr><center><b>$albumNO online</b></center>
";
/*
* --------------------------------------------------
* Support functions
* --------------------------------------------------
*/
function saveCacheup() {
/*
* this function saves the last 5 updated albums into the cache file
* its reads the data straight from the arrays
*/
global $dateArray;
global $nameArray;
global $albumDate;
global $albumName;
reset($dateArray);
reset($nameArray);
reset($albumDate);
reset($albumName);
if ($fd = fs_fopen(CACHEUP_FILE, "w")) {
$currentKey = key($dateArray);
fwrite($fd, "$nameArray[$currentKey]/$albumName[$currentKey]/$albumDate[$currentKey]
");
for ($i = 0; $i <= 3; $i++) {
$current = next($dateArray);
$currentKey = key($dateArray);
fwrite($fd, "$nameArray[$currentKey]/$albumName[$currentKey]/$albumDate[$currentKey]
");
}
fclose($fd);
}
}
function readCacheup() {
/*
* this function reads in the 5 last updated albums from the cache file
* this information, that it reads in, can be upto 10 minutes out of date
*/
global $cacheName;
global $cacheDate;
global $cacheRealName;
$cacheName = array();
$cacheDate = array();
$cacheRealName = array();
$i = 0;
if ($fd = fs_fopen(CACHEUP_FILE, "r")) {
while ($line = fgets($fd, 4096)) {
list($cacheName[$i], $cacheRealName[$i], $cacheDate[$i]) = explode("/", $line);
$i++;
}
fclose($fd);
}
}
function scanAlbumsup() {
/*
* this function scans through the albums and loads the arrays with information
* this is the time consuming section. (3secs - worst case, mostly 1-2 secs)
*/
global $dateArray;
global $nameArray;
global $albumDate;
global $albumName;
global $gallery;
$dateArray = array();
$nameArray = array();
$albumDate = array();
$albumName = array();
/* Read the album list */
$albumDB = new AlbumDB();
$gallery->session->albumName = "";
/* If there are albums in our list, display them in the table */
$numAlbums = $albumDB->numAlbums($gallery->user);
if (!$gallery->session->albumListPage) {
$gallery->session->albumListPage = 1;
}
$start = ($gallery->session->albumListPage - 1) * 3 + 1;
$end = $numAlbums;
for ($i = $start; $i <= $end; $i++) {
$gallery->album = $albumDB->getAlbum($gallery->user, $i);
$isRoot = $gallery->album->isRoot(); // Only display album if it is a root album
if($isRoot) {
$tmpAlbumName = $gallery->album->fields["name"];
$modDate = $gallery->album->getLastModificationDate();
// here we use unix timestamps for date storage - seems the easiest way to sort later
$modDate = strtotime ($modDate);
$nameArray[$i] = $tmpAlbumName;
$dateArray[$i] = $modDate;
$albumDate[$i] = $gallery->album->getLastModificationDate();
$albumName[$i] = editField($gallery->album, "title", $edit);
// sort the date array so that we can use it for reference later
arsort ($dateArray);
} // if($isRoot) end
} // for () end
}
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 && ($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 -->