Version: current

      1 <?php
      2 /**
      3  * Событие (мероприятие)
      4  *
      5  * @author menedem
      6  *
      7  * @package event
      8  */
      9 class Event extends Page {
     10
     11     static $can_be_root = false;
     12     static $allowed_children = 'none';
     13
     14     static $db = array (
     15         'Title' => 'Text',
     16         'EventDate' => 'Date',
     17         'StartTime' => 'Time',
     18         'EndTime' => 'Time',
     19         'EventPlace' => 'Varchar(200)'
     20     );
     21
     22     static $defaults = array(
     23         'AutoChild' => 0,
     24         'ShowInMenus' => 0,
     25         'ShowInSiteMap' => 0,
     26         'ShowOnlyInTab' => 1
     27     );
     28
     29     static $summary_fields = array(
     30         'Title',
     31         'EventDate',
     32         'StartTime',
     33         'EndTime',
     34         'EventPlace',
     35         //'Content'
     36     );
     37
     38     static $indexes = array(
     39         'EventDate' => true,
     40         'FullEventDate' => '(EventDate,StartTime)',
     41     );
     42
     43
     44     /**
     45      * По строковой дате (20140101) возвращает объект с полями год, месяц и день
     46      * проверяя дату на корректность
     47      *
     48      * @param <type> $dateString
     49      *
     50      * @return DataObject
     51      */
     52     static function parseDate($dateString) {
     53         $rs = new DataObject();
     54         $rs->year = '0';
     55         $rs->month = '0';
     56         $rs->day = '0';
     57         $rs->full_date = false;
     58
     59         switch(strlen($dateString)) {
     60             case 8:
     61             $rs->year = (int)substr($dateString, 0, 4);
     62             $rs->month = (int)substr($dateString, 4, 2);
     63             $rs->day = (int)substr($dateString, 6, 2);
     64             $rs->full_date = true;
     65             break;
     66             case 6:
     67             $rs->year = (int)substr($dateString, 0, 4);
     68             $rs->month = (int)substr($dateString, 4, 2);
     69             break;
     70             case 4:
     71             $rs->year = (int)substr($dateString, 0, 4);
     72             break;
     73         }
     74
     75         //Дописываем лидирующий нолик
     76         if ($rs->month != '0') {
     77             $rs->month = substr('0' . $rs->month, -2, 2);
     78         }
     79         if ($rs->day != '0') {
     80             $rs->day = substr('0' . $rs->day, -2, 2);
     81         }
     82         return $rs;
     83     }
     84
     85     /**
     86      * Получение списка событий между двумя датами нужного родителя
     87      *
     88      * @param string $startDate
     89      * @param string $endDate
     90      * @param int $parentID
     91      *
     92      * @return DataObjectSet
     93      */
     94     static function get_events($startDate, $endDate, $parentID) {
     95         $where = "ParentID = '{$parentID}'";
     96
     97         if ($endDate != null && strlen($endDate) == 8 && strlen($startDate) == 8) {
     98             $endDate = self::parseDate($endDate);
     99             $startDate = self::parseDate($startDate); //. ' 00:00:00';
    100             $endDate = $endDate->year . '-' . $endDate->month . '-' . $endDate->day;
    101             $startDate = $startDate->year . '-' . $startDate->month . '-' . $startDate->day;
    102             $endDate .= ' 23:59:59';
    103             $startDate .= ' 00:00:00';
    104             $where .= " AND EventDate between '{$startDate}' and '{$endDate}'"; //Возможно добавить конвертацию дат в MySQL: STR_TO_DATE('2008-08-14 00:00:00', '%Y-%m-%d %H:%i:%s')
    105
    106         } else if ($startDate != null){
    107             $parsedStartDate = self::parseDate($startDate);
    108
    109             if ($parsedStartDate->month == '0') {
    110                 $where .= ' AND EventDate >= ' . $parsedStartDate->year . '-01-01 AND EventDate <= ' . $parsedStartDate->year . '-12-31';
    111             } elseif ($parsedStartDate->day == '0') {
    112                 $where .= ' AND EventDate >= ' . $parsedStartDate->year . ' - ' . $parsedStartDate->month . '-01';
    113                 $where .= ' AND EventDate < ' . $parsedStartDate->year . ' - ' . ($parsedStartDate->month + 1) . '-01';
    114             } else {
    115                 $where .= ' AND EventDate = ' . $parsedStartDate->year . ' - ' . $parsedStartDate->month . '-' . $parsedStartDate->day;
    116             }
    117         }
    118         $sort = 'EventDate';
    119         if ($holder = DataObject::get_by_id('EventHolder', $parentID)) {
    120             $sort = $holder->getSQLSort();
    121         }
    122         $rs = DataObject::get('Event', $where, $sort);
    123         return $rs;
    124     }
    125
    126     /**
    127      * Возвращает $count ближайших событий
    128      *
    129      * @param int $count
    130      *
    131      * @return DataObjectSet
    132      */
    133     static function getNearestEvents($count = 5) {
    134         return DataObject::get('Event', 'EventDate >= NOW()', 'EventDate ASC', '', $count);
    135     }
    136
    137     public function getCMSFields() {
    138         $fields = parent::getCMSFields();
    139         $fields->addFieldToTab('Root.Content.Main', new DateField('EventDate', $this->FieldLabel('EventDate')), 'Content');
    140         $fields->addFieldToTab('Root.Content.Main', new TimeField('StartTime', $this->FieldLabel('StartTime')), 'Content');
    141         $fields->addFieldToTab('Root.Content.Main', new TimeField('EndTime', $this->FieldLabel('EndTime')), 'Content');
    142         $fields->addFieldToTab('Root.Content.Main', new TextField('EventPlace', $this->FieldLabel('EventPlace')), 'Content');
    143         return $fields;
    144     }
    145
    146     public function getCMSActions() {
    147         Requirements::javascript('webylon/javascript/SubpageAction.js');
    148         $fields = parent::getCMSActions();
    149         $fields->push(new HiddenField('backLink', '', $this->Parent()->ID));
    150         $fields->unshift(new FormAction('goBack', _t('Event.BACKBUTTON', 'Back')));
    151         return $fields;
    152     }
    153 }
    154
    155 class Event_Controller extends Page_Controller {
    156
    157 }

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