You are here

function ajax_comments_submit_dispatch in AJAX Comments 5

File

./ajax_comments.module, line 72

Code

function ajax_comments_submit_dispatch($form_id, $form_values) {
  $js_enabled = $form_values['js_enabled'];
  $edit = _comment_form_submit($form_values);
  if ($cid = comment_save($edit)) {
    $errors = form_get_errors();
    if (!$errors) {
      $node = node_load($edit['nid']);

      // update node stats
      _comment_update_node_statistics($node->nid);
      node_tag_new($node->nid);

      // Single comment view.
      $query = 'SELECT c.cid, c.pid, c.nid, c.subject, c.comment, c.format, c.timestamp, c.name, c.mail, c.homepage, u.uid, u.name AS registered_name, u.picture, u.data, c.score, c.users, c.status FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d';
      $query_args = array(
        $cid,
      );
      if (!user_access('administer comments')) {
        $query .= ' AND c.status = %d';
        $query_args[] = COMMENT_PUBLISHED;
      }
      $result = db_query($query, $query_args);
      if ($comment = db_fetch_object($result)) {
        $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
        $links = module_invoke_all('link', 'comment', $comment, 0);
        foreach (module_implements('link_alter') as $module) {
          $function = $module . '_link_alter';
          $function($node, $links);
        }
        $preview .= theme('comment_view', $comment, $links);
      }
      if ($comment->pid) {
        $preview = '<div class="indented">' . $preview . '<div>';
      }
      $redirect = drupal_get_path_alias('node/' . $comment->nid) . '?r' . $cid . '#comment-' . $cid;
    }
    $result = array(
      'status' => TRUE,
      'data' => array(
        'errors' => $errors,
        'message' => theme('status_messages'),
        'preview' => $preview,
        'destination' => $redirect,
      ),
    );
    if ($js_enabled != 1) {
      return $js_enabled;
    }
    print drupal_to_js($result);
  }
}