7911 Posts in 1354 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Random Images from ImageGalleryPage on another page type
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 913 Views |
-
Random Images from ImageGalleryPage on another page type

29 May 2010 at 12:11am
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 -
Re: Random Images from ImageGalleryPage on another page type

29 May 2010 at 12:51am
I have since discovered the cause of the mismatched thumbnails and GalleryLink that is retrieved. The original image names that were uploaded all start with the same name, eg: one ImageGalleryPage has several albums with images that all have a file named photo01.jpg. When the thumbnail is being created, not enough of a unique name is being generated and if a thumbnail of the same name already exists it does not get overwritten.
As I said in my previous post I am sure there is a smarter way to do this.
Thanks again.
David -
Re: Random Images from ImageGalleryPage on another page type

29 May 2010 at 2:26am
Try:
$number_of_images = 5;
$gallery = DataObject::get_one("ImageGalleryPage"); // if you have more than one, add a filter here
$items = DataObject::get("ImageGalleryItem","ImageGalleryPageID = {$gallery->ID}","RAND()",null,$number_of_images);
return $gallery->GalleryItems(null,$items); -
Re: Random Images from ImageGalleryPage on another page type

31 May 2010 at 9:39pm
Thanks Uncle Cheese... This works great for one ImageGalleryPage and I can set a filter to choose a gallery, but I was hoping to return random images from all ImageGalleryPages with the function. Is it possible to get this function to do this.
If you have any nudges in the right direction it would be greatly appreciated.
Thanks again
David
| 913 Views | ||
|
Page:
1
|
Go to Top |

