You are here

function _feedapi_node_save in FeedAPI 5

Same name and namespace in other branches
  1. 6 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 232
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;

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

  // Construct the node object
  $node = new stdClass();
  if (isset($feed_item->nid)) {
    $node->nid = $feed_item->nid;
  }
  $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);
  $node->title = $feed_item->title;
  if (empty($node->title) && !empty($feed_item->description)) {

    // Explode to words and use the first 3 words.
    $words = preg_split("/[\\s,]+/", $feed_item->description);
    $node->title = $words[0] . ' ' . $words[1] . ' ' . $words[2];
  }
  $node->uid = $feed_node->uid;
  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->body = $feed_item->description;
  $node->teaser = node_teaser($feed_item->description);

  // 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_object_prepare($node);

  // 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);
    }
  }
  else {
    node_save($node);
  }
  return $feed_item;
}