You are here

function _ajax_comments_nodejs_view_added in AJAX Comments 7

Implements _ajax_comments_nodejs_view_added().

1 string reference to '_ajax_comments_nodejs_view_added'
ajax_comments_nodejs_menu in ajax_comments_nodejs/ajax_comments_nodejs.module
Implements hook_menu().

File

ajax_comments_nodejs/ajax_comments_nodejs.module, line 95
AJAX Comments Nodejs Integration module file

Code

function _ajax_comments_nodejs_view_added($cid) {
  $commands = array();
  if (!empty($cid)) {
    $comment = comment_load($cid);
    $node = node_load($comment->nid, NULL, TRUE);
    if ($comment) {
      $can_view_comments = user_access('access comments') && $comment->status == COMMENT_PUBLISHED && node_access('view', $node);
      $can_admin_comments = user_access('administer comments') && node_access('view', $node);
      if ($can_view_comments || $can_admin_comments) {
        $comment->ajax_comments_nodejs_class = 'ajax-comments-nodejs-new';
        $comment_build = comment_view($comment, $node);
        $comment_output = drupal_render($comment_build);

        /**
         * comment_goodnes module and comment_sort_created compatibility:
         * 1 - Older first
         * 2 - Newer first
         */
        $sort = _ajax_comments_get_comment_sort_order($node);
        $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
        if ($mode == COMMENT_MODE_THREADED && $comment->pid > 0) {

          // Reply in threaded mode
          $commands[] = array(
            'command' => 'ajaxCommentsAddDummyDivAfter',
            'selector' => '.comment-wrapper-' . $comment->pid,
            'class' => 'indented',
          );
          if ($sort == 1) {

            // Newer first.
            $commands[] = array(
              'command' => 'ajaxCommentsAppend',
              'selector' => '.comment-wrapper-' . $comment->pid . ' + .indented',
              'html' => $comment_output,
            );
          }
          else {

            // Older first.
            $commands[] = array(
              'command' => 'ajaxCommentsPrepend',
              'selector' => '.comment-wrapper-' . $comment->pid . ' + .indented',
              'html' => $comment_output,
            );
          }
        }
        else {

          // Flat mode or not reply to comment
          if ($sort == 1) {

            // Newer first.
            $commands[] = array(
              'command' => 'ajaxCommentsAfter',
              'selector' => '.comment-wrapper-nid-' . $comment->nid . ' > .ajax-comment-wrapper:last',
              'html' => $comment_output,
            );
          }
          else {

            // Older first.
            $commands[] = array(
              'command' => 'ajaxCommentsBefore',
              'selector' => '.comment-wrapper-nid-' . $comment->nid . '> .ajax-comment-wrapper:first',
              'html' => $comment_output,
            );
          }
        }
      }
    }
  }
  $output = array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
  return $output;
}