Version: current

      1 <?php
      2 /**
      3  * Виджет "Спецпредложения"
      4  * Выводит товары из заданного спец.каталога
      5  *
      6  * @package widgets
      7  * @author  menedem
      8  */
      9 class SpecialCatalogHomepageWidget extends HomepageWidget {
     10     static $db = array(
     11         'ProductCount' => 'Int',
     12         'AnimationType' => 'Varchar',
     13         'DelayTime' => 'Int',
     14     );
     15
     16     static $defaults = array(
     17         'ProductCount' => 5,
     18     );
     19
     20     static $has_one = array(
     21         'Holder' => 'SpecialCatalog'
     22     );
     23
     24     /**
     25      * Набор типов анимации Виджета Спецпредложений
     26      */
     27     static $animation_types = array(
     28         'manual',
     29         'autobyone',
     30         'autobygroup',
     31         'random',
     32     );
     33
     34     /**
     35      * Установка набора типов анимации Виджета Спецпредложений
     36      *
     37      * @param array $types
     38      */
     39     static function set_animation_types($types = array()) {
     40         if (!is_array($types)) $types = array($types);
     41         self::$animation_types = $types;
     42     }
     43
     44     public $needObjects = array(
     45         'SpecialCatalog' => 'Page'
     46     );
     47
     48     function getCMSFields() {
     49         if (!$this->canCreate()) {
     50             return $this->getFailCMSFields();
     51         }
     52         $fields = parent::getCMSFields();
     53         $tab = $fields->findOrMakeTab('Root.Main');
     54         if (count(self::$animation_types) == 0) {
     55             $fields->removeByName('AnimationType');
     56             $fields->removeByName('DelayTime');
     57         } elseif (count(self::$animation_types) == 1) {
     58             $fields->replaceField('AnimationType', new HiddenField('AnimationType', '', self::$animation_types[0]));
     59             $tab->push(new HeaderField('AnimationSettings', _t('SliderHomepageWidget.AnimationSettings')));
     60             $tab->push($fields->dataFieldByName('DelayTime'));
     61         } else {
     62             $tab->push(new HeaderField('AnimationSettings', _t('SliderHomepageWidget.AnimationSettings')));
     63             $tab->push(new DropdownField('AnimationType', $this->fieldLabel('AnimationType'), WebylonWidget::get_array_localization($this->class, 'AnimationType', self::$animation_types)));
     64             $tab->push($fields->dataFieldByName('DelayTime'));
     65         }
     66
     67         $holders = DataObject::get('SpecialCatalog');
     68         if ($holders) {
     69             $holders = $holders->map();
     70         } else {
     71             $holders = array();
     72         }
     73         $fields->replaceField('HolderID', new DropdownField('HolderID', $this->fieldLabel('Holder'), $holders));
     74
     75         return $fields;
     76     }
     77
     78     /**
     79      * Список предложение
     80      *
     81      * @return DataObjectSet
     82      */
     83     function Offers() {
     84         if ($this->HolderID && $this->Holder()) {
     85             if ($this->ProductCount) {
     86                 return $this->Holder()->LinkedProducts(null, null, null, $this->ProductCount);
     87             }
     88         }
     89         return false;
     90     }
     91
     92     function hasContent() {
     93         if ($this->Offers()) {
     94             return true;
     95         }
     96         return false;
     97     }
     98
     99     /**
    100      * Ссылка на раздел с предложениями
    101      *
    102      * @return string
    103      */
    104     function getMoreLink() {
    105         if ($this->HolderID && $this->Holder()) {
    106             return $this->Holder()->Link();
    107         }
    108         return false;
    109     }
    110
    111     /**
    112      * Переводит время задержки в миллисекунды
    113      *
    114      * @return int
    115      */
    116     function DelayTimeMS() {
    117         return $this->DelayTime * 1000;
    118     }
    119 }
    120
    121

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