You are here

function ajax_comments_submit in AJAX Comments 6

Comment submit routine.

2 string references to 'ajax_comments_submit'
ajax_comments_form_comment_form_alter in ./ajax_comments.module
Implementation of hook_form_FORM_ID_alter().
ajax_comments_js in ./ajax_comments.pages.inc
AHAH callback.

File

./ajax_comments.module, line 176
Implements AJAX handling for Drupal comment forms.

Code

function ajax_comments_submit($form, &$form_state) {

  //remove self
  unset($form_state['submit_handlers']);

  // ..and standart comments submit handler
  foreach ($form['#submit'] as $key => $value) {
    if ($value == 'comment_form_submit') {
      unset($form['#submit'][$key]);
    }
  }

  //execute all others
  form_execute_handlers('submit', $form, $form_state);

  //save comment just like comments module do it
  $edit = $form_state['values'];
  _comment_form_submit($edit);
  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);
      $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.signature, u.picture, u.data, 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;
      }
      $query = db_rewrite_sql($query, 'c', 'cid');
      $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);
        drupal_alter('link', $links, $node);

        //render our comment and get it back to AHAH handler
        $output .= theme('comment_view', $comment, $node, $links);
        $form_state['storage']['ajax_comment'] = $output;
      }
    }
  }
  return $output;
}