You are here

ParserIcalDate.inc in iCal feed parser 7.2

Classes for using date_api ical parsing.

File

includes/ParserIcalDate.inc
View source
<?php

/**
 * @file
 *  Classes for using date_api ical parsing.
 */
class ParserIcalDateModule extends ParserIcalFeeds {

  /**
   * Output sources this parser offers.
   *
   * Includes additional field for the handler for output.
   *
   * @see ParserIcalCreator::getMappingSources().
   * @see ParserIcalCreator::getSourceElement().
   */
  protected static $sources = array(
    'summary' => array(
      'name' => 'Summary',
      'description' => 'A short summary or subject for the calendar component.',
      'parser_ical_handler' => 'formatText',
    ),
    'description' => array(
      'name' => 'Description',
      'description' => 'A more complete description calendar component than that provided by the "summary" property.',
      'parser_ical_handler' => 'formatText',
    ),
    'dtstart' => array(
      'name' => 'Date for feed item',
      'description' => 'Start time for the feed item.',
      'parser_ical_handler' => 'formatDateTime',
    ),
    /*    'dtend' => array(
          'name' => 'Date end',
          'description' => 'End time for the feed item.',
          'parser_ical_handler' => 'formatDateTime',
          ), */
    'uid' => array(
      'name' => 'UID',
      'description' => 'UID of feed item',
      'parser_ical_handler' => 'formatText',
    ),
    'url' => array(
      'name' => 'URL',
      'description' => 'URL for the feed item.',
      'parser_ical_handler' => 'formatText',
    ),
    'location' => array(
      'name' => 'Location text',
      'description' => 'Text of location property of feed item.',
      'parser_ical_handler' => 'formatText',
    ),
  );

  /**
   * Load and run parser implementation of FeedsParser::parse().
   *
   * @params - change these to generic required paramters.
   */
  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
    self::loadLibrary();
    $feed_output = $fetcher_result
      ->getRaw();
    $feed_folded = explode("\n", $feed_output);
    $parsed = date_ical_parse($feed_folded);
    $ical = $parsed[0];
    $result = new ParserIcalResult();
    $result->title = isset($ical['X-WR-CALNAME']) ? $ical['X-WR-CALNAME'] : '';
    $result->description = isset($ical['X-WR-CALDESC']) ? $ical['X-WR-CALDESC'] : '';
    $timezone = isset($ical['X-WR-TIMEZONE']) ? $ical['X-WR-TIMEZONE'] : '';
    if (!empty($timezone)) {
      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();
    if (isset($ical['VEVENT'])) {
      foreach ($ical['VEVENT'] as $event) {
        $components[] = new ParserIcalDateModuleComponent('vevent', $event);
      }
    }
    $result->items = $components;
    return $result;
  }

  /******
   * Source output formatters.
   *
   * Could be in a class of their own?
   **/

  /**
   * Format text fields.
   */
  public function formatText($property_key, $property, ParserIcalComponentInterface $item, FeedsParserResult $result, FeedsSource $source) {
    return $property;
  }

  /**
   * Format datetime fields.
   */
  public function formatDateTime($property_key, $property, ParserIcalComponentInterface $item, FeedsParserResult $result, FeedsSource $source) {
    $dtstart = $item
      ->getProperty("dtstart");
    $dtend = $item
      ->getProperty("dtend");

    # drupal_set_message(print_r($dtstart, TRUE));
    $dstart = new FeedsDateTime($dtstart["datetime"]);
    $dend = new FeedsDateTime($dtend["datetime"]);
    $dt = new FeedsDateTimeElement($dstart, $dend, $dtstart["tz"]);

    # drupal_set_message(print_r($property_key, TRUE));

    # drupal_set_message(print_r($dt, TRUE));

    # drupal_set_message("<pre>" . print_r($item, TRUE) . "</pre>");
    return $dt;
  }

  /**
   * Load external iCalcreator class from libary.
   */
  public static function loadLibrary() {
    include_once drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
  }

}

/**
 * A wrapper on iCalcreator component class.
 */
class ParserIcalDateModuleComponent implements ParserIcalComponentInterface {
  protected $component;
  protected $type;
  public function __construct($type, $component) {
    $this->type = strtoupper($type);
    $this->component = $component;
  }
  public function getComponentType() {
    return $this->type;
  }
  public function setComponentType($type) {
    $this->type = strtoupper($type);
  }
  public function getProperty($name) {
    $name = strtoupper($name);
    return isset($this->component[$name]) ? $this->component[$name] : NULL;
  }
  public function setProperty($name, $value) {
    $name = strtoupper($name);
    $this->component[$name] = $value;
  }

}

Classes

Namesort descending Description
ParserIcalDateModule @file Classes for using date_api ical parsing.
ParserIcalDateModuleComponent A wrapper on iCalcreator component class.