IcalLib.php 3.2 KB

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