You are here

views_rss_views_fields.theme.inc in Views RSS 6

Same filename and directory in other branches
  1. 7 views/views_rss_views_fields.theme.inc

File

views/views_rss_views_fields.theme.inc
View source
<?php

/**
 * Template preprocessor for views-view-views-rss-fields.tpl.php.
 */
function template_preprocess_views_view_views_rss_fields(&$vars) {
  $view = $vars['view'];

  // Set basic info - title, description - about the feed
  if ($view->display_handler
    ->get_option('sitename_title')) {
    $title = variable_get('site_name', 'Drupal');
    if ($slogan = variable_get('site_slogan', '')) {
      $title .= ' - ' . $slogan;
    }
  }
  else {
    $title = $view
      ->get_title();
  }
  $vars['viewtitle'] = check_plain($title);
  if ($view->style_options['feed_settings']['feed_description']) {
    $description = $view->style_options['feed_settings']['feed_description'];
  }
  else {
    $description = variable_get('site_mission', '');
  }
  $vars['description'] = theme('views_rss_feed_description', $description, $view);

  // Base URL for link tag
  global $base_url;
  $vars['link'] = $base_url;

  // Grab the rows, push to field mapping function, gather namespaces.
  $elements = $namespaces = array();
  $rows = '';
  $items = $view->style_plugin
    ->map_rows($vars['rows']);
  foreach ($items as $item) {
    $rows .= theme('views_rss_fields_item', $item);
    foreach ($item as $k => $v) {
      $elements[$k] = $k;
    }
  }
  $vars['rows'] = $rows;
  foreach ($elements as $e) {
    if ($namespace = $view->style_plugin
      ->xml_namespace($e)) {
      $namespaces[] = "xmlns:{$namespace['local']}=\"{$namespace['namespace']}\"";
    }
  }
  $vars['namespaces'] = implode(' ', array_unique($namespaces));

  // Set Headers
  drupal_set_header('Content-Type: application/rss+xml; charset=utf-8');
}

/**
 * Template preprocessor for views-rss-fields-item.tpl.php.
 */
function template_preprocess_views_rss_fields_item(&$vars) {
  $row = '';
  foreach ($vars['item'] as $key => $values) {
    if ($values) {
      switch ($key) {

        // <enclosure> element was already formatter before
        // by selected field formatter.
        case 'enclosure':
        case 'source':
          $row .= $vars['item'][$key] . "\n";
          break;
        default:

          // Some feed elements could contain multiple values (ie. 'Category'),
          // therefore let's first make sure that EACH feed element is an array
          // (even if it has one value only) and then loop through all of them.
          if (!is_array($values)) {
            $values = array(
              $values,
            );
          }
          foreach ($values as $value) {
            $row .= "<{$key}>" . $value . "</{$key}>\n";
          }
      }
    }
  }
  $vars['row'] = $row;
}

/**
 * Theme function for feed icon.
 */
function theme_views_rss_feed_icon($url, $title, $icon) {
  if ($image = theme('image', $icon, t('Download RSS Feed'), $title)) {
    return '<a href="' . check_url($url) . '" class="feed-icon">' . $image . '</a>';
  }
}

/**
 * Theme function for feed description.
 */
function theme_views_rss_feed_description($description, $view) {
  return $description;
}

/**
 * Theme function for feed item <source> element.
 */
function theme_views_rss_feed_item_source($view) {
  global $base_url;
  return strtr('<source url="!feed_url">!feed_title</source>', array(
    '!feed_url' => $base_url . '/' . $view
      ->get_url(),
    '!feed_title' => $view
      ->get_title(),
  ));
}

/**
 * Generic formatter for 'filefield' CCK fields
 * added to RSS <enclosure> element.
 */
function theme_views_rss_formatter_enclosure_file($element) {

  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return;
  }

  // Return formatted XML element.
  return strtr('<enclosure url="!url" length="!length" type="!type" />', array(
    '!url' => url($element['#item']['filepath'], array(
      'absolute' => TRUE,
    )),
    '!length' => $element['#item']['filesize'],
    '!type' => $element['#item']['filemime'],
  ));
}

/**
 * Formatter for 'image' and 'filefield' CCK fields containing images
 * added to RSS <enclosure> element. Besides default image size
 * also provides option to use imagecache'd images.
 */
function theme_views_rss_formatter_enclosure_image($element) {

  // Inside a view $element may contain NULL data. In that case, just return.
  if (empty($element['#item']['fid'])) {
    return;
  }

  // Generate simple array of imagecache presets.
  $presets = array();
  foreach (imagecache_presets() as $preset) {
    $presets[] = $preset['presetname'];
  }

  // Get full image URL based on provided imagecache preset.
  $preset_name = str_replace('views_rss_enclosure_image', '', $element['#formatter']);
  if ($preset_name && in_array($preset_name, array_keys($presets))) {
    $path = imagecache_create_url($preset_name, $element['#item']['filepath']);
  }
  else {
    $path = $element['#item']['filepath'];
  }

  // Return formatted XML element.
  return strtr('<enclosure url="!url" length="!length" type="!type" />', array(
    '!url' => url($path, array(
      'absolute' => TRUE,
    )),
    '!length' => $element['#item']['filesize'],
    '!type' => $element['#item']['filemime'],
  ));
}

Functions

Namesort descending Description
template_preprocess_views_rss_fields_item Template preprocessor for views-rss-fields-item.tpl.php.
template_preprocess_views_view_views_rss_fields Template preprocessor for views-view-views-rss-fields.tpl.php.
theme_views_rss_feed_description Theme function for feed description.
theme_views_rss_feed_icon Theme function for feed icon.
theme_views_rss_feed_item_source Theme function for feed item <source> element.
theme_views_rss_formatter_enclosure_file Generic formatter for 'filefield' CCK fields added to RSS <enclosure> element.
theme_views_rss_formatter_enclosure_image Formatter for 'image' and 'filefield' CCK fields containing images added to RSS <enclosure> element. Besides default image size also provides option to use imagecache'd images.