I was having problems with the random image header crapping out when a sub-album had only child albums and no pictures of it's own. The
original getRandomImagesAlbum() is built on a very long SQL-query which I couldn't quite wrap my head around.
Thus I set out to rebuild and improve upon it, and this is my solution. It'll select 1 random image from the current dir or any of it's subdirs - and is not limited by the depth of the directory tree.
EDIT:Sorry. BBPress pissed the code to pieces due to all SQL-backticks. I'll fix it and get it back up soon.
EDIT2:Backticks removed from the SQL-queries. Still looks like crap though. Nothing I can do about I'm afraid, sorry.
`/*
Returns the ID of all sub-albums, relative to the current album. If $_zp_current_album is not set, it'll return null.*/`
`
function getAllSubAlbumIDs()
{
global $_zp_current_album;
if (!isset($_zp_current_album)) { return null; }
$query = "SELECT id FROM " . prefix('albums') . " WHERE folder LIKE '" . $_zp_current_album->getFolder() . "%'";
$subIDs = query_full_array($query);
return $subIDs;
}`
`
/* Returns an Image-object, randomly selected from current directory or any of it's subdirectories.
returns null if $_zp_current_album isn't set, and if no images can be found. You might want it to fall back on a
placeholder image instead. */`
`
function getRandomImagesAlbum() {
$images;
$subIDs = getAllSubAlbumIDs($rootAlbum);
if($subIDs == null) {return null;}; //no subdirs avaliable
foreach ($subIDs as $ID)
{
$query = 'SELECT id , albumid , filename , title FROM images WHERE albumid = "'. $ID['id'] .'"';
$images = array_merge($images, query_full_array($query));
}
if(count($images) < 1){return null;}; //no images avaliable in _any_ subdirectory
$randomImage = $images[array_rand($images)];
$folderPath = query_single_row("SELECT folder FROM " .prefix('albums'). " WHERE id = '" .$randomImage['albumid']. "'" );
$image = new Image(new Album(new Gallery(), $folderPath['folder']), $randomImage['filename']);
return $image;
}
`
Comments
`$query = 'SELECT id , albumid , filename , title FROM ' .prefix('images'). ' WHERE albumid = "'. $ID['id'] .'"';`
Is what line 60 should say.