I am trying to get a random selection of images from all albums on my site to display on the home page. I had some help with the page controller function but I am sure there is a better way to achieve what I am after.
function Pictures() {
$nrtoshow = (0<$this->NumberToShow) ? $this->NumberToShow : $this->defaults["NumberToShow"];
$pictures = DB::query("SELECT f.`ID`, f.`Filename`, f.`Name`, g.`Caption`, s.`URLSegment`, s.`MetaTitle`, s.`ParentID`, a.`AlbumName` FROM `ImageGalleryItem` g LEFT JOIN `SiteTree_Live` s ON g.`ImageGalleryPageID` = s.`ID` LEFT JOIN `File` f ON f.`ID` = g.`ImageID` LEFT JOIN ImageGalleryAlbum a on g.`AlbumID` = a.`ID` WHERE f.`ClassName` = 'ImageGalleryImage' ORDER BY RAND() LIMIT 0,$nrtoshow;");
$thumbnails = new DataObjectSet;
foreach ($pictures as $picture){
$thumbnail = new Image($picture);
$thumbnail = $thumbnail->getFormattedImage( "PaddedImage", $this->ThumbnailWidth, $this->ThumbnailHeight);
$thumbnail->setField("GalleryLink", Director::baseURL().$picture["URLSegment"]."/album/".str_replace(" ", "-", strtolower($picture["AlbumName"])));
$thumbnail->setField("Name", $picture["Name"]);
$thumbnail->setField("Title", "Go to the".$picture["MetaTitle"]." - ".$picture["AlbumName"]."album");
$thumbnails->push($thumbnail);
}
unset($pictures);
return $thumbnails;
}
ThumbnailWidth, ThumbnailHeight and NumberToShow are fields that I have added to the page type.
The code above works for the most part although sometimes the thumbnails displayed are not the same as the GalleryLink and Title that is attached to the thumbnail.
Any assistance in correcting/improving this code would be greatly appreciated.
Thanks
David