You are here

function patterns_get_patterns in Patterns 5

Same name and namespace in other branches
  1. 6.2 patterns.module \patterns_get_patterns()
  2. 6 patterns.module \patterns_get_patterns()
2 calls to patterns_get_patterns()
patterns_import_submit in ./patterns.module
patterns_list in ./patterns.module

File

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

Code

function patterns_get_patterns($reset = true) {
  patterns_load();
  if ($reset || !variable_get('patterns_loaded', false)) {

    // Get a listing of enabled patterns
    $enabled = array();
    $result = db_query('SELECT file FROM {patterns} WHERE status = 1');
    while ($pattern = db_fetch_object($result)) {
      $enabled[] = $result->file;
    }
    $path = file_create_path(variable_get('patterns_save_xml', 'patterns'));
    $priority = array();

    // Get uploaded patterns first
    if (file_check_directory($path)) {

      // Check if .htaccess file exists in path, if not insert it
      _patterns_check_file_dir();
      foreach (file_scan_directory($path, '.\\.xml') as $file) {

        // Don't save enabled pattern backups
        if (strpos($file->filename, $path . '/enabled/') === 0) {
          continue;
        }

        // Set priority so these patterns won't get over-written
        $priority[] = $file->name;

        // Can't update existing patterns that are enabled
        if (in_array($file->filename, $enabled)) {
          continue;
        }

        // Load and save pattern
        if ($pattern = patterns_load_xml($file->filename)) {
          patterns_save_pattern($pattern, $file->filename);
        }
      }
    }

    // Get per-site patterns next
    foreach (file_scan_directory(conf_path() . '/patterns', '.\\.xml') as $file) {

      // Can't update existing patterns that are enabled
      if (in_array($file->filename, $enabled) || in_array($file->name, $priority)) {
        continue;
      }
      $priority[] = $file->name;

      // Load and save pattern
      if ($pattern = patterns_load_xml($file->filename)) {
        patterns_save_pattern($pattern, $file->filename);
      }
    }

    // Get profile patterns next
    global $profile;
    foreach (file_scan_directory('profiles/' . $profile . '/patterns', '.\\.xml') as $file) {

      // Can't update existing patterns that are enabled
      if (in_array($file->filename, $enabled) || in_array($file->name, $priority)) {
        continue;
      }
      $priority[] = $file->name;

      // Load and save pattern
      if ($pattern = patterns_load_xml($file->filename)) {
        patterns_save_pattern($pattern, $file->filename);
      }
    }

    // Last get the default patterns
    foreach (file_scan_directory(drupal_get_path('module', 'patterns') . '/patterns', '.\\.xml') as $file) {

      // Can't update existing patterns that are enabled
      if (in_array($file->filename, $enabled) || in_array($file->name, $priority)) {
        continue;
      }
      $pattern = patterns_load_xml($file->filename);

      // Load and save pattern
      if ($pattern = patterns_load_xml($file->filename)) {
        patterns_save_pattern($pattern, $file->filename);
      }
    }
    variable_set('patterns_loaded', time());
  }
  $result = db_query('SELECT * FROM {patterns}');
  while ($pattern = db_fetch_object($result)) {
    $patterns[$pattern->pid] = $pattern;
    $data = unserialize($patterns[$pattern->pid]->pattern);
    $patterns[$pattern->pid]->pattern = $data;
    foreach ($data as $key => $section) {
      if ($data[$key]['tag'] == 'info') {
        $patterns[$pattern->pid]->info = array();
        array_shift($section);
        foreach ($section as $property) {
          $patterns[$pattern->pid]->info[$property['tag']] = $property['value'];
        }
      }
    }
  }
  return $patterns;
}