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