You are here

function comment_upload_js in Comment Upload 6

Same name and namespace in other branches
  1. 5 comment_upload.module \comment_upload_js()

React to the Attach button; update file information on AHAH request.

1 string reference to 'comment_upload_js'
comment_upload_menu in ./comment_upload.module
Implementation of hook_menu().

File

./comment_upload.module, line 437
Provides file attachment functionality for comments.

Code

function comment_upload_js() {
  $cached_form_state = array();
  $files = array();

  // Load the form from the Form API cache.
  if (empty($_POST) || !($cached_form = form_get_cache($_POST['form_build_id'], $cached_form_state)) || !isset($cached_form['#comment_upload_storage']) || !isset($cached_form['attachments'])) {
    form_set_error('form_token', t('Validation error, please try again. The file you attempted to upload may be too large. If this error persists, please contact the site administrator.'));
    $output = theme('status_messages');
    print drupal_to_js(array(
      'status' => TRUE,
      'data' => $output,
    ));
    exit;
  }
  $form_state = array(
    'values' => $_POST,
  );
  comment_upload_process_files($cached_form, $form_state);
  if (!empty($form_state['values']['files'])) {
    foreach ($form_state['values']['files'] as $fid => $file) {
      if (isset($cached_form['#comment_upload_storage'][$fid])) {
        $files[$fid] = $cached_form['#comment_upload_storage'][$fid];
      }
    }
  }
  $form = comment_upload_upload_form($files);
  unset($cached_form['attachments']['wrapper']['new']);
  $cached_form['attachments']['wrapper'] = array_merge($cached_form['attachments']['wrapper'], $form);
  $cached_form['attachments']['#collapsed'] = FALSE;
  form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state);
  foreach ($files as $fid => $file) {
    if (is_numeric($fid)) {
      $form['files'][$fid]['description']['#default_value'] = $form_state['values']['files'][$fid]['description'];
      $form['files'][$fid]['list']['#default_value'] = !empty($form_state['values']['files'][$fid]['list']);
      $form['files'][$fid]['remove']['#default_value'] = !empty($form_state['values']['files'][$fid]['remove']);
      $form['files'][$fid]['weight']['#default_value'] = $form_state['values']['files'][$fid]['weight'];
    }
  }

  // Render the form for output.
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
    '#tree' => FALSE,
    '#parents' => array(),
  );

  // Normally, we would call drupal_alter($form_id, $form, $form_state).
  // However, drupal_alter() normally supports just one byref parameter. Using
  // the __drupal_alter_by_ref key, we can store any additional parameters
  // that need to be altered, and they'll be split out into additional params
  // for the hook_form_alter() implementations.
  $data =& $form;
  $form_state_empty = array();
  $data['__drupal_alter_by_ref'] = array(
    &$form_state_empty,
  );
  drupal_alter('form', $data, 'comment_upload_js');
  $form_state = array(
    'submitted' => FALSE,
  );
  $form = form_builder('comment_upload_js', $form, $form_state);
  $output = theme('status_messages') . drupal_render($form);

  // We send the updated file attachments form.
  // Don't call drupal_json(). ahah.js uses an iframe and
  // the header output by drupal_json() causes problems in some browsers.
  $GLOBALS['devel_shutdown'] = FALSE;
  echo drupal_to_js(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}