<?php

/**
 * Implementation of hook_views_api().
 */
function ffpc_views_api() {
  return array('api' => 2);
}

function template_preprocess_ffpc_view_podcast_feed(&$vars) {
  global $base_url;
  global $language;

  $view     = &$vars['view'];
  $options  = &$vars['options'];
  $items    = &$vars['rows'];
  $style    = &$view->style_plugin;

  if (!empty($options['mission_description'])) {
    $description = variable_get('site_mission', '');
  }
  else {
    $description = $options['description'];
  }

  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();
  }

  // Figure out which display which has a path we're using for this feed. If there isn't
  // one, use the global $base_url
  $link_display_id = $view->display_handler->get_link_display();
  if ($link_display_id && !empty($view->display[$link_display_id])) {
    $path = $view->display[$link_display_id]->handler->get_path();
  }

  if ($path) {
    $path = $view->get_url(NULL, $path);
    $url_options = array('absolute' => TRUE);
    if (!empty($view->exposed_raw_input)) {
      $url_options['query'] = $view->exposed_raw_input;
    }

    // Compare the link to the default home page; if it's the default home page, just use $base_url.
    if ($path == variable_get('site_frontpage', 'node')) {
      $path = '';
    }

    $vars['link'] = check_url(url($path, $url_options));
  }

  // This is where we add additional elements to the podcast.
  $args = array(
    'itunes:owner' => array(
      'itunes:email' => variable_get('site_mail', ini_get('sendmail_from')),
    ),
  );
  
  $vars['namespaces'] = drupal_attributes($style->namespaces);
  $vars['channel'] = format_rss_channel($title, $vars['link'], $description, $items, $language->language, $args);

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