Version: current

      1 <?php
      2
      3 class CalendarUtil {
      4     const ONE_DAY = "OneDay";
      5     const SAME_MONTH_SAME_YEAR = "SameMonthSameYear";
      6     const DIFF_MONTH_SAME_YEAR = "DiffMonthSameYear";
      7     const DIFF_MONTH_DIFF_YEAR = "DiffMonthDiffYear";
      8
      9     const ONE_DAY_HEADER = "OneDayHeader";
     10     const MONTH_HEADER = "MonthHeader";
     11     const YEAR_HEADER = "YearHeader";
     12
     13     public static $months_map = array(
     14         '01' => 'Jan',
     15         '02' => 'Feb',
     16         '03' => 'Mar',
     17         '04' => 'Apr',
     18         '05' => 'May',
     19         '06' => 'Jun',
     20         '07' => 'Jul',
     21         '08' => 'Aug',
     22         '09' => 'Sep',
     23         '10' => 'Oct',
     24         '11' => 'Nov',
     25         '12' => 'Dec'
     26     );
     27     private static $format_character_placeholders = array(
     28         '%{sWeekDayShort}',
     29         '%{sWeekDayFull}',
     30         '%{sDayNumShort}',
     31         '%{sDayNumFull}',
     32         '%{sDaySuffix}',
     33         '%{sMonNumShort}',
     34         '%{sMonNumFull}',
     35         '%{sMonShort}',
     36         '%{sMonFull}',
     37         '%{sMonFullDate}',
     38         '%{sYearShort}',
     39         '%{sYearFull}',
     40         '%{eWeekDayShort}',
     41         '%{eWeekDayFull}',
     42         '%{eDayNumShort}',
     43         '%{eDayNumFull}',
     44         '%{eDaySuffix}',
     45         '%{eMonNumShort}',
     46         '%{eMonNumFull}',
     47         '%{eMonShort}',
     48         '%{eMonFull}',
     49         '%{eMonFullDate}',
     50         '%{eYearShort}',
     51         '%{eYearFull}'
     52     );
     53
     54     private static function format_character_replacements($start, $end) {
     55         return array(
     56             self::i18n_date('%a', $start), // sWeekDayShort
     57             self::i18n_date('%A', $start), // sWeekDayFull
     58             date('d', $start), // sDayNumFull
     59             date('j', $start), //sDayNumShort
     60             date('S', $start), // sDaySuffix
     61             date('n', $start), // sMonNumShort
     62             date('m', $start), // sMonNumFull
     63             self::i18n_date('%b', $start), // sMonShort
     64             self::i18n_date('%B', $start), // sMonFull
     65             _t('CalendarMonthInDate.'. date('F', $start), self::i18n_date('%B', $start)), // sMonFullDate
     66             date('y', $start), // sYearShort
     67             date('Y', $start), // sYearFull
     68
     69             self::i18n_date('%a', $end), // eWeekDayShort
     70             self::i18n_date('%A', $end), // eWeekDayFull
     71             date('d', $end), // eDayNumFull
     72             date('j', $end), // eDayNumFull
     73             date('S', $end), // eDaySuffix
     74             date('n', $end), // eMonNumShort
     75             date('m', $end), // eMonNumFull
     76             self::i18n_date('%b', $end), // eMonShort
     77             self::i18n_date('%B', $end), // eMonFull
     78             _t('CalendarMonthInDate.'. date('F', $end), self::i18n_date('%B', $end)), // sMonFullDate
     79             date('y', $end), // eYearShort
     80             date('Y', $end), // eYearFull
     81         );
     82     }
     83
     84     static $NeedTranslate = false;
     85     public static function i18n_date($char, $ts) {
     86         // Need to figure out how we're handling non- UTF-8 users.
     87         //return utf8_encode(strftime($char, $ts));
     88         if(self::$NeedTranslate)
     89             return self::translate(strftime($char, $ts));
     90         return strftime($char, $ts);
     91     }
     92     public static function SetNeedTranslate(){
     93         self::$NeedTranslate = true;
     94     }
     95     public static function translate($str){
     96         return _t('CalendarTranslate.'.$str,$str);
     97     }
     98     public static function localize($start, $end, $key) {
     99         global $customDateTemplates;
    100         global $lang;
    101         if (is_array($customDateTemplates) && isset($customDateTemplates[$key]))
    102             $template = $customDateTemplates[$key];
    103         else {
    104             $template = _t("Calendar.$key", $key);
    105         }
    106
    107         return str_replace(self::$format_character_placeholders, self::format_character_replacements($start, $end), $template);
    108     }
    109
    110     public static function getMonthsMap() {
    111         return array(
    112             '01' => self::i18n_date('%b', strtotime('2000-01-01')),
    113             '02' => self::i18n_date('%b', strtotime('2000-02-01')),
    114             '03' => self::i18n_date('%b', strtotime('2000-03-01')),
    115             '04' => self::i18n_date('%b', strtotime('2000-04-01')),
    116             '05' => self::i18n_date('%b', strtotime('2000-05-01')),
    117             '06' => self::i18n_date('%b', strtotime('2000-06-01')),
    118             '07' => self::i18n_date('%b', strtotime('2000-07-01')),
    119             '08' => self::i18n_date('%b', strtotime('2000-08-01')),
    120             '09' => self::i18n_date('%b', strtotime('2000-09-01')),
    121             '10' => self::i18n_date('%b', strtotime('2000-10-01')),
    122             '11' => self::i18n_date('%b', strtotime('2000-11-01')),
    123             '12' => self::i18n_date('%b', strtotime('2000-12-01'))
    124         );
    125     }
    126
    127     public static function getDaysMap() {
    128         $days = array();
    129         for ($i = 1; $i <= 31; $i++) {
    130             $day = $i < 10 ? '0' . $i : $i;
    131             $days[$day] = $day;
    132         }
    133         return $days;
    134     }
    135
    136     public static function getYearsMap() {
    137         $years = array();
    138         for ($i = (date('Y') - 5); $i <= (date('Y') + 5); $i++)
    139             $years[$i] = $i;
    140         return $years;
    141     }
    142
    143     public static function getDateFromString($str) {
    144         $str = str_replace('-', '', $str);
    145         if (is_numeric($str)) {
    146             $missing = (8 - strlen($str));
    147             if ($missing > 0) {
    148                 while ($missing > 0) {
    149                     $str .= "01";
    150                     $missing-=2;
    151                 }
    152             }
    153             return substr($str, 0, 4) . "-" . substr($str, 4, 2) . "-" . substr($str, 6, 2);
    154         } else {
    155             return date('Y-m-d');
    156         }
    157     }
    158
    159     public static function date_info_from_ics($dtstart, $dtend) {
    160         $start_date = null;
    161         $end_date = null;
    162         $start_time = null;
    163         $end_time = null;
    164
    165         $start = explode("T", $dtstart);
    166         $start_date = CalendarUtil::getDateFromString($start[0]);
    167         if (isset($start[1]))
    168             $start_time = substr($start[1], 0, 2) . ":" . substr($start[1], 2, 2) . ":" . "00";
    169
    170
    171         $end = explode("T", $dtend);
    172         $end_date = CalendarUtil::getDateFromString($end[0]);
    173         if (isset($end[1]))
    174             $end_time = substr($end[1], 0, 2) . ":" . substr($end[1], 2, 2) . ":" . "00";
    175
    176         return array($start_date, $end_date, $start_time, $end_time);
    177     }
    178
    179     public static function getDateFromURL() {
    180         $params = Controller::curr()->urlParams;
    181         if (isset($params['ID'])) {
    182             return CalendarUtil::getDateFromString($params['ID']);
    183         }
    184         else
    185             return false;
    186     }
    187
    188     public static function get_calendar_for($url_segment) {
    189         return $url_segment === null ? DataObject::get_one("Calendar") : DataObject::get_one("Calendar", "URLSegment = '$url_segment'");
    190     }
    191
    192     public static function CollapseDatesAndTimes($dates) {
    193         // Stupid thing doesn't work.
    194         return $dates;
    195
    196         /* $date_list = new DataObjectSet();
    197           $dates = $dates->toArray();
    198           for($i=0; $i < sizeof($dates); $i++) {
    199           $original_date = $dates[$i];
    200           $original_date->Times = new DataObjectSet();
    201           $current_date = $original_date;
    202           $j = 0;
    203           while(($current_date->StartDate == $original_date->StartDate) && ($current_date->EventID == $original_date->EventID)) {
    204           $j++;
    205           $original_date->Times->push($current_date);
    206           if(isset($dates[$i+$j]))
    207           $current_date = $dates[$i+$j];
    208           else
    209           break;
    210           }
    211           $i += $j;
    212           $date_list->push($original_date);
    213           }
    214           return $date_list; */
    215     }
    216
    217     public static function Microformat($date, $time, $offset = true) {
    218         if (!$date)
    219             return "";
    220
    221         $ts = strtotime($date . " " . $time);
    222
    223         if ($ts < 1)
    224             return "";
    225
    226         $ret = date('Ymd', $ts) . "T" . date('His', $ts);
    227         return $offset ? $ret . CalendarDateTime::$offset : $ret;
    228     }
    229
    230     /**
    231      * This function is used to write a date range in the common
    232      * human-readable format on the front end. If the date range spans
    233      * days within the month, we do not rewrte the month name. If it spans
    234      * two different months, we write both month names, both days, but
    235      * do not rewrite the year, etc. Though it appears to break the MVC
    236      * guidelines, it is a lot cleaner than conducting all of this logic
    237      * on the frontend.
    238      */
    239     static function getDateString($start_date, $end_date) {
    240         $strStartDate = null;
    241         $strEndDate = null;
    242
    243         $start = strtotime($start_date);
    244         $end = strtotime($end_date);
    245
    246         $start_year = date("Y", $start);
    247         $start_month = date("m", $start);
    248
    249         $end_year = date("Y", $end);
    250         $end_month = date("m", $end);
    251
    252         // Invalid date. Get me out of here!
    253         if ($start < 1)
    254             return;
    255
    256         // Only one day long!
    257         else if ($start == $end || !$end || $end < 1)
    258             $key = self::ONE_DAY;
    259
    260         else {
    261             if ($start_year == $end_year)
    262                 $key = ($start_month == $end_month) ? self::SAME_MONTH_SAME_YEAR : self::DIFF_MONTH_SAME_YEAR;
    263             else
    264                 $key = self::DIFF_MONTH_DIFF_YEAR;
    265         }
    266         $date_string = self::localize($start, $end, $key);
    267         $break = strpos($date_string, "%{e");
    268         if ($break !== FALSE) {
    269             $strStartDate = substr($date_string, 0, $break);
    270             $strEndDate = substr($date_string, $break + 1, strlen($date_string) - strlen($strStartDate));
    271             return array($strStartDate, $strEndDate);
    272         }
    273
    274         return array($date_string, "");
    275     }
    276
    277     static function differenceInMonths($dateObj1, $dateObj2) {
    278         return (($dateObj1->format('Y') * 12) + $dateObj1->format('n')) - (($dateObj2->format('Y') * 12) + $dateObj2->format('n'));
    279     }
    280
    281     function date_sort(&$data) {
    282         uasort($data, array("CalendarUtil", "date_sort_callback"));
    283     }
    284
    285     /**
    286      * Callback used by column_sort
    287      */
    288     function date_sort_callback($a, $b) {
    289         if ($a->StartDate == $b->StartDate) {
    290             if ($a->StartTime == $b->StartTime)
    291                 return 0;
    292             else if (strtotime($a->StartTime) > strtotime($b->StartTime))
    293                 return 1;
    294             else
    295                 return -1;
    296         }
    297         else if (strtotime($a->StartDate) > strtotime($b->StartDate))
    298             return 1;
    299         else
    300             return -1;
    301     }
    302
    303 }
    304 ?>

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