You are here

function media_query_media_browser_alter in D7 Media 7.2

Same name and namespace in other branches
  1. 7.4 media.media.inc \media_query_media_browser_alter()
  2. 7.3 media.media.inc \media_query_media_browser_alter()

Implements hook_query_TAG_alter().

@todo: Potentially move this into media.module in a future version of Media.

File

./media.media.inc, line 47
Media module integration for the Media module.

Code

function media_query_media_browser_alter($query) {

  // Ensure that the query is against the file_managed table.
  $tables = $query
    ->getTables();
  if (empty($tables['file_managed'])) {
    throw new Exception(t('Media browser being queried without the file_managed table.'));
  }
  $alias = $tables['file_managed']['alias'];

  // How do we validate these?  I don't know.
  // I think PDO should protect them, but I'm not 100% certain.
  $params = media_get_browser_params();

  // Gather any file restrictions.
  $types = !empty($params['types']) ? $params['types'] : array();
  $schemes = !empty($params['schemes']) ? $params['schemes'] : array();
  $extensions = !empty($params['file_extensions']) ? explode(' ', $params['file_extensions']) : array();
  $or = db_or();

  // Filter out files with restricted types.
  if (!empty($types)) {
    $query
      ->condition($alias . '.type', $types, 'IN');
  }

  // Filter out files with restricted schemes.
  if (!empty($schemes)) {
    $local_or = db_or();
    $local_and = db_and();

    // Gather all of the local stream wrappers.
    $local_stream_wrappers = media_get_local_stream_wrappers();
    foreach ($schemes as $scheme) {

      // Only local files have extensions.
      // Filter out files with restricted extensions.
      if (!empty($extensions) && isset($local_stream_wrappers[$scheme])) {
        $mimetypes = array();
        foreach ($extensions as $extension) {
          $mimetype = media_get_extension_mimetype($extension);
          if ($mimetype) {
            $mimetypes[] = $mimetype;
          }
        }
        $local_and
          ->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
        if (count($mimetypes)) {
          $local_and
            ->condition($alias . '.filemime', $mimetypes, 'IN');
        }
        $local_or
          ->condition($local_and);
        $local_and = db_and();
      }
      else {
        $local_or
          ->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
      }
    }
    $or
      ->condition($local_or);
  }
  if ($or
    ->count()) {
    $query
      ->condition($or);
  }
}