You are here

feedapi_inherit.module in FeedAPI 5

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

Inherit group 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 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 and organic group information from feed to feed items.');
    case 'feedapi/full_name':
      return t('FeedAPI Inherit - pass on taxonomy and organic group information from feed to feed items.');
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function feedapi_inherit_nodeapi(&$node, $op) {
  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) {
  switch ($type) {
    case 'processors':
      if (module_exists('og')) {
        $form['inherit_og'] = array(
          '#type' => 'checkbox',
          '#title' => t('Feed nodes inherit organic group settings from parent feed.'),
          '#default_value' => TRUE,
        );
      }
      $form['inherit_taxonomy'] = array(
        '#type' => 'checkbox',
        '#title' => t('Feed nodes inherit taxonomy settings from 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'];
  }

  // Pass on taxonomy data
  if ($inherit_taxonomy && module_exists('taxonomy')) {
    $terms = taxonomy_node_get_terms($feed_node->nid);
    foreach ($terms as $tid => $term) {
      if (!$item_node->taxonomy[$tid]) {
        $item_node->taxonomy[$tid] = $term;
      }
    }
  }

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

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.