Version: current

     1 <?php
     2 /**
     3  * Виджет "Блок новостей"
     4  * Позволяет разместить в боковой колонке блок новостей
     5  *
     6  * @package widgets
     7  * @author  menedem
     8  */
     9 class NewsSidebarWidget extends SidebarWidget {
    10     static $db = array(
    11         'NewsCount' => 'Int',
    12         'ShowOnlyImportant' => 'Boolean',
    13     );
    14
    15     static $defaults = array(
    16         'NewsCount' => 2,
    17     );
    18
    19     static $has_one = array(
    20         'Holder' => 'NewsHolder'
    21     );
    22
    23     public $needObjects = array(
    24         'NewsHolder' => 'Page'
    25     );
    26
    27     function getCMSFields() {
    28         if (!$this->canCreate()) {
    29             return $this->getFailCMSFields();
    30         }
    31
    32         $fields = parent::getCMSFields();
    33         $holders = DataObject::get('NewsHolder');
    34         if ($holders) {
    35             $holders = $holders->map();
    36         } else {
    37             $holders = array();
    38         }
    39         $fields->replaceField('HolderID', new DropdownField("HolderID", $this->fieldLabel('Holder'), $holders, "", null, _t('Widgets.All', 'All')));
    40
    41         return $fields;
    42     }
    43
    44     function NewsList() {
    45         if ($this->HolderID && $this->Holder()) {
    46             return $this->Holder()->Entries(0, $this->NewsCount, $this->ShowOnlyImportant);
    47         }
    48         $where = "Status LIKE '%Published%'";
    49         if ($this->ShowOnlyImportant) {
    50             $where .= " AND Important = 1";
    51         }
    52         return DataObject::get('NewsEntry', $where, "Date DESC", '', $this->NewsCount);
    53     }
    54
    55     function hasContent() {
    56         if ($this->NewsList()) {
    57             return true;
    58         }
    59         return false;
    60     }
    61
    62     function getMoreLink() {
    63         if ($this->HolderID && $this->Holder()) {
    64             return $this->Holder()->Link();
    65         }
    66         if ($holder = DataObject::get_one('NewsHolder')) {
    67             return $holder->Link();
    68         }
    69         return false;
    70     }
    71
    72 }
    73
    74

[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. -