1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Summon plugins and render the media browser.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Media browser page callback.
|
10
|
*/
|
11
|
function media_browser($selected = NULL) {
|
12
|
$output = array();
|
13
|
$output['#attached']['library'][] = array('media', 'media_browser_page');
|
14
|
|
15
|
$params = media_set_browser_params();
|
16
|
|
17
|
// If one or more files have been selected, the browser interaction is now
|
18
|
// complete. Return empty page content to the dialog which now needs to close,
|
19
|
// but populate Drupal.settings with information about the selected files.
|
20
|
if (isset($params['fid'])) {
|
21
|
$fids = is_array($params['fid']) ? $params['fid'] : array($params['fid']);
|
22
|
if (!is_numeric($fids[0])) {
|
23
|
throw new Exception('Error selecting media, fid param is not an fid or an array of fids');
|
24
|
}
|
25
|
$files = file_load_multiple($fids);
|
26
|
foreach ($files as $file) {
|
27
|
media_browser_build_media_item($file);
|
28
|
}
|
29
|
$setting = array('media' => array('selectedMedia' => array_values($files)));
|
30
|
drupal_add_js($setting, 'setting');
|
31
|
return $output;
|
32
|
}
|
33
|
|
34
|
$plugins = media_get_browser_plugin_info();
|
35
|
|
36
|
// Allow parameters to provide a list of enabled or disabled media browser
|
37
|
// plugins.
|
38
|
if (!empty($params['enabledPlugins'])) {
|
39
|
$plugins = array_intersect_key($plugins, array_fill_keys($params['enabledPlugins'], 1));
|
40
|
}
|
41
|
elseif (!empty($params['disabledPlugins'])) {
|
42
|
$plugins = array_diff_key($plugins, array_fill_keys($params['disabledPlugins'], 1));
|
43
|
}
|
44
|
|
45
|
// Render plugins.
|
46
|
$plugin_output = array();
|
47
|
foreach ($plugins as $key => $plugin_info) {
|
48
|
// Support the old CTools style handler definition.
|
49
|
if (!isset($plugin_info['class']) && !empty($plugin_info['handler'])) {
|
50
|
if (is_string($plugin_info['handler'])) {
|
51
|
$plugin_info['class'] = $plugin_info['handler'];
|
52
|
}
|
53
|
elseif (isset($plugin_info['handler']['class'])) {
|
54
|
$plugin_info['class'] = $plugin_info['handler']['class'];
|
55
|
}
|
56
|
}
|
57
|
|
58
|
if (empty($plugin_info['class']) || !class_exists($plugin_info['class'])) {
|
59
|
continue;
|
60
|
}
|
61
|
|
62
|
$plugin = new $plugin_info['class']($plugin_info, $params);
|
63
|
if ($plugin->access()) {
|
64
|
$plugin_output[$key] = $plugin->view();
|
65
|
if (!empty($plugin_output[$key]) && is_array($plugin_output[$key])) {
|
66
|
$plugin_output[$key] += array(
|
67
|
'#title' => $plugin_info['title'],
|
68
|
'#weight' => isset($plugin_info['weight']) ? $plugin_info['weight'] : 0,
|
69
|
);
|
70
|
}
|
71
|
else {
|
72
|
unset($plugin_output[$key]);
|
73
|
continue;
|
74
|
}
|
75
|
}
|
76
|
else {
|
77
|
continue;
|
78
|
}
|
79
|
|
80
|
// We need to get a submit and cancel button on each tab. If the plugin
|
81
|
// is not returning a form element we need to add a submit button.
|
82
|
// This is a fairly broad assumption.
|
83
|
if (empty($plugin_output[$key]['#form']) && !empty($plugin_output[$key]['#markup'])) {
|
84
|
$fake_buttons = '<div class="form-actions form-wrapper">';
|
85
|
$fake_buttons .= l(t('Submit'), '', array(
|
86
|
'attributes' => array(
|
87
|
'class' => array('button', 'button-yes', 'fake-submit', $key),
|
88
|
),
|
89
|
));
|
90
|
$fake_buttons .= l(t('Cancel'), '', array(
|
91
|
'attributes' => array(
|
92
|
'class' => array('button', 'button-no', 'fake-cancel', $key),
|
93
|
),
|
94
|
));
|
95
|
$fake_buttons .= '</div>';
|
96
|
$plugin_output[$key]['#markup'] .= $fake_buttons;
|
97
|
}
|
98
|
|
99
|
// I'm not sure if it is ever the case that a plugin form will ever have
|
100
|
// the correct cancel button so we add it here. Put it inline with the
|
101
|
// current submit button. This is a fairly broad assumption.
|
102
|
if (!empty($plugin_output[$key]['form']['actions']) && !isset($plugin_output[$key]['form']['actions']['cancel'])) {
|
103
|
$plugin_output[$key]['form']['actions']['cancel'] = array(
|
104
|
'#type' => 'link',
|
105
|
'#title' => t('Cancel'),
|
106
|
'#href' => '',
|
107
|
'#attributes' => array(
|
108
|
'class' => array(
|
109
|
'button',
|
110
|
'button-no',
|
111
|
'fake-cancel',
|
112
|
$key,
|
113
|
),
|
114
|
),
|
115
|
'#weight' => 100,
|
116
|
);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
// Allow modules to change the tab names or whatever else they want to change
|
121
|
// before we render. Perhaps this should be an alter on the theming function
|
122
|
// that we should write to be making the tabs.
|
123
|
drupal_alter('media_browser_plugins', $plugin_output);
|
124
|
|
125
|
$tabs = array();
|
126
|
$settings = array('media' => array('browser' => array()));
|
127
|
|
128
|
foreach (element_children($plugin_output, TRUE) as $key) {
|
129
|
// Add any JavaScript settings from the browser tab.
|
130
|
if (!empty($plugin_output[$key]['#settings'])) {
|
131
|
$settings['media']['browser'][$key] = $plugin_output[$key]['#settings'];
|
132
|
}
|
133
|
|
134
|
// If this is a "ajax" style tab, add the href, otherwise an id. jQuery UI
|
135
|
// will use an href value to load content from that url
|
136
|
$tabid = 'media-tab-' . check_plain($key);
|
137
|
if (!empty($plugin_output[$key]['#callback'])) {
|
138
|
$href = $plugin_output[$key]['#callback'];
|
139
|
}
|
140
|
else {
|
141
|
$attributes = array(
|
142
|
'class' => array('media-browser-tab'),
|
143
|
'id' => $tabid,
|
144
|
'data-tabid' => $key,
|
145
|
);
|
146
|
// Create a div for each tab's content.
|
147
|
$plugin_output[$key] += array(
|
148
|
'#prefix' => '<div '. drupal_attributes($attributes) . ">\n",
|
149
|
'#suffix' => "</div>\n",
|
150
|
);
|
151
|
}
|
152
|
|
153
|
$attributes = array(
|
154
|
'href' => '#' . $tabid,
|
155
|
'data-tabid' => $key,
|
156
|
'title' => $plugin_output[$key]['#title'],
|
157
|
);
|
158
|
$tabs[]['element'] = array(
|
159
|
'#markup' => '<li><a' . drupal_attributes($attributes) . '>' . check_plain($plugin_output[$key]['#title']) . "</a></li>\n",
|
160
|
);
|
161
|
}
|
162
|
|
163
|
drupal_add_js($settings, 'setting');
|
164
|
|
165
|
$output['title'] = array(
|
166
|
'#markup' => t('Select a file')
|
167
|
);
|
168
|
|
169
|
$output['tabset']['tabs'] = array(
|
170
|
'#theme' => 'menu_local_tasks',
|
171
|
'#attributes' => array('class' => array('tabs', 'primary')),
|
172
|
'#primary' => $tabs,
|
173
|
);
|
174
|
|
175
|
$output['tabset']['panes'] = $plugin_output;
|
176
|
|
177
|
return $output;
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* Provides a singleton of the params passed to the media browser.
|
182
|
*
|
183
|
* This is useful in situations like form alters because callers can pass
|
184
|
* id="wysiywg_form" or whatever they want, and a form alter could pick this up.
|
185
|
* We may want to change the hook_media_browser_plugin_view() implementations to
|
186
|
* use this function instead of being passed params for consistency.
|
187
|
*
|
188
|
* It also offers a chance for some meddler to meddle with them.
|
189
|
*
|
190
|
* @see media_browser()
|
191
|
*/
|
192
|
function media_set_browser_params() {
|
193
|
$params = &drupal_static(__FUNCTION__, array());
|
194
|
|
195
|
if (empty($params)) {
|
196
|
// Build out browser settings. Permissions- and security-related behaviors
|
197
|
// should not rely on these parameters, since they come from the HTTP query.
|
198
|
// @TODO make sure we treat parameters as user input.
|
199
|
$params = drupal_get_query_parameters() + array(
|
200
|
'types' => array(),
|
201
|
'multiselect' => FALSE,
|
202
|
);
|
203
|
|
204
|
// Transform text 'true' and 'false' to actual booleans.
|
205
|
foreach ($params as $k => $v) {
|
206
|
if ($v === 'true') {
|
207
|
$params[$k] = TRUE;
|
208
|
}
|
209
|
elseif ($v === 'false') {
|
210
|
$params[$k] = FALSE;
|
211
|
}
|
212
|
}
|
213
|
|
214
|
array_walk_recursive($params, 'media_recursive_check_plain');
|
215
|
|
216
|
// Allow modules to alter the parameters.
|
217
|
drupal_alter('media_browser_params', $params);
|
218
|
}
|
219
|
|
220
|
return $params;
|
221
|
}
|
222
|
|
223
|
|
224
|
/**
|
225
|
* For sanity in grammar.
|
226
|
*
|
227
|
* @see media_set_browser_params()
|
228
|
*/
|
229
|
function media_get_browser_params() {
|
230
|
return media_set_browser_params();
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* Attaches media browser javascript to an element.
|
235
|
*
|
236
|
* @param array $element
|
237
|
* The element array to attach to.
|
238
|
*/
|
239
|
function media_attach_browser_js(&$element) {
|
240
|
$javascript = media_browser_js();
|
241
|
foreach ($javascript as $key => $definitions) {
|
242
|
foreach ($definitions as $definition) {
|
243
|
$element['#attached'][$key][] = $definition;
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Helper function to define browser javascript.
|
250
|
*/
|
251
|
function media_browser_js() {
|
252
|
$settings = array(
|
253
|
'browserUrl' => url('media/browser', array(
|
254
|
'query' => array('render' => 'media-popup'),
|
255
|
)
|
256
|
),
|
257
|
'styleSelectorUrl' => url('media/-media_id-/format-form', array(
|
258
|
'query' => array('render' => 'media-popup'),
|
259
|
)
|
260
|
),
|
261
|
);
|
262
|
|
263
|
$js = array(
|
264
|
'library' => array(
|
265
|
array('media', 'media_browser'),
|
266
|
),
|
267
|
'js' => array(
|
268
|
array(
|
269
|
'data' => array('media' => $settings),
|
270
|
'type' => 'setting',
|
271
|
),
|
272
|
),
|
273
|
);
|
274
|
return $js;
|
275
|
}
|
276
|
|
277
|
/**
|
278
|
* Menu callback for testing the media browser.
|
279
|
*/
|
280
|
function media_browser_testbed($form) {
|
281
|
media_attach_browser_js($form);
|
282
|
|
283
|
$form['test_element'] = array(
|
284
|
'#type' => 'media',
|
285
|
'#media_options' => array(
|
286
|
'global' => array(
|
287
|
'types' => array('video', 'audio'),
|
288
|
),
|
289
|
),
|
290
|
);
|
291
|
|
292
|
$launcher = '<a href="#" id="launcher"> Launch Media Browser</a>';
|
293
|
|
294
|
$form['options'] = array(
|
295
|
'#type' => 'textarea',
|
296
|
'#title' => 'Options (JSON)',
|
297
|
'#rows' => 10,
|
298
|
);
|
299
|
|
300
|
$form['launcher'] = array(
|
301
|
'#markup' => $launcher,
|
302
|
);
|
303
|
|
304
|
$form['result'] = array(
|
305
|
'#type' => 'textarea',
|
306
|
'#title' => 'Result',
|
307
|
);
|
308
|
|
309
|
$js = <<<EOF
|
310
|
Drupal.behaviors.mediaTest = {
|
311
|
attach: function(context, settings) {
|
312
|
var delim = "---";
|
313
|
var recentOptions = [];
|
314
|
var recentOptionsCookie = jQuery.cookie("recentOptions");
|
315
|
if (recentOptionsCookie) {
|
316
|
recentOptions = recentOptionsCookie.split("---");
|
317
|
}
|
318
|
|
319
|
var recentSelectBox = jQuery('<select id="recent_options" style="width:100%"></select>').change(function() { jQuery('#edit-options').val(jQuery(this).val())});
|
320
|
|
321
|
jQuery('.form-item-options').append('<label for="recent_options">Recent</a>');
|
322
|
jQuery('.form-item-options').append(recentSelectBox);
|
323
|
jQuery('.form-item-options').append(jQuery('<a href="#">Reset</a>').click(function() {alert('reset'); jQuery.cookie("recentOptions", null); window.location.reload(); }));
|
324
|
|
325
|
jQuery.each(recentOptions, function (idx, val) {
|
326
|
recentSelectBox.append(jQuery('<option></option>').val(val).html(val));
|
327
|
});
|
328
|
|
329
|
|
330
|
jQuery('#launcher').click(function () {
|
331
|
jQuery('#edit-result').val('');
|
332
|
var options = {};
|
333
|
var optionsTxt = jQuery('#edit-options').val();
|
334
|
if (optionsTxt) {
|
335
|
// Store it in the recent box
|
336
|
recentOptionsCookie += "---" + optionsTxt
|
337
|
jQuery.cookie("recentOptions", recentOptionsCookie, { expires: 7 });
|
338
|
recentSelectBox.append(jQuery('<option></option>').val(optionsTxt).html(optionsTxt));
|
339
|
options = eval('(' + optionsTxt + ')');
|
340
|
}
|
341
|
Drupal.media.popups.mediaBrowser(Drupal.behaviors.mediaTest.mediaSelected, options);
|
342
|
return false;
|
343
|
});
|
344
|
},
|
345
|
|
346
|
mediaSelected: function(selectedMedia) {
|
347
|
var result = JSON.stringify(selectedMedia);
|
348
|
jQuery('#edit-result').val(result);
|
349
|
}
|
350
|
}
|
351
|
|
352
|
EOF;
|
353
|
|
354
|
drupal_add_js($js, array('type' => 'inline'));
|
355
|
return $form;
|
356
|
}
|
357
|
|
358
|
/**
|
359
|
* Adds properties to the file.
|
360
|
*
|
361
|
* Additional properties on this file are needed by the media browser JS code.
|
362
|
*/
|
363
|
function media_browser_build_media_item($file) {
|
364
|
$preview = media_get_thumbnail_preview($file);
|
365
|
$file->preview = drupal_render($preview);
|
366
|
$file->url = file_create_url($file->uri);
|
367
|
}
|