You are here

function discussthis_nodeapi in Discuss This! 6

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

\brief hook_nodeapi implementation

This function ensures that deleted nodes and topics unlink nodes and topics that are linked via the Discuss This! module.

In View mode, this function generates comments at the bottom of the page being displayed. The comments are taken from the topic.

\param[in,out] $node the node being acted on \param[in] $op the operation being done to the node \param[in] $teaser whether this is a teaser view \param[in] $page whether this is a full page view

File

./discussthis.module, line 96
Associations discussions in forums with specific nodes

Code

function discussthis_nodeapi(&$node, $op, $teaser, $page) {
  switch ($op) {
    case 'validate':
      if ($node && user_access('override discuss this forums') && isset($node->discussthis)) {
        if (!empty($node->discussthis['discussthis_topic'])) {
          $topic_nid = $node->discussthis['discussthis_topic'];

          // valid integer?
          // and if valid, is that this very node? if so, that's not good either
          if ($topic_nid == (int) $topic_nid && !is_null($node->nid) && $topic_nid != $node->nid && is_numeric($topic_nid)) {

            // make sure we can actually load that node and that's a forum's node
            $topic = node_load(array(
              'nid' => (int) $topic_nid,
            ));
            if (!$topic || $topic->type != 'forum') {
              $topic_nid = FALSE;
            }
          }
          else {
            $topic_nid = FALSE;
          }
          if (!$topic_nid) {

            // ugly error setting which works
            form_set_error('discussthis][discussthis_topic', t('The Discuss This! forum topic #@nid was not found (or you chose this very node.) Please, try again. Use the auto-complete feature or enter the exact node identifier. If you did not change the topic identifier, it could have been deleted while you were editing this node. Simply clear the topic entry in that case.', array(
              '@nid' => $node->discussthis['discussthis_topic'],
            )));
            unset($node->discussthis['discussthis_topic']);
          }
        }

        // the forum should never be wrong with a select unless we're working with a hacker
        if (!empty($node->discussthis['discussthis_forum'])) {

          // valid integer? representing a valid forum?
          $forum_tid = $node->discussthis['discussthis_forum'];
          if ((int) $forum_tid != -1) {
            if ($forum_tid == (int) $node->discussthis['discussthis_forum']) {
              $vid = variable_get('forum_nav_vocabulary', '');
              $term = taxonomy_get_term($forum_tid);
              if (!$term || $term->vid != $vid) {
                form_set_error('discussthis][discussthis_forum', t('The Discuss This! forum #@tid was not found.', array(
                  '@tid' => $forum_tid,
                )));
                unset($node->discussthis['discussthis_forum']);
              }
            }
            else {
              unset($node->discussthis['discussthis_forum']);
            }
          }
        }
      }
      break;
    case 'insert':
    case 'update':
      if ($node) {
        if ($node->type == 'forum') {
        }
        else {
          if (user_access('override discuss this forums')) {

            // the forum field is only available for nodes that are
            // not yet attached to a topic, so it may not be defined
            if (isset($node->discussthis['discussthis_forum'])) {
              $discussthis_forum['nid'] = $node->nid;
              $discussthis_forum['forum_tid'] = $node->discussthis['discussthis_forum'];
              _discussthis_set_forum($discussthis_forum);
            }

            // topics are defined by nid
            db_lock_table('discussthis');
            $sql = "DELETE FROM {discussthis} WHERE nid = %d";
            db_query($sql, $node->nid);
            if (!empty($node->discussthis['discussthis_topic'])) {
              $sql = "INSERT INTO {discussthis} (nid, topic_nid) VALUES (%d, %d)";
              db_query($sql, $node->nid, $node->discussthis['discussthis_topic']);
            }
            db_unlock_tables();
          }
        }
      }
      break;
    case 'delete':

      // drop any reference to that node
      //
      // PROBLEM: Note that if someone just initiated a "Discuss this"
      // form, then they will have a surprise when saving since the
      // page will be gone. I don't see any way around that though.
      //
      $sql = 'DELETE FROM {discussthis} WHERE nid = %d OR topic_nid = %d';
      db_query($sql, $node->nid, $node->nid);
      $sql = 'DELETE FROM {discussthis_forums} WHERE nid = %d';
      db_query($sql, $node->nid);
      break;
    case 'view':

      // only pages being displayed by themselves have comments shown
      if (!$teaser && $page) {
        $node->content['body']['#value'] .= discussthis_comment_render($node);
      }
      break;
  }
}