You are here

function patterns_execute_pattern_drushbatch in Patterns 6.2

This is a fork of patterns_execute_patterns_batch(), and will be adapted to use the drush batch api instead @todo (evergreen todo): make sure this is always in sync with patterns_execute_pattern_batch().

File

./patterns.drush.inc, line 406
Drush Patterns module commands

Code

function patterns_execute_pattern_drushbatch($pattern, $params = array()) {
  set_time_limit(0);
  if (!is_object($pattern)) {
    $pattern = patterns_get_pattern($pattern);
    if (!$pattern) {
      return FALSE;
    }
  }
  $pattern->subpatterns_run_mode = $params['run-subpatterns'];
  $pattern_details = patterns_get_pattern_details($pattern, TRUE);
  $modules = $pattern_details['modules'];
  $actions = $pattern_details['actions'];
  $actions_map = array(
    'patterns' => $pattern_details['info'],
    'map' => $pattern_details['actions_map'],
  );
  $info = reset($pattern_details['info']);

  // If there are no actions or modules, most likely the pattern
  // was not created correctly.
  if (empty($actions) && empty($modules)) {
    drupal_set_message(t('Could not recognize pattern %title, aborting.', array(
      '%title' => $info['title'],
    )), 'error');
    return FALSE;
  }
  $result = patterns_install_modules($modules);
  if (!$result['success']) {
    drupal_set_message($result['error_message'], 'error');
    return FALSE;
  }
  $result = patterns_prepare_actions($actions, $actions_map);
  if (!$result['success']) {
    drupal_set_message($result['error_message'], 'error');
    return FALSE;
  }
  $batch = array(
    'title' => t('Processing pattern %pattern', array(
      '%pattern' => $info['title'],
    )),
    //    'init_message' => t('Running action @current out of @total', array('@current' => 1, '@total' => count($actions))),
    'progress_message' => t('Running action @current out of @total'),
    'operations' => array(),
    'finished' => 'patterns_batch_finish',
  );
  for ($i = 0; $i < count($actions); $i++) {
    $batch['operations'][] = array(
      'patterns_batch_actions',
      array(
        $actions[$i],
        $i,
        $actions_map,
      ),
    );
  }
  $_SESSION['patterns_batch_info'] = $pattern_details['info'];
  batch_set($batch);
  return TRUE;
}