IcalLib.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. //$path = dirname(__FILE__);
  3. //require_once($path . DS . 'Vendor' . DS . 'ical'.DS.'ical.php');
  4. App::import('Vendor', 'Tools.ical', array('file'=>'ical/ical.php'));
  5. App::import('Vendor', 'Tools.icalobject', array('file'=>'ical/i_cal_object.php'));
  6. App::uses('CakeTime', 'Utility');
  7. /**
  8. * A wrapper for the Ical/Ics calendar lib
  9. * @see http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2 for details
  10. *
  11. * @author Mark Scherer
  12. * @license MIT
  13. * @cakephp 2.0
  14. * 2010-09-14 ms
  15. */
  16. class IcalLib {
  17. public $Ical;
  18. public $Time;
  19. public function __construct() {
  20. $this->ICalObject = new ICalObject();
  21. }
  22. /** BUILDING **/
  23. /**
  24. *
  25. * some automagic
  26. * - array urls are transformed in (full) absolute urls
  27. * - id => uid with @host
  28. * - start/end/timestamp to atom
  29. * - class to upper
  30. * @return string $icalContent (single vevent)
  31. * 2011-10-10 ms
  32. */
  33. public function build($data, $addStartAndEnd = true) {
  34. if (isset($data['start'])) {
  35. $data['dtstart'] = CakeTime::toAtom($data['start']);
  36. $data['dtstart'] = str_replace(array('-', ':'), '', $data['dtstart']);
  37. unset($data['start']);
  38. }
  39. if (isset($data['end'])) {
  40. $data['dtend'] = CakeTime::toAtom($data['end']);
  41. $data['dtend'] = str_replace(array('-', ':'), '', $data['dtend']);
  42. unset($data['end']);
  43. }
  44. if (isset($data['timestamp'])) {
  45. $data['dtstamp'] = CakeTime::toAtom($data['timestamp']);
  46. $data['dtstamp'] = str_replace(array('-', ':'), '', $data['dtstamp']);
  47. unset($data['timestamp']);
  48. }
  49. if (isset($data['id'])) {
  50. $data['uid'] = $data['id'].'@'.env('HTTP_HOST');
  51. unset($data['id']);
  52. }
  53. if (isset($data['class'])) {
  54. $data['class'] = strtoupper($data['class']);
  55. }
  56. if (isset($data['url']) && is_array($data['url'])) {
  57. $data['url'] = Router::url($data['url'], true);
  58. }
  59. $res = $this->ICalObject->create($data);
  60. if ($addStartAndEnd) {
  61. $res = 'BEGIN:VEVENT'.PHP_EOL.trim($res).PHP_EOL.'END:VEVENT';
  62. }
  63. return $res;
  64. }
  65. public function createStart($data = array()) {
  66. $defaults = array(
  67. 'version' => '2.0',
  68. 'prodid' => '-//'.env('HTTP_HOST'),
  69. 'method' => 'PUBLISH',
  70. );
  71. $data = am($defaults, $data);
  72. $res = array();
  73. $res[] = 'BEGIN:VCALENDAR';
  74. foreach ($data as $key => $val) {
  75. $res[] = strtoupper($key).':'.$val;
  76. }
  77. return implode(PHP_EOL, $res);
  78. }
  79. public function createEnd() {
  80. return 'END:VCALENDAR';
  81. }
  82. /** PARSING **/
  83. public function parse($url) {
  84. if (!file_exists($url) || !($res = file_get_contents($url))) {
  85. return false;
  86. }
  87. $this->Ical = new ical($url);
  88. if ($this->Ical->parse()) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. /**
  94. * @return array
  95. * 2010-09-14 ms
  96. */
  97. public function getCalendarInfos() {
  98. return $this->Ical->get_calender_data();
  99. }
  100. /**
  101. * key => value with key as unixTimeStamp and value as summary
  102. * @return array
  103. * 2010-09-14 ms
  104. */
  105. public function getEventsAsList() {
  106. $res = array();
  107. $events = $this->getEvents();
  108. foreach ($events as $event) {
  109. $res[$event['DTSTART']['unixtime']] = $event['SUMMARY'];
  110. }
  111. return $res;
  112. }
  113. /**
  114. * @return array $events or false on failure
  115. * 2010-09-14 ms
  116. */
  117. public function getEvents() {
  118. return $this->Ical->get_sort_event_list();
  119. }
  120. /**
  121. * @return array $todos or false on failure
  122. * 2010-09-14 ms
  123. */
  124. public function getTodos() {
  125. return $this->Ical->get_todo_list();
  126. }
  127. }