You are here

function calendar_ical_add_feeds in Calendar 5.2

Same name and namespace in other branches
  1. 5 calendar_ical.module \calendar_ical_add_feeds()

Bring an ical feed into the calendar.

@todo probably need to add more validation of the results in case the url doesn't work.

Parameters

$view - the view being manipulated:

$refresh - whether or not to force a refresh of the feed:

Return value

$nodes to add to calendar

1 call to calendar_ical_add_feeds()
calendar_ical_calendar_add_items in ./calendar_ical.module
Implementation of hook_calendar_add_items().

File

./calendar_ical.module, line 97
Adds ical functionality to Calendar.

Code

function calendar_ical_add_feeds($view, $refresh = FALSE) {
  $nodes = array();
  $feeds = variable_get('calendar_feeds_' . $view->name, array());
  $expire = intval(variable_get('calendar_ical_expire_' . $view->name, 9676800) + time());
  foreach ($feeds as $delta => $feed) {
    if ($view->build_type == 'page') {
      $GLOBALS['calendar_stripe_labels'][$feed['stripe']] = $feed['name'];
    }
    if (!$refresh && ($cached = cache_get('calendar_feeds_' . $view->name . ':' . $feed['url'], calendar_ical_cache()))) {
      $nodes += (array) unserialize($cached->data);
    }
    else {
      $expire = intval(variable_get('calendar_ical_expire_' . $view->name, 9676800) + time());
      require_once './' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
      switch ($feed['type']) {
        case 'ical':
          $filename = $feed['url'];
          $default = $feed['link'];
          $new_nodes = array();
          $calendars = date_ical_import($filename);
          if (empty($calendars)) {
            continue;
          }
          foreach ($calendars as $calendar) {
            if (empty($calendar) || empty($calendar['VEVENT'])) {
              continue;
            }
            foreach ($calendar['VEVENT'] as $key => $value) {
              if (empty($value['DTSTART'])) {
                continue;
              }
              $node = new stdClass();
              $node->nid = $value['UID'];
              $node->title = $value['SUMMARY'];
              $node->label = $feed['name'];
              $node->teaser = $value['DESCRIPTION'];
              $node->timezone = $value['DTSTART']['tz'];
              $node->calendar_start_date = date_ical_date($value['DTSTART'], $value['DTSTART']['tz']);
              $node->calendar_start = date_format_date($node->calendar_start_date, 'custom', DATE_FORMAT_DATETIME);
              $node->calendar_end_date = date_ical_date($value['DTEND'], $value['DTEND']['tz']);
              $node->calendar_end = date_format_date($node->calendar_end_date, 'custom', DATE_FORMAT_DATETIME);
              $show_tz = ' e';
              $granularity = $value['DTSTART']['type'] == 'DATE' ? array(
                'year',
                'month',
                'day',
              ) : array(
                'year',
                'month',
                'day',
                'hour',
                'minute',
                'second',
              );
              $node->format_time = variable_get('calendar_time_format_' . $view->name, 'H:i');
              $node->format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), $granularity) . $show_tz;
              $node->all_day = $value['all_day'];
              $node->location = $value['LOCATION'];
              $node->stripe = $feed['stripe'];
              $node->stripe_label = $feed['name'];
              $node->remote = TRUE;
              $node->uid = $value['uid'] ? $value['UID'] : calendar_get_node_link($node, $default);
              $node->url = $value['URL'] ? $value['URL'] : calendar_get_node_link($node, $default);
              $node->calendar_node_theme = 'calendar_ical_node';
              $node->calendar_field_theme = 'calendar_ical_field';
              $new_nodes[$value['UID']] = $node;
            }
          }
          if (!empty($new_nodes)) {
            cache_set('calendar_feeds_' . $view->name . ':' . $feed['url'], calendar_ical_cache(), serialize($new_nodes), $expire);
            $nodes += (array) $new_nodes;
          }
          break;
      }
    }
  }
  return $nodes;
}