Version: current

      1 <?php
      2 // Баннеры на сайте (вместо анонсов)
      3 class BannerWidget extends SidebarWidget {
      4     static $db = array(
      5         'BannerCount' => 'Int',
      6         'SortType' => 'Varchar', //условно
      7         'SectionLinkTitle' => 'Varchar(200)',
      8     );
      9
     10     // Ссылка на раздел
     11     static $has_one = array(
     12         'SectionLink' => 'SiteTree',
     13     );
     14
     15     static $has_many = array(
     16         'Items' => 'BannerWidget_Item'
     17     );
     18
     19     static $defaults = array(
     20         'BannerCount' => 2,
     21     );
     22
     23     static $sort_types = array(
     24         'sorted',
     25         'random',
     26     );
     27
     28     static function set_sort_types($types) {
     29         self::$show_types = $types;
     30     }
     31
     32     function getCMSFields() {
     33         $fields = parent::getCMSFields();
     34         $fields->replaceField('SortType', new DropdownField('SortType', $this->fieldLabel('SortType'), WebylonWidget::get_array_localization('WebylonWidget', 'SortType', self::$sort_types)));
     35
     36         $dom = new ImageDataObjectManager(
     37             $this,
     38             'Items',
     39             'BannerWidget_Item',
     40             null,
     41             null,
     42             null,
     43             "WidgetID = {$this->ID}"
     44         );
     45         $dom->setRelationAutoSetting(true);
     46         $previewFields = new LiteralField("css", "<style>div#Root_Main.tab {height: 80%}</style>");
     47         $dom->setPreviewFieldFor($previewFields);
     48         $fields->replaceField('Items', $dom);
     49
     50         $parentIDField = new TreeDropdownField('SectionLinkID', $this->fieldLabel('SectionLink'), 'SiteTree');
     51         $fields->replaceField('SectionLinkID', $parentIDField);
     52         $fields->insertAfter($fields->dataFieldByName('SectionLinkTitle'), 'SectionLinkID');
     53
     54         return $fields;
     55     }
     56
     57     function BannerList() {
     58         $items = $this->Items('Active = 1');
     59         if ($this->SortType == 'sorted') {
     60             $items->sort('SortOrder', 'ASC');
     61         }
     62         if ($this->SortType == 'random') {
     63             $t = $items->toArray();
     64             shuffle($t);
     65             $items = new DataObjectSet();
     66             foreach($t as $item) {
     67                 $items->push($item);
     68             }
     69         }
     70         return $items->getRange(0, $this->BannerCount);
     71     }
     72
     73     function SectionLinkData() {
     74         if ($this->SectionLinkID && ($sectionLink = $this->SectionLink()) && $sectionLink->ID) {
     75             return new ArrayData(array(
     76                 'Title' => ($this->SectionLinkTitle ? $this->SectionLinkTitle : $sectionLink->Title),
     77                 'Link' => $sectionLink->Link(),
     78             ));
     79         }
     80         return false;
     81     }
     82 }
     83
     84 class BannerWidget_Item extends WebylonWidget_Item {
     85     static $has_one = array(
     86         'Image' => 'Image',
     87     );
     88
     89     function getCMSFields() {
     90         $fields = parent::getCMSFields();
     91
     92         $fields->removeByName('Image');
     93         if ($this->ImageID && $this->Image()->ID) {
     94             $tab = $fields->findOrMakeTab('Root.Image', $this->fieldLabel('Image'));
     95             $URL = $this->Image()->SetHeight(200)->URL;
     96             $tab->push(new LiteralField("icon", "<div class='current-image'><img src='$URL' alt='$this->Image()->Filename' title='$this->Image()->Filename'  /></div>"));
     97         }
     98         return $fields;
     99     }
    100 }

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