You are here

views_atom.module in Views Atom 7

Same filename and directory in other branches
  1. 6 views_atom.module

File

views_atom.module
View source
<?php

/**
 * Implements hook_init().
 */
function views_atom_init() {

  //require_once drupal_get_path('module', 'views_atom') .'/views_atom.theme.inc';
}

/**
 * Implements hook_views_api().
 */
function views_atom_views_api() {
  return array(
    'api' => 2.0,
  );
}

/**
 * Implements hook_theme().
 */
function views_atom_theme() {
  return array(
    'views_atom_fields_item' => array(
      'variables' => array(
        'content' => '',
        'view' => NULL,
        'item' => NULL,
      ),
      'file' => 'views_atom.theme.inc',
      'template' => 'views-atom-fields-item',
    ),
    'views_rss_feed_icon' => array(
      'variables' => array(
        'url',
        'title',
        'icon',
      ),
      'file' => 'views_atom.theme.inc',
    ),
    'views_atom_tombstone' => array(
      'variables' => array(
        'title' => NULL,
        'use_push' => FALSE,
        'hub_url' => NULL,
        'feed_url' => NULL,
        'updated' => array(),
        'show_updated_comment' => TRUE,
        'items' => NULL,
      ),
      'file' => 'views_atom.theme.inc',
      'template' => 'views-atom-tombstone',
    ),
  );
}

/**
 * Returns an array containing information about all Feed displays in the system.
 *
 * @return array
 *   An array of descriptive arrays about the available feed displays.
 */
function views_atom_get_feed_displays() {
  static $used_views = array();
  if (empty($used_views)) {
    views_include('cache');
    $cache = views_cache_get("views_atom:feeds");
    if (isset($cache->data)) {
      $used_views = $cache->data;
    }
    else {
      $views = views_get_all_views();
      foreach ($views as $view) {
        foreach ($view->display as $display_id => $display) {
          if ($display->display_plugin == 'feed') {
            $title = $view
              ->get_title();
            if (!$title) {
              $title = $view->name;
            }
            $used_views[] = array(
              'name' => $view->name,
              'display' => $display_id,
              'title' => $title,
              'display_title' => $display->display_title,
            );
          }
        }
        $view
          ->destroy();
      }
      views_cache_set("views_atom:feeds", $used_views);
    }
  }
  return $used_views;
}

/**
 * Generate a GUID for Atom feeds.
 *
 * @param $entity_type
 *   The type of entity for which to generate a URI, such as "node" or "user".
 * @param $entity_id
 *   The ID of the entity.
 * @param $options
 *   An array of options, currently only use_existing_from_feed supported.
 * @return
 *   A unique string that identifies the specified entity.
 */
function views_atom_guid($entity_type, $entity_id, $options = array()) {

  // This is currently set to the absolute system path until a better UUID
  // system can be implemented
  $guid = url("{$entity_type}/{$entity_id}", array(
    'absolute' => TRUE,
    'alias' => TRUE,
    'purl' => array(
      'disabled' => TRUE,
    ),
    'language' => '',
  ));

  // See if there already exists a guid for this node if configured to.
  if (!empty($options['use_existing_from_feed']) && $entity_type == 'node' && module_exists('feeds')) {
    $temp_guid = db_query("SELECT guid FROM {feeds_node_item} WHERE nid = :nid", array(
      ':nid' => $entity_id,
    ))
      ->fetchField();
    if ($temp_guid) {
      $guid = $temp_guid;
    }
  }
  return $guid;
}

/**
 * Sanitize a string for an atom feed.
 *
 * Certain HTML character entities are not valid in XML and cause character
 * encoding to go completely bananas.  This function converts those characters
 * to their unicode equivalents.
 *
 * @link http://changelog.ca/log/2006/06/12/making_nbsp_work_with_xml_rss_and_atom
 * @param $string
 *   The string to sanitize. If any other data type is passed it is returned
 *   unaffected.
 */
function views_atom_sanitize($string) {
  $search = array();
  $replace = array();
  if (empty($search)) {
    $replacements = array(
      ' & ' => ' &#38; ',
      '&amp;' => '&#38;',
      '&nbsp;' => '&#160;',
      '\\r\\n' => '\\n',
    );
    $search = array_keys($replacements);
    $replace = array_values($replacements);
  }
  if (is_string($string)) {
    return str_replace($search, $replace, $string);
  }
  return $string;
}

/**
 * Implementation of hook_views_atom_render().
 */
function field_views_atom_render($node, $entity_xml) {

  // @todo, this is still completely node-centric
  $fields = field_info_instances('node', $node->type);
  foreach ($fields as $field_name => $field_config) {
    if (!empty($node->{$field_name})) {
      $field = array_filter($node->{$field_name});
      $field_info = field_info_field($field_name);
      $field_xml = $entity_xml
        ->addChild('field');
      $field_xml
        ->addAttribute('type', $field_info['type']);
      $field_xml
        ->addAttribute('name', $field_name);
      foreach ($field as $language_code => $field_language_instances) {
        $field_instance_language_xml = $field_xml
          ->addChild('language');
        $field_instance_language_xml
          ->addAttribute('name', $language_code);

        // @todo Need to account for language.
        foreach ($field_language_instances as $instance) {
          $field_instance_xml = $field_instance_language_xml
            ->addChild('field-instance');
          foreach ($instance as $column => $value) {
            $serialize = FALSE;
            if (is_array($value)) {
              $value = serialize($value);
              $serialize = TRUE;
            }
            $element_xml = $field_instance_xml
              ->addChild('column', views_atom_sanitize($value));
            $element_xml
              ->addAttribute('name', $column);
            if (!empty($serialize)) {
              $element_xml
                ->addAttribute('serialize', $serialize);
            }
          }
        }
      }
      module_invoke_all('views_atom_render_field', $field_xml, $field, $field_info['type']);
    }
  }
}

/**
 * Implements hook_views_atom_render_field().
 */
function file_views_atom_render_field(SimpleXMLElement $field_xml, $field, $field_type) {

  // For filefields, also encode the complete URL to the file so that it can be
  // pulled by the remote system.
  if ($field_type == 'file') {
    _file_views_atom_render_field($field_xml, $field, $field_type);
  }
}

/**
 * Implements hook_views_atom_render_field().
 */
function image_views_atom_render_field(SimpleXMLElement $field_xml, $field, $field_type) {

  // Use the same helper function as file module.
  if ($field_type == 'image') {
    _file_views_atom_render_field($field_xml, $field, $field_type);
  }
}

/**
 * Helper function for hook_views_atom_render_field().
 *
 * Drupal 7 splits image and file fields. However, the needed hook logic is the same
 * for both.
 */
function _file_views_atom_render_field(SimpleXMLElement $field_xml, $field, $field_type) {

  // For filefields, also encode the complete URL to the file so that it can be
  // pulled by the remote system.
  // $language counter
  $l = 0;
  foreach ($field as $language_key => $field_language_instances) {
    foreach ($field_language_instances as $i => $instance) {
      $url = file_create_url($instance['uri']);
      $new_column = $field_xml->{'language'}[$l]->{'field-instance'}[$i]
        ->addChild('column', $url);
      $new_column
        ->addAttribute('name', 'full_url');
    }
    $l++;
  }
}

/**
 * Implements hook_views_atom_render_field().
 *
 * @todo, complete the upgrade of this function from 6 to 7.
 */
function taxonomy_views_atom_render_field(SimpleXMLElement $field_xml, $field, $field_type) {

  // For filefields, also encode the complete URL to the file so that it can be
  // pulled by the remote system.
  if ($field_type == 'taxonomy_term_reference') {
    $l = 0;
    foreach ($field as $language_key => $field_language_instances) {
      foreach ($field_language_instances as $i => $instance) {
        if (!empty($instance['tid']) && ($term = taxonomy_term_load($instance['tid']))) {

          // @todo, discern what data is needed here.
          $new_column = $field_xml->{'language'}[$l]->{'field-instance'}[$i]
            ->addChild('column', $term->name);
          $new_column
            ->addAttribute('name', 'term_name');

          /*
                   // $term_path = url(taxonomy_term_path($term), array('absolute' => TRUE, 'alias' => TRUE));
          $vocabulary = taxonomy_vocabulary_load($term->vid);

          //$field_xml = $taxonomy_xml->addChild('term');
          $field_xml->addAttribute('name', $term->name);
          //$field_xml->addChild('link', $term_path);
          $field_xml->addChild('label', $term->name);
          $field_xml->addChild('title', $term->name);
          $field_xml->addChild('subject', $term->name);
          $field_xml->addChild('description', views_atom_sanitize($term->description));
          $field_xml->addChild('taxonomy_vocabulary', $vocabulary->name);
          */
        }
      }
      $l++;
    }
  }
}

Functions

Namesort descending Description
field_views_atom_render Implementation of hook_views_atom_render().
file_views_atom_render_field Implements hook_views_atom_render_field().
image_views_atom_render_field Implements hook_views_atom_render_field().
taxonomy_views_atom_render_field Implements hook_views_atom_render_field().
views_atom_get_feed_displays Returns an array containing information about all Feed displays in the system.
views_atom_guid Generate a GUID for Atom feeds.
views_atom_init Implements hook_init().
views_atom_sanitize Sanitize a string for an atom feed.
views_atom_theme Implements hook_theme().
views_atom_views_api Implements hook_views_api().
_file_views_atom_render_field Helper function for hook_views_atom_render_field().