IcalHelper.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 = array();
  15. public function __construct($View = null, $config = array()) {
  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 = array();
  26. }
  27. /**
  28. * Add a new ical record.
  29. *
  30. * @return bool Success
  31. */
  32. public function add($data = array()) {
  33. //TODO: validate!
  34. $this->_data[] = $data;
  35. return true;
  36. }
  37. /**
  38. * Returns complete ical calender file content to output.
  39. *
  40. * @return string
  41. */
  42. public function generate($globalData = array(), $addStartAndEnd = true) {
  43. $res = array();
  44. foreach ($this->_data as $data) {
  45. $res[] = $this->Ical->build($data);
  46. }
  47. $res = implode(PHP_EOL . PHP_EOL, $res);
  48. if ($addStartAndEnd) {
  49. $res = $this->Ical->createStart($globalData) . PHP_EOL . PHP_EOL . $res . PHP_EOL . PHP_EOL . $this->Ical->createEnd();
  50. }
  51. return $res;
  52. }
  53. }