v1.php
76 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * The iCalendar parser is based on the integration provided by Jonathan Goode. |
| 16 | * |
| 17 | * @link https://github.com/u01jmg3/ics-parser |
| 18 | * |
| 19 | * @since 1.7.3 |
| 20 | */ |
| 21 | class VAPIcalParserV1 extends VAPIcalParser |
| 22 | { |
| 23 | /** |
| 24 | * Implements the algorithm used to parse the iCalendar buffer. |
| 25 | * |
| 26 | * @param string $buffer |
| 27 | * |
| 28 | * @return array |
| 29 | */ |
| 30 | protected function parseBuffer($buffer) |
| 31 | { |
| 32 | // define default timezone |
| 33 | $this->options->def('timezone', JFactory::getApplication()->get('offset', 'UTC')); |
| 34 | |
| 35 | // init calendar parser |
| 36 | $cal = new \ICal\ICal($buffer, [ |
| 37 | // inject default timezone |
| 38 | 'defaultTimeZone' => $this->options->get('timezone'), |
| 39 | // ignore all the events prior the current date minus the specified amount |
| 40 | 'filterDaysBefore' => $this->options->get('exclude_prev_days', null), |
| 41 | // ignore all the events after the current date plus the specified amount |
| 42 | 'filterDaysAfter' => $this->options->get('exclude_next_days', null), |
| 43 | ]); |
| 44 | |
| 45 | $tz = new DateTimeZone($this->options->get('timezone')); |
| 46 | |
| 47 | $list = []; |
| 48 | |
| 49 | foreach ($cal->events() as $event) |
| 50 | { |
| 51 | // convert dates and times in UTC |
| 52 | $start = JFactory::getDate($event->dtstart_tz, $tz); |
| 53 | $end = JFactory::getDate($event->dtend_tz, $tz); |
| 54 | |
| 55 | // fetch the creation date from the most appropriate property |
| 56 | $created = $event->created ? $event->created : $event->dtstamp; |
| 57 | |
| 58 | // build event wrapper |
| 59 | $list[] = new VAPIcalEvent([ |
| 60 | 'uid' => $event->uid, |
| 61 | 'start' => $start->toISO8601(), |
| 62 | 'end' => $end->toISO8601(), |
| 63 | 'created' => $created ? JFactory::getDate($created)->toISO8601() : null, |
| 64 | 'modified' => $event->last_modified ? JFactory::getDate($event->last_modified)->toISO8601() : null, |
| 65 | 'summary' => $event->summary, |
| 66 | 'description' => $event->description, |
| 67 | 'location' => $event->location, |
| 68 | 'organizer' => $event->organizer, |
| 69 | 'attendee' => $event->attendee, |
| 70 | ]); |
| 71 | } |
| 72 | |
| 73 | return $list; |
| 74 | } |
| 75 | } |
| 76 |