You are here

feedapi_inherit.module in FeedAPI 6

Same filename and directory in other branches
  1. 5 feedapi_inherit/feedapi_inherit.module

Inherit group language and taxonomy settings from feeds to news items This is an add on processor for FeedAPI

@author Aron Novak <aron at novaak dot net>

Sponsored by Development Seed

File

feedapi_inherit/feedapi_inherit.module
View source
<?php

/**
 * @file
 * Inherit group language and taxonomy settings from feeds to news items
 * This is an add on processor for FeedAPI
 *
 * @author
 *  Aron Novak <aron at novaak dot net>
 *
 * Sponsored by Development Seed
 */

/**
 * Implementation of hook_help().
 */
function feedapi_inherit_help($section) {
  switch ($section) {
    case 'admin/help#feedapi_inherit':
      return t('FeedAPI Inherit - pass on taxonomy, author, language and organic group information from feed to feed items.');
    case 'feedapi/full_name':
      return t('FeedAPI Inherit - pass on taxonomy, author, language and organic group information from feed to feed items.');
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function feedapi_inherit_nodeapi(&$node, $op) {
  if (!isset($node->feedapi_node)) {
    return;
  }
  switch ($op) {
    case 'prepare':
      if ($node->feedapi_node->feed_nids) {
        foreach ($node->feedapi_node->feed_nids as $feed_nid) {
          $feed_node = node_load($feed_nid);
          if (feedapi_enabled_type($feed_node->type, 'feedapi_inherit')) {
            _feedapi_inherit_do_inherit($node, $feed_node);
          }
        }
      }
      break;
  }
}

/**
 * Implementation of hook_feedapi_item().
 * Do nothing here, this function only makes us a feedapi processor,
 * which means that we can be enabled/disabled on a per node type
 * basis.
 */
function feedapi_inherit_feedapi_item($op) {
}

/**
 * Implementation of hook_feedapi_settings_form().
 * If a module provides parsers and processors it MUST evaluate the $type variable
 * to return different forms for parsers and processors.
 * There might be a better term for parsers and processors than $type.
 */
function feedapi_inherit_feedapi_settings_form($type) {
  $form = array();
  switch ($type) {
    case 'processors':
      if (module_exists('og')) {
        $form['inherit_og'] = array(
          '#type' => 'checkbox',
          '#title' => t('Feed item nodes inherit organic group settings from parent feed.'),
          '#default_value' => TRUE,
        );
      }
      if (module_exists('translation')) {
        $form['inherit_language'] = array(
          '#type' => 'checkbox',
          '#title' => t('Feed nodes inherit language settings from parent feed.'),
          '#default_value' => TRUE,
        );
      }
      $form['inherit_taxonomy'] = array(
        '#type' => 'checkbox',
        '#title' => t('Feed item nodes inherit taxonomy settings from parent feed.'),
        '#default_value' => TRUE,
      );
      $form['inherit_author'] = array(
        '#type' => 'checkbox',
        '#title' => t('Feed item nodes will be authored by the same user as the parent feed.'),
        '#default_value' => TRUE,
      );
      break;
  }
  return $form;
}

/**
 * Inherit the feed's taxonomy and og data to the given feed item.
 * Todo: split this into a function for taxonomy and another one for og - easier maintainable.
 */
function _feedapi_inherit_do_inherit(&$item_node, $feed_node) {
  $inherit_taxonomy = TRUE;
  $inherit_og = TRUE;
  if ($feed_node->feed->settings['processors']['feedapi_inherit']) {
    $inherit_taxonomy = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_taxonomy'];
    $inherit_og = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_og'];
    $inherit_author = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_author'];
    $inherit_translation = $feed_node->feed->settings['processors']['feedapi_inherit']['inherit_language'];
  }

  // Pass on author data
  if ($inherit_author) {
    $item_node->uid = $feed_node->uid;
  }

  // Pass on taxonomy data
  if ($inherit_taxonomy && module_exists('taxonomy')) {
    $terms = taxonomy_node_get_terms($feed_node);
    if (!(isset($item_node->taxonomy) && is_array($item_node->taxonomy))) {
      $item_node->taxonomy = array();
    }
    foreach ($terms as $tid => $term) {
      $vid = $term->vid;
      $vocabulary = taxonomy_vocabulary_load($vid);
      if ($vocabulary->multiple) {
        if (!(isset($item_node->taxonomy[$vid]) && is_array($item_node->taxonomy[$vid]))) {
          $item_node->taxonomy[$vid] = array();
        }
        if (!in_array($tid, $item_node->taxonomy[$vid])) {
          $item_node->taxonomy[$vid][$tid] = $tid;
        }
      }
      else {
        if (!$item_node->taxonomy[$vid]) {
          $item_node->taxonomy[$vid] = $tid;
        }
      }
    }
  }

  // Pass on group data
  if ($inherit_og && module_exists('og')) {
    if (isset($feed_node->og_public)) {
      if (!isset($item_node->og_public)) {
        $item_node->og_public = 1;
      }
      $item_node->og_public = $item_node->og_public & $feed_node->og_public;
    }
    if (isset($feed_node->og_groups)) {
      if (!(isset($item_node->og_groups) && is_array($item_node->og_groups))) {
        $item_node->og_groups = array();
      }
      $item_node->og_groups = array_merge($feed_node->og_groups, $item_node->og_groups);
      foreach ($item_node->og_groups as $gid) {
        if ($gid != 0) {
          $transformed_groups[$gid] = $gid;
        }
      }
      $item_node->og_groups = $transformed_groups;
    }
    if (isset($feed_node->og_groups_names)) {
      if (!is_array($item_node->og_groups_names)) {
        $item_node->og_groups_names = array();
      }
      $item_node->og_groups_names = array_merge($feed_node->og_groups_names, $item_node->og_groups_names);
    }
  }

  // Pass on language data
  if ($inherit_translation && module_exists('translation')) {
    $item_node->language = $feed_node->language;
  }
}

Functions

Namesort descending Description
feedapi_inherit_feedapi_item Implementation of hook_feedapi_item(). Do nothing here, this function only makes us a feedapi processor, which means that we can be enabled/disabled on a per node type basis.
feedapi_inherit_feedapi_settings_form Implementation of hook_feedapi_settings_form(). If a module provides parsers and processors it MUST evaluate the $type variable to return different forms for parsers and processors. There might be a better term for parsers and processors than $type.
feedapi_inherit_help Implementation of hook_help().
feedapi_inherit_nodeapi Implementation of hook_nodeapi().
_feedapi_inherit_do_inherit Inherit the feed's taxonomy and og data to the given feed item. Todo: split this into a function for taxonomy and another one for og - easier maintainable.