PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Calendar/CalendarDataController.php

http://github.com/modolabs/Kurogo-Mobile-Web
PHP | 193 lines | 154 code | 28 blank | 11 comment | 19 complexity | ff22302040e7d61952cc70b773652d45 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CalendarDataController
  4. * @package ExternalData
  5. * @subpackage Calendar
  6. */
  7. /**
  8. * Retrieves and parses calendar data
  9. * @package ExternalData
  10. * @subpackage Calendar
  11. */
  12. class CalendarDataController extends DataController
  13. {
  14. protected $DEFAULT_PARSER_CLASS='ICSDataParser';
  15. const DEFAULT_EVENT_CLASS='ICalEvent';
  16. const START_TIME_LIMIT=-2147483647;
  17. const END_TIME_LIMIT=2147483647;
  18. protected $cacheFolder = 'Calendar';
  19. protected $startDate;
  20. protected $endDate;
  21. protected $calendar;
  22. protected $requiresDateFilter=true;
  23. protected $contentFilter;
  24. protected $supportsSearch = false;
  25. public function setRequiresDateFilter($bool)
  26. {
  27. $this->requiresDateFilter = $bool ? true : false;
  28. }
  29. public function addFilter($var, $value)
  30. {
  31. switch ($var)
  32. {
  33. case 'search':
  34. if ($this->supportsSearch) {
  35. return parent::addFilter($var, $value);
  36. } else {
  37. $this->contentFilter = $value;
  38. }
  39. break;
  40. default:
  41. return parent::addFilter($var, $value);
  42. }
  43. }
  44. public function setStartDate(DateTime $time)
  45. {
  46. $clearCache = $this->startDate && $time->format('U') < $this->startTimestamp();
  47. $this->startDate = $time;
  48. if ($clearCache) {
  49. $this->clearInternalCache();
  50. }
  51. }
  52. public function startTimestamp()
  53. {
  54. return $this->startDate ? $this->startDate->format('U') : false;
  55. }
  56. public function setEndDate(DateTime $time)
  57. {
  58. $clearCache = $this->endDate && $time->format('U') > $this->endTimestamp();
  59. $this->endDate = $time;
  60. if ($clearCache) {
  61. $this->clearInternalCache();
  62. }
  63. }
  64. public function endTimestamp()
  65. {
  66. return $this->endDate ? $this->endDate->format('U') : false;
  67. }
  68. public function getEventCategories()
  69. {
  70. return $this->parser->getEventCategories();
  71. }
  72. public function setDuration($duration, $duration_units)
  73. {
  74. if (!$this->startDate) {
  75. return;
  76. } elseif (!preg_match("/^-?(\d+)$/", $duration)) {
  77. throw new KurogoDataException("Invalid duration $duration");
  78. }
  79. $this->endDate = clone($this->startDate);
  80. switch ($duration_units)
  81. {
  82. case 'year':
  83. case 'day':
  84. case 'month':
  85. $this->endDate->modify(sprintf("%s%s %s", $duration>=0 ? '+' : '', $duration, $duration_units));
  86. $this->clearInternalCache();
  87. break;
  88. default:
  89. throw new KurogoDataException("Invalid duration unit $duration_units");
  90. break;
  91. }
  92. }
  93. protected function init($args)
  94. {
  95. $args['EVENT_CLASS'] = isset($args['EVENT_CLASS']) ? $args['EVENT_CLASS'] : self::DEFAULT_EVENT_CLASS;
  96. parent::init($args);
  97. }
  98. public function getNextEvent($todayOnly=false) {
  99. $start = new DateTime();
  100. $start->setTime(date('H'), floor(date('i')/5)*5, 0);
  101. $this->setStartDate($start);
  102. if ($todayOnly) {
  103. $end = new DateTime();
  104. $end->setTime(23,59,59);
  105. $this->setEndDate($end);
  106. }
  107. $event = $this->getItemByIndex(0);
  108. return $event;
  109. }
  110. public function getItem($id, $time=null)
  111. {
  112. //use the time to limit the range of events to seek (necessary for recurring events)
  113. if ($time = filter_var($time, FILTER_VALIDATE_INT)) {
  114. $start = new DateTime(date('Y-m-d H:i:s', $time));
  115. $start->setTime(0,0,0);
  116. $end = clone $start;
  117. $end->setTime(23,59,59);
  118. $this->setStartDate($start);
  119. $this->setEndDate($end);
  120. }
  121. $items = $this->events();
  122. foreach($items as $key => $item) {
  123. if($id == $item->get_uid()) {
  124. return $item;
  125. }
  126. }
  127. return false;
  128. }
  129. public function getEvent($id) {
  130. if (!$this->calendar) {
  131. $this->calendar = $this->getParsedData();
  132. }
  133. return $this->calendar->getEvent($id);
  134. }
  135. protected function events($limit=null)
  136. {
  137. if (!$this->calendar) {
  138. $this->calendar = $this->getParsedData();
  139. }
  140. $startTimestamp = $this->startTimestamp() ? $this->startTimestamp() : CalendarDataController::START_TIME_LIMIT;
  141. $endTimestamp = $this->endTimestamp() ? $this->endTimestamp() : CalendarDataController::END_TIME_LIMIT;
  142. $range = new TimeRange($startTimestamp, $endTimestamp);
  143. return $this->calendar->getEventsInRange($range, $limit);
  144. }
  145. protected function clearInternalCache()
  146. {
  147. $this->calendar = null;
  148. parent::clearInternalCache();
  149. }
  150. public function items($start=0, $limit=null)
  151. {
  152. $items = $this->events($limit);
  153. $events = array();
  154. foreach ($items as $occurrence) {
  155. if ($this->contentFilter) {
  156. if ( (stripos($occurrence->get_description(), $this->contentFilter)!==FALSE) || (stripos($occurrence->get_summary(), $this->contentFilter)!==FALSE)) {
  157. $events[] = $occurrence;
  158. }
  159. } else {
  160. $events[] = $occurrence;
  161. }
  162. }
  163. return $this->limitItems($events, $start, $limit);
  164. }
  165. }