Version: current

      1 <?php
      2
      3 /**
      4  * Description of NewsHolder
      5  *
      6  * @todo Проверять при dev/build есть ли хоть один Newsholder и если нет создавать с DevEditMode='Fixed'
      7  *
      8  * @author inxo
      9  */
     10 class NewsHolder extends Page {
     11
     12     static $icon = 'news/images/rss';
     13     static $allowed_children = array('NewsEntry', 'NewsHolder');
     14
     15     static $db = array(
     16         'SortType' => 'Int', // Сортировка 2 - ASC 1 - DESC
     17         'ShowNewsFromChildHolders' => 'Boolean', // показывать новости из дочерних разделов
     18     );
     19
     20     static $defaults = array(
     21         'SortType' => 1,
     22         'AutoChild' => 0,
     23         "ShowInMenus" => 0,
     24         "ShowInSearch" => 0,
     25     );
     26
     27     static $subpage_children = 'NewsEntry';
     28
     29     public static function getDateFromString($str) {
     30         $str = str_replace('-', '', $str);
     31         if (is_numeric($str)) {
     32             $missing = (8 - strlen($str));
     33             if ($missing > 0) {
     34                 while ($missing > 0) {
     35                     $str .= "01";
     36                     $missing-=2;
     37                 }
     38             }
     39             return substr($str, 0, 4) . "-" . substr($str, 4, 2) . "-" . substr($str, 6, 2);
     40         } else {
     41             return date('Y-m-d');
     42         }
     43     }
     44
     45     function getCMSFields() {
     46         $fields = parent::getCMSFields();
     47         $fields->addFieldToTab('Root.Content.Main',
     48                 new OptionsetField('SortType', $this->fieldLabel('SortType'), array(
     49                     1 => _t('NewsHolder.TYPE_SORT_DESC', "DESC"),
     50                     2 => _t('NewsHolder.TYPE_SORT_ASC', "ASC"),
     51                 )),
     52                 'Content'
     53         );
     54
     55         if (DataObject::get('NewsHolder', "ParentID = {$this->ID}")) {
     56             $fields->addFieldToTab('Root.Content.Main', new CheckboxField('ShowNewsFromChildHolders', $this->fieldLabel('ShowNewsFromChildHolders')));
     57         }
     58
     59         $fields->addFieldToTab('Root.Content', new Tab('tabSubPages', _t('NewsHolder.TABLABEL', 'News')), 'Metadata');
     60         $subClass = self::$subpage_children;
     61         $sp = new SubpageListField('Subpages', $this, $subClass, 'Date DESC');
     62         $sp->Sortable = false;
     63         $url = '<a href=\"admin/show/$ID\">$value</a>';
     64         $sp->setFieldFormatting(array_combine(array_keys(singleton($subClass)->summaryFields()), array_fill(0, count(singleton($subClass)->summaryFields()), $url)));
     65         $fields->addFieldToTab('Root.Content.tabSubPages', $sp);
     66
     67         return $fields;
     68     }
     69
     70     /*
     71      * Получение новостей
     72      */
     73     public function Entries($start = '', $limit = '', $onlyImportant=false, $start_date = null, $end_date = null) {
     74         $filter = '';
     75         if ($start_date) {
     76             $filter .= ' AND DATE(Date) >= \'' . $start_date . '\'';
     77             if ($end_date) {
     78                 $filter .= ' AND DATE(Date) <= \'' . $end_date . '\'';
     79             }
     80         }
     81         if ($onlyImportant) {
     82             $filter .= " AND Important = 1";
     83         }
     84         if ($limit != '') {
     85             if ($start != '') {
     86                 $limit = "$start,$limit";
     87             }
     88         }
     89
     90         if ($this->SortType == 1) {
     91             $sort = 'DESC';
     92         } else {
     93             $sort = 'ASC';
     94         }
     95         $parentWhere = "`ParentID` = {$this->ID}";
     96         // ??? может нужно искать детей-холдеров рекурсивно
     97         if ($this->ShowNewsFromChildHolders && ($childHolders = DataObject::get('NewsHolder', "ParentID = {$this->ID}"))) {
     98             $IDs = $childHolders->map('ID', 'ID');
     99             $IDs[] = $this->ID;
    100             $parentWhere = "`ParentID` IN(". implode(', ', $IDs).")";
    101         }
    102         $res = DataObject::get("NewsEntry", "{$parentWhere} AND `Status` LIKE '%Published%' $filter", "`NewsEntry`.Date $sort, `NewsEntry`.ID $sort", "", "$limit");
    103         return $res;
    104     }
    105 }
    106
    107 class NewsHolder_Controller extends Page_Controller {
    108
    109     protected $view;
    110     protected $start_date;
    111     protected $end_date;
    112
    113     function init() {
    114         parent::init();
    115         RSSFeed::linkToFeed($this->Link("rss"), _t('NewsHolder.RSSFEED', "RSS feed"));
    116     }
    117
    118     function index() {
    119         $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
    120         if ($start < 0) $start = 0;
    121
    122         $sc = SiteConfig::current_site_config();
    123
    124         $limit = $sc->NewsPerPage;
    125         $limit = ($limit > 0) ? $limit : 20;
    126         $data = $this->Entries($start, $limit);
    127         if ($this->hasMethod('setSEOVars')) {
    128             $this->setSEOVars($data);   //Выставляем SEO-переменные (ф-я setSEOVars находится в Webylon Page_Controller)
    129         }
    130         return $this->render(array('Items' => $data));
    131     }
    132
    133     public function view() {
    134         $this->parseURL();
    135
    136         $start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
    137         if ($start < 0) $start = 0;
    138
    139         $sc = SiteConfig::current_site_config();
    140
    141         $limit = $sc->NewsPerPage;
    142         $limit = ($limit > 0) ? $limit : 10;
    143         $data = $this->Entries($start, $limit, false, date('Y-m-d', $this->start_date), date('Y-m-d', $this->end_date));
    144         if ($this->hasMethod('setSEOVars')) {
    145             $this->setSEOVars($data); //Выставляем SEO-переменные (ф-я setSEOVars находится в Webylon Page_Controller)
    146         }
    147         return $this->render(array('Items' => $data));
    148     }
    149
    150     function CurrentPeriod() {
    151         switch ($this->view) {
    152             case 'year':
    153                 return $this->year;
    154
    155             case 'month':
    156                 return $this->StartDate()->formatI18N('%B %Y');
    157
    158             case 'day':
    159                 return $this->StartDate()->formatI18N('%e.%m.%Y');
    160
    161             case 'range':
    162                 return $this->StartDate()->rangeString($this->EndDate()) ;
    163         }
    164         return false;
    165     }
    166
    167     function StartDate() {
    168         if (!$this->start_date) return false;
    169         $date = new Date('StartDate');
    170         $date->setValue(date('Y-m-d', $this->start_date));
    171         return $date;
    172     }
    173
    174     function EndDate() {
    175         if (!$this->end_date) return false;
    176         $date = new Date('EndDate');
    177         $date->setValue(date('Y-m-d', $this->end_date));
    178         return $date;
    179     }
    180
    181     function ViewType() {
    182         return $this->view;
    183     }
    184
    185     function rss() {
    186         global $project;
    187         HTTP::set_cache_age(3600);
    188
    189         $sc = SiteConfig::current_site_config();
    190         $Name = $sc->Title;
    191         $altName = $project . _t('NewsHolder.RSSNAME', ' news');
    192         $entries = $this->Entries(0, $sc->NewsInRSS);
    193
    194         if ($entries) {
    195             $rss = new RSSFeed($entries, $this->Link() . 'rss', ($Name ? $Name : $altName), "", "Title", "Description");
    196             $rss->outputToBrowser();
    197         }
    198     }
    199
    200     /**
    201      * Парсинг URL с датой
    202      */
    203     public function parseURL() {
    204         if ($this->urlParams['Action'] && $this->urlParams['Action'] == "view") {
    205             $this->start_date = strtotime(NewsHolder::getDateFromString($this->urlParams['ID']));
    206             // User has specified an end date.
    207             if (isset($this->urlParams['OtherID'])) {
    208                 $this->view = "range";
    209                 $this->end_date = strtotime(NewsHolder::getDateFromString($this->urlParams['OtherID']));
    210             }
    211             // No end date specified. Now we have to make one based on the amount of data given in the start date.
    212             // e.g. 2008-08 will show the entire month of August, and 2008-08-03 will only show events for one day.
    213             else {
    214                 switch (strlen(str_replace("-", "", $this->urlParams['ID']))) {
    215                     case 8:
    216                         $this->view = "day";
    217                         $this->end_date = strtotime("+1 day", $this->start_date)-1;
    218                         break;
    219
    220                     case 6:
    221                         $this->view = "month";
    222                         $this->end_date = strtotime(date('Y-m', $this->start_date) . date('-t', $this->start_date));
    223                         break;
    224
    225                     case 4:
    226                         $this->view = "year";
    227                         $this->end_date = strtotime(date('Y-12-31', $this->start_date));
    228                         break;
    229
    230                     default:
    231                         $this->view = "default";
    232                         $this->end_date = strtotime("+1 month", $this->start_date);
    233                         break;
    234                 }
    235             }
    236         } else {
    237             $this->view = "default";
    238             $this->start_date = strtotime(date('Y-m-d'));
    239             $this->end_date = strtotime("+6 month", $this->start_date);
    240         }
    241     }
    242 }
    243

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