Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created June 6, 2012 15:37
Show Gist options
  • Select an option

  • Save nfreear/2882742 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/2882742 to your computer and use it in GitHub Desktop.
Drupal iCal parser plugin - HOOK_parser_ical_filter_item / ou_ical / E-PD / N.D.Freear 6 June 2012
<?php
// $Id$
/**
* @file
* parser_ical include for actually parsing feed.
*/
/**
* Parse iCal feeds.
*
* Pinched pretty much directly from version 1 for now.
*/
function _parser_ical_parse($feed_content) {
include_once(drupal_get_path('module', 'date_api') .'/date_api_ical.inc');
$feed_folded = explode("\n", $feed_content);
$ical_parsed = date_ical_parse($feed_folded);
// TODO, should we change this to a foreach() instead of just picking
// out the first one in case there is more than one CALENDAR grouping
// in a feed? Not sure that ever actually happens.
$ical = $ical_parsed[0]; // Pick out the first CALENDAR group.
// Any or all of these items could be missing, so always use isset() to test.
$parsed_source = array();
// Detect the title
$parsed_source['title'] = isset($ical['X-WR-CALNAME']) ? $ical['X-WR-CALNAME'] : '';
// Detect the description
$parsed_source['description'] = isset($ical['X-WR-CALDESC']) ? $ical['X-WR-CALDESC'] : '';
$parsed_source['link'] = ''; // @todo do we have one from the feed?
$parsed_source['date']['calscale'] = isset($ical['CALSCALE']) ? $ical['CALSCALE'] : '';
$parsed_source['date']['timezone'] = isset($ical['X-WR-TIMEZONE']) ? $ical['X-WR-TIMEZONE'] : '';
$parsed_source['items'] = array();
if (array_key_exists('VEVENT', $ical)) {
foreach ($ical['VEVENT'] as $event) {
$item = array();
$item['title'] = $event['SUMMARY'];
$item['description'] = isset($event['DESCRIPTION']) ? $event['DESCRIPTION'] : '';
$item['author'] = ''; // @todo can we get this, what format
$item['url'] = isset($event['URL']) ? $event['URL'] : '';
// Make sure a valid timestamp is created.
if (isset($event['DTSTAMP'])) {
$date = date_ical_date($event['DTSTAMP']);
if (date_is_valid($date, DATE_OBJECT)) {
$item['timestamp'] = date_format($date, 'U');
}
}
elseif (isset($event['DTSTART'])) {
// a majority of feeds do have a DTSTART and it's sensible enough for a node date
// although it maybe in the future...
$date = date_ical_date($event['DTSTART']);
if (date_is_valid($date, DATE_OBJECT)) {
$item['timestamp'] = date_format($date, 'U');
}
}
else {
// now falling back but it will cause constant updates
$item['timestamp'] = FEEDS_REQUEST_TIME;
}
$item['guid'] = isset($event['UID']) ? $event['UID'] : ''; // intention
$item['tags'] = isset($event['CATEGORIES']) ? explode(',', $event['CATEGORIES']) : array();
$item['ical_date'] = new ParserIcalDateTimeElement($event);
$item['ical_location'] = $event['LOCATION'];
//ou-specific
$item = module_invoke_all('parser_ical_filter_item', $item, $event);
if (isset($item['exclude'] && $item['exclude']) {
continue;
}
//ou-specific ends.
$parsed_source['items'][] = $item;
}
}
return $parsed_source;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment