You are here

function heartbeat_comments_heartbeat_load in Heartbeat 6.4

Implementation of hook_heartbeat_load().

File

modules/heartbeat_comments/heartbeat_comments.module, line 159
heartbeat_comments.module Heartbeat comments can come with two possible

Code

function heartbeat_comments_heartbeat_load(&$messages, HeartbeatAccess $stream) {
  drupal_add_js(drupal_get_path('module', 'heartbeat_comments') . '/heartbeat_comments.js');
  $comments_order = variable_get('heartbeat_comments_order', 'recent_on_top');
  drupal_add_js(array(
    'heartbeat_comments_order' => $comments_order,
  ), "setting");
  $comments_position = variable_get('heartbeat_comments_position', 'down');
  drupal_add_js(array(
    'heartbeat_comments_position' => $comments_position,
  ), "setting");
  $comment_cache_enabled = variable_get('heartbeat_comments_cache', 1);
  foreach ($messages as $key => $message) {
    if (!empty($message->template->attachments['heartbeat_comments'])) {

      // Get the original message from the translation table if needed.
      $messages[$key]->tuaid = heartbeat_get_tuaid($message->uaid);
      $node = NULL;
      $node_comment = 0;
      if ($message->nid > 0) {
        $message_nid_cache = $message->nid;

        // Only nid & type is needed here (also for heartbeat_get_reactions)
        // so node_load is really over the top here.
        $node = new stdClass();
        $node->nid = $message->nid;
        $node->uid = $message->nid_info['uid'];
        $node->type = $message->nid_info['type'];
        $node->format = $message->nid_info['format'];
        $node->status = 1;

        // unpublished nodes ignore access control
        $node_comment = $message->template->attachments['comment_comments'] && variable_get('comment_' . $node->type, 2) > 0;
      }
      if (!$node_comment) {

        // Reset the nid to 0 since we don't use it for node-type disabled comments.
        $message_nid_cache = 0;
      }

      // Check access on the form submission.
      $show_form = user_access('add heartbeat comment');
      if ($message_nid_cache && $node_comment) {
        $show_form = user_access('post comments');
      }
      $messages[$key]->additions->object_type = '';
      if (!$show_form && $GLOBALS['user']->uid == 0) {
        $output = '<div class="user-login-teaser">' . t('!login or !register to make a comment', array(
          '!login' => l(t('Login'), 'user'),
          '!register' => l(t('register'), 'user/register'),
        )) . '</div>';
      }
      elseif ($comment_cache_enabled && ($objects_cache = cache_get('heartbeat:comments:' . $messages[$key]->tuaid . ':' . $message_nid_cache))) {

        // Make sure the textarea.js script is attached.
        drupal_add_js('misc/textarea.js');
        $output = $objects_cache->data;
      }
      else {
        $messages[$key]->additions->comments_order = $comments_order;
        $messages[$key]->additions->comments_position = $comments_position;

        // Normal heartbeat comments
        $messages[$key]->additions->node_comment = 0;
        $messages[$key]->additions->object_type = 'activity';

        // For the single message, make sure all comments are loaded.
        $messages[$key]->additions->all_comments = $stream instanceof SingleHeartbeat;

        // HeartbeatActivity.additions.comment_count is cached in the message itself.
        // Node comment reactions follow core. Here an extra query is needed to
        // the node comment statistics table.
        if (isset($node) && $message->template->attachments['comment_comments'] && variable_get('comment_' . $node->type, 2) > 0) {
          $messages[$key]->additions->node_comment = 1;
          $messages[$key]->additions->object_type = $node->type;
          $messages[$key]->additions->comment_count = db_result(db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = %d', $node->nid));
        }

        // Fetch the comments if needed.
        $messages[$key]->additions->reactions = array();
        if ($messages[$key]->additions->comment_count > 0) {
          $messages[$key]->additions->reactions = heartbeat_get_reactions($messages[$key]->tuaid, $messages[$key]->additions->node_comment, $node, $messages[$key]->additions->all_comments);
          $messages[$key]->classes .= 'has-comments';
        }
        else {
          $messages[$key]->classes .= 'no-comments';
        }
        $output = heartbeat_comments_widget($message->template->attachments, $messages[$key]);
        if ($comment_cache_enabled) {
          cache_set('heartbeat:comments:' . $messages[$key]->tuaid . ':' . $message_nid_cache, $output);
        }
      }
      $message->additions->heartbeat_comments = array(
        '_cached' => $output,
      );
    }
  }
}