Version: current

      1 <?php
      2
      3 /**
      4  * Виджет "Слайдер"
      5  * Показывает сменяющиеся изображения
      6  *
      7  * @package widgets
      8  * @author  menedem
      9  */
     10 class SliderHomepageWidget extends HomepageWidget {
     11     static $db = array(
     12         'SlideHeight' => 'Int',
     13         'ShowSlideTitle' => 'Boolean',
     14         'TitleFontSize' => 'Int',
     15         'ShowNavigation' => 'Boolean',
     16         'AnimationType' => 'Varchar',
     17         'DelayTime' => 'Int',
     18     );
     19
     20
     21     static $has_many = array(
     22         'Items' => 'SliderHomepageWidget_Item'
     23     );
     24
     25     static $defaults = array(
     26         'ShowSlideTitle' => 0,
     27         'ShowNavigation' => 1,
     28         'DelayTime' => 5,
     29         'SlideHeight' => 300,
     30     );
     31
     32     /**
     33      * Набор типов анимации Слайдера
     34      */
     35     static $animation_types = array(
     36         'horizontal',
     37         'vertical',
     38         'fade',
     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
     54         $sizes = array();
     55         for($i = 8; $i <= 32; $i=$i+2) {
     56             $sizes[$i] = $i;
     57         }
     58         for($i = 36; $i <= 64; $i=$i+4) {
     59             $sizes[$i] = $i;
     60         }
     61         $fields->replaceField('TitleFontSize', new DropdownField('TitleFontSize', $this->fieldLabel('TitleFontSize'), $sizes, null, null, _t('COMMON.default','default')));
     62
     63         if (count(self::$animation_types) == 0) {
     64             $fields->removeByName('AnimationType');
     65             $fields->removeByName('DelayTime');
     66         } elseif (count(self::$animation_types) == 1) {
     67             $fields->replaceField('AnimationType', new HiddenField('AnimationType', '', self::$animation_types[0]));
     68             $fields->addFieldToTab('Root.Main', new HeaderField('AnimationSettings', _t('SliderHomepageWidget.AnimationSettings')), 'AnimationType');
     69         } else {
     70             $fields->addFieldToTab('Root.Main', new HeaderField('AnimationSettings', _t('SliderHomepageWidget.AnimationSettings')), 'AnimationType');
     71             $fields->replaceField('AnimationType', new DropdownField('AnimationType', $this->fieldLabel('AnimationType'), WebylonWidget::get_array_localization($this->class, 'AnimationType', self::$animation_types)));
     72         }
     73
     74         $dom = new ImageDataObjectManager(
     75             $this,
     76             'Items',
     77             'SliderHomepageWidget_Item',
     78             null,
     79             null,
     80             null,
     81             "WidgetID = {$this->ID}"
     82         );
     83         $dom->setRelationAutoSetting(true);
     84
     85         $previewFields = new LiteralField("css", "<style>div#Root_Main.tab {height: 80%}</style>");
     86         $dom->setPreviewFieldFor($previewFields);
     87
     88         $fields->replaceField('Items', $dom);
     89         return $fields;
     90     }
     91
     92     /**
     93      * Список активных слайдов
     94      *
     95      * @return DataObjectSet
     96      */
     97     function ActiveItems() {
     98         return $this->Items('Active = 1');
     99     }
    100
    101     /**
    102      * Переводит время задержки в миллисекунды
    103      *
    104      * @return int
    105      */
    106     function DelayTimeMS() {
    107         return $this->DelayTime * 1000;
    108     }
    109 }
    110
    111 class SliderHomepageWidget_Item extends WebylonWidget_Item {
    112     static $has_one = array(
    113         'Image' => 'Image',
    114     );
    115
    116     function getCMSFields() {
    117         $fields = parent::getCMSFields();
    118
    119         $fields->removeByName('Image');
    120         if ($this->ImageID && $this->Image()->ID) {
    121             $tab = $fields->findOrMakeTab('Root.Image', $this->fieldLabel('Image'));
    122             $URL = $this->Image()->SetHeight(200)->URL;
    123             $tab->push(new LiteralField("icon", "<div class='current-image'><img src='$URL' alt='".$this->Image()->Filename."' title='".$this->Image()->Filename."'  /></div>"));
    124         }
    125         return $fields;
    126     }
    127
    128     function AutoresizeMaxWidth() {
    129         return WebylonWidget::$max_image_width;
    130     }
    131
    132     function AutoresizeMaxHeight() {
    133         return WebylonWidget::$max_image_heigth;
    134     }
    135 }
    136

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