You are here

public function HeartbeatActivityCommentsPlugin::loadAttachments in Heartbeat 7

loadAttachments().

Overrides iHeartbeatPlugin::loadAttachments

File

modules/heartbeat_comments/plugins/activitycomments.inc, line 69
Defines activity comments.

Class

HeartbeatActivityCommentsPlugin
Class HeartbeatFlagAttachmentPlugin.

Code

public function loadAttachments(HeartbeatActivity &$heartbeatActivity, $name = NULL) {

  // Create required additions made to the activity object.
  $this
    ->createActivityAdditions($heartbeatActivity);
  if (!$this->accessComment) {
    return;
  }

  // Load from cache if configured and available.
  if ($this->settings['heartbeat_comments_cache'] && ($objects_cache = cache_get('heartbeat:comments:' . $heartbeatActivity->uaid . ':' . $heartbeatActivity->message_nid_cache))) {

    // Make sure the textarea.js script is attached.
    drupal_add_js('misc/textarea.js');
    $comments->heartbeat_comments = array(
      '_cached' => $objects_cache->data,
    );
  }
  else {

    // For the single message, make sure all comments are loaded.
    $this->showAllComments = isset($this->stream) && $this->stream instanceof SingleActivity;

    // HeartbeatActivity.additions.$commentCount 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($this->parentNode) && $this->isNodeComment) {
      $result = db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(
        ':nid' => $this->parentNode->nid,
      ));
    }
    else {
      $result = db_query('SELECT COUNT(hcid) AS comment_count FROM {heartbeat_comments} WHERE uaid = :uaid', array(
        ':uaid' => $heartbeatActivity->uaid,
      ));
    }
    foreach ($result as $row) {
      $this->commentCount = $row->comment_count;
    }

    // Fetch the comments if needed.
    if ($this->commentCount > 0) {
      $this->reactions = heartbeat_get_reactions($heartbeatActivity->uaid, $this->isNodeComment, $this->parentNode, $this->showAllComments);
      $heartbeatActivity->classes .= 'has-comments';
    }
    else {
      $heartbeatActivity->classes .= 'no-comments';
    }
  }

  // Attach the fully loaded plugin to the message.
  $heartbeatActivity
    ->add_plugin($this->settings['plugin_name'], $this);
}