You are here

function privatemsg_attachments_upload_js in Privatemsg 6.2

Menu callback for JavaScript-based uploads.

See upload_js() from upload.module.

1 string reference to 'privatemsg_attachments_upload_js'
privatemsg_attachments_menu in privatemsg_attachments/privatemsg_attachments.module
Implements hook_menu().

File

privatemsg_attachments/privatemsg_attachments.module, line 176
Allows users to add attachments to messages

Code

function privatemsg_attachments_upload_js() {
  $form_state = array(
    'values' => $_POST,
    'storage' => NULL,
    'submitted' => FALSE,
  );

  // Load the form from the Form API cache.
  if (!($cached_form = form_get_cache($_POST['form_build_id'], $form_state)) || !isset($cached_form['attachments'])) {
    form_set_error('form_token', t('Validation error, please try again. If this error persists, please contact the site administrator.'));
    $output = theme('status_messages');
    print drupal_to_js(array(
      'status' => TRUE,
      'data' => $output,
    ));
    exit;
  }

  // Handle new uploads, and merge tmp files.
  _privatemsg_attachments_upload_submit($cached_form, $form_state);
  if (!empty($form_state['values']['files'])) {
    foreach ($form_state['values']['files'] as $fid => $file) {
      if (is_array($form_state['values']['files'][$fid]) && isset($form_state['storage']['files'][$fid]) && is_array($form_state['storage']['files'][$fid])) {
        $form_state['values']['files'][$fid] += $form_state['storage']['files'][$fid];
      }
    }
  }
  $form_state['storage']['files'] = $form_state['values']['files'];
  $form = _privatemsg_attachments_form($form_state['values']['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, $form_state);
  foreach ($form_state['values']['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(),
    '#id' => '_privatemsg_upload_form',
  );
  $form['__drupal_alter_by_ref'] = array(
    &$form_state,
  );
  drupal_alter('form', $form, '_privatemsg_upload_form');
  $form_state = array(
    'submitted' => FALSE,
  );
  $form = form_builder('_privatemsg_upload_form', $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.
  print drupal_to_js(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}