You are here

function _feedapi_node_save in FeedAPI 6

Same name and namespace in other branches
  1. 5 feedapi_node/feedapi_node.module \_feedapi_node_save()

Create a node from the feed item Store the relationship between the node and the feed item

1 call to _feedapi_node_save()
_feedapi_node_update in feedapi_node/feedapi_node.module
Update a node which already assigned to a feed item

File

feedapi_node/feedapi_node.module, line 268
Handle how the feed items are represented as a content Handle the processing of the feed items

Code

function _feedapi_node_save($feed_item, $feed_nid, $settings = array()) {

  // Avoid error message flood when creating tons of items.
  static $error_msg = FALSE;
  module_load_include('inc', 'node', 'node.pages');

  // Don't save anything if neither url nor guid given.
  if (!$feed_item->options->original_url) {
    if (!$feed_item->options->guid) {
      return $feed_item;
    }
  }

  // If there are dupes on other feeds, don't create new feed item, but link this feed
  // to existing feed item.
  // Heads up: if there is a duplicate on the SAME feed,
  // _feedapi_node_save() won't even be called.
  if (isset($feed_item->feedapi_node->duplicates)) {
    foreach ($feed_item->feedapi_node->duplicates as $fi_nid => $f_nids) {
      $feed_item_node = node_load($fi_nid);
      $feed_item_node->feedapi_node->feed_nids[$feed_nid] = $feed_nid;
      node_object_prepare($feed_item_node);
      node_save($feed_item_node);
    }

    //mark this item as updated.
    $feed_item->is_updated = TRUE;
    return FALSE;
  }

  // Constructs the node object.
  $node = new stdClass();
  if (isset($feed_item->nid)) {
    $node->nid = $feed_item->nid;
    $node->vid = db_result(db_query("SELECT vid FROM {node} WHERE nid = %d", $node->nid));
  }

  // Determines the node type.
  if (empty($settings['content_type'])) {
    $ct_types = node_get_types();
    $ct_options = array();
    if (is_array($ct_types)) {
      foreach ($ct_types as $key => $data) {
        $ct_options[$key] = $data->name;
      }
    }
    if (array_key_exists('story', $ct_options)) {
      $node->type = 'story';
    }
    else {
      $node->type = current(array_keys($ct_options));
    }
  }
  else {
    $node->type = $settings['content_type'];
  }
  if (feedapi_enabled_type($node->type)) {
    if ($error_msg !== TRUE) {
      drupal_set_message(t('Please disable FeedAPI for !item content-type.', array(
        '!item' => $node->type,
      )), 'error');
      $error_msg = TRUE;
    }
    return FALSE;
  }

  // Get the default options from the cont
  $options = variable_get('node_options_' . $node->type, FALSE);
  if (is_array($options)) {
    $node->status = in_array('status', $options) ? 1 : 0;
    $node->promote = in_array('promote', $options) ? 1 : 0;
    $node->sticky = in_array('sticky', $options) ? 1 : 0;
  }
  else {
    $node->status = 1;
  }
  $feed_node = node_load($feed_nid);
  $type = node_get_types('type', $node->type);

  // Use feedapi_mapper_map() if FeedAPI Mapper is version 2.x (= feedapi_mapper_unique() is present)
  if (function_exists('feedapi_mapper_map') && function_exists('feedapi_mapper_unique')) {
    $node = feedapi_mapper_map($feed_node, 'feedapi_node', $feed_item, $node);
    if (empty($node->teaser) && $type->has_body) {
      $node->teaser = node_teaser($node->body);
    }
  }
  else {
    if ($type->has_title) {
      $node->title = $feed_item->title;
    }
    if ($type->has_body) {
      $node->body = $feed_item->description;
      $node->teaser = node_teaser($feed_item->description);
    }
  }
  $node->format = isset($settings['input_format']) ? $settings['input_format'] : FILTER_FORMAT_DEFAULT;

  // Stick feed item on node so that add on modules can act on it.
  // A feed item can come in from more than one feed.
  $node->feedapi_node->feed_nids[$feed_nid] = $feed_nid;
  $node->feedapi_node->feed_item = $feed_item;

  // For backwards compatibility - todo: move to using feedapi_node->feed_nids and feedapi_node->feed_item.
  $node->feedapi->feed_nid = $feed_nid;
  $node->feedapi->feed_item = $feed_item;
  $node->created = time();
  node_object_prepare($node);
  if (!isset($feed_item->nid)) {
    $node->created = isset($settings['node_date']) && $settings['node_date'] == 'feed' ? $feed_item->options->timestamp : time();
  }
  else {
    $node->created = db_result(db_query("SELECT created FROM {node} WHERE nid = %d", $feed_item->nid));
  }
  $node->uid = $feed_node->uid;
  node_save($node);
  return $feed_item;
}