You are here

function calendar_ical_add_feeds in Calendar 5

Same name and namespace in other branches
  1. 5.2 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 206
Adds ical functionality to Calendar.

Code

function calendar_ical_add_feeds($view, $refresh = FALSE) {
  $nodes = array();
  calendar_load_date_api();
  calendar_ical_load_date_ical();
  $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());
      switch ($feed['type']) {
        case 'ical':
          $filename = $feed['url'];
          $events = date_ical_import($filename);
          $new_nodes = array();
          foreach ($events as $key => $value) {
            if (is_numeric($key) && $value['TYPE'] == 'VEVENT') {
              $date = date_ical_date($value['DTSTART'], $value['DTSTART']['tz']);
              $start = $date->local->timestamp;
              $start_timezone = $date->local->timezone;
              $start_offset = $date->local->offset;
              $date = date_ical_date($value['DTEND'], $value['DTEND']['tz']);
              $end = $date->local->timestamp;
              if (empty($start)) {
                continue;
              }
              $node = new StdClass();
              $node->nid = $value['UID'];
              $node->title = $value['SUMMARY'];
              $node->label = $feed['name'];
              $node->teaser = $value['DESCRIPTION'];
              $node->calendar_start = $start;
              $node->start_offset = $start_offset;
              $node->calendar_end = $end;
              $node->end_offset = $date->local->offset;
              $show_tz = ' e';
              $time_format = variable_get('calendar_time_format_' . $view->name, 'H:i');
              $full_format = date_limit_format(variable_get('date_format_short', 'm/d/Y - H:i'), $value['DTSTART']['granularity']) . $show_tz;
              $node->start_time_format = $value['DTSTART']['all_day'] ? t('All day') : date_format_date($time_format, $start, $start_offset, $start_timezone);
              $node->end_time_format = $value['DTEND']['all_day'] ? t('All day') : date_format_date($time_format, $end, $start_offset, $start_timezone);
              $node->start_format = date_format_date($full_format, $start, $start_offset, $start_timezone);
              $node->end_format = date_format_date($full_format, $end, $start_offset, $start_timezone);
              $node->all_day = $value['DTSTART']['all_day'];
              $node->stripe = $feed['stripe'];
              $node->remote = TRUE;
              $node->uid = $value['uid'] ? $value['UID'] : (is_numeric($node->nid) ? url("node/{$node->nid}", NULL, NULL, 1) : $node->nid);
              $node->url = $value['URL'] ? $value['URL'] : (is_numeric($node->nid) ? url("node/{$node->nid}", NULL, NULL, 1) : $node->nid);
              $node->calendar_node_theme = 'calendar_ical_node';
              $new_nodes[$value['UID']] = $node;
            }
          }
          cache_set('calendar_feeds_' . $view->name . ':' . $feed['url'], calendar_ical_cache(), serialize($new_nodes), $expire);
          $nodes += (array) $new_nodes;
          break;
      }
    }
  }
  return $nodes;
}