Version: current

      1 <?php
      2 /**
      3  * Виджет "Текстовые анонсы"
      4  * Позволяет выводить список текстовых анонсов в боковой панели сайта
      5  *
      6  * @package widgets
      7  * @author  menedem
      8  */
      9 class TextAnonsWidget extends SidebarWidget {
     10     static $db = array(
     11         'AnonsCount' => 'Int',
     12         'SortType' => 'Varchar',
     13         'SectionLinkTitle' => 'Varchar(200)',
     14     );
     15
     16     // Ссылка на раздел
     17     static $has_one = array(
     18         'SectionLink' => 'SiteTree',
     19     );
     20
     21     static $has_many = array(
     22         'Items' => 'TextAnonsWidget_Item',
     23     );
     24
     25     static $defaults = array(
     26         'AnonsCount' => 2,
     27         'SortType' => 'random',
     28     );
     29
     30     static $sort_types = array(
     31         'random',
     32         'sorted',
     33         'last',
     34     );
     35
     36     static function set_sort_types($types) {
     37         self::$sort_types = $types;
     38     }
     39
     40     function getCMSFields() {
     41         $fields = parent::getCMSFields();
     42         $fields->replaceField('SortType', new DropdownField('SortType', $this->fieldLabel('SortType'), WebylonWidget::get_array_localization('WebylonWidget', 'SortType', self::$sort_types)));
     43         $dom = new DataObjectManager(
     44             $this,
     45             'Items',
     46             'TextAnonsWidget_Item',
     47             null,
     48             null,
     49             "WidgetID = {$this->ID}"
     50         );
     51         $dom->setRelationAutoSetting(true);
     52         $fields->replaceField('Items', $dom);
     53
     54         $parentIDField = new TreeDropdownField('SectionLinkID', $this->fieldLabel('SectionLink'), 'SiteTree');
     55         $fields->replaceField('SectionLinkID', $parentIDField);
     56         $fields->insertAfter($fields->dataFieldByName('SectionLinkTitle'), 'SectionLinkID');
     57
     58         return $fields;
     59     }
     60
     61     function ActiveItems() {
     62         $items = $this->Items('Active = 1');
     63         if ($this->SortType == 'sorted') {
     64             $items->sort('SortOrder', 'ASC');
     65         }
     66         if ($this->SortType == 'random') {
     67             $t = $items->toArray();
     68             shuffle($t);
     69             $items = new DataObjectSet();
     70             foreach($t as $item) {
     71                 $items->push($item);
     72             }
     73         }
     74         return $items->getRange(0, $this->AnonsCount);
     75     }
     76
     77     function SectionLinkData() {
     78         if ($this->SectionLinkID && ($sectionLink = $this->SectionLink()) && $sectionLink->ID) {
     79             return new ArrayData(array(
     80                 'Title' => ($this->SectionLinkTitle ? $this->SectionLinkTitle : $sectionLink->Title),
     81                 'Link' => $sectionLink->Link(),
     82             ));
     83         }
     84         return false;
     85     }
     86 }
     87
     88 class TextAnonsWidget_Item extends WebylonWidget_Item {
     89     static $db = array(
     90         'Text' => 'HTMLText',
     91     );
     92
     93     static $has_one = array(
     94         'Image' => 'Image',
     95     );
     96
     97     function getCMSFields() {
     98         $f = parent::getCMSFields();
     99         // TODO маленький wisiwig редактор (без вставки изобржаений и файлов)
    100         $f->replaceField('Text', new SimpleHTMLEditorField('Text', $this->fieldLabel('Text'), array('h3' => false, 'h4' => false, 'h5' => false)));
    101         return $f;
    102     }
    103 }
    104

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