You are here

function background_batch_process_batch in Background Process 7

Same name and namespace in other branches
  1. 8 background_batch/background_batch.module \background_batch_process_batch()
  2. 6 background_batch/background_batch.module \background_batch_process_batch()
  3. 7.2 background_batch/background_batch.module \background_batch_process_batch()

Processes the batch.

Unless the batch has been marked with 'progressive' = FALSE, the function issues a drupal_goto and thus ends page execution.

This function is not needed in form submit handlers; Form API takes care of batches that were set during form submission.

Parameters

$redirect: (optional) Path to redirect to when the batch has finished processing.

$url: (optional - should only be used for separate scripts like update.php) URL of the batch processing page.

File

background_batch/background_batch.module, line 236
This module adds background processing to Drupals batch API

Code

function background_batch_process_batch($redirect = NULL, $url = 'batch', $redirect_callback = 'drupal_goto') {
  $batch =& batch_get();
  drupal_theme_initialize();
  if (isset($batch)) {

    // Add process information
    $process_info = array(
      'current_set' => 0,
      'progressive' => TRUE,
      'url' => $url,
      'url_options' => array(),
      'source_url' => $_GET['q'],
      'redirect' => $redirect,
      'theme' => $GLOBALS['theme_key'],
      'redirect_callback' => $redirect_callback,
    );
    $batch += $process_info;

    // The batch is now completely built. Allow other modules to make changes
    // to the batch so that it is easier to reuse batch processes in other
    // environments.
    drupal_alter('batch', $batch);

    // Assign an arbitrary id: don't rely on a serial column in the 'batch'
    // table, since non-progressive batches skip database storage completely.
    $batch['id'] = db_next_id();

    // Move operations to a job queue. Non-progressive batches will use a
    // memory-based queue.
    foreach ($batch['sets'] as $key => $batch_set) {
      _batch_populate_queue($batch, $key);
    }

    // Initiate processing.
    // Now that we have a batch id, we can generate the redirection link in
    // the generic error message.
    $t = get_t();
    $batch['error_message'] = $t('Please continue to <a href="@error_url">the error page</a>', array(
      '@error_url' => url($url, array(
        'query' => array(
          'id' => $batch['id'],
          'op' => 'finished',
        ),
      )),
    ));

    // Clear the way for the drupal_goto() redirection to the batch processing
    // page, by saving and unsetting the 'destination', if there is any.
    if (isset($_GET['destination'])) {
      $batch['destination'] = $_GET['destination'];
      unset($_GET['destination']);
    }

    // Store the batch.
    db_insert('batch')
      ->fields(array(
      'bid' => $batch['id'],
      'timestamp' => REQUEST_TIME,
      'token' => drupal_get_token($batch['id']),
      'batch' => serialize($batch),
    ))
      ->execute();

    // Set the batch number in the session to guarantee that it will stay alive.
    $_SESSION['batches'][$batch['id']] = TRUE;

    // Redirect for processing.
    $function = $batch['redirect_callback'];
    if (function_exists($function)) {

      // $function($batch['url'], array('query' => array('op' => 'start', 'id' => $batch['id'])));
    }
  }
  background_process_start('_background_batch_process_callback', $batch);
}