Version: current

     1 <?php
     2
     3 class PhotoAlbumHomepageWidget extends HomepageWidget {
     4
     5     static $db = array(
     6         'AlbumCount' => 'Int',
     7     );
     8
     9     static $many_many = array(
    10         'LinkedAlbums' => 'PhotoAlbumPage',
    11     );
    12
    13     public $needObjects = array(
    14         'PhotoAlbumPage' => 'Page',
    15     );
    16
    17     function getCMSFields() {
    18         $fields = parent::getCMSFields();
    19         $ctf = new ManyManyComplexTableField(
    20             $this,
    21             'LinkedAlbums',
    22             'PhotoAlbumPage',
    23             array('Title' => _t('PhotoAlbumHomepageWidget.AlbumTitle', 'Альбом'))
    24         );
    25         $ctf->setPermissions(array());
    26         $fields->addFieldToTab('Root.LinkedAlbums', $ctf);
    27
    28         return $fields;
    29     }
    30
    31     function Albums() {
    32         if ($this->hasContent()) {
    33             //return random albums
    34             $albumIDs = $this->LinkedAlbums()->map('ID', 'ID');
    35             shuffle($albumIDs);
    36             $albums = new DataObjectSet();
    37             foreach($albumIDs as $albumID) {
    38                 $albums->push($this->LinkedAlbums()->find('ID', $albumID));
    39                 if (($this->AlbumCount > 0) && ($albums->Count() >= $this->AlbumCount)) {
    40                     break;
    41                 }
    42             }
    43             return $albums;
    44         }
    45         return false;
    46     }
    47
    48     function hasContent() {
    49         return ($this->LinkedAlbums()->Count()) ? true : false;
    50     }
    51 }
    52
    53

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -