You are here

function feedapi_node_nodeapi in FeedAPI 6

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

Implementation of hook_nodeapi().

File

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

Code

function feedapi_node_nodeapi(&$node, $op, $teaser) {
  switch ($op) {
    case 'load':
      $result = db_query('SELECT fi.*, ff.feed_nid FROM {feedapi_node_item} fi JOIN {feedapi_node_item_feed} ff ON fi.nid = ff.feed_item_nid WHERE fi.nid = %d', $node->nid);
      while ($f = db_fetch_object($result)) {
        $node->feedapi_node = $f;
        $feed_nids[$f->feed_nid] = $f->feed_nid;
      }
      if (isset($node->feedapi_node)) {
        $node->feedapi_node->feed_nids = $feed_nids;
        unset($node->feedapi_node->feed_nid);
      }
      break;
    case 'insert':
      if (isset($node->feedapi_node) && $node->feedapi_node->feed_item) {

        // Why do we stick the nid on the feed item here?
        $node->feedapi_node->feed_item->nid = $node->nid;
        foreach ($node->feedapi_node->feed_nids as $feed_nid) {
          db_query("INSERT INTO {feedapi_node_item_feed} (feed_nid, feed_item_nid) VALUES (%d, %d)", $feed_nid, $node->nid);
        }
        $feed_item = $node->feedapi_node->feed_item;
        $arrived = time();
        db_query("INSERT INTO {feedapi_node_item} (nid, url, timestamp, arrived, guid) VALUES (%d, '%s', %d, %d, '%s')", $node->nid, $feed_item->options->original_url, $feed_item->options->timestamp, $arrived, $feed_item->options->guid);

        // Construct $node->feedapi_node component.
        // This should look the same as when loaded from DB.
        $node->feedapi_node->url = $feed_item->options->original_url;
        $node->feedapi_node->guid = $feed_item->options->guid;
        $node->feedapi_node->arrived = $arrived;
        $node->feedapi_node->timestamp = $feed_item->options->timestamp;
        $node->feedapi_node->nid = $node->nid;
      }
      break;
    case 'update':
      if (isset($node->feedapi_node)) {
        if ($node->feedapi_node->feed_item) {
          $feed_item = $node->feedapi_node->feed_item;
          db_query("UPDATE {feedapi_node_item} SET url = '%s', timestamp = %d, guid = '%s' WHERE nid = %d", $feed_item->options->original_url, $feed_item->options->timestamp, $feed_item->options->guid, $node->nid);
        }
        db_query('DELETE FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d', $node->nid);
        foreach ($node->feedapi_node->feed_nids as $feed_nid) {
          db_query("INSERT INTO {feedapi_node_item_feed} (feed_nid, feed_item_nid) VALUES (%d, %d)", $feed_nid, $node->nid);
        }
      }
      break;
    case 'delete':
      if (isset($node->feedapi_node)) {
        db_query('DELETE FROM {feedapi_node_item} WHERE nid = %d', $node->nid);
        db_query('DELETE FROM {feedapi_node_item_feed} WHERE feed_item_nid = %d', $node->nid);
      }
      if (isset($node->feed)) {
        db_query('DELETE FROM {feedapi_node_item_feed} WHERE feed_nid = %d', $node->nid);
      }
      break;
  }
}