You are here

function ajax_comments_submit_js in AJAX Comments 7

Builds the comment.

1 string reference to 'ajax_comments_submit_js'
ajax_comments_form_comment_form_alter in ./ajax_comments.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function ajax_comments_submit_js($form, &$form_state) {

  // Return the actual form if it contains errors.
  if (form_get_errors()) {

    // Remove comment preview
    if (isset($form['comment_preview'])) {
      unset($form['notify_text']);
      unset($form['comment_preview']);
    }
    return $form;
  }

  // This is to remove the "Your comment has been posted" status message that
  // will appear upon refresh. This seems dirty but it means we don't have to
  // rewrite the whole comment_form_submit(). Please chime in if you think this
  // is dumb.
  ajax_comments_remove_status();
  $cid = empty($form['cid']['#value']) ? 0 : $form['cid']['#value'];
  $pid = empty($form_state['comment']->pid) ? 0 : $form_state['comment']->pid;
  $key = _ajax_comments_cache_key($form_state['comment']->nid, $pid);
  if (cache_get($key)) {

    // This form cached. Remove it from cache.
    cache_clear_all($key, 'cache');
  }
  $comment = comment_load($form_state['comment']->cid);
  $node = $form['#node'];
  $notify_text = variable_get('ajax_comments_notify', '') ? theme('ajax_comments_notify_text', array(
    'comment' => $comment,
  )) : '';
  $comment_build = comment_view($comment, $node);

  // remove messages
  if (variable_get('ajax_comments_notify')) {
    $commands[] = ajax_command_remove('.messages.warning');
    $commands[] = ajax_command_remove('.messages.status');
  }

  // Don't display as a preview as this is being submitted.
  unset($comment_build['comment_body']['#object']->in_preview);
  unset($form_state['comment_preview']);
  $comment_output = drupal_render($comment_build);

  // Are we editing a comment.
  if (isset($form['cid']['#value'])) {

    //$commands[] = ajax_command_replace('.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, $comment_output);
    $commands[] = array(
      'command' => 'ajaxCommentsReplace',
      'selector' => '.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid,
      'html' => $comment_output,
    );
  }
  elseif (!empty($form_state['values']['pid'])) {
    $mode = variable_get('comment_default_mode_' . $node->type, COMMENT_MODE_THREADED);
    if (!empty($mode)) {

      // Threaded. Append comment to parent wrapper.

      //$commands[] = ajax_command_replace('.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, $comment_output);
      $commands[] = array(
        'command' => 'ajaxCommentsReplace',
        'selector' => '.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid,
        'html' => $comment_output,
      );
    }
    else {

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

        // Older first. Append comment to last wrapper.
        $commands[] = ajax_command_invoke('.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');

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

        // Newer first. Append comment to top.
        $commands[] = ajax_command_invoke('.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');

        //$commands[] = ajax_command_prepend('.comment-wrapper-nid-' . $comment->nid, $comment_output);
        $commands[] = array(
          'command' => 'ajaxCommentsPrepend',
          'selector' => '.comment-wrapper-nid-' . $comment->nid,
          'html' => $comment_output,
        );
      }
    }
    if (!variable_get('ajax_comments_disable_scroll', 0)) {
      $commands[] = array(
        'command' => 'ajaxCommentsScrollToElement',
        'selector' => '.comment-wrapper-' . $comment->cid,
      );
    }
  }
  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-' . $comment->nid . ' > .ajax-comment-wrapper:last', $comment_output);
      $commands[] = array(
        'command' => 'ajaxCommentsAfter',
        'selector' => '.comment-wrapper-nid-' . $comment->nid . ' > .ajax-comment-wrapper:last',
        'html' => $comment_output,
      );
    }
    else {

      // Newer first. Append comment to top.

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

    // If we have a default form, update it with a new one.
    if (variable_get('comment_form_location_' . $node->type, COMMENT_FORM_BELOW) == COMMENT_FORM_BELOW && empty($form_state['build_info']['args'][1]['flag'])) {
      $new_form_state = array();
      $new_form_state['build_info']['args'][] = (object) array(
        'nid' => $node->nid,
      );

      // Don't pull from cache.
      $new_form_state['input'] = array();
      $new_form_build = drupal_build_form($form['#form_id'], $new_form_state);
      $commands[] = ajax_command_replace('.ajax-comments-reply-form-' . $form_state['comment']->nid . '-' . $pid . '-' . $cid, drupal_render($new_form_build));
      if (!variable_get('ajax_comments_disable_scroll', 0) && _ajax_comments_get_comment_sort_order($node) == 2) {
        $commands[] = array(
          'command' => 'ajaxCommentsScrollToElement',
          'selector' => '.comment-wrapper-nid-' . $comment->nid,
        );
      }
    }
    else {
      $commands[] = ajax_command_invoke('.ajax-comments-reply-form-' . $comment->nid . '-' . $pid . '-' . $cid, 'remove');
    }
  }
  if (!empty($form_state['build_info']['args'][1]['flag'])) {

    // Submiting comment via Views Add Comment Form
    switch ((int) $form_state['build_info']['args'][1]['flag']) {
      case 1:
        $commands[] = ajax_command_invoke('.views-comment-result-nid-' . $comment->nid, 'show');
        break;
      case 2:
        $commands[] = ajax_command_invoke('.ajax-comments-reply-' . $comment->nid . '-' . $pid, 'show');
        break;
    }
  }
  else {

    // Show reply to comment link
    $commands[] = ajax_command_invoke('.ajax-comments-reply-' . $comment->nid . '-' . $pid, 'show');
  }

  // Show notify
  if (!empty($notify_text)) {

    //$commands[] = ajax_command_before('.comment-wrapper-' . $comment->cid, $notify_text);
    $commands[] = array(
      'command' => 'ajaxCommentsBefore',
      'selector' => '.comment-wrapper-' . $comment->cid,
      'html' => $notify_text,
    );
  }
  $output = array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
  return $output;
}