public function parseData($contents) { $calendar = new ICalendar(); $nesting = array(); $contents = str_replace("\r\n", "\n", $contents); $lines = explode("\n", $this->unfold($contents)); foreach ($lines as $line) { $contentline = $this->contentline($line); $contentname = $contentline['name']; $value = $contentline['value']; $params = $contentline['params']; switch($contentname) { case 'BEGIN': switch ($value) { case 'VEVENT': $nesting[] = new $this->eventClass; break; case 'VCALENDAR': $nesting[] = $calendar; break; case 'VTIMEZONE': $nesting[] = new ICalTimeZone(); break; case 'DAYLIGHT': $nesting[] = new ICalDaylight(); break; case 'STANDARD': $nesting[] = new ICalStandard(); break; case 'VTODO': $nesting[] = new ICalTodo(); break; case 'VJOURNAL': $nesting[] = new ICalJournal(); break; case 'VFREEBUSY': $nesting[] = new ICalFreeBusy(); break; case 'VALARM': $nesting[] = new ICalAlarm(); break; default: throw new ICalendarException('unknown component type ' . $value); break; } break; case 'END': $last_object = array_pop($nesting); $last_obj_name = $last_object->get_name(); if ($last_obj_name != $value) { throw new ICalendarException("BEGIN $last_obj_name ended by END $value"); } switch ($value) { case 'VEVENT': if ($calendar->timezone) { $last_object->set_attribute('TZID', $calendar->timezone->tzid); } $calendar->add_event($last_object); break; case 'VTIMEZONE': $calendar->timezone = $last_object; break; case 'VCALENDAR': break 3; } break; default: end($nesting)->set_attribute($contentname, $value, $params); break; } } return $calendar; }
public function parseData($data) { $calendar = new ICalendar(); $data = json_decode($data, true); $items = isset($data['data']['items']) ? $data['data']['items'] : array(); $total = 0; foreach ($items as $item) { if (!isset($item['when'])) { //probably an orphaned event. continue; } $event = new $this->eventClass(); $event->setUID($item['id']); $event->setSummary($item['title']); $event->setDescription($item['details']); if (isset($item['location'])) { $event->setLocation($item['location']); } if (count($item['when']) > 1) { throw new KurogoDataException("Need to handle multiple when values. Please report this as a bug including calendar and event used"); } $start = new DateTime($item['when'][0]['start']); $end = new DateTime($item['when'][0]['end']); if (stripos($item['when'][0]['start'], 'T') !== false) { $range = new TimeRange($start->format('U'), $end->format('U')); } else { //make all day events last until 11:59 of the end day $range = new DayRange($start->format('U'), $end->format('U') - 1); } $event->setRange($range); $calendar->add_event($event); $total++; } $this->setTotalItems($total); return $calendar; }
public function parseData($contents) { $calendar = new ICalendar(); $nesting = array(); $contents = str_replace("\r\n", "\n", $contents); $lines = explode("\n", $this->unfold($contents)); foreach ($lines as $line) { $contentline = $this->contentline($line); $contentname = $contentline['name']; $value = $contentline['value']; $params = $contentline['params']; switch ($contentname) { case 'BEGIN': switch ($value) { case 'VEVENT': $addEvent = true; $nesting[] = new $this->eventClass(); break; case 'VCALENDAR': $nesting[] = $calendar; break; case 'VTIMEZONE': $nesting[] = new ICalTimeZone(); break; case 'DAYLIGHT': $nesting[] = new ICalDaylight(); break; case 'STANDARD': $nesting[] = new ICalStandard(); break; case 'VTODO': $nesting[] = new ICalTodo(); break; case 'VJOURNAL': $nesting[] = new ICalJournal(); break; case 'VFREEBUSY': $nesting[] = new ICalFreeBusy(); break; case 'VALARM': $nesting[] = new ICalAlarm(); break; default: if ($this->haltOnParseErrors) { throw new ICalendarException('unknown component type ' . $value); } else { Kurogo::log(LOG_WARNING, "Unknown ICS type {$value}", 'data'); } break; } break; case 'END': $last_object = array_pop($nesting); $last_obj_name = $last_object->get_name(); if ($last_obj_name != $value) { if ($this->haltOnParseErrors) { throw new ICalendarException("BEGIN {$last_obj_name} ended by END {$value}"); } else { Kurogo::log(LOG_WARNING, "BEGIN {$last_obj_name} ended by END {$value}", 'data'); $value = null; //throw it away } } switch ($value) { case 'VEVENT': if ($calendar->timezone) { $last_object->set_attribute('TZID', $calendar->timezone->tzid); } if ($addEvent) { $calendar->add_event($last_object); } break; case 'VTIMEZONE': $calendar->timezone = $last_object; break; case 'VCALENDAR': break 3; } break; default: try { end($nesting)->set_attribute($contentname, $value, $params); } catch (ICalendarException $e) { if ($this->haltOnParseErrors) { throw $e; } Kurogo::log(LOG_WARNING, $e->getMessage(), 'data'); $addEvent = false; } break; } } $events = $calendar->getEvents(); Kurogo::log(LOG_DEBUG, "Found " . count($events) . " events", 'data'); $this->setTotalItems(count($events)); return $calendar; }