<?php

/**
 * Implementation of hook_perm(). [Channel]
 */
function ffpc_perm() {
  return array('administer ffpc');
}

/**
 * Implementation of hook_menu(). [Channel]
 */
function ffpc_menu() {
  $items['admin/settings/ffpc'] = array(
    'description' => 'Filefield Podcast settings.',
    'title' => 'Filefield Podcast',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('ffpc_admin_settings'),
    'access arguments' => array('administer ffpc'),
  );
  return $items;
}

/**
 * Admin settings form [Channel]
 */
function ffpc_admin_settings() {
  $form['ffpc_item_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings for individual podcast episodes'),
    '#description' => t('Control where various values come from.')
  );
  $form['ffpc_item_settings']['ffpc_episode_title_field'] = array(
    '#type' => 'radios',
    '#description' => t('Control where the title of each episode will come from. The node title or from within the file itself (using the ID3 tag).'),
    '#title' => t('Episode titles'),
    "#default_value" => variable_get('ffpc_episode_title_field', 'node'),
    '#options' => array('node' => t('Node title'), 'file' => t('File ID3 tag')),
  );

  return system_settings_form($form);
}

/**
 * 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));
	// [Channel]
	$channel->link = $vars['link']; // Default link is the views path
  }
  
  // [Channel]
  // Prepare channel data for formatting
  $channel->language = $language->language;
  $channel->args = array(); // Set by our own hook_ffpc_channel()
  
  // Allow modules to modify/add extra information via hooks
  // $channel contains specific key->value pairs for channel data
  // the $view is also passed so modules can utilize its data
  module_invoke_all('ffpc_channel', $channel, $view);
  if (is_array($channel)) {
    $channel = $channel[0];
  }
  

  // 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);
  /* ffpc without ffpc_channels
  $vars['channel'] = format_rss_channel($title, $vars['link'], $description, $items, $language->language, $args);
  */
  
  // [Channel]
  // module_invoke_all merges duplicate keys on arrays, we're using our own hook for a default object (see hook_ffpc_channel)
  $vars['channel'] = format_rss_channel($channel->title,
                                        $channel->link,
                                        $channel->description,
                                        $items, // Items can only be modified through hook_nodeapi > rss item
                                        $channel->language,
                                        $channel->args
                                        );

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


// [Channel]
/**
 * Implementation of hook_ffpc_channel().
 * Allows modules to add/modify values to $channel object
 */
function ffpc_ffpc_channel(&$channel){
  // This is where we add additional elements to the podcast.
  $channel->args = array(
    'itunes:owner' => array(
      'itunes:email' => variable_get('site_mail', ini_get('sendmail_from')),
    ),
  );

  return $channel; // Only ffpc returns a value, don't return a value in your module
}