media.media.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @file
  4. * Media module integration for the Media module.
  5. */
  6. /**
  7. * Implements hook_media_browser_plugin_info().
  8. */
  9. function media_media_browser_plugin_info() {
  10. $info['upload'] = array(
  11. 'title' => t('Upload'),
  12. 'weight' => -10,
  13. 'class' => 'MediaBrowserUpload',
  14. );
  15. // Add a plugin for each View display using the 'media_browser' display type.
  16. $view_weight = 10;
  17. foreach (views_get_enabled_views() as $view) {
  18. foreach ($view->display as $display) {
  19. if ($display->display_plugin == 'media_browser') {
  20. $title = $display->display_title;
  21. if (!empty($display->display_options['title'])) {
  22. $title = $display->display_options['title'];
  23. }
  24. $info["{$view->name}--{$display->id}"] = array(
  25. 'title' => $title,
  26. // @TODO make this configurable.
  27. 'weight' => $view_weight++,
  28. 'class' => 'MediaBrowserView',
  29. 'view_name' => $view->name,
  30. 'view_display_id' => $display->id,
  31. );
  32. }
  33. }
  34. }
  35. return $info;
  36. }
  37. /**
  38. * Implements hook_query_TAG_alter().
  39. *
  40. * @todo: Potentially move this into media.module in a future version of Media.
  41. */
  42. function media_query_media_browser_alter($query) {
  43. // Ensure that the query is against the file_managed table.
  44. $tables = $query->getTables();
  45. if (empty($tables['file_managed'])) {
  46. throw new Exception(t('Media browser being queried without the file_managed table.'));
  47. }
  48. $alias = $tables['file_managed']['alias'];
  49. // How do we validate these? I don't know.
  50. // I think PDO should protect them, but I'm not 100% certain.
  51. $params = media_get_browser_params();
  52. // Gather any file restrictions.
  53. $types = !empty($params['types']) ? $params['types'] : array();
  54. $schemes = !empty($params['schemes']) ? $params['schemes'] : array();
  55. $extensions = !empty($params['file_extensions']) ? explode(' ', $params['file_extensions']) : array();
  56. $or = db_or();
  57. // Filter out files with restricted types.
  58. if (!empty($types)) {
  59. $query->condition($alias . '.type', $types, 'IN');
  60. }
  61. // Filter out files with restricted schemes.
  62. if (!empty($schemes)) {
  63. $local_or = db_or();
  64. $local_and = db_and();
  65. // Gather all of the local stream wrappers.
  66. $local_stream_wrappers = media_get_local_stream_wrappers();
  67. foreach ($schemes as $scheme) {
  68. // Only local files have extensions.
  69. // Filter out files with restricted extensions.
  70. if (!empty($extensions) && isset($local_stream_wrappers[$scheme])) {
  71. $mimetypes = array();
  72. foreach ($extensions as $extension) {
  73. $mimetype = media_get_extension_mimetype($extension);
  74. if ($mimetype) {
  75. $mimetypes[] = $mimetype;
  76. }
  77. }
  78. $local_and->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
  79. if (count($mimetypes)) {
  80. $local_and->condition($alias . '.filemime', $mimetypes, 'IN');
  81. }
  82. $local_or->condition($local_and);
  83. $local_and = db_and();
  84. }
  85. else {
  86. $local_or->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
  87. }
  88. }
  89. $or->condition($local_or);
  90. }
  91. if ($or->count()) {
  92. $query->condition($or);
  93. }
  94. }
  95. /**
  96. * Implements hook_form_FORM_ID_alter().
  97. */
  98. function media_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
  99. $view = $form_state['view'];
  100. $display = $form_state['display'];
  101. if ($view->name == 'media_default' && $display->id == 'media_browser_1') {
  102. $params = media_get_browser_params();
  103. // Remove any unsupported types from the 'Type' filter.
  104. if (!empty($form['type']) && !empty($params['types'])) {
  105. foreach (array_keys($form['type']['#options']) as $type) {
  106. if ($type != 'All' && !in_array($type, $params['types'])) {
  107. unset($form['type']['#options'][$type]);
  108. }
  109. }
  110. }
  111. }
  112. }