You are here

function comment_form_add_preview in Drupal 4

Same name and namespace in other branches
  1. 5 modules/comment/comment.module \comment_form_add_preview()
  2. 6 modules/comment/comment.module \comment_form_add_preview()
1 string reference to 'comment_form_add_preview'
comment_form in modules/comment.module

File

modules/comment.module, line 1362
Enables users to comment on published content.

Code

function comment_form_add_preview($form, $edit) {
  global $user;
  drupal_set_title(t('Preview comment'));
  $output = '';

  // Invoke full validation for the form, to protect against cross site
  // request forgeries (CSRF) and setting arbitrary values for fields such as
  // the input format. Preview the comment only when form validation does not
  // set any errors.
  drupal_validate_form($form['form_id']['#value'], $form);
  if (!form_get_errors()) {
    $comment = (object) _comment_form_submit($edit);

    // Attach the user and time information.
    if ($edit['author']) {
      $account = user_load(array(
        'name' => $edit['author'],
      ));
    }
    elseif ($user->uid && !isset($edit['is_anonymous'])) {
      $account = $user;
    }
    if ($account) {
      $comment->uid = $account->uid;
      $comment->name = check_plain($account->name);
    }
    $comment->timestamp = $edit['timestamp'] ? $edit['timestamp'] : time();
    $output .= theme('comment_view', $comment);
  }
  $form['comment_preview'] = array(
    '#value' => $output,
    '#weight' => -100,
    '#prefix' => '<div class="preview">',
    '#suffix' => '</div>',
  );
  $output = '';
  if ($edit['pid']) {
    $comment = db_fetch_object(db_query('SELECT c.*, u.uid, u.name AS registered_name, u.picture, u.data FROM {comments} c INNER JOIN {users} u ON c.uid = u.uid WHERE c.cid = %d AND c.status = %d', $edit['pid'], COMMENT_PUBLISHED));
    $comment = drupal_unpack($comment);
    $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
    $output .= theme('comment_view', $comment);
  }
  else {
    $form['#suffix'] = node_view(node_load($edit['nid']));
    $edit['pid'] = 0;
  }
  $form['comment_preview_below'] = array(
    '#value' => $output,
    '#weight' => 100,
  );
  return $form;
}