Version: current

      1 <?php
      2
      3 /**
      4  * Виджет Карусель (ex "Информационный блок")
      5  * Показывает набор рекламных блоков, с автоматической или ручной сменой
      6  *
      7  * @package widgets
      8  * @author  menedem
      9  */
     10 class CarouselHomepageWidget extends HomepageWidget {
     11     static $db = array(
     12         'Text' => 'HTMLText',
     13         'ShowNavigation' => 'Boolean',
     14         'AnimationType' => 'Varchar',
     15         'DelayTime' => 'Int',
     16         'SectionLinkTitle' => 'Varchar(200)',
     17     );
     18
     19     // Ссылка на раздел
     20     static $has_one = array(
     21         'SectionLink' => 'SiteTree',
     22     );
     23
     24     static $has_many = array(
     25         'Items' => 'CarouselHomepageWidget_Item'
     26     );
     27
     28     /**
     29      * Набор типов анимации Карусели
     30      */
     31     static $animation_types = array(
     32         'manualone',
     33         'manualgroup',
     34         'rotateone',
     35         'rotategroup',
     36         'shadegroup',
     37         'random',
     38         'showall',
     39     );
     40
     41     /**
     42      * Установка набора типов анимации Карусели
     43      *
     44      * @param array $types
     45      */
     46     static function set_animation_types($types = array()) {
     47         if (!is_array($types)) $types = array($types);
     48         self::$animation_types = $types;
     49     }
     50
     51     function getCMSFields() {
     52         $fields = parent::getCMSFields();
     53         if (count(self::$animation_types) == 0) {
     54             $fields->removeByName('AnimationType');
     55             $fields->removeByName('DelayTime');
     56         } elseif (count(self::$animation_types) == 1) {
     57             $fields->replaceField('AnimationType', new HiddenField('AnimationType', '', self::$animation_types[0]));
     58             $fields->addFieldToTab('Root.Main', new HeaderField('AnimationSettings', _t('CarouselHomepageWidget.AnimationSettings')), 'AnimationType');
     59         } else {
     60             $fields->addFieldToTab('Root.Main', new HeaderField('AnimationSettings', _t('CarouselHomepageWidget.AnimationSettings')), 'AnimationType');
     61             $fields->replaceField('AnimationType', new DropdownField('AnimationType', $this->fieldLabel('AnimationType'), WebylonWidget::get_array_localization($this->class, 'AnimationType', self::$animation_types)));
     62         }
     63
     64         $fields->dataFieldByName('Text')->setRows(20);
     65
     66         $dom = new DataObjectManager(
     67             $this,
     68             'Items',
     69             'CarouselHomepageWidget_Item',
     70             null,
     71             null,
     72             "WidgetID = {$this->ID}"
     73         );
     74         $dom->setRelationAutoSetting(true);
     75         $fields->replaceField('Items', $dom);
     76
     77         $parentIDField = new TreeDropdownField('SectionLinkID', $this->fieldLabel('SectionLink'), 'SiteTree');
     78         $fields->replaceField('SectionLinkID', $parentIDField);
     79         $fields->insertAfter($fields->dataFieldByName('SectionLinkTitle'), 'SectionLinkID');
     80
     81         return $fields;
     82     }
     83
     84     function ActiveItems() {
     85         return $this->Items('Active = 1');
     86     }
     87
     88     function SectionLinkData() {
     89         if ($this->SectionLinkID && ($sectionLink = $this->SectionLink()) && $sectionLink->ID) {
     90             return new ArrayData(array(
     91                 'Title' => ($this->SectionLinkTitle ? $this->SectionLinkTitle : $sectionLink->Title),
     92                 'Link' => $sectionLink->Link(),
     93             ));
     94         }
     95         return false;
     96     }
     97
     98     /**
     99      * Переводит время задержки в миллисекунды
    100      *
    101      * @return int
    102      */
    103     function DelayTimeMS() {
    104         return $this->DelayTime * 1000;
    105     }
    106
    107 }
    108
    109 class CarouselHomepageWidget_Item extends WebylonWidget_Item {
    110     static $db = array(
    111         'Text' => 'HTMLText',
    112     );
    113
    114     static $has_one = array(
    115         'Image' => 'Image',
    116     );
    117
    118     function getCMSFields() {
    119         $f = parent::getCMSFields();
    120         // TODO маленький wisiwig редактор (без вставки изображений и файлов)
    121         $f->replaceField('Text', new SimpleHTMLEditorField('Text', $this->fieldLabel('Text'), array('h3' => false, 'h4' => false, 'h5' => false)));
    122         return $f;
    123     }
    124 }
    125

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