You are here

views_rss_yandex.module in Views RSS: Yandex Elements 6

Same filename and directory in other branches
  1. 8 views_rss_yandex.module
  2. 7 views_rss_yandex.module

Provides Yandex namespace and <item> elements for Views RSS module.

File

views_rss_yandex.module
View source
<?php

/**
 * @file
 * Provides Yandex namespace and <item> elements for Views RSS module.
 */

/**
 * Implements hook_views_rss_namespaces().
 */
function views_rss_yandex_views_rss_namespaces() {
  $namespaces['yandex'] = array(
    'prefix' => 'xmlns',
    'uri' => 'http://news.yandex.ru',
  );
  if (!module_exists('views_rss_media')) {
    $namespaces['media'] = array(
      'prefix' => 'xmlns',
      'uri' => 'http://search.yahoo.com/mrss/',
    );
  }
  return $namespaces;
}

/**
 * Implements hook_views_rss_channel_elements().
 */
function views_rss_yandex_views_rss_channel_elements() {
  $elements['yandex:logo'] = array(
    'description' => t('Path to the image to be used as the artwork for your feed, for example <em>sites/default/files/AllAboutEverything.jpg</em>. Allowed image formats are GIF, JPEG or PNG.'),
    'preprocess functions' => array(
      'views_rss_yandex_preprocess_channel_yandex_logo',
    ),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:logo type=square'] = array(
    'description' => t('Path to the image to be used as the artwork for your feed, for example <em>sites/default/files/AllAboutEverything.jpg</em>. Allowed image formats are GIF, JPEG or PNG. Minimum image width and height is 180 pixels.'),
    'preprocess functions' => array(
      'views_rss_yandex_preprocess_channel_yandex_logo_square',
    ),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  return $elements;
}

/**
 * Preprocess function for channel <yandex:logo> element.
 */
function views_rss_yandex_preprocess_channel_yandex_logo(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['value'])) {
    return;
  }
  $path = $variables['value'];
  $variables['value'] = file_create_url($path);
}

/**
 * Preprocess function for channel <yandex:logo type="square"> element.
 */
function views_rss_yandex_preprocess_channel_yandex_logo_square(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['value'])) {
    return;
  }

  // Change the element key and add the type="square" attribute.
  $path = $variables['value'];
  $variables['value'] = array(
    'value' => file_create_url($path),
    'element' => 'yandex:logo',
    'arguments' => array(
      'type' => 'square',
    ),
  );
}

/**
 * Implements hook_views_rss_item_elements().
 */
function views_rss_yandex_views_rss_item_elements() {
  $elements['yandex:full-text'] = array(
    'description' => t('Full message text for search index.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
    'preprocess functions' => array(
      'views_rss_htmlspecialchars',
    ),
  );
  $elements['yandex:genre'] = array(
    'description' => t('Should be either lenta, message, article or interview.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:tags'] = array(
    'description' => t('Tags, separate multiple items with comma.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
    'preprocess functions' => array(
      'views_rss_htmlspecialchars',
      'views_rss_core_preprocess_channel_category',
    ),
  );
  $elements['yandex:online'] = array(
    'description' => t('Link to online broadcast xml feed.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:enclosure'] = array(
    'description' => t('Handles several enclosure elements per item.'),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  $elements['yandex:pdalink'] = array(
    'description' => t('Link to the mobile version of the content.'),
    'preprocess functions' => array(
      'views_rss_yandex_preprocess_item_yandex_pdalink',
    ),
    'help' => 'http://partner.news.yandex.ru/tech.pdf',
  );
  return $elements;
}

/**
 * Preprocess function for item <pdalink> element.
 */
function views_rss_yandex_preprocess_item_yandex_pdalink(&$variables) {

  // No value = no preprocessing.
  if (empty($variables['value'])) {
    return;
  }

  // Change the element key.
  $variables['element'] = 'pdalink';
}

/**
 * Implements hook_views_rss_options_form_validate().
 */
function views_rss_yandex_views_rss_options_form_validate($form, &$form_state) {

  // Validate channel <yandex:logo> element.
  if (!empty($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo'])) {

    // Do not validate absolute URLs, as this could mean external image.
    if (!valid_url($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo'], TRUE)) {

      // Check that image exists and is in acceptable format.
      $real_path = realpath($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo']);
      if (!file_exists($real_path)) {
        form_set_error('style_options][channel][yandex][views_rss_yandex][logo', t('Unable to find %image or incorrect image format.', array(
          '%image' => $form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo'],
        )));
      }
    }
  }

  // Validate channel <yandex:logo type="square"> element.
  if (!empty($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'])) {

    // Do not validate absolute URLs, as this could mean external image.
    if (!valid_url($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'], TRUE)) {

      // Check that image exists and is in acceptable format.
      $real_path = realpath($form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square']);
      if (!file_exists($real_path)) {
        form_set_error('style_options][channel][yandex][views_rss_yandex][logo type=square', t('Unable to find %image or incorrect image format.', array(
          '%image' => $form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
        )));
      }
      else {
        list($width, $height) = getimagesize($real_path);

        // Check image width and height.
        if ($width < 180 || $height < 180) {
          form_set_error('style_options][channel][yandex][views_rss_yandex][logo type=square', t("Minimum allowed width/height of an image for feed channel's &lt;yandex:logo type=square&gt; element is 180 pixels. Specified %image is !width x !height pixels.", array(
            '%image' => $form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
            '!width' => $width,
            '!height' => $height,
          )));
        }
        elseif ($width != $height) {
          form_set_error('style_options][channel][yandex][views_rss_yandex][logo type=square', t("Width and height of an image for feed channel's &lt;yandex:logo type=square&gt; element should be equal. Specified %image is !width x !height pixels.", array(
            '%image' => $form_state['values']['style_options']['channel']['yandex']['views_rss_yandex']['logo type=square'],
            '!width' => $width,
            '!height' => $height,
          )));
        }
      }
    }
  }
}

Functions

Namesort descending Description
views_rss_yandex_preprocess_channel_yandex_logo Preprocess function for channel <yandex:logo> element.
views_rss_yandex_preprocess_channel_yandex_logo_square Preprocess function for channel <yandex:logo type="square"> element.
views_rss_yandex_preprocess_item_yandex_pdalink Preprocess function for item <pdalink> element.
views_rss_yandex_views_rss_channel_elements Implements hook_views_rss_channel_elements().
views_rss_yandex_views_rss_item_elements Implements hook_views_rss_item_elements().
views_rss_yandex_views_rss_namespaces Implements hook_views_rss_namespaces().
views_rss_yandex_views_rss_options_form_validate Implements hook_views_rss_options_form_validate().