From 45f729a86274c2dc14542cec1c9d15246e7f6ffc Mon Sep 17 00:00:00 2001
From: Guillaume Viguier-Just <guillaume@viguierjust.com>
Date: Mon, 20 Aug 2012 11:58:32 +0200
Subject: [PATCH] Issue #1721678 by sonicthoughts: ical parser

---
 date_ical.info              |    1 +
 date_ical.module            |   39 ++++++++++++++++++++++++
 plugins/FeedsIcalParser.inc |   69 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+)
 create mode 100644 plugins/FeedsIcalParser.inc

diff --git a/date_ical.info b/date_ical.info
index 2a7d13a..93672a4 100644
--- a/date_ical.info
+++ b/date_ical.info
@@ -4,5 +4,6 @@ package = Date/Time
 core = 7.x
 dependencies[] = date_views
 dependencies[] = entity
+dependencies[] = libraries (2.x)
 files[] = date_ical_plugin_row_ical_feed.inc
 files[] = date_ical_plugin_style_ical_feed.inc
diff --git a/date_ical.module b/date_ical.module
index 69a33c2..1422596 100644
--- a/date_ical.module
+++ b/date_ical.module
@@ -27,6 +27,45 @@ function date_ical_theme($existing, $type, $theme, $path) {
 }
 
 /**
+ * Implements hook_libraries_info()
+ */
+function date_ical_libraries_info() {
+  $libraries['ics-parser'] = array(
+    'name' => 'iCal Reader',
+    'vendor url' => 'http://code.google.com/p/ics-parser/',
+    'version' => '1.0',
+    'files' => array(
+      'php' => array(
+        'class.iCalReader.php'
+      )
+    )
+  );
+  return $libraries;
+}
+
+/**
+ * Implements hook_feeds_plugins()
+ */
+function date_ical_feeds_plugins() {
+  $info = array();
+  if (($library = libraries_detect('ics-parser')) && !empty($library['installed'])) {
+    $path = drupal_get_path('module', 'date_ical') . '/plugins';
+    $info['FeedsIcalParser'] = array(
+      'name' => 'Ical Parser',
+      'description' => 'Parse iCal feeds.',
+      'help' => 'Parse events provided in iCal format.',
+      'handler' => array(
+        'parent' => 'FeedsParser',
+        'class' => 'FeedsIcalParser',
+        'file' => 'FeedsIcalParser.inc',
+        'path' => $path,
+      ),
+    );
+  }
+  return $info;
+}
+
+/**
  * The theme for the ical icon.
  */
 function theme_date_ical_icon($variables) {
diff --git a/plugins/FeedsIcalParser.inc b/plugins/FeedsIcalParser.inc
new file mode 100644
index 0000000..d1f1ac9
--- /dev/null
+++ b/plugins/FeedsIcalParser.inc
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * Class definition for iCal Parser.
+ *
+ * Parses iCal feeds.
+ */
+class FeedsIcalParser extends FeedsParser {
+
+  /**
+   * Implements FeedsParser::parse().
+   */
+  public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
+    if (($library = libraries_load('ics-parser')) && !empty($library['loaded'])) {
+      $ical = new ical($fetcher_result->getFilePath());
+      $result = new FeedsParserResult();
+      $events = $ical->events();
+      if (is_array($events)) {
+        foreach ($events as &$item) {
+          if (isset($item['URL;VALUE=URI'])) {
+            $item['URL'] = $item['URL;VALUE=URI'];
+          }
+          $result->items[] = $item;
+        }
+      }
+      return $result;
+    }
+  }
+
+  /**
+   * Return mapping sources.
+   *
+   * At a future point, we could expose data type information here,
+   * storage systems like Data module could use this information to store
+   * parsed data automatically in fields with a correct field type.
+   */
+  public function getMappingSources() {
+    return array(
+      'SUMMARY' => array(
+        'name' => t('Title'),
+        'description' => t('Title of the event.'),
+      ),
+      'DESCRIPTION' => array(
+        'name' => t('Description'),
+        'description' => t('Description of the event.'),
+      ),
+      'DTSTART' => array(
+        'name' => t('Date: Start'),
+        'description' => t('Start Date of the event.'),
+      ),
+      'DTEND' => array(
+        'name' => t('Date: End'),
+        'description' => t('End Date of the event.'),
+      ),
+      'URL' => array(
+        'name' => t('URL'),
+        'description' => t('URL of the event.'),
+      ),
+      'RRULE' => array(
+        'name' => t('Repeat Rule'),
+        'description' => t('Repeat Rule of the event.'),
+      ),
+      'LOCATION' => array(
+        'name' => t('Location'),
+        'description' => t('Location of the event.'),
+      ),
+     ) + parent::getMappingSources();
+  }
+}
-- 
1.7.9.5

