You are here

public function HeartbeatActivityCommentsPlugin::renderAttachmentsContent in Heartbeat 7

renderAttachmentsContent().

Overrides iHeartbeatPlugin::renderAttachmentsContent

File

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

Class

HeartbeatActivityCommentsPlugin
Class HeartbeatFlagAttachmentPlugin.

Code

public function renderAttachmentsContent(HeartbeatActivity $heartbeatActivity) {
  if (!$this->accessComment) {
    return;
  }

  // Keep a variable to indicate if the comments for this activity need to be cached.
  $cache = $this->settings['heartbeat_comments_cache'];

  // Check if the user has access to the comment widget/list.
  if (!$this->canComment) {
    $attachment = '<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>';
    $heartbeatActivity
      ->add_attachment($attachment);

    //return;
  }

  // check if we can return data from cache.
  if (isset($heartbeatActivity->additions->heartbeat_comments, $heartbeatActivity->additions->heartbeat_comments['_cached'])) {
    $list = $heartbeatActivity->additions->heartbeat_comments['_cached'];
    $extraCssClass = empty($list) ? "heartbeat-comments-nocomments" : "heartbeat-comments-comments";

    // Don't cache it if it's already been cached.
    $cache = FALSE;
  }
  else {
    $list = '';
    $extraCssClass = "heartbeat-comments-nocomments";
    if ($this->commentCount > 0) {
      $extraCssClass = "heartbeat-comments-comments";
      $comments_per_page = $this->HEARTBEAT_REACTIONS_PER_PAGE;
      if ($this->isNodeComment && variable_get('comment_' . $this->objectType, 2) > 0) {
        $comments_per_page = $this->HEARTBEAT_NODE_COMMENTS_PER_PAGE;
      }
      if (!$this->showAllComments && $this->commentCount > $comments_per_page) {
        $this->hasMoreComments = TRUE;
        $this->reactions = array_slice($this->reactions, 0, $comments_per_page);
      }
      if ($this->orderStyle == 'oldest_on_top') {
        $this->reactions = array_reverse($this->reactions);
      }
      $list .= theme('heartbeat_comments', array(
        'comments' => $this->reactions,
        'activity' => $heartbeatActivity,
      ));
    }
  }

  // Cache the output if not cached yet.
  if ($cache) {
    cache_set('heartbeat:comments:' . $heartbeatActivity->uaid . ':' . $heartbeatActivity->message_nid_cache, $list);
  }

  // Create the list and the form in correct order.
  $output = '';
  $output .= '<div class="heartbeat-comments heartbeat-comments-' . $this->objectType . ' ' . $extraCssClass . '">';
  if ($this->orderPosition == 'up') {
    $output .= $list;
  }

  // Render the form if user is allowed to.
  if ($this->canComment) {
    $elements = drupal_get_form('heartbeat_comments_form', $heartbeatActivity);
    $output .= drupal_render($elements);
  }
  if ($this->orderPosition == 'down') {
    $output .= $list;
  }
  $output .= '</div>';

  // Create the attachment output.
  if ($this->commentDisplay == 'block') {
    $display = $this->commentCount || !empty($heartbeatActivity->additions->comment_open_override) ? 'block' : 'none';
  }
  else {
    $display = 'none';
  }
  $attachment = '<div id="heartbeat-comments-wrapper-' . $heartbeatActivity->uaid . '" class="heartbeat-comments-wrapper" style="display: ' . $display . ';">';
  $attachment .= $output;
  $attachment .= '</div>';
  $heartbeatActivity
    ->add_attachment($attachment);
}