You are here

function gallery_assist_upload_js in Gallery Assist 6

Upload callback over ahah. Prepare the data for save and rebuild the form.

1 string reference to 'gallery_assist_upload_js'
gallery_assist_menu in ./gallery_assist.module
Implementation of hook_menu().

File

./gallery_assist.module, line 1535
Drupal content type with gallery functionality.

Code

function gallery_assist_upload_js() {
  $cached_form_state = array();
  $items = array();

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

  // Build the path to satisfy the gallery form pager.
  $_GET['q'] = 'node/' . $cached_form['#node']->nid . '/edit';
  $_GET['page'] = $_POST['get_pager_value'];

  // Build form_state from the POST data.
  $form_state = array(
    'values' => $_POST,
  );
  $form_state['gallery_assist_upload_js'] = 1;
  $form_state += $_FILES;
  $form_state['values']['ga_save_caller'] = 'upload_js';

  // Send the data to be processed by upload or update items.
  gallery_assist_form_submit($cached_form, $form_state);

  // Prepare the updated form.
  if (!empty($form_state['values']['gallery_items'])) {
    foreach ($form_state['values']['gallery_items'] as $item) {
      if (empty($item['remove'])) {
        $items[$pid] = $form_state['values']['gallery_items'][$pid];
      }
    }
  }
  $node = $cached_form['#node'];
  $node = node_load($node->nid);
  $node->gall_items = $items;
  $form = _gallery_assist_form($node);
  unset($cached_form['gallery_assist_item']['wrapper']['new']);
  $cached_form['gallery_assist_item']['wrapper'] = array_merge($cached_form['gallery_assist_item']['wrapper'], $form);
  $cached_form['gallery_assist_item']['#collapsed'] = FALSE;
  form_set_cache($_POST['form_build_id'], $cached_form, $cached_form_state);
  foreach ($items as $pid => $item) {
    if (is_numeric($pid)) {
      $form['gallery_items'][$pid]['ptitle']['#default_value'] = $form_state['values']['gallery_items'][$pid]['ptitle'];
      $form['gallery_items'][$pid]['pdescription']['#default_value'] = $form_state['values']['gallery_items'][$pid]['pdescription'];
      $form['gallery_items'][$pid]['remove']['#default_value'] = !empty($form_state['values']['gallery_items'][$pid]['remove']);
      $form['gallery_items'][$pid]['weight']['#default_value'] = $form_state['values']['gallery_items'][$pid]['weight'];
    }
  }
  $form += array(
    '#post' => $_POST,
    '#programmed' => FALSE,
    '#tree' => FALSE,
    '#parents' => array(),
  );

  // Render the form for output.
  $form_id = 'gallery_assist_upload_js';

  //  drupal_alter('form', $form_state, $form_id);
  $data = array(
    $form,
    $form_state,
    $form_id,
  );
  drupal_alter('form', $data);
  $form_state = array(
    'submitted' => FALSE,
  );
  $form = form_builder('gallery_assist_upload_js', $form, $form_state);
  $output = theme('status_messages') . drupal_render($form);

  // The same as by module upload etc.
  // We send the updated file 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;
}