You are here

function _context_addassets_scandir in Context Add Assets 6

Same name and namespace in other branches
  1. 7 context_addassets.module \_context_addassets_scandir()

Scan drupal location for a type of file.

Parameters

string $filetype: extension of the file your looking for. Note: No leading period, for filetype use "js" instead of ".js"

@param string $where the location function will search. Currently Supports: Modules, Themes (Active), and Abitrary File Paths

@return Array An array indexed by file paths containing strings describing each path "Theme Key - File Name"

1 call to _context_addassets_scandir()
context_reaction_addassets_base::_context_addassets_search in plugins/context_reaction_addassets_base.inc
* Scan active themes for js files. * *

File

./context_addassets.module, line 265

Code

function _context_addassets_scandir($filetype = NULL, $where = 'themes') {

  // If $filetype has leading "." - remove it.
  if ($filetype[0] == '.') {
    $filetype = drupal_substr(1, drupal_strlen($filetype));
  }

  // Future setting to include all themes
  $include_all = FALSE;
  switch ($where) {

    // If want to find assets in modules.
    case 'modules':

      // Grab selected modules
      $modules = variable_get('context_addassets_index_modules', array());
      foreach ($modules as $path) {
        if (!$path) {
          continue;
        }
        $dir = realpath('.') . base_path() . $path;
        $mask = ".+\\." . $filetype;
        $files_raw[$path] = file_scan_directory($dir, $mask);
      }
      break;

    // If want to find assets in file paths.
    case 'paths':

      // Initialize path array index at zero.
      $path_index = 0;

      // Grab every set file path
      while ($path = variable_get('context_addassets_index_path' . $path_index, NULL)) {
        $dir = realpath('.') . base_path() . $path;
        $mask = ".+\\." . $filetype;
        $files_raw[$path] = file_scan_directory($dir, $mask);
        $path_index += 1;
      }
      break;

    // If you want to find assets in themes
    case 'themes':
    default:

      // Setup vars to avoid foreach fails.
      $selected_themes = array();
      $files_raw = array();

      // We'll grab active themes
      $themes = list_themes();
      foreach ($themes as $item) {

        // Only lists the theme if the theme is enabled.
        // Drupal's list_themes() function returns an array of objects, so we extract an array from each of the objects.
        $list = get_object_vars($item);

        // Only list the theme if it is enabled.
        if ($include_all == FALSE and $list['status'] or $include_all == TRUE) {
          $list = get_object_vars($item);
          $path = explode('/', $list['filename']);
          unset($path[count($path) - 1]);
          $path = implode('/', $path);
          $selected_themes[] = array(
            'name' => $list['info']['name'],
            'path' => $path,
          );
        }
      }

      // Scan $selected_themes for css files.
      foreach ($selected_themes as $theme) {
        $dir = realpath('.') . base_path() . $theme['path'];
        $name = $theme['name'];
        $mask = ".+\\." . $filetype;
        $files_raw[$name] = file_scan_directory($dir, $mask);
      }
      break;
  }

  // switch $where
  // No files found.
  if (!is_array($files_raw)) {
    return;
  }

  // Remove the full path.
  foreach ($files_raw as $key => $value) {
    foreach ($value as $value_key => $file) {
      $theme_path = drupal_get_path('theme', $key);
      $dir = realpath('.') . base_path() . ($theme_path ? $theme_path . "/" : $theme_path);
      $file_name = str_replace($dir, '', $file->filename);
      $file_path = str_replace(realpath('.') . '/', '', $file->filename);
      if (!is_array($key)) {
        $key = explode('/', $key);
        $key = $key[count($key) - 1];
      }
      $file_name = explode("/{$key}/", $file_name);
      $file_name = $file_name[1] ? $file_name[1] : $file_name[0];
      $file_files[$file_path] = $key . ' -- ' . $file_name;
    }
  }
  return $file_files;
}