You are here

public function DateIcalIcalcreatorParser::parse in Date iCal 7.2

Load and run parser implementation of FeedsParser::parse().

@params - change these to generic required paramters.

File

includes/DateIcalIcalcreatorParser.inc, line 82
Classes implementing Date iCal's iCalcreator-based parser functionality.

Class

DateIcalIcalcreatorParser
@file Classes implementing Date iCal's iCalcreator-based parser functionality.

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  self::loadLibrary();

  // Read the iCal feed into memory.
  $ical_feed_contents = $fetcher_result
    ->getRaw();

  // Create the calendar object and parse the feed.
  $calendar = new vcalendar();
  if (!$calendar
    ->parse($ical_feed_contents)) {
    $url = $source->config[$source->importer->config['fetcher']['plugin_key']]['source'];
    throw new DateIcalParseException(t('Error parsing iCal feed: %url', array(
      '%url' => $url,
    )));
  }

  // Allow modules to alter the vcalendar object before we interpret its properties.
  $context = array(
    'source' => $source,
    'fetcher_result' => $fetcher_result,
  );
  drupal_alter('date_ical_icalcreator_calendar', $calendar, $context);

  //
  // Set a result object.
  //
  $result = new DateIcalParserResult();

  // FeedsResult properties
  $xcalname = $calendar
    ->getProperty('X-WR-CALNAME');
  $result->title = !empty($xcalname) ? $xcalname[1] : '';
  $xcaldesc = $calendar
    ->getProperty('X-WR-CALDESC');
  $result->description = !empty($xcaldesc) ? $xcaldesc[1] : '';
  $result->link = NULL;

  // Additional DateIcalParserResult properties
  $xtimezone = $calendar
    ->getProperty('X-WR-TIMEZONE');
  if (!empty($xtimezone)) {
    try {
      $tz = new DateTimeZone($xtimezone[1]);
      $result->timezone = $tz;
    } catch (Exception $e) {
      $source
        ->log('parse', 'Invalid X-WR-TIMEZONE: %error', array(
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_NOTICE);
    }
  }

  // DEV NOTES:
  // Due to the way that the loop after this one manipulates the $components array, all the work that gets done in here
  // gets overridden. However, we probably *should* be using this, somehow, since I think it was in the old version
  // in order to handle non-standard VTIMEZONES. Maybe?
  $components = array();
  while ($component = $calendar
    ->getComponent('VTIMEZONE')) {
    $components[$component
      ->getProperty('tzid')] = new DateIcalIcalcreatorComponent($component);
  }
  $result->timezones = $components;

  // Separate the individual feed items into DateIcalIcalcreatorComponents.
  $components = array();
  $component_types = array(
    'vevent',
    'vtodo',
    'vjournal',
    'vfreebusy',
    'valarm',
  );
  foreach ($component_types as $component_type) {
    while ($component = $calendar
      ->getComponent($component_type)) {
      $component = new DateIcalIcalcreatorComponent($component);

      // Allow modules to alter the DateIcalIcalcreatorComponent before we
      // parse it into Feeds-readable data.
      $context = array(
        'calendar' => $calendar,
        'source' => $source,
        'fetcher_result' => $fetcher_result,
        'parser_result' => $result,
      );
      drupal_alter('date_ical_icalcreator_component', $component, $context);
      $components[] = $component;
    }
  }
  $result->items = $components;
  return $result;
}