Jump to:

7935 Posts in 1536 Topics by 943 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1081 Views
  • Chucky2k
    Avatar
    Community Member
    32 Posts

    Random Images from ImageGalleryPage on another page type Link to this post

    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

  • Chucky2k
    Avatar
    Community Member
    32 Posts

    Re: Random Images from ImageGalleryPage on another page type Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random Images from ImageGalleryPage on another page type Link to this post

    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);

  • Chucky2k
    Avatar
    Community Member
    32 Posts

    Re: Random Images from ImageGalleryPage on another page type Link to this post

    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

    1081 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.