You are here

function patterns_batch_action in Patterns 7.2

Same name and namespace in other branches
  1. 7 includes/core/batch.inc \patterns_batch_action()

Executes a batch action.

@TODO Doc.

Parameters

array $action:

mixed $place index of the current operation within the batch_set: s_map [$pid pattern id, $index ??]

array $context:

1 string reference to 'patterns_batch_action'
patterns_execute_pattern_batch in includes/core/batch.inc
Starts preliminary operations for pattern execution.

File

includes/core/batch.inc, line 122
Running patterns using Drupal's batch API.

Code

function patterns_batch_action($action, $data, $place, $actions_map, $section, &$context) {

  // Since we are in the batch, we need to load some things once more.
  module_load_include('inc', 'patterns', 'includes/core/common');
  module_load_include('inc', 'patterns', 'includes/core/modules');
  module_load_include('inc', 'patterns', 'includes/core/token');
  patterns_io_load_components();

  // Nothing to do if there is no action
  if (empty($data) || empty($action)) {
    $context['finished'] = 1;
    $context['results']['abort'] = 1;
    $context['results']['error_message'] = t('Cannot execute empty action.', 'error');
    return FALSE;
  }

  // Saving the name of the section in the context
  $context['results']['section'] = $section;

  // Start a timer. Since we want each action to be its own http request, we need
  // to ensure the batch api will decide to do it like that by making each action
  // take at least a second to execute
  timer_start('patterns_action');

  /////////////////////////

  // PREPARATION
  $results = patterns_prepare_action($action, $data);
  if ($results['status'] == PATTERNS_ERR) {
    $context['finished'] = 1;
    $context['results']['abort'] = 1;
    $context['results']['error_message'] = NULL;
    $context['results']['idx'] = $place;
    return FALSE;
  }

  ///////////////////////

  // skip action execution if an error is encountered in some of the previous operations
  if (!empty($context['results']['abort'])) {
    return;
  }
  $results = patterns_implement_action($action, $data, $context['results']['identifiers'], $place, $actions_map);
  if ($results['status'] == PATTERNS_ERR) {

    // TODO: handle better?
    // we use 'results' to keep track of errors and abort execution if required
    $context['results']['abort'] = TRUE;
    $context['results']['error_message'] = $results['msg'];
  }
  if (timer_read('patterns_action') < 1000) {
    @usleep(1000 - timer_read('patterns_action'));
  }
}