You are here

class date_ical_plugin_style_ical_feed in Date iCal 7.3

Same name and namespace in other branches
  1. 7 date_ical_plugin_style_ical_feed.inc \date_ical_plugin_style_ical_feed
  2. 7.2 includes/date_ical_plugin_style_ical_feed.inc \date_ical_plugin_style_ical_feed

Defines a Views style plugin that renders iCal feeds.

Hierarchy

Expanded class hierarchy of date_ical_plugin_style_ical_feed

1 string reference to 'date_ical_plugin_style_ical_feed'
date_ical_views_plugins in includes/date_ical.views.inc
Implements hook_views_plugins().

File

includes/date_ical_plugin_style_ical_feed.inc, line 10
Views style plugin for the Date iCal module.

View source
class date_ical_plugin_style_ical_feed extends views_plugin_style {

  /**
   * Internal helper function.
   */
  protected function _get_option($option_name) {
    return isset($this->options[$option_name]) ? $this->options[$option_name] : '';
  }

  /**
   * Sets up the iCal feed icon on calendar pages.
   */
  public function attach_to($display_id, $path, $title) {
    $url_options = array();
    $input = $this->view
      ->get_exposed_input();
    if ($input) {
      $url_options['query'] = $input;
    }
    $url_options['absolute'] = TRUE;

    // Only add arguments to ical path for the contextual filters in the feed display.
    // Clone view to prevent affecting the page view where this is attached.
    $clone = $this->view
      ->clone_view();
    $clone
      ->set_display($this->display->id);
    $contextual_filters = $clone
      ->get_items('argument');
    $arg_number = count($contextual_filters);

    // Only include as many arguments as the feed display supports.
    foreach ($clone->args as $key => $arg) {
      if ($key >= $arg_number) {
        unset($clone->args[$key]);
      }
    }
    $url = url($clone
      ->get_url(NULL, $path), $url_options);

    // If the user didn't disable the option, change the scheme to webcal://
    // so calendar clients can automatically subscribe via the iCal link.
    if (!$this
      ->_get_option('disable_webcal')) {
      $url = str_replace(array(
        'http://',
        'https://',
      ), 'webcal://', $url);
    }

    // Render the feed icon and header tag (except during a View Preview and
    // if the display is disabled).
    if (empty($this->view->live_preview) && (!isset($this->display->display_options['enabled']) || $this->display->display_options['enabled'])) {
      $tooltip = t('Add to My Calendar');
      if (!isset($this->view->feed_icon)) {

        // In PHP 5.5, you're not allowed to concatinate onto an unset
        // property. But we need to do a concat, because there may be
        // other attachments.
        $this->view->feed_icon = '';
      }
      $variables = array(
        'url' => check_url($url),
        'tooltip' => $tooltip,
        'view' => $this->view,
      );
      $this->view->feed_icon .= theme('date_ical_icon', $variables);
      drupal_add_html_head_link(array(
        'rel' => 'alternate',
        'type' => 'text/calendar',
        'title' => $tooltip,
        'href' => $url,
      ));
    }
  }

  /**
   * Set up the options for the style plugin.
   */
  public function option_definition() {
    $false_bool = array(
      'default' => FALSE,
      'bool' => TRUE,
    );
    $options = parent::option_definition();
    $options['cal_name'] = array(
      'default' => array(),
    );
    $options['no_calname'] = $false_bool;
    $options['disable_webcal'] = $false_bool;
    $options['exclude_dtstamp'] = $false_bool;
    $options['unescape_punctuation'] = $false_bool;
    return $options;
  }

  /**
   * Build the form for setting the style plugin's options.
   */
  public function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);

    // Allow users to override the default Calendar name (X-WR-CALNAME).
    $form['cal_name'] = array(
      '#type' => 'textfield',
      '#title' => t('iCal Calendar Name'),
      '#default_value' => $this
        ->_get_option('cal_name'),
      '#description' => t('This will appear as the title of the iCal feed. If left blank, the View Title will be used.
        If that is also blank, the site name will be inserted as the iCal feed title.'),
    );
    $form['no_calname'] = array(
      '#type' => 'checkbox',
      '#title' => t('Exclude Calendar Name'),
      '#default_value' => $this
        ->_get_option('no_calname'),
      '#description' => t("Excluding the X-WR-CALNAME value from the iCal Feed causes\n        some calendar clients to add the events in the feed to an existing calendar, rather\n        than creating a whole new calendar for them."),
    );
    $form['disable_webcal'] = array(
      '#type' => 'checkbox',
      '#title' => t('Disable webcal://'),
      '#default_value' => $this
        ->_get_option('disable_webcal'),
      '#description' => t("By default, the feed URL will use the webcal:// scheme, which allows calendar\n        clients to easily subscribe to the feed. If you want your users to instead download this iCal\n        feed as a file, activate this option."),
    );
    $form['exclude_dtstamp'] = array(
      '#type' => 'checkbox',
      '#title' => t('Exclude DTSTAMP'),
      '#default_value' => $this
        ->_get_option('exclude_dtstamp'),
      '#description' => t("By default, the feed will set each event's DTSTAMP property to the time at which the feed got downloaded.\n        Some feed readers will (incorrectly) look at the DTSTAMP value when they compare different downloads of the same feed, and\n        conclcude that the event has been updated (even though it hasn't actually changed). Enable this option to exclude the DTSTAMP\n        field from your feeds, so that these buggy feed readers won't mark every event as updated every time they check."),
    );
    $form['unescape_punctuation'] = array(
      '#type' => 'checkbox',
      '#title' => t('Unescape Commas and Semicolons'),
      '#default_value' => $this
        ->_get_option('unescape_punctuation'),
      '#description' => t('In order to comply with the iCal spec, Date iCal will "escape" commas and semicolons (prepend them with backslashes).
        However, many calendar clients are bugged to not unescape these characters, leaving the backslashes littered throughout your events.
        Enable this option to have Date iCal unescape these characters before it exports the iCal feed.'),
    );
  }

  /**
   * Render the event arrays returned by the row plugin into a VCALENDAR.
   */
  public function render() {
    if (empty($this->row_plugin) || !in_array($this->row_plugin->plugin_name, array(
      'date_ical',
      'date_ical_fields',
    ))) {
      debug('date_ical_plugin_style_ical_feed: This style plugin supports only the "iCal Entity" and "iCal Fields" row plugins.', NULL, TRUE);
      return t('To enable iCal output, the view Format must be configured to Show: iCal Entity or iCal Fields.');
    }
    if ($this->row_plugin->plugin_name == 'date_ical_fields' && empty($this->row_plugin->options['date_field'])) {

      // Because the Date field is required by the form, this error state will
      // rarely occur. But I ran across it during testing, and the error that
      // resulted was totally non-sensical, so I'm adding this just in case.
      return t("When using the iCal Fields row plugin, the Date field is required. Please set it up using the Settings link under 'Format -> Show: iCal Fields'.");
    }
    $events = array();
    foreach ($this->view->result as $row_index => $row) {
      $this->view->row_index = $row_index;
      $row->index = $row_index;
      try {
        $events[] = $this->row_plugin
          ->render($row);
      } catch (Exception $e) {
        debug($e
          ->getMessage(), NULL, TRUE);
        return $e
          ->getMessage();
      }
    }
    unset($this->view->row_index);

    // Try to load the iCalcreator library.
    $library = libraries_load('iCalcreator');
    if (!$library['loaded']) {

      // The iCalcreator library isn't available, so we can't output anything.
      $output = t('Please install the iCalcreator library to enable iCal output.');
    }
    else {

      // Create a vcalendar object using the iCalcreator library.
      $config = array(
        'unique_id' => 'Date iCal',
      );
      $vcalendar = new vcalendar($config);
      $vcalendar
        ->setMethod('PUBLISH');

      // Only include the X-WR-CALNAME property if the user didn't enable
      // the "Exclude Calendar Name" option.
      if (!$this
        ->_get_option('no_calname')) {
        $cal_name = $this
          ->_get_option('cal_name');
        if (empty($cal_name)) {
          $cal_name = $this->view
            ->get_title();
          if (empty($cal_name)) {
            $cal_name = variable_get('site_name', 'Drupal');
          }
        }
        if (!empty($cal_name)) {
          $vcalendar
            ->setProperty('X-WR-CALNAME', $cal_name, array(
            'VALUE' => 'TEXT',
          ));
        }
      }

      // Now add the VEVENTs.
      $timezones = array();
      foreach ($events as $event) {
        if (empty($event)) {

          // The row plugin returned NULL for this row, which can happen due to
          // either various error conditions, or because an RRULE is involved.
          // When this happens, just skip it.
          continue;
        }
        $vevent = $vcalendar
          ->newComponent('vevent');
        $vevent
          ->setUid($event['uid']);
        $vevent
          ->setSummary($event['summary']);

        // Get the start date as an array.
        $start = $event['start']
          ->toArray();
        $start_timezone = $event['start']
          ->getTimezone()
          ->getName();
        $timezones[$start_timezone] = $start_timezone;
        if ($event['all_day']) {

          // All Day events need to be DATEs, rather than DATE-TIMEs.
          $vevent
            ->setDtstart($start['year'], $start['month'], $start['day'], FALSE, FALSE, FALSE, FALSE, array(
            'VALUE' => 'DATE',
          ));
        }
        else {
          $vevent
            ->setDtstart($start['year'], $start['month'], $start['day'], $start['hour'], $start['minute'], $start['second'], $start_timezone);
        }

        // Add the Timezone info to the start date, for use later.
        $start['tz'] = $event['start']
          ->getTimezone();

        // Only add the end date if there is one.
        if (!empty($event['end'])) {
          $end = $event['end']
            ->toArray();
          $end_timezone = $event['end']
            ->getTimezone()
            ->getName();
          $timezones[$end_timezone] = $end_timezone;
          if ($event['all_day']) {
            $vevent
              ->setDtend($end['year'], $end['month'], $end['day'], FALSE, FALSE, FALSE, FALSE, array(
              'VALUE' => 'DATE',
            ));
          }
          else {
            $vevent
              ->setDtend($end['year'], $end['month'], $end['day'], $end['hour'], $end['minute'], $end['second'], $end_timezone);
          }
          $end['tz'] = $event['end']
            ->getTimezone();
        }

        // Handle repeating dates from the date_repeat module.
        if (!empty($event['rrule']) && module_exists('date_repeat')) {

          // Split the rrule into an RRULE and any additions and exceptions.
          module_load_include('inc', 'date_api', 'date_api_ical');
          module_load_include('inc', 'date_repeat', 'date_repeat_calc');
          list($rrule, $exceptions, $additions) = date_repeat_split_rrule($event['rrule']);

          // Add the RRULE itself. We need to massage the data a bit, since
          // iCalcreator expects RRULEs to be in a different format than how
          // Date API gives them to us.
          $vevent
            ->setRrule(_date_ical_convert_rrule_for_icalcreator($rrule));

          // Convert any exceptions to EXDATE properties.
          if (!empty($exceptions)) {
            $exdates = array();
            foreach ($exceptions as $exception) {
              $except = date_ical_date($exception, 'UTC');
              $except
                ->setTimezone($start['tz']);
              $exception_array = $except
                ->toArray();
              $exdates[] = array(
                'year' => $exception_array['year'],
                'month' => $exception_array['month'],
                'day' => $exception_array['day'],
                // Use the time information from the start date, since Date
                // doesn't store time info for EXDATEs.
                'hour' => $start['hour'],
                'min' => $start['minute'],
                'second' => $start['second'],
                'tz' => $start['tz']
                  ->getName(),
              );
            }

            // Add each exclusion as a separate EXDATE property.
            // The spec supports putting multiple date values into one EXDATE,
            // but several popular calendar clients (*cough* Apple *cough*)
            // are bugged, and do not recognize multi-value EXDATEs.
            $value = $event['all_day'] == 1 ? "DATE" : "DATE-TIME";
            foreach ($exdates as $exdate) {
              $vevent
                ->setExdate(array(
                $exdate,
              ), array(
                "VALUE" => $value,
              ));
            }
          }

          // Convert any additions to RDATE properties.
          if (!empty($additions)) {
            $rdates = array();
            foreach ($additions as $addition) {
              $add = date_ical_date($addition, 'UTC');
              $add
                ->setTimezone($start['tz']);
              $addition_array = $add
                ->toArray();
              $rdate = array(
                'year' => $addition_array['year'],
                'month' => $addition_array['month'],
                'day' => $addition_array['day'],
                // If the user's copy of Date has support for time in RDATEs,
                // use that. Otherwise use the time from the start date.
                'hour' => !empty($addition_array['hour']) ? $addition_array['hour'] : $start['hour'],
                'min' => !empty($addition_array['minute']) ? $addition_array['minute'] : $start['minute'],
                'second' => !empty($addition_array['second']) ? $addition_array['second'] : $start['second'],
                'tz' => $start['tz']
                  ->getName(),
              );

              // If an end date was was calculated above, use that too.
              // iCalcreator expects RDATEs that have end dates to be
              // specified as array($start_rdate, $end_rdate).
              if (isset($end)) {
                $rdate_with_end = array(
                  $rdate,
                );
                $rdate_with_end[] = array(
                  'year' => $addition_array['year'],
                  'month' => $addition_array['month'],
                  'day' => $addition_array['day'],
                  // If the user's copy of Date has support for time in RDATEs,
                  // use that. Otherwise use the time from the end date.
                  'hour' => !empty($addition_array['hour']) ? $addition_array['hour'] : $end['hour'],
                  'min' => !empty($addition_array['minute']) ? $addition_array['minute'] : $end['minute'],
                  'second' => !empty($addition_array['second']) ? $addition_array['second'] : $end['second'],
                  'tz' => $end['tz']
                    ->getName(),
                );
                $rdate = $rdate_with_end;
              }
              $rdates[] = $rdate;
            }

            // Add each addition as a separate RDATE property.
            // The spec supports putting multiple date values into one RDATE,
            // but several popular calendar clients (*cough* Apple *cough*)
            // are bugged, and do not recognize multi-value RDATEs.
            foreach ($rdates as $rdate) {
              $vevent
                ->setRdate(array(
                $rdate,
              ));
            }
          }
        }
        if (!empty($event['url'])) {
          $vevent
            ->setUrl($event['url'], array(
            'type' => 'URI',
          ));
        }
        if (!empty($event['location'])) {
          $vevent
            ->setLocation($event['location']);
        }
        if (!empty($event['description'])) {
          $vevent
            ->setDescription($event['description']);
        }
        if (!empty($event['categories'])) {
          $vevent
            ->setCategories($event['categories']);
        }
        if (!empty($event['last-modified'])) {
          $lm = $event['last-modified']
            ->toArray();
          $vevent
            ->setLastModified($lm['year'], $lm['month'], $lm['day'], $lm['hour'], $lm['minute'], $lm['second'], $lm['timezone']);
        }
        if (!empty($event['created'])) {
          $created = $event['created']
            ->toArray();
          $vevent
            ->setCreated($created['year'], $created['month'], $created['day'], $created['hour'], $created['minute'], $created['second'], $created['timezone']);
        }

        // Allow other modules to alter the vevent before it's exported.
        drupal_alter('date_ical_export_vevent', $vevent, $this->view, $event);
      }

      // Now add to the calendar all the timezones used by the events.
      foreach ($timezones as $timezone) {
        if (strtoupper($timezone) != 'UTC') {
          iCalUtilityFunctions::createTimezone($vcalendar, $timezone);
        }
      }

      // Allow other modules to alter the vcalendar before it's exported.
      drupal_alter('date_ical_export_vcalendar', $vcalendar, $this->view);
      $output = $vcalendar
        ->createCalendar();

      // iCalcreator escapes all commas and semicolons in string values, as the
      // spec demands. However, some calendar clients are buggy and fail to
      // unescape these characters. Users may choose to unescape them here to
      // sidestep those clients' bugs.
      // NOTE: This results in a non-compliant iCal feed, but it seems like a
      // LOT of major clients are bugged this way.
      if ($this
        ->_get_option('unescape_punctuation')) {
        $output = str_replace('\\,', ',', $output);
        $output = str_replace('\\;', ';', $output);
      }

      // In order to respect the Exclude DTSTAMP option, we unfortunately have
      // to parse out the DTSTAMP properties after they get rendered. Simply
      // using deleteProperty('DTSTAMP') doesn't work, because iCalcreator
      // considers the DTSTAMP to be essential, and will re-create it when
      // createCalendar() is called.
      if ($this
        ->_get_option('exclude_dtstamp')) {
        $filtered_lines = array();
        foreach (explode("\r\n", $output) as $line) {
          if (strpos($line, 'DTSTAMP') === 0) {
            continue;
          }
          $filtered_lines[] = $line;
        }
        $output = implode("\r\n", $filtered_lines);
      }
    }

    // These steps shouldn't be run during Preview on the View page.
    if (empty($this->view->live_preview)) {

      // Prevent devel module from appending queries to ical export.
      $GLOBALS['devel_shutdown'] = FALSE;
      drupal_add_http_header('Content-Type', 'text/calendar; charset=UTF-8');
      drupal_add_http_header('Cache-Control', 'no-cache, must-revalidate');
      drupal_add_http_header('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');

      // For sites with Clean URLs disabled, the Display's "path" value ends
      // up only in the query args, meaning the filename won't be set properly
      // when users download the feed. So we need to manually instruct browsers
      // to download a .ics file.
      if (!variable_get('clean_url', FALSE)) {
        $path_array = explode('/', $this->display->display_options['path']);
        $filename = end($path_array);
        drupal_add_http_header('Content-Disposition', "attachment; filename=\"{$filename}\"");
      }
    }

    // Allow other modules to alter the rendered calendar.
    drupal_alter('date_ical_export_post_render', $output, $this->view);
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
date_ical_plugin_style_ical_feed::attach_to public function Sets up the iCal feed icon on calendar pages.
date_ical_plugin_style_ical_feed::options_form public function Build the form for setting the style plugin's options. Overrides views_plugin_style::options_form
date_ical_plugin_style_ical_feed::option_definition public function Set up the options for the style plugin. Overrides views_plugin_style::option_definition
date_ical_plugin_style_ical_feed::render public function Render the event arrays returned by the row plugin into a VCALENDAR. Overrides views_plugin_style::render
date_ical_plugin_style_ical_feed::_get_option protected function Internal helper function.
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::init public function Initialize a style plugin.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate