Version: current

      1 <?php
      2 /**
      3  * Контейнер для данных анонса
      4  *
      5  * @author inxo
      6  */
      7 class Announcement extends Page {
      8     static $icon = 'anons/img/anons';
      9
     10     static $can_be_root = false;
     11     static $allowed_children = 'none';
     12     static $default_parent = 'AnnouncementHolder';
     13
     14     static $db = array(
     15             'Date' => 'Date',
     16             'LinkType' => 'Int',
     17             'Description' => 'Text',
     18             'ExternalLink' => 'Text',
     19     );
     20
     21     static $has_one = array(
     22             'Photo' => 'Image',
     23             'InternalLink' => 'SiteTree'
     24     );
     25
     26     static $defaults = array(
     27             'LinkType' => 0,
     28             'ShowOnlyInTab' => 0,
     29             'ShowInSearch' => 0,
     30             'ShowInMenus' => 0,
     31             'AutoChild' => 0,
     32     );
     33     static $summary_fields = array(
     34             'Title','Date','Link','Description'
     35     );
     36
     37     static $searchable_fields = array(
     38         'Title','Description','Content'
     39     );
     40
     41     public function Link() {
     42         $Link = false;
     43         switch ($this->LinkType) {
     44             case 1:
     45                 $Link = $this->ExternalLink;
     46                 break;
     47             case 2:
     48                 $Link = $this->InternalLink()->Link();
     49                 break;
     50             case 3:
     51                 $Link = Controller::join_links(Director::baseURL(), $this->RelativeLink());
     52                 break;
     53             default:
     54                 break;
     55         }
     56         return $Link;
     57     }
     58
     59     function getCMSFields() {
     60         $fields = parent::getCMSFields();
     61
     62         $fields->addFieldToTab('Root.Content.Main', $dateField = new DateField('Date', $this->fieldLabel('Date')));
     63         if (!property_exists('FormField', 'use_html5') || !FormField::use_html5()) {
     64             $dateField->setLocale('ru_RU');
     65             $dateField->setConfig('dateformat', 'dd/MM/yyyy');
     66             $dateField->setConfig('showcalendar',1);
     67         }
     68
     69         $fields->addFieldToTab('Root.Content.Main', new TextField('Title', $this->fieldLabel('Title')));
     70         $fields->addFieldToTab('Root.Content.Main', new TextareaField('Description', $this->fieldLabel('Description')));
     71
     72         $fields->addFieldToTab('Root.Content.Main', $imageField = new ImageField('Photo', $this->fieldLabel('Photo')));
     73         $imageField->setFolderName('Anons');
     74
     75         // задаем max размеры для ImageAutoResize
     76         if ($this->ParentID && AnnouncementHolder::is_slider_holder($this->ParentID)) {
     77             $imageField->AutoresizeMaxWidth = AnnouncementHolder::$max_slider_width;
     78         }
     79
     80         $fields->addFieldToTab('Root.Content', new Tab('Link', $this->fieldLabel(_t('Announcement.tab_Link', 'Information'))), 'PagePhotos');
     81
     82         $fields->addFieldToTab('Root.Content.Link', $type = new SelectionGroup('LinkType', array(
     83             '0//' . _t('Announcement.NoLink', 'No Link') => new LiteralField('NoLink', ''),
     84             '1//' . _t('Announcement.ExternalLink', 'External Link') => new TextField('ExternalLink', _t('Announcement.ExternalLink', 'External Link')),
     85             '2//' . _t('Announcement.InternalLink', 'Internal Link') => new TreeDropdownField('InternalLinkID', _t('Announcement.InternalLink', 'Internal Link'), 'SiteTree'),
     86             '3//' . _t('Announcement.db_Content', 'Content') => $fields->dataFieldByName('Content')
     87         )));
     88         $fields->removeFieldFromTab('Root.Content.Main', 'Content');
     89
     90         $this->extend('hideCMSFields', $fields); // extend для скрытия полей из админки
     91         return $fields;
     92     }
     93
     94     function onBeforeWrite() {
     95         parent::onBeforeWrite();
     96
     97         // нельзя ссылаться на самого себя - происходит зацикливание
     98         if ($this->InternalLinkID == $this->ID) {
     99             $this->InternalLinkID = 0;
    100         }
    101     }
    102
    103     public function  getCMSActions() {
    104         Requirements::javascript('webylon/javascript/SubpageAction.js');
    105         $fields = parent::getCMSActions();
    106         $fields->push(new HiddenField('backLink', '', $this->Parent()->ID));
    107         $fields->insertBefore(new FormAction('goBack', _t('Announcement.BACKBUTTON','Back')),'action_unpublish');
    108         return $fields;
    109     }
    110
    111     public function populateDefaults() {
    112         parent::populateDefaults();
    113         $this->Date = date('d/m/Y');
    114     }
    115
    116     public function Widget($id=null, $limit=null) {
    117         if (is_numeric($id) && $id > 0) {
    118             $parent = DataObject::get_by_id(self::$default_parent, $id);
    119         }
    120         else if ($id) {
    121             $parent = DataObject::get_one(self::$default_parent, "URLSegment='" . Convert::raw2sql($id) . "'");
    122         }
    123         else {
    124             $parent = DataObject::get_one(self::$default_parent);
    125         }
    126
    127         if (!$parent) return FALSE;
    128
    129         $showtype = $parent->ShowTypeBlock;
    130         if (!$showtype) return FALSE;
    131
    132         switch ($showtype) {
    133             case 1:
    134                 $sort = 'Date DESC';
    135                 break;
    136             case 2:
    137                 $sort = 'RAND()';
    138                 break;
    139             case 3:
    140                 $sort = 'Sort';
    141                 break;
    142             default:
    143                 break;
    144         }
    145         if ($limit == null) {
    146             if ($showtype == 3) {
    147                 $limit = '';
    148             } else {
    149                 $limit = $parent->PerBlock;
    150             }
    151         }
    152         return DataObject::get($this->ClassName, "`ParentID` = {$parent->ID} AND `Status` LIKE '%Published%'", $sort, '', $limit);
    153     }
    154 }
    155
    156 class Announcement_Controller extends Page_Controller {
    157
    158 }
    159

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