function batch_ajax_callback

6.x batch.module batch_ajax_callback($batch_id)
5.x batch.module batch_ajax_callback($batch_id)

This function is called by ajax, and will trigger each run of the batch operation function, then return appropriate results back to our javascript.

Parameters

unknown_type $batch_id:

File

modules/batch/batch.module, line 68

Code

function batch_ajax_callback($batch_id) {

  $rtn = array();


  $batch = batch_get($batch_id);
  if ($batch ["token"] != batch_get_token()) {
    //  Not allowed!  Bad token.
    header('Content-Type: application/json');
    print json_encode(array("error" => t("An error has occured-- you are now allowed to run this batch, or the batch does not exist.")));
    exit();
  }


  ///////////////////////////////////
  // Run the batch operation.
  if (isset($batch ["file"]) && $batch ["file"] != "") {
    require_once ($batch ["file"]);
  }
  $operation_callback = $batch ["operation"][0];

  // If the operation callback doesn't exist, throw an error.
  if (!function_exists($operation_callback)) {
    header('Content-Type: application/json');
    print json_encode(array("error" => t("An error has occured: Cannot find operation callback function @function", array("@function" => addslashes($operation_callback)))));
    exit();
  }

  $operation_args = $batch ["operation"][1];
  // Add our $batch to the beginning of the $args array.
  //array_unshift($operation_args, $batch);
  $params = array(0 => &$batch);
  foreach ($operation_args as $val) {
    $params [] = $val;
  }

  call_user_func_array($operation_callback, $params);



  // Coming out of this, $batch should be modified
  // Save it back to the database.
  db_query("UPDATE batch_queue 
            SET batch_data = '?'
            WHERE batch_id = '?' ", serialize($batch), $batch_id);


  // Output relavant results....
  $current = $batch ["results"]["current"];
  $total = $batch ["results"]["total"];
  $percent = "";
  if ($total > 0) {
    $percent = round(($current / $total) * 100, 1) * 1;
  }


  $rtn ["progress_message"] = t($batch ["progress_message"], array("@current" => $current, "@total" => $total, "@percent" => $percent));
  $rtn ["percent"] = $percent;
  $rtn ["display_percent"] = $batch ["display_percent"];
  $rtn ["finished"] = "";
  if ($batch ["results"]["finished"] == TRUE) {
    $rtn ["finished"] = "finished";
  }




  header('Content-Type: application/json');
  $rtn ["success"] = "SUCCESS"; // let javascript know we DID finish executing correctly.
  print json_encode($rtn);

  exit();
}