media.media.inc 3.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

/**
 * @file
 * Media module integration for the Media module.
 */

/**
 * Implements hook_media_browser_plugin_info().
 */
function media_media_browser_plugin_info() {
  $info['upload'] = array(
    'title' => t('Upload'),
    'weight' => -10,
    'class' => 'MediaBrowserUpload',
  );

  // Add a plugin for each View display using the 'media_browser' display type.
19
  $view_weight = 10;
20 21 22 23 24 25 26 27 28
  foreach (views_get_enabled_views() as $view) {
    foreach ($view->display as $display) {
      if ($display->display_plugin == 'media_browser') {
        $title = $display->display_title;
        if (!empty($display->display_options['title'])) {
          $title = $display->display_options['title'];
        }
        $info["{$view->name}--{$display->id}"] = array(
          'title' => $title,
29 30
          // @TODO make this configurable.
          'weight' => $view_weight++,
31 32 33 34 35 36 37 38 39 40
          'class' => 'MediaBrowserView',
          'view_name' => $view->name,
          'view_display_id' => $display->id,
        );
      }
    }
  }

  return $info;
}
41 42

/**
43
 * Implements hook_query_TAG_alter().
44 45
 *
 * @todo: Potentially move this into media.module in a future version of Media.
46 47 48 49
 */
function media_query_media_browser_alter($query) {
  // Ensure that the query is against the file_managed table.
  $tables = $query->getTables();
50

51 52 53
  if (empty($tables['file_managed'])) {
    throw new Exception(t('Media browser being queried without the file_managed table.'));
  }
54

55 56 57 58
  $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.
59
  $params = media_get_browser_params();
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  // 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();
86
        foreach ($extensions as $extension) {
87
          $mimetype = media_get_extension_mimetype($extension);
88 89 90
          if ($mimetype) {
            $mimetypes[] = $mimetype;
          }
91
        }
92
        $local_and->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
93 94 95
        if (count($mimetypes)) {
          $local_and->condition($alias . '.filemime', $mimetypes, 'IN');
        }
96 97
        $local_or->condition($local_and);
        $local_and = db_and();
98
      }
99 100
      else {
        $local_or->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
101
      }
102
    }
103

104
    $or->condition($local_or);
105 106
  }

107 108
  if ($or->count()) {
    $query->condition($or);
109 110
  }
}
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/**
 * Implements hook_form_FORM_ID_alter().
 */
function media_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  $view = $form_state['view'];
  $display = $form_state['display'];

  if ($view->name == 'media_default' && $display->id == 'media_browser_1') {
    $params = media_get_browser_params();

    // Remove any unsupported types from the 'Type' filter.
    if (!empty($form['type']) && !empty($params['types'])) {
      foreach (array_keys($form['type']['#options']) as $type) {
        if ($type != 'All' && !in_array($type, $params['types'])) {
          unset($form['type']['#options'][$type]);
        }
      }
    }
  }
}