You are here

function discussthis_nodeapi in Discuss This! 5

Same name and namespace in other branches
  1. 6 discussthis.module \discussthis_nodeapi()

hook_nodeapi implementation This is the meat of the module. Here we add the Discuss This link which will create a new forum topic if none exists, or link to the existing one if it does. Also adds forum override dropdown on add/edit screen for users with override permissions

Parameters

node the node being acted on:

op the operation being done to the node:

teaser whether this is a teaser view:

page whether this is a full page view:

File

./discussthis.module, line 80

Code

function discussthis_nodeapi(&$node, $op, $teaser, $page) {
  $enabled = variable_get('discussthis_node_' . $node->type, 0);

  // If this is an discussthis-enabled node type, treat the per-node fields appropriately
  if ($enabled) {
    switch ($op) {
      case 'validate':

        // validate forum override selection
        break;
      case 'insert':

      // create or update/move forum topic
      case 'update':
        if ($node && user_access('override discuss this forums')) {
          $forum_tid = $node->discussthis['forum'];
          $typed_title = $node->discussthis['topic'];

          // unquote commas and/or doublequotes created by autocomplete function before storing
          $typed_title = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $typed_title));
          $typed_title = trim($typed_title);

          // store the forum and topic mapping in the db
          _discussthis_set_forum($node->nid, $forum_tid);
          _discussthis_set_topic($node->nid, $typed_title);
        }
        break;
      case 'view':

        # NOT teaser means what, exactly?
        if (!$teaser && user_access('access discuss this links')) {
          $topic_nid = _discussthis_get_topic($node->nid);

          // lookup a nid for the topic, if it exists (otherwise 0)

          # (optionally?) grab X most recent comments, and attach them to the node:
          if ($show_comments = variable_get('discussthis_comments', 10)) {
            $result = db_query_range('SELECT c.nid, c.uid, c.name, c.subject, c.cid, c.timestamp FROM {comments} c INNER JOIN {node} n ON n.nid = c.nid WHERE c.nid = %d AND n.status = 1 AND c.status = %d ORDER BY c.cid DESC', $topic_nid, COMMENT_PUBLISHED, 0, $show_comments);
            while ($comment = db_fetch_object($result)) {
              $comments[] = theme('discussthis_comment', $topic_nid, $comment);
            }
            $node->content['discussthis_comments'] = array(
              '#value' => theme('item_list', $comments),
              '#weight' => 50,
            );
          }
        }
        break;
      case 'load':

        // return  array of fields => values to be merged into $node
        break;
      case 'delete':

        // drop db row for this node.. what should happen to the forum topic?
        break;
    }
  }
  elseif ($node->type == 'forum') {
    switch ($op) {
      case 'load':

        # Lookup the forum topic nid
        $discuss_nid = _discussthis_get_node($node->nid);
        if ($discuss_nid) {
          drupal_set_message("this forum topic is associated with node {$discuss_nid}");
          $node->discuss_nid = $discuss_nid;
        }
        break;
      case 'delete':

        // drop db row for this forum nid
        $sql = 'DELETE FROM {discussthis} WHERE topic_nid = %d';
        db_query($sql, $node->nid);
        break;
      case 'insert':

      # Store the did
      case 'update':

        # Change the forum topic ID
        _discussthis_set_tid($node->discuss_nid, $node->nid);
        break;
    }
  }
}