--- feeds/plugins/FeedsNodeProcessor.inc.orig 2010-07-18 21:33:43.000000000 +0300 +++ feeds/plugins/FeedsNodeProcessor.inc 2010-07-29 14:35:25.000000000 +0300 @@ -44,6 +44,13 @@ class FeedsNodeProcessor extends FeedsPr // Map and save node. If errors occur don't stop but report them. try { $this->map($item, $node); + + // Add attributes inherited from the source feed node to the node. + if ($source->feed_nid) { + $feed_node = node_load($source->feed_nid); + $this->inherit($feed_node, $node); + } + node_save($node); if (!empty($nid)) { $batch->updated++; @@ -152,6 +159,7 @@ class FeedsNodeProcessor extends FeedsPr 'update_existing' => FEEDS_SKIP_EXISTING, 'expire' => FEEDS_EXPIRE_NEVER, 'mappings' => array(), + 'inherit' => array(), 'author' => 0, ); } @@ -208,6 +216,22 @@ class FeedsNodeProcessor extends FeedsPr ), '#default_value' => $this->config['update_existing'], ); + $disabled = !feeds_importer($this->id)->config['content_type']; + foreach ($this->getInheritors() as $key => $info) { + $options[$key] = sprintf('%s', $info['description'], $info['name']); + $defaults[$key] = $disabled ? 0 : (isset($this->config['inherit'][$key]) ? $this->config['inherit'][$key] : 0); + } + $description = $disabled ? + t('Only available for importers that are attached to a content type (Basic settings).') : + t('Copy one or more properties from the feed node to feed item nodes created by this importer.'); + $form['inherit'] = array( + '#type' => 'checkboxes', + '#title' => t('Inherit from feed node'), + '#description' => $description, + '#default_value' => array_keys(array_filter($defaults)), + '#disabled' => $disabled, + '#options' => $options, + ); return $form; } @@ -382,13 +406,162 @@ class FeedsNodeProcessor extends FeedsPr } return ''; } + + /** + * Invoke inheritors depending on configuration. + */ + protected function inherit($item_node, $feed_node) { + $inheritors = $this->getInheritors(); + foreach ($this->config['inherit'] as $key => $value) { + if ($value && function_exists($inheritors[$key]['callback'])) { + $inheritors[$key]['callback']($feed_node, $item_node); + } + } + } + + /** + * Get a list of available inheritors. + */ + protected function getInheritors() { + $inheritors = array(); + if (module_exists('og')) { + $inheritors['og'] = array( + 'name' => t('Inherit organic group(s)'), + 'description' => t('Feed item nodes inherit organic group settings from feed node.'), + 'callback' => 'feeds_inherit_og', + ); + } + if (module_exists('locale')) { + $inheritors['translation'] = array( + 'name' => t('Inherit language'), + 'description' => t('Feed nodes inherit language settings from feed node.'), + 'callback' => 'feeds_inherit_translation', + ); + } + if (module_exists('taxonomy')) { + $inheritors['taxonomy'] = array( + 'name' => t('Inherit taxonomy'), + 'description' => t('Feed item nodes inherit taxonomy terms from feed node.'), + 'callback' => 'feeds_inherit_taxonomy', + ); + } + if (module_exists('location')) { + $inheritors['location'] = array( + 'name' => t('Inherit location'), + 'description' => t('Feed nodes inherit location settings from feed node.'), + 'callback' => 'feeds_inherit_location', + ); + } + $inheritors['user'] = array( + 'name' => t('Inherit author'), + 'description' => t('Feed item nodes will be authored by the same user as the feed node.'), + 'callback' => 'feeds_inherit_user', + ); + drupal_alter('feeds_node_processor_inheritors', $inheritors); + return $inheritors; + } } /** - * Copy of node_delete() that circumvents node_access(). - * - * Used for batch deletion. + * @defgroup inheritors Inheritors for Feeds node processor. + * @{ */ + +/** +* Inherit OG properties. +*/ +function feeds_inherit_og($item_node, $feed_node) { + if (in_array($feed_node->type, og_get_types('group'))) { + $item_node->og_groups = array(); + $gid = $feed_node->nid; + $item_node->og_groups = array($gid => $gid); + $item_node->og_groups_both = array($gid => $feed_node->title); + } else { + 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); + } + } +} + +/** + * Inherit translation properties. + */ +function feeds_inherit_translation($item_node, $feed_node) { + $item_node->language = $feed_node->language; +} + +/** + * Inherit taxonomy properties. + */ +function feeds_inherit_taxonomy($item_node, $feed_node) { + $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; + } + } + } +} + +/** + * Inherit user properties. +*/ +function feeds_inherit_user($item_node, $feed_node) { + $item_node->uid = $feed_node->uid; +} + +/** + * Inherit location properties. +*/ +function feeds_inherit_location($item_node, $feed_node) { + $item_node->locations[0] = $feed_node->locations[0]; + $item_node->locations[0] = $feed_node->locations[0]; +} + +/** + * @} + */ + +/** + * Copy of node_delete() that circumvents node_access(). + * + * Used for batch deletion. + */ function _feeds_node_delete($nid) { $node = node_load($nid);