You are here

function ajax_comments_reply in AJAX Comments 7

Callback for clicking "reply". Note: $pid is an optional parameter. This functionality is utilized by the "Add new comment" link on pages where there is no default comment form (comment_form_location is COMMENT_FORM_SEPARATE_PAGE)

1 string reference to 'ajax_comments_reply'
ajax_comments_menu in ./ajax_comments.module
Implements hook_menu().

File

./ajax_comments.module, line 568
AJAX comments module file.

Code

function ajax_comments_reply($node, $pid = 0, $flag = 0) {
  if (!user_access('post comments')) {
    return MENU_ACCESS_DENIED;
  }

  // If there is a pid this is a reply to a comment.
  if (!empty($pid)) {
    if (!user_access('access comments')) {
      return MENU_ACCESS_DENIED;
    }

    // Make sure the comment is valid and published.
    if (!($comments = comment_load_multiple(array(
      $pid,
    ), array(
      'status' => COMMENT_PUBLISHED,
    )))) {
      return MENU_NOT_FOUND;
    }
    $comment = $comments[$pid];

    // Make sure the comment belongs to this node.
    if ($comment->nid != $node->nid) {
      return MENU_NOT_FOUND;
    }
  }
  if (!user_is_anonymous() || user_is_anonymous() && variable_get('cache', 0) != 0) {

    // Authenticated user or anonymous and page cache is enabled
    $key = _ajax_comments_cache_key($node->nid, $pid);
    if ($cache = cache_get($key)) {

      // Get form from cache
      $form_build = $cache->data;
    }
    else {

      // Build form and Save to cache*/
      $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array(
        'nid' => $node->nid,
        'pid' => $pid,
      ), array(
        'flag' => $flag,
      ));
      cache_set($key, $form_build, 'cache', time() + 360);
    }
  }
  else {

    // Anonymous and page cache is disabled
    $form_build = drupal_get_form("comment_node_{$node->type}_form", (object) array(
      'nid' => $node->nid,
      'pid' => $pid,
    ), array(
      'flag' => $flag,
    ));
  }
  $form = trim(drupal_render($form_build));
  if (variable_get('ajax_comments_reply_autoclose') && !empty($pid)) {
    $commands[] = ajax_command_remove('.ajax-comments-form-reply');
    $commands[] = ajax_command_invoke('.ajax-comments-reply', 'show');
  }

  // Add the new form.
  if (!empty($pid)) {
    $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
    if (empty($mode)) {
      $commands[] = ajax_command_after('.comment-wrapper-' . $pid, $form);
    }
    else {

      // Add div with class "indented" if they are not exist
      $commands[] = array(
        'command' => 'ajaxCommentsAddDummyDivAfter',
        'selector' => '.comment-wrapper-' . $pid,
        'class' => 'indented',
      );

      // Check sort by comment_goodness.
      if (_ajax_comments_get_comment_sort_order($node) == 1) {

        // Newer first.
        $commands[] = ajax_command_append('.comment-wrapper-' . $pid . ' + .indented', $form);
      }
      else {

        // Older first.
        $commands[] = ajax_command_prepend('.comment-wrapper-' . $pid . ' + .indented', $form);
      }
    }
  }
  else {

    // Check Views Add form
    if (!empty($flag)) {
      $commands[] = ajax_command_after('.views-comment-wrapper-nid-' . $node->nid . ' > .ajax-comment-wrapper:last', $form);
    }
    else {

      // Check sort by comment_goodness.
      if (_ajax_comments_get_comment_sort_order($node) == 1) {

        // Older first. Append comment to last wrapper.
        $commands[] = ajax_command_after('.comment-wrapper-nid-' . $node->nid . ' > .ajax-comment-wrapper:last', $form);
      }
      else {

        // Newer first. Append comment to top.
        $commands[] = ajax_command_before('.comment-wrapper-nid-' . $node->nid . '> .ajax-comment-wrapper:first', $form);
      }
    }
  }

  // Hide reply to comment link
  $commands[] = ajax_command_invoke('.ajax-comments-reply-' . $node->nid . '-' . $pid, 'hide');
  if (!variable_get('ajax_comments_disable_scroll', 0)) {
    $commands[] = array(
      'command' => 'ajaxCommentsScrollToElement',
      'selector' => '.ajax-comments-reply-form-' . $node->nid . '-' . $pid . '-0',
    );
    $commands[] = ajax_command_invoke('.ajax-comments-reply-form-' . $node->nid . '-' . $pid . '-0 .form-textarea', 'focus');
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}