You are here

function patterns_execute_pattern_batch in Patterns 7.2

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_execute_pattern_batch()
  2. 6 patterns.module \patterns_execute_pattern_batch()
  3. 7 includes/core/batch.inc \patterns_execute_pattern_batch()

Starts preliminary operations for pattern execution.

E.g.: loading additional modules, and creating the array of patterns actions.

If there are no errors, it creates the batch array of operations. Each of them is a call to patterns_batch_actions with different parameters.

@TODO Do we still need to pass $pattern here?

Parameters

stdClass $pattern Pattern object as loaded by patterns_get_pattern().:

array $params Parameters for executing the pattern. Array format as follows::

  • pid => Pid of the pattern as it is in the database
  • run-subpatterns => ['first-update', always', 'update', 'first', 'never']
  • quickrun => boolean flag to indicate that the execution was initiated from a quickrun and the pattern is already loaded into $pattern.

$patterns_details:

$actions_map:

File

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

Code

function patterns_execute_pattern_batch($pattern, $params, $patterns_details, $actions_map) {
  $pattern_details = current($patterns_details);
  $pid = key($patterns_details);
  $info = $pattern_details['info'];
  $infos = array(
    $pid => $info,
  );
  $modules = $pattern_details['modules'];
  $sections = $pattern_details['sections'];
  $include_options = patterns_parser_build_include_options($pid, $params['run-subpatterns']);

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

  // Looping through the sections

  ///////////////////////////////
  $tot_sec = count($sections);
  $cur_sec = 1;
  $_SESSION['patterns_batch_counter'] = 0;
  $_SESSION['patterns_batch_total'] = $tot_sec;
  foreach ($sections as $section => $actions) {

    // by default this pattern should run included ?
    // this setting can be overwritten by the pattern file itself
    $actions = patterns_parser_retrieve_actions_from_section($actions, $include_options);

    // This can happen if includes are disabled
    if (count($actions) == 0) {
      drupal_set_message(t('Section "%section" did not contain any valid action. Please check your policy for included patterns.', array(
        '%section' => $section,
      )), 'warning');
      continue;
    }

    // Reformat the actions. $actions passed as reference.
    patterns_reformat_actions($actions);
    $progress_params = array(
      '%section' => $section,
      '%cur_sec' => $cur_sec,
      '%tot_sec' => $tot_sec,
    );
    $progress = t('Running section "%section" (%cur_sec/%tot_sec), action @current out of @total', $progress_params);
    $batch = array(
      //'title' => t('Processing section %section of pattern %pattern', array('%section' => $section, '%pattern' => $info['title'])),
      'title' => t('Processing pattern %pattern', array(
        '%pattern' => $info['title'],
      )),
      'progress_message' => $progress,
      'operations' => array(),
      'file' => drupal_get_path('module', 'patterns') . '/includes/core/batch.inc',
      'finished' => 'patterns_batch_finish',
      'init_message' => t('Initializing section "%section" (%cur_sec/%tot_sec)', array(
        '%section' => $section,
        '%cur_sec' => $cur_sec,
        '%tot_sec' => $tot_sec,
      )),
    );
    $total = count($actions);
    $i = 0;
    foreach ($actions as $action) {
      $batch['operations'][] = array(
        'patterns_batch_action',
        array(
          $action['action'],
          $action['data'],
          $i,
          $actions_map,
          $section,
        ),
      );
      $i++;
    }
    $_SESSION['patterns_batch_info'] = $infos;
    batch_set($batch);
    $cur_sec++;
  }
  return TRUE;
}