Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Get 5 random images from 3 latest galleries


Reply


4 Posts   1655 Views

Avatar
jezikk

Community Member, 10 Posts

23 June 2009 at 9:01pm

Hi folks,

I have a problem with getting some random images from the latest galleries.
I would like to have in my GalleryHolder template shown structure as this:
<h2>Gallery Title no 1</h2>
<img no=1>
...
...
<img no=5>
<p>$Content description</p>

<h2>Gallery Title no 2</h2>
<img no=1>
...
...
<img no=5>
<p>$Content description</p>

etc....

I made some code in my GalleryHolder.php

function LatestGallery($limit=3, $limitIMG=5) {

$sqlQuery = new SQLQuery();
$sqlQuery->select = array("Gallery.ID AS GalleryID, Gallery.FolderID, File.FileName,SiteTree_Live.URLSegment, SiteTree_Live.Title");
$sqlQuery->from = array("GalleryPage Gallery
LEFT JOIN SiteTree_Live ON SiteTree_Live.ID = Gallery.ID
LEFT JOIN File ON File.ID = Gallery.FolderID");
$sqlQuery->where = array();
$sqlQuery->orderby = "Gallery.ID DESC";
$sqlQuery->limit = $limit;
$rawSQL = $sqlQuery->sql();
$result = $sqlQuery->execute();

$doSet = new DataObjectSet();

foreach($result as $k => $row) {
$FolderID = $row['FolderID'];
$Filename = $row['FileName'];
$URL = $row['URLSegment'];
$Title = $row['Title'];
$sqlQuery2 = new SQLQuery();
$sqlQuery2->select = array("*");
$sqlQuery2->from = array("File");
$sqlQuery2->where = array("ParentID=$FolderID");
$sqlQuery2->orderby = "RAND()";
$sqlQuery2->limit = $limitIMG;
$rawSQL2 = $sqlQuery2->sql();
$result2 = $sqlQuery2->execute();

$ArraySet['URL'] = $URL;
$ArraySet['Title'] = $Title;
foreach($result2 as $a => $v) {
$ArraySet["IMG_$a"] = $v['Filename'];

}
$doSet->push(new ArrayData($ArraySet));
}

return $doSet;
}

In my template I use <% control LatestGallery %> to get access to this DataObject.
However, I have one big problem, and I cannot find a nice solution:
- in my template I have an url to the original image, but how to manage thumbnail creation from the template. I would like to have image 120x80px shown in my template instead of original image 800x600px.

My environment: SS 2.3.2 + Gallery Module.

thx in advance

Avatar
UncleCheese

Forum Moderator, 4096 Posts

24 June 2009 at 2:31am

Wow. This is way too much code for what you're trying to do. Use the ORM that Silverstripe gives you! This stuff is totally unnecessary:

$sqlQuery = new SQLQuery();
$sqlQuery->select = array("Gallery.ID AS GalleryID, Gallery.FolderID, File.FileName,SiteTree_Live.URLSegment, SiteTree_Live.Title");
$sqlQuery->from = array("GalleryPage Gallery
LEFT JOIN SiteTree_Live ON SiteTree_Live.ID = Gallery.ID
LEFT JOIN File ON File.ID = Gallery.FolderID");

I don't know much about the Gallery module, but this is much easier to do with the ImageGallery module, which offers more robust uploading and management of your images with a more flexible API for what you're trying to do. Read more in the DataObjectManager forum.

Avatar
jezikk

Community Member, 10 Posts

24 June 2009 at 2:48am

I decided to write an exact SQL as the DataObject give too much excess, especially when I have more the 100 galleries and ~10000 u/u a day.

Is the a way to migrate galleries from old module Gallery to new ImageGallery ?
Is the ImageGallery lighter than the old one ?
Is the a simple way to do the task as I mentioned in my previous post.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

24 June 2009 at 9:08am

Yes, yes, and yes. The big difference between the ImageGallery and Gallery modules is that ImageGallery allows a gallery to have many albums. So you can manage all of your photos in one place, only sectioned into albums.

Give it a try at http://dataobjectmanager.carlinowebdesign.com

user: admin
pass: password