You are here

function calendar_plugin_style_ical::render in Calendar 7.2

Same name and namespace in other branches
  1. 6.2 calendar_ical/calendar_plugin_style_ical.inc \calendar_plugin_style_ical::render()
  2. 7 calendar_ical/calendar_plugin_style_ical.inc \calendar_plugin_style_ical::render()

Render the display in this style.

Overrides views_plugin_style_rss::render

File

calendar_ical/calendar_plugin_style_ical.inc, line 147
Views style plugin for the Calendar iCal module.

Class

calendar_plugin_style_ical
Default style plugin to render an iCal feed.

Code

function render() {
  module_load_include('inc', 'calendar', 'includes/calendar');

  // Transfer the style options to the view object so they
  // can be easily accessed in the theme.
  $style_options = $this->options;
  $this->view->date_info->summary_field = $style_options['summary_field'];
  $this->view->date_info->description_field = $style_options['description_field'];
  $this->view->date_info->location_field = $style_options['location_field'];

  // Evaluate our argument values and figure out which values to use.
  $date_filter = NULL;
  foreach ($this->view->filter as $id => $handler) {
    if (date_views_handler_is_date($handler, 'filter')) {
      $date_filter = $handler;
      $key = 'filter';
      $name = $id;
    }
  }
  $i = 0;
  foreach ($this->view->argument as $id => $handler) {
    if (date_views_handler_is_date($handler, 'argument')) {
      $date_filter = $handler;
      $key = 'argument';
      $name = $id;
      $pos = $i;
      $i++;
    }
  }
  if (!empty($date_filter)) {
    if ($key == 'argument') {
      $this->view->date_info->date_arg = !empty($this->view->args) ? $this->view->args[$date_filter->position] : '';
      $this->view->date_info->date_arg_pos = $pos;

      // TODO Decide if we want to provide a date here or not.
      // Adding this now is to prevent fatal errors later if the
      // view is used in unexpected ways without a date being set.
      if (empty($date_filter->min_date)) {
        $value = $date_filter
          ->get_default_argument();
        $range = $date_filter->date_handler
          ->arg_range($value);
        $date_filter->min_date = $range[0];
        $date_filter->max_date = $range[1];
      }
    }
    elseif ($key == 'filter') {

      // TODO Decide if we want to provide a date here or not.
      // Adding this now is to prevent fatal errors later if the
      // view is used in unexpected ways without a date being set.
      if (empty($date_filter->min_date)) {
        $value = $date_filter
          ->date_default_value('value');
        $range = $date_filter->date_handler
          ->arg_range($value);
        $date_filter->min_date = $range[0];
        $date_filter->max_date = $range[1];
      }
    }
    $this->view->date_info->granularity = !empty($date_filter->granularity) ? $date_filter->granularity : $date_filter->options['granularity'];
    $this->view->date_info->min_date = $date_filter->min_date;
    $this->view->date_info->max_date = $date_filter->max_date;
    if (empty($this->view->date_info->date_fields)) {
      $this->view->date_info->date_fields = array();
    }
    $this->view->date_info->date_fields = array_merge($this->view->date_info->date_fields, array_keys($date_filter->options['date_fields']));
  }

  // Render each field into an output array.
  $result = (array) $this->view->result;
  $this->view->style_plugin
    ->render_fields($result);
  $rendered_items = !empty($this->view->style_plugin->rendered_fields) ? $this->view->style_plugin->rendered_fields : array();
  $items = array();
  $calendar_fields = date_views_fields($this->view->base_table);
  $calendar_fields = array_keys($calendar_fields['alias']);

  // Try to figure out the 'id' for this collection of items.
  // The id field is often not a field but instead an 'additional field',
  // so this is cludgy.
  foreach ($result as $num => $row) {
    $keys = array_keys((array) $row);
    foreach ($keys as $key) {
      if (strlen($key) == 3 && substr($key, -2) == 'id' && !empty($row->{$key})) {
        $id = $key;
      }
    }
  }
  foreach ($result as $num => $row) {
    $copy = clone $row;
    $items[$num]->id = !empty($row->{$id}) ? $row->{$id} : NULL;
    $items[$num]->rendered = !empty($rendered_items[$num]) ? $rendered_items[$num] : '';
    $items[$num]->raw = $copy;
    $items[$num]->calendar_fields = new stdClass();
    foreach ($row as $key => $value) {
      if (in_array($key, $calendar_fields)) {
        $items[$num]->calendar_fields->{$key} = $value;
      }
    }
  }

  // Massage the resulting items into formatted calendar items.
  $items = calendar_build_nodes($this->view, $items);

  // Merge in items from other sources.
  foreach (module_implements('calendar_add_items') as $module) {
    $function = $module . '_calendar_add_items';
    if (function_exists($function)) {
      if ($feeds = $function($this->view)) {
        foreach ($feeds as $feed) {
          $items = $feed;
        }
      }
    }
  }
  return theme($this
    ->theme_functions(), array(
    'view' => $this->view,
    'options' => $this->options,
    'items' => $items,
  ));
}