You are here

function disqus_nodeapi in Disqus 6

Same name and namespace in other branches
  1. 5 disqus.module \disqus_nodeapi()

Implementation of hook_nodeapi().

File

./disqus.module, line 75

Code

function disqus_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  // See if Disqus is active on this node.
  $types = variable_get('disqus_nodetypes', array());
  if (!empty($types[$node->type])) {
    switch ($op) {
      case 'presave':

        // For programmatic insertion of nodes via node_save.
        if (!isset($node->disqus['status'])) {
          $node->disqus['status'] = TRUE;
        }
        break;
      case 'load':

        // Check which Disqus domain to use.
        $domain = variable_get('disqus_domain', '');
        if (!empty($domain)) {

          // Save the data to the node object.
          $node->disqus = array(
            'domain' => $domain,
          );

          // Apply the Disqus status to the node.
          $status = db_fetch_object(db_query("SELECT status FROM {disqus} WHERE nid = %d", $node->nid));
          $node->disqus['status'] = isset($status->status) ? (bool) $status->status : TRUE;

          // Build the absolute URL without the alias for the disqus_url flag.
          $node->disqus['url'] = url("node/{$node->nid}", array(
            'absolute' => TRUE,
          ));

          // Build the title.
          $node->disqus['title'] = check_plain(strip_tags($node->title));

          // Provide the identifier.
          $node->disqus['identifier'] = 'node/' . $node->nid;

          // The developer flag must always be set when the node is unpublished.
          if ($node->status == 0) {
            $node->disqus['developer'] = 1;
          }
          elseif ($developer = variable_get('disqus_developer', FALSE)) {
            $node->disqus['developer'] = intval($developer);
          }
        }
        break;
      case 'delete':
        db_query('DELETE FROM {disqus} WHERE nid = %d', $node->nid);
        break;
      case 'insert':
        $data = array(
          'nid' => $node->nid,
          'status' => isset($node->disqus['status']) ? $node->disqus['status'] : TRUE,
        );
        drupal_write_record('disqus', $data);
        break;
      case 'update':

        // Write the status to the table, taking the node ID as the update key.
        db_query('DELETE FROM {disqus} WHERE nid = %d', $node->nid);
        $data = array(
          'nid' => $node->nid,
          'status' => isset($node->disqus['status']) ? $node->disqus['status'] : TRUE,
        );
        drupal_write_record('disqus', $data);
        break;
      case 'view':

        // See if we are to display Disqus in the current context.
        if (!$a3 && $a4 && isset($node->disqus) && user_access('view disqus comments') && $node->disqus['status'] == 1) {

          // Inject Disqus into the node object.
          switch (variable_get('disqus_location', 'content_area')) {
            case 'content_area':

              // Inject into the node content.
              $node->content['disqus'] = array(
                '#value' => theme('disqus_comments', $node->disqus),
                '#weight' => variable_get('disqus_weight', 50),
              );
              break;
            case 'variable':

              // Inject it into a variable.
              $node->disqus_comments = theme('disqus_comments', $node->disqus);
              break;
          }
        }
        break;
    }
  }
}