You are here

function _background_batch_page_do_js in Background Process 6

Same name and namespace in other branches
  1. 7.2 background_batch/background_batch.pages.inc \_background_batch_page_do_js()
  2. 7 background_batch/background_batch.pages.inc \_background_batch_page_do_js()

Do one pass of execution and inform back the browser about progression (used for JavaScript-mode only).

1 call to _background_batch_page_do_js()
_background_batch_page in background_batch/background_batch.pages.inc
State-based dispatcher for the batch processing page.

File

background_batch/background_batch.pages.inc, line 226
Pages for background batch.

Code

function _background_batch_page_do_js() {

  // HTTP POST required.
  if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    drupal_set_message(t('HTTP POST is required.'), 'error');
    drupal_set_title(t('Error'));
    return '';
  }
  $batch =& batch_get();
  $id = $batch['id'];
  session_save_session(FALSE);
  $percentage = t('N/A');
  $message = '';
  if ($progress = progress_get_progress('_background_batch:' . $id)) {
    $percentage = $progress->progress * 100;
    $message = $progress->message;
    progress_estimate_completion($progress);

    // Check wether ETA information should be shown.
    if (variable_get('background_batch_show_eta', BACKGROUND_BATCH_PROCESS_ETA)) {
      $message = "ETA: " . ($progress->estimate ? format_date((int) $progress->estimate, 'large') : t('N/A')) . "<br/>{$message}";
    }
    else {
      $js_setting['batch']['initMessage'] = $message;
    }
  }
  if (count($batch['sets'][$batch['current_set']]['operations']) == 0) {

    // The background process has self-destructed, and the batch job is done.
    $percentage = 100;
    $message = '';
  }
  elseif ($process = background_process_get_process('background_batch:' . $id)) {
    _background_batch_initiate($process);
  }
  else {

    // Not running ... and stale?
    _background_batch_initiate();
  }
  drupal_json(array(
    'status' => TRUE,
    'percentage' => sprintf("%.02f", $percentage),
    'message' => $message,
  ));
}