You are here

function feedapi_node_feedapi_after_refresh in FeedAPI 5

Implements hook_feedapi_after_refresh($feed). Handle the promote N items to the frontpage setting

File

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

Code

function feedapi_node_feedapi_after_refresh($feed) {
  if ($promote = $feed->settings['processors']['feedapi_node']['promote']) {
    $result = db_query("SELECT n.nid FROM {node} n JOIN {feedapi_node_item_feed} ff  ON ff.feed_item_nid = n.nid  WHERE ff.feed_nid = %d AND n.promote = 1", $feed->nid);
    $nids_promoted_now = array();
    $nids_top_n = array();
    while ($item = db_fetch_array($result)) {
      $nids_promoted_now[] = $item['nid'];
    }
    $result = pager_query("SELECT n.nid FROM {node} n JOIN {feedapi_node_item_feed} ff  ON ff.feed_item_nid = n.nid  WHERE ff.feed_nid = %d ORDER BY n.created DESC", $promote, 0, NULL, $feed->nid);
    while ($item = db_fetch_array($result)) {
      $nids_top_n[] = $item['nid'];
    }
    $to_promote = array_diff($nids_top_n, $nids_promoted_now);
    $to_demote = array_diff($nids_promoted_now, $nids_top_n);
    if (count($to_promote) > 0) {
      db_query("UPDATE {node} SET promote = 1 WHERE nid IN (%s)", implode(',', $to_promote));
    }
    if (count($to_demote) > 0) {
      db_query("UPDATE {node} SET promote = 0 WHERE nid IN (%s)", implode(',', $to_demote));
    }
  }
}