You are here

function ajax_comments_js in AJAX Comments 6

AHAH callback.

1 string reference to 'ajax_comments_js'
ajax_comments_menu in ./ajax_comments.module
Implementation of hook_menu().

File

./ajax_comments.pages.inc, line 11
AJAX comments form handling and callbacks.

Code

function ajax_comments_js() {
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = ajax_comments_form_get_cache($form_build_id, $form_state);
  if ($form) {
    $args = $form['#parameters'];
    $form_id = array_shift($args);
  }
  else {
    $form_id = 'comment_form';
    $edit['nid'] = $_POST['nid'];
    $args = array(
      $form_id,
      $edit,
    );
    $form_state['post'] = $_POST;

    // Use a copy of the function's arguments for manipulation
    $args_temp = $args;
    $args_temp[0] =& $form_state;
    array_unshift($args_temp, $form_id);
    $form = call_user_func_array('drupal_retrieve_form', $args_temp);
    $form_build_id = 'form-' . md5(uniqid(mt_rand(), true));
    $form['#build_id'] = $form_build_id;
    drupal_prepare_form($form_id, $form, $form_state);
    $original_form = $form;
    $cacheable = FALSE;

    // No need to cache; amost done
    $form['#post'] = $_POST;
  }
  $form_state['post'] = $form['#post'] = $_POST;
  $form['#programmed'] = $form['#redirect'] = FALSE;
  if ($form_state['post']['op'] == $form_state['values']['preview']) {
    $form['#after_build'] = array(
      'comment_form_add_preview',
    );
  }
  else {
    unset($form['#after_build']);
  }
  drupal_process_form($form_id, $form, $form_state);
  $errors = form_get_errors();
  if (!$errors) {

    // Prepare output
    if ($form_state['values']['op'] == $form_state['values']['preview']) {
      $output = '<div class="preview-item">' . $form['comment_preview']['#value'] . '</div>';
      if ($output && $form_state['values']['pid']) {
        $output = '<div class="indented">' . $output . '<div>';
      }
    }
    elseif ($form_state['values']['op'] == $form_state['values']['submit']) {
      $output = '<div class="comment-new-success">' . $form_state['storage']['ajax_comment'] . '</div>';
    }
    $form = form_builder($form_id, $form, $form_state);

    // Add Submit button if preview was required
    $node = node_load($form_state['values']['nid']);
    $op = $form_state['values']['op'];
    if (variable_get('comment_preview_' . $node->type, COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_REQUIRED && ($op == t('Preview') || $op == t('Save'))) {
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
        '#weight' => 19,
        '#executes_submit_callback' => TRUE,
      );
      $form['submit']['#id'] = "ajax-comments-submit";
      $form['submit']['#submit'] = array(
        'ajax_comments_submit',
      );
      $form['submit']['#ahah'] = array(
        'path' => 'ajax_comments/js',
        'wrapper' => 'comment-form-content',
        'event' => 'click',
        'method' => 'before',
        'effect' => 'ajaxCommentsSubmit',
        'progress' => array(
          'type' => '1bar',
          'message' => t('Please wait...'),
        ),
      );
      $form = form_builder($form_id, $form, $form_state);
      form_set_cache($form_build_id, $form, $form_state);
    }
  }
  unset($form['#suffix']);
  unset($form['#prefix']);
  $output = theme('status_messages') . $output;
  if ($output) {
    $javascript = drupal_add_js(NULL, NULL);
    if (isset($javascript['setting'])) {
      $output .= '<script type="text/javascript">jQuery.extend(Drupal.settings, ' . drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) . ');</script>';
    }
    drupal_json(array(
      'status' => TRUE,
      'data' => $output,
    ));
  }
}