Jump to:

7940 Posts in 1543 Topics by 946 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Random order DataObject front end

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1 2
Go to End
Author Topic: 4897 Views
  • pinkp
    Avatar
    Community Member
    168 Posts

    Random order DataObject front end Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random order DataObject front end Link to this post

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

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

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Random order DataObject front end Link to this post

    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
    {
    }
    ?>

  • mattclegg
    Avatar
    Community Member
    56 Posts

    Re: Random order DataObject front end Link to this post

    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)

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: Random order DataObject front end Link to this post

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

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Random order DataObject front end Link to this post

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

  • ayyurek
    Avatar
    Community Member
    41 Posts

    Re: Random order DataObject front end Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Random order DataObject front end Link to this post

    ^ what he said.

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

    4897 Views
Page: 1 2
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.