You are here

public function ParserIcalCreator::parse in iCal feed parser 7.2

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

@params - change these to generic required paramters.

File

includes/ParserIcalCreator.inc, line 69
Basic classes.

Class

ParserIcalCreator
@file Basic classes.

Code

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

  // get the temporary file path
  $filename = $fetcher_result
    ->getFilePath();

  //
  // create the parser and load results
  //
  $config = array(
    'unique_id' => '',
    // do we need one for incoming parsing?
    'url' => $filename,
  );
  $parser = new vcalendar($config);
  if (!$parser
    ->parse()) {
    throw new ParserIcalParseException(t('Error parsing %source iCal file %file', array(
      '%source' => 'todo',
      '%file' => $filename,
    )));
  }

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

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

  // additional ParserIcalResult() properties
  $xprop = $parser
    ->getProperty('X-WR-TIMEZONE');
  if (!empty($xprop)) {
    try {
      $tz = new DateTimeZone($xprop[1]);
      $result->timezone = $tz;
    } catch (Exception $e) {
      $source
        ->log('parse', 'Invalid X-WR-TIMEZONE: %error', array(
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_NOTICE);
    }
  }
  $components = array();
  while ($component = $parser
    ->getComponent('TIMEZONE')) {
    $components[$component
      ->getProperty('tzid')] = new ParserIcalCreatorComponent($component);
  }
  $result->timezones = $components;

  // feed items themselves
  $components = array();
  $component_types = array(
    'vevent',
    'vtodo',
    'vjournal',
    'vfreebusy',
    'valarm',
  );

  // @todo: admin per feed configurable; check each makes sense.
  foreach ($component_types as $component_type) {
    while ($component = $parser
      ->getComponent($component_type)) {
      $components[] = new ParserIcalCreatorComponent($component);
    }
  }
  $result->items = $components;
  return $result;
}