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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Random order DataObject front end


Go to End


11 Posts   9866 Views

Avatar
pinkp

Community Member, 182 Posts

21 June 2010 at 11:44am

Is it possible to display the DataObjects in a random order on the page?
I've read how to pull a single random object but I would just like the order of the li's to be random each time the page is visited. Would it involve the sort order or a new function?

Thanks

Avatar
UncleCheese

Forum Moderator, 4102 Posts

21 June 2010 at 11:54am

Edited: 21/06/2010 11:54am

Nope.. just use RAND() as your sort clause.

return $this->SomeObjects(null, RAND());

Avatar
pinkp

Community Member, 182 Posts

21 June 2010 at 12:18pm

sorry to be a pain but where do I place this in my php... I'm getting errors.. thnks

$this->Slideshows(null, RAND());

SlideshowPage.php

<?php
class SlideshowPage extends Page
{
	
	 static $icon = "slideshow/images/treeicons/slideshow";

	
	 static $db = array(
	 		'ShWidth'  => 'Int',
	 		'ShSpeed'  => 'Int',
	 		'ShSpeed2'  => 'Int',
			'ShFadeSpeed'  => 'Int',
			'ShFadeSpeed2'  => 'Int',
			'ShPosition' => "Varchar(100)",
			'ShVerticlePositionBottom' => 'Boolean',
			'BackGroundColor' => "Varchar(6)",


   );
	
	static $defaults = array (
		'ShWidth' => '300',
		'ShSpeed' => '2000',
		'ShFadeSpeed' => '1000',
		'ShSpeed2' => '2000',
		'ShFadeSpeed2' => '1000'
	);
	
	static $has_many = array (
		'Slideshows' => 'Slideshow'
	);
	
	public function getCMSFields()
	{
		$f = parent::getCMSFields();
		
	    $f->addFieldToTab("Root.Content.SlideshowConfiguration", new NumericField('ShWidth','Slideshow Width (pixels)'));
	    $f->addFieldToTab("Root.Content.SlideshowConfiguration", new NumericField('ShSpeed','Slide Duration (sec) i.e. "2000 = 2 sec"'));
	    $f->addFieldToTab("Root.Content.SlideshowConfiguration", new NumericField('ShFadeSpeed','Transition / Fade Duration i.e. "1 = Blink of an Eye 1000 = Steady"'));
		 $f->addFieldToTab("Root.Content.SlideshowConfiguration", new NumericField('ShSpeed2','Right Slide Duration (sec) i.e. "2000 = 2 sec"'));
	    $f->addFieldToTab("Root.Content.SlideshowConfiguration", new NumericField('ShFadeSpeed2','Right Transition / Fade Duration i.e. "1 = Blink of an Eye 1000 = Steady"'));
		
		 $array = array(
      "margin-right: auto; margin-left: auto; clear: both;" => "Centre",
      "float: left;" => "Left with Content Wrapped",
      "float: right;" => "Right with Content Wrapped"
);
		   
  $f->addFieldToTab("Root.Content.SlideshowConfiguration", new DropdownField(
  'ShPosition',
  'Horizontal Image Position',
  $array
)); 
	  
    $f->addFieldToTab("Root.Content.SlideshowConfiguration", new CheckBoxField('ShVerticlePositionBottom','Position image under content (otherwise image will be above)'));

		
		$manager = new DataObjectManager(
			$this, // Controller
			'Slideshows', // Source name
			'Slideshow', // Source class
			array('ShTitle' => 'Title', 'ShLink' => 'Image Link', 'ShLeftorRight' => 'Left or Right Slideshow', 'Thumbnail' => 'Image'), // Headings
			'getCMSFields_forPopup' // Detail fields function or FieldSet
			// Filter clause
			// Sort clause
			// Join clause
		);

		$manager->setPluralTitle('Slideshow Images');
		$manager->setAddTitle('Slideshow Image');
		
		$f->addFieldToTab("Root.Content.Slideshow", $manager);
		
	    $f->addFieldToTab("Root.Content.Colours", new ColorField('BackGroundColor','Page Background Colour'));

		return $f;

	}

}
class SlideshowPage_Controller extends Page_Controller
{
}
?>

Avatar
mattclegg

Community Member, 56 Posts

7 July 2010 at 10:55am

Edited: 01/06/2011 1:34am

You probably want it inside a function in your controller class. Do you want something like;

class SlideshowPage_Controller extends Page_Controller{
 function MyRandomObject(){
  return DataObject::get('Slideshows',null,RAND());
 }
}

?

#Fixed typo -thanks pinkp (below)

Avatar
Tama

Community Member, 138 Posts

6 September 2010 at 11:51am

I'm trying to work out how to apply this method to some code I'm working on. Basically I'm trying to get CMS Workflow request e-mails to go to 3 random publishers:

$publishers = $this->owner->Page()->PublisherMembers();
// I'd like to randomise $publishers here
// Select 3 publishers
			$publishers = $publishers -> getRange(0, 3);
			foreach($publishers as $publisher) $emailsToSend[] = array($author, $publisher);

Avatar
pinkp

Community Member, 182 Posts

13 May 2011 at 11:04pm

I still can't get this to work, a little help would be appreciated. thank you.

I've added:

class SliderPage_Controller extends Page_Controller{ 
function MyRandomObject(){ 
return DataObject::get('Sliders',null,RAND()'); 
} 
}  

to my

SliderPage.php

It has no effect....

Should it read: return DataObject::get('Sliders','null','RAND()');
?

Although this changes nothing.

I've tried UncleCheeses advise for the Sort Clause

I can't find enough documentation to work this out properly but I asuming it goes here:

   $manager = new DataObjectManager( 
         $this, // Controller 
         'Slideshows', // Source name 
         'Slideshow', // Source class 
         array('ShTitle' => 'Title', 'ShLink' => 'Image Link', 'ShLeftorRight' => 'Left or Right Slideshow', 'Thumbnail' => 'Image'), // Headings 
         'getCMSFields_forPopup' // Detail fields function or FieldSet 
         // Filter clause 
         // Sort clause 
         // Join clause 
      );

but how should it look / read?
like this...? but this does not have any effect.


		$manager = new DataObjectManager(
			$this, // Controller
			'Sliders', // Source name
			'Slider', // Source class
			array('SliderTitle' => 'Title', 'SliderLink' => 'Image Link', 'Thumbnail' => 'Image'), // Headings
			'getCMSFields_forPopup', // Detail fields function or FieldSet
			'',// Filter clause
			'(null, RAND());'// Sort clause
			// Join clause
		);

Avatar
ayyurek

Community Member, 41 Posts

14 May 2011 at 12:41am


class SlideshowPage extends Page
{ 

static $has_many = array (
      'SlideshowItems' => 'SlideshowItem'
   ); 

}

class SlideshowPage_Controller extends Page_Controller{ 


function RandomSlideshowItems {
	return DataObject::get("SlideshowItem", "", "RAND()", "");
    }
}

And call in template

<% control RandomSlideShowItems %>
$Title
<% end_control %>

You are in the wrong place. Put inside the controller of SlideshowPage. It's not related to getCMSFields function. getCMSFields is a function for backend, not frontend.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 May 2011 at 4:06am

^ what he said.

--------------------
SilverStripe tips, tutorials, screencasts and more: http://www.leftandmain.com

Go to Top