You are here

public function DateiCalFeedsParser::parse in Date iCal 7.3

Implements FeedsParser::parse().

File

includes/DateiCalFeedsParser.inc, line 19
DateiCalFeedsParser is Date iCal's Feeds parser plugin.

Class

DateiCalFeedsParser
@file DateiCalFeedsParser is Date iCal's Feeds parser plugin.

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  $library = libraries_load('iCalcreator');
  if (!$library['loaded']) {
    throw new DateIcalException(t('Unable to load the iCalcreator library. Please ensure that it is properly installed.'));
  }
  $state = $source
    ->state(FEEDS_PARSE);

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

  // Parse the feed into an iCalcreator vcalendar object.
  $calendar = new vcalendar();
  if ($calendar
    ->parse($ical_feed_contents) === FALSE) {
    $plugin = $source->importer->config['fetcher']['plugin_key'];
    $url = $source->config[$plugin]['source'];
    throw new DateIcalException(t('Parsing the data from %url failed. Please ensure that this URL leads to a valid iCal feed.', array(
      '%url' => $url,
    )));
  }

  // Total hack to get around iCalcreator's mistreatment of UID "0".
  if (empty($calendar->components[0]->uid) || empty($calendar->components[0]->uid['value'])) {
    $calendar->components[0]->uid = array(
      'value' => 'zero',
      'params' => NULL,
    );
  }

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

  // We've got a vcalendar object created from the feed data. Now we need to
  // convert that vcalendar into an array of Feeds-compatible data arrays.
  // ParserVcalendar->parse() does that.
  require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_ical') . '/libraries/ParserVcalendar.inc';
  $parser = new ParserVcalendar($calendar, $source, $fetcher_result, $source
    ->getConfigFor($this));

  // Using the stored progress pointer (or 0 if it's not set),
  // determine which section of the feed to parse, then parse it.
  $offset = isset($state->pointer) ? $state->pointer : 0;
  $limit = $source->importer
    ->getLimit();
  $rows = $parser
    ->parse($offset, $limit);

  // Report progress.
  $state->total = $parser
    ->getTotalComponents();

  // We need to add 1 to the index of the last parsed componenent so that
  // the subsequent batch starts on the first unparsed component.
  $state->pointer = $parser
    ->getLastComponentParsed() + 1;
  $state
    ->progress($state->total, $state->pointer);
  return new FeedsParserResult($rows);
}