IcalHelper.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('IcalLib', 'Tools.Lib');
  4. /**
  5. * Uses ical lib
  6. * tipps see http://labs.iamkoa.net/2007/09/07/create-downloadable-ical-events-via-cake/
  7. *
  8. * needs ical layout
  9. * needs Router::parseExtensions('ics') in router.php
  10. *
  11. */
  12. class IcalHelper extends AppHelper {
  13. public $Ical;
  14. protected $_data = [];
  15. public function __construct($View = null, $config = []) {
  16. parent::__construct($View, $config);
  17. $this->Ical = new IcalLib();
  18. }
  19. /**
  20. * IcalHelper::reset()
  21. *
  22. * @return void
  23. */
  24. public function reset() {
  25. $this->_data = [];
  26. }
  27. /**
  28. * Add a new ical record.
  29. *
  30. * @param array $data
  31. * @return bool Success
  32. */
  33. public function add($data = []) {
  34. //TODO: validate!
  35. $this->_data[] = $data;
  36. return true;
  37. }
  38. /**
  39. * Returns complete ical calender file content to output.
  40. *
  41. * @param array $globalData
  42. * @param bool $addStartAndEnd
  43. * @return string
  44. */
  45. public function generate($globalData = [], $addStartAndEnd = true) {
  46. $res = [];
  47. foreach ($this->_data as $data) {
  48. $res[] = $this->Ical->build($data);
  49. }
  50. $res = implode(PHP_EOL . PHP_EOL, $res);
  51. if ($addStartAndEnd) {
  52. $res = $this->Ical->createStart($globalData) . PHP_EOL . PHP_EOL . $res . PHP_EOL . PHP_EOL . $this->Ical->createEnd();
  53. }
  54. return $res;
  55. }
  56. }