You are here

function patterns_get_pattern_details in Patterns 6

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_get_pattern_details()

Return an array with detailed information about the pattern

1 call to patterns_get_pattern_details()
patterns_execute_pattern_batch in ./patterns.module

File

./patterns.module, line 1815
Enables extremely simple adding/removing features to your site with minimal to no configuration

Code

function patterns_get_pattern_details($pattern, $recursive = FALSE, &$pids = array()) {

  // prevent infinite recursion
  if (in_array($pattern->pid, $pids)) {
    return array();
  }
  $pids[$pattern->pid] = $pattern->pid;
  $actions = !empty($pattern->pattern['actions']) ? $pattern->pattern['actions'] : array();
  $modules = !empty($pattern->pattern['modules']) ? $pattern->pattern['modules'] : array();
  $patterns[$pattern->pid] = (array) $pattern;
  $patterns[$pattern->pid] = array_merge($patterns[$pattern->pid], $patterns[$pattern->pid]['pattern']['info']);
  unset($patterns[$pattern->pid]['pattern']);
  if ($recursive) {
    $result = array(
      'modules' => $modules,
      'info' => $patterns,
    );
    foreach ($actions as $key => $action) {
      if ($action['tag'] == 'pattern') {

        // determine pattern name
        if (!empty($action['value'])) {
          $name = $action['value'];
        }
        elseif (!empty($action['name'])) {
          $name = $action['name'];
        }
        if (!($p = patterns_get_pattern($name))) {

          // just give a warning and try to continue
          drupal_set_message(t('Action #%key in %file: Pattern %pattern not found.<br>Pattern execution will try to continue without it.', array(
            '%key' => $key + 1,
            '%file' => $pattern->title,
            '%pattern' => $name,
          )), 'warning');
          continue;
        }

        // Decide if sub-pattern needs to be run based on the mode defined within the pattern or selected in UI at the time of form submission
        // @TODO: UI setting should be able to override a setting defined within the pattern
        $modes = array(
          'first-update',
          'always',
          'update',
          'first',
          'never',
        );
        if (!empty($action['run']) && in_array($action['run'], $modes)) {
          $mode = $action['run'];
        }
        else {
          $mode = $pattern->subpatterns_run_mode;
        }
        switch ($mode) {
          case 'never':

            // don't run sub-pattern
            drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to be skipped.', array(
              '%key' => $key + 1,
              '%file' => $pattern->title,
              '%pattern' => $name,
            )), 'status');
            continue 2;
            break;
          case 'first':

            // Only run on first run
            if ($p->status) {
              drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on the first run.', array(
                '%key' => $key + 1,
                '%file' => $pattern->title,
                '%pattern' => $name,
              )), 'status');
              continue 2;
            }
            break;
          case 'update':

            // Only run on pattern update
            if ($p->enabled >= $p->updated) {
              drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on pattern update.', array(
                '%key' => $key + 1,
                '%file' => $pattern->title,
                '%pattern' => $name,
              )), 'status');
              continue 2;
            }
            break;
          case 'first-update':

            // Only run on first run or pattern update
            if ($p->status && $p->enabled >= $p->updated) {
              drupal_set_message(t('Action #%key in %file: Pattern %pattern not ran because the pattern was set to execute only on first run or update.', array(
                '%key' => $key + 1,
                '%file' => $pattern->title,
                '%pattern' => $name,
              )), 'status');
              continue 2;
            }
            break;
          case 'always':
          default:

            // Run always
            break;
        }
        $a = patterns_get_pattern_details($p, TRUE, $pids);
        if (is_array($a) && empty($a)) {

          // An empty array is returned on infinite recursion detection
          drupal_set_message(t('Action #%key in %file: Infinite recursion detected while attempting to run pattern %pattern.<br>Pattern execution will try to continue without it.', array(
            '%key' => $key + 1,
            '%file' => $pattern->title,
            '%pattern' => $name,
          )), 'warning');
          continue;
        }

        // array_merge doesn't preserve numeric array keys
        // so we handle 'info' separately
        $info = $result['info'];
        $result = array_merge_recursive($result, $a);
        $result['info'] = $info + $a['info'];
      }
      else {
        $result['actions'][] = $action;
        $result['actions_map'][] = array(
          'pid' => $pattern->pid,
          'index' => $key,
        );
      }
    }
    $result['modules'] = array_merge(array_unique($result['modules']));

    // Remove pid from recursion stack

    //unset($pids[$pattern->pid]);
    return $result;
  }

  // Remove pid from recursion stack

  //unset($pids[$pattern->pid]);
  return array(
    'actions' => $actions,
    'modules' => $modules,
    'info' => $patterns,
  );
}