PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/code/CalendarUtil.php

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