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 = "OneDayHeader";
10 const = "MonthHeader";
11 const = "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),
57 self::i18n_date('%A', $start),
58 date('d', $start),
59 date('j', $start),
60 date('S', $start),
61 date('n', $start),
62 date('m', $start),
63 self::i18n_date('%b', $start),
64 self::i18n_date('%B', $start),
65 _t('CalendarMonthInDate.'. date('F', $start), self::i18n_date('%B', $start)),
66 date('y', $start),
67 date('Y', $start),
68
69 self::i18n_date('%a', $end),
70 self::i18n_date('%A', $end),
71 date('d', $end),
72 date('j', $end),
73 date('S', $end),
74 date('n', $end),
75 date('m', $end),
76 self::i18n_date('%b', $end),
77 self::i18n_date('%B', $end),
78 _t('CalendarMonthInDate.'. date('F', $end), self::i18n_date('%B', $end)),
79 date('y', $end),
80 date('Y', $end),
81 );
82 }
83
84 static $NeedTranslate = false;
85 public static function i18n_date($char, $ts) {
86
87
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
194 return $dates;
195
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
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 232 233 234 235 236 237 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
253 if ($start < 1)
254 return;
255
256
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 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.
-