You are here

function patterns_get_patterns in Patterns 6

Same name and namespace in other branches
  1. 5 patterns.module \patterns_get_patterns()
  2. 6.2 patterns.module \patterns_get_patterns()
4 calls to patterns_get_patterns()
patterns_feed in ./patterns.module
Prints XML Feed of published (public) patterns
patterns_import_submit in ./patterns.module
patterns_list in ./patterns.module
patterns_settings in ./patterns.module
Display the pattern settings form

File

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

Code

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

    // Get a listing of enabled patterns
    $enabled = array();

    // Below SQL query commented out for the time being because it's preventing
    // pattern code stored in the patterns table to be updated/refreshed after
    // pattern file has been modified.
    // @todo: After it's confirmed that this is not causing any side effects this
    // function has to be cleaned from all the references to $enabled array.
    //    $result = db_query('SELECT file FROM {patterns} WHERE status = 1');
    //
    //    while ($pattern = db_fetch_object($result)) {
    //      $enabled[] = $pattern->file;
    //    }
    $priority = array();
    $errors = array();

    // Get list of directories to scan for patterns
    $patterns_paths = patterns_paths();

    // get valid file extensions
    $mask = '.\\.(' . implode('|', patterns_file_types()) . ')$';

    // prepare list of files/folders to be excluded
    // 'enabled' - Don't save enabled pattern backups
    $no_mask = array(
      '.',
      '..',
      'CVS',
      '.svn',
      'enabled',
    );
    foreach ($patterns_paths as $path) {
      foreach (file_scan_directory($path, $mask, $no_mask) 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;

        // choose appropriate function based on the file extension
        $func = 'patterns_load_' . substr($file->basename, strlen($file->name) + 1);

        // Load and save pattern
        if ($pattern = $func($file->filename)) {
          patterns_save_pattern($pattern, $file->filename, $file->name);
        }
        else {
          $errors[] = $file->filename;
        }
      }
    }
    variable_set('patterns_loaded', time());
  }
  $result = db_query('SELECT * FROM {patterns}');
  $messages = array();
  while ($pattern = db_fetch_object($result)) {

    // skip pattern if its file is missing
    if (!is_file($pattern->file)) {
      continue;
    }

    // skip pattern if loading failed and report that to the user
    if (in_array($pattern->file, $errors)) {
      $messages[] = t("Pattern couldn't be loaded from the file '%file'", array(
        '%file' => $pattern->file,
      ));
      continue;
    }
    $patterns[$pattern->pid] = $pattern;
    $data = unserialize($pattern->pattern);
    $patterns[$pattern->pid]->pattern = $data;
    $patterns[$pattern->pid]->info = $data['info'];
  }
  if (!empty($messages)) {
    drupal_set_message(implode('<br>', $messages) . '<br>' . t('Make sure that above file(s) are readable and contain valid data.'), 'error');
  }
  return $patterns;
}