You are here

function _parser_ical_parse in iCal feed parser 7

Same name and namespace in other branches
  1. 5 parser_ical.module \_parser_ical_parse()
  2. 6.2 parser_ical.dateapi.inc \_parser_ical_parse()
  3. 6 parser_ical.feedapi.inc \_parser_ical_parse()

Parse iCal feeds.

Pinched pretty much directly from version 1 for now.

1 call to _parser_ical_parse()
ParserIcalFeedsParser::parse in includes/ParserIcalFeedsParser.inc
Parse content fetched by fetcher.

File

./parser_ical.dateapi.inc, line 13
parser_ical include for actually parsing feed.

Code

function _parser_ical_parse($feed_content) {
  include_once drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  $feed_folded = explode("\n", $feed_content);
  $ical_parsed = date_ical_parse($feed_folded);

  // TODO, should we change this to a foreach() instead of just picking
  // out the first one in case there is more than one CALENDAR grouping
  // in a feed? Not sure that ever actually happens.
  $ical = $ical_parsed[0];

  // Pick out the first CALENDAR group.
  // Any or all of these items could be missing, so always use isset() to test.
  $parsed_source = array();

  // Detect the title
  $parsed_source['title'] = isset($ical['X-WR-CALNAME']) ? $ical['X-WR-CALNAME'] : '';

  // Detect the description
  $parsed_source['description'] = isset($ical['X-WR-CALDESC']) ? $ical['X-WR-CALDESC'] : '';
  $parsed_source['link'] = '';

  // @todo do we have one from the feed?
  $parsed_source['date']['calscale'] = isset($ical['CALSCALE']) ? $ical['CALSCALE'] : '';
  $parsed_source['date']['timezone'] = isset($ical['X-WR-TIMEZONE']) ? $ical['X-WR-TIMEZONE'] : '';
  $parsed_source['items'] = array();
  if (array_key_exists('VEVENT', $ical)) {
    foreach ($ical['VEVENT'] as $event) {
      $item = array();
      $item['title'] = $event['SUMMARY'];
      $item['description'] = isset($event['DESCRIPTION']) ? $event['DESCRIPTION'] : '';
      $item['author'] = '';

      // @todo can we get this, what format
      $item['url'] = isset($event['URL']) ? $event['URL'] : '';

      // Make sure a valid timestamp is created.
      if (isset($event['DTSTAMP'])) {
        $date = date_ical_date($event['DTSTAMP']);

        # TODO no longer needed? what's the output of date_ical_date?

        # if needed what replaces it?

        # if (date_is_valid($date, DATE_OBJECT)) {
        $item['timestamp'] = date_format($date, 'U');

        # }
      }
      elseif (isset($event['DTSTART'])) {

        // a majority of feeds do have a DTSTART and it's sensible enough for a node date
        // although it maybe in the future...
        $date = date_ical_date($event['DTSTART']);

        # TODO See above

        # if (date_is_valid($date, DATE_OBJECT)) {
        $item['timestamp'] = date_format($date, 'U');

        # }
      }
      else {

        // now falling back but it will cause constant updates
        $item['timestamp'] = FEEDS_REQUEST_TIME;
      }
      $item['guid'] = isset($event['UID']) ? $event['UID'] : '';

      // intention
      $item['tags'] = isset($event['CATEGORIES']) ? explode(',', $event['CATEGORIES']) : array();
      $item['ical_date'] = new ParserIcalDateTimeElement($event);
      $item['ical_location'] = $event['LOCATION'];
      $parsed_source['items'][] = $item;
    }
  }
  return $parsed_source;
}