Commit 9bce5cce authored by Dave Reid's avatar Dave Reid

Issue #1293908, 1293660 by Dave Reid, becw, aaron, brantwynn, askibinsky,...

Issue #1293908, 1293660 by Dave Reid, becw, aaron, brantwynn, askibinsky, Devin Carlson: Ensure field configuration is applied to Media library views and removed the deprecated media browser library.
parent cee54f03
......@@ -81,14 +81,6 @@
position: relative;
}
/* Library form */
#media-tab-library #scrollbox {
height: 300px;
overflow-x: hidden;
overflow-y: auto;
}
/* Format form */
#media-format-form {
......
<?php
/**
* @file
* Definition of MediaBrowserLibrary.
*/
/**
* Media browser plugin for displaying the media browser library.
*
* @deprecated
*/
class MediaBrowserLibrary extends MediaBrowserPlugin {
/**
* Implements MediaBrowserPluginInterface::access().
*/
public function access($account = NULL) {
// @todo Replace with file_access().
return media_access('view', $account);
}
/**
* Implements MediaBrowserPlugin::view().
*/
public function view() {
$path = drupal_get_path('module', 'media');
$build['#attached']['js'][] = $path . '/js/plugins/media.library.js';
$build['#settings']['viewMode'] = 'thumbnails';
$build['#settings']['getMediaUrl'] = url('media/browser/list');
$build['#settings'] += $this->params;
$build['#markup'] = '<div id="container"><div id="scrollbox"><ul id="media-browser-library-list" class="media-list-thumbnails"></ul><div id="status"></div></div></div>';
return $build;
}
}
......@@ -65,22 +65,16 @@ abstract class MediaBrowserPlugin implements MediaBrowserPluginInterface {
* plugin's hook_media_browser_plugin_info() then it will be used,
* otherwise a default of 0 will be used.
* - form: If the plugin is to display a native Drupal form, then the output
* of drupal_get_form should be returned into the 'form' render key.
* of drupal_get_form should be returned into the 'form' render key. If a
* form's callback isn't normally loaded, module_load_include() should be
* used to ensure that the form can be displayed.
*
* Example:
* Example usage:
* @code
* return array(
* '#attached' => array(
* 'js' => array(
* drupal_get_path('module', 'media') . '/js/plugins/media.library.js',
* ),
* ),
* '#settings' => array(
* 'viewMode' => 'thumbnails',
* 'getMediaUrl' => url('media/browser/list'),
* ),
* '#markup' => '<div>Library goes here</div>',
* );
* module_load_include('inc', 'mymodule', 'mymodule.pages');
* $build['#attached']['js'][] = drupal_get_path('module', 'mymodule') . '/js/mymodule.media.browser.js';
* $build['form'] = drupal_get_form('mymodule_media_form');
* return $build;
* @endcode
*
* @return
......
......@@ -196,98 +196,6 @@ function media_get_browser_params() {
return media_set_browser_params();
}
/**
* AJAX Callback function to return a list of media files
*
* @TODO this should probably live with the 'library' media browser plugin.
*/
function media_browser_list() {
$params = drupal_get_query_parameters();
// How do we validate these? I don't know.
// I think PDO should protect them, but I'm not 100% certain.
array_walk_recursive($params, '_media_recursive_check_plain');
$remote_types = !empty($params['types']) ? $params['types'] : NULL;
$url_include_patterns = !empty($params['url_include_patterns']) ? $params['url_include_patterns'] : NULL;
$url_exclude_patterns = !empty($params['url_exclude_patterns']) ? $params['url_exclude_patterns'] : NULL;
$allowed_schemes = !empty($params['schemes']) ? array_filter($params['schemes']) : array();
$start = isset($params['start']) ? $params['start'] : 0;
$limit = isset($params['limit']) ? $params['limit'] : media_variable_get('browser_pager_limit');
$query = db_select('file_managed', 'f');
$query->fields('f', array('fid'));
$query->range($start, $limit);
$query->orderBy('f.timestamp', 'DESC');
// Add conditions based on remote file type *or* local allowed extensions.
$or_condition = db_or();
// Include local files with the allowed extensions.
if (!empty($params['file_extensions'])) {
$extensions = array_filter(explode(' ', $params['file_extensions']));
$local_wrappers = array_intersect_key(media_get_local_stream_wrappers(), $allowed_schemes);
if (!empty($local_wrappers) && !empty($extensions)) {
$local_condition = db_or();
foreach (array_keys($local_wrappers) as $scheme) {
foreach ($extensions as $extension) {
$local_condition->condition('f.uri', db_like($scheme . '://') . '%' . db_like('.' . $extension), 'LIKE');
}
}
$or_condition->condition($local_condition);
}
}
// Include remote files with the allowed file types.
if (!empty($remote_types)) {
$remote_wrappers = array_intersect_key(media_get_remote_stream_wrappers(), $allowed_schemes);
if (!empty($remote_wrappers)) {
$remote_condition = db_and();
$wrapper_condition = db_or();
foreach (array_keys($remote_wrappers) as $scheme) {
$wrapper_condition->condition('f.uri', db_like($scheme . '://') . '%', 'LIKE');
}
$remote_condition->condition($wrapper_condition);
$remote_condition->condition('f.type', $remote_types, 'IN');
$or_condition->condition($remote_condition);
}
}
if ($or_condition->count()) {
$query->condition($or_condition);
}
if ($url_include_patterns) {
$query->condition('f.uri', '%' . db_like($url_include_patterns) . '%', 'LIKE');
// Insert stream related restrictions here.
}
if ($url_exclude_patterns) {
$query->condition('f.uri', '%' . db_like($url_exclude_patterns) . '%', 'NOT LIKE');
}
// @todo Implement granular editorial access: http://drupal.org/node/696970.
// In the meantime, protect information about private files from being
// discovered by unprivileged users. See also media_view_page().
if (!user_access('administer files')) {
$query->condition('f.uri', db_like('private://') . '%', 'NOT LIKE');
}
$query->condition('f.status', FILE_STATUS_PERMANENT);
foreach (array_keys(media_get_hidden_stream_wrappers()) as $name) {
$query->condition('f.uri', db_like($name . '://') . '%', 'NOT LIKE');
}
$fids = $query->execute()->fetchCol();
$files = file_load_multiple($fids);
foreach ($files as $file) {
media_browser_build_media_item($file);
}
drupal_json_output(array('media' => array_values($files)));
exit();
}
/**
* Silly function to recursively run check_plain on an array.
*
......
......@@ -137,10 +137,6 @@ function media_variable_default($name = NULL) {
// Used in media.xml.inc: how long to cache retrieved remote data.
'xml_cache_expire' => 3600,
// Browser defaults in media.browser.inc.
'browser_viewtype_default' => 'thumbnails',
'browser_pager_limit' => 40,
'import_batch_size' => 20,
'fromurl_supported_schemes' => array('http', 'https', 'ftp', 'smb', 'ftps'),
'type_icon_directory' => drupal_get_path('module', 'media') . '/images/types',
......@@ -152,6 +148,8 @@ function media_variable_default($name = NULL) {
'field_select_media_text' => NULL,
'field_remove_media_text' => NULL,
'browser_library_empty_message' => NULL,
'browser_pager_limit' => NULL,
'browser_viewtype_default' => NULL,
);
}
......
(function ($) {
namespace('Drupal.media.browser');
Drupal.behaviors.mediaLibrary = {
attach: function (context, settings) {
var library = new Drupal.media.browser.library(Drupal.settings.media.browser.library);
$('#media-browser-tabset').bind('tabsshow', function (event, ui) {
if (ui.tab.hash === '#media-tab-library') {
// Grab the parameters from the Drupal.settings object
var params = {};
for (var parameter in Drupal.settings.media.browser.library) {
params[parameter] = Drupal.settings.media.browser.library[parameter];
}
library.start($(ui.panel), params);
$('#scrollbox').bind('scroll', library, library.scrollUpdater);
}
});
}
};
Drupal.media.browser.library = function (settings) {
this.settings = Drupal.media.browser.library.getDefaults();
$.extend(this.settings, settings);
this.done = false; // Keeps track of if the last request for media returned 0 results.
this.cursor = 0; // keeps track of what the last requested media object was.
this.mediaFiles = []; // An array of loaded media files from the server.
this.selectedMediaFiles = [];
};
Drupal.media.browser.library.getDefaults = function () {
return {
emtpyMessage: Drupal.t('There is nothing in your media library. Select the Upload tab above to add a file.'),
limit: 15
};
};
Drupal.media.browser.library.prototype.start = function (renderElement, params) {
this.renderElement = renderElement;
this.params = params;
// Change the behavior dependent on multiselect
if (params.multiselect) {
this.clickFunction = this.multiSelect;
} else {
this.clickFunction = this.singleSelect;
}
this.loadMedia();
};
/**
* Appends more media onto the list
*/
Drupal.media.browser.library.prototype.loadMedia = function () {
var that = this;
$('#status').text(Drupal.t('Loading...')).show();
$.extend(this.params, {start: this.cursor, limit: this.settings.limit});
var gotMedia = function (data, status) {
$('#status').text('').hide();
if (data.media.length < that.params.limit) {
// We remove the scroll event listener, nothing more to load after this.
$('#scrollbox').unbind('scroll');
}
that.mediaFiles = that.mediaFiles.concat(data.media);
that.render(that.renderElement);
// Remove the flag that prevents loading of more media
that.loading = false;
};
var errorCallback = function () {
alert(Drupal.t('Error getting media.'));
};
$.ajax({
url: this.settings.getMediaUrl,
type: 'GET',
dataType: 'json',
data: this.params,
error: errorCallback,
success: gotMedia
});
};
Drupal.media.browser.library.prototype.scrollUpdater = function (e){
if (!e.data.loading) {
var scrollbox = $('#scrollbox');
var scrolltop = scrollbox.attr('scrollTop');
var scrollheight = scrollbox.attr('scrollHeight');
var windowheight = scrollbox.attr('clientHeight');
var scrolloffset = 20;
if(scrolltop >= (scrollheight - (windowheight + scrolloffset))) {
// Set a flag so we don't make multiple concurrent AJAX calls
e.data.loading = true;
// Fetch new items
e.data.loadMedia();
}
}
};
/**
* Fetches the next media object and increments the cursor.
*/
Drupal.media.browser.library.prototype.getNextMedia = function () {
if (this.cursor >= this.mediaFiles.length) {
return false;
}
var ret = this.mediaFiles[this.cursor];
this.cursor += 1;
return ret;
};
Drupal.media.browser.library.prototype.render = function (renderElement) {
if (this.mediaFiles.length < 1) {
$('<div id="media-empty-message" class="media-empty-message"></div>').appendTo(renderElement)
.html(this.emptyMessage);
return;
}
else {
var mediaList = $('#media-browser-library-list', renderElement);
// If the list doesn't exist, bail.
if (mediaList.length === 0) {
throw('Cannot continue, list element is missing');
}
}
while (this.cursor < this.mediaFiles.length) {
var mediaFile = this.getNextMedia();
var data = {};
data.obj = this;
data.file = mediaFile;
var listItem = $('<li></li>').appendTo(mediaList)
.attr('id', 'media-item-' + mediaFile.fid)
.html(mediaFile.preview)
.bind('click', data, this.clickFunction);
}
};
Drupal.media.browser.library.prototype.mediaSelected = function (media) {
Drupal.media.browser.selectMedia(media);
};
Drupal.media.browser.library.prototype.singleSelect = function (event) {
var lib = event.data.obj;
var file = event.data.file;
event.preventDefault();
event.stopPropagation();
$('.media-item').removeClass('selected');
$('.media-item', $(this)).addClass('selected');
lib.mediaSelected([event.data.file]);
return false;
}
Drupal.media.browser.library.prototype.multiSelect = function (event) {
var lib = event.data.obj
var file = event.data.file;
event.preventDefault();
event.stopPropagation();
// Turn off or on the selection of this item
$('.media-item', $(this)).toggleClass('selected');
// Add or remove the media file from the array
var index = $.inArray(file, lib.selectedMediaFiles);
if (index == -1) {
// Media file isn't selected, add it
lib.selectedMediaFiles.push(file);
} else {
// Media file has previously been selected, remove it
lib.selectedMediaFiles.splice(index, 1);
}
// Pass the array of selected media files to the invoker
lib.mediaSelected(lib.selectedMediaFiles);
return false;
}
}(jQuery));
......@@ -8,7 +8,6 @@ dependencies[] = views
files[] = includes/MediaReadOnlyStreamWrapper.inc
files[] = includes/MediaBrowserPluginInterface.inc
files[] = includes/MediaBrowserPlugin.inc
files[] = includes/MediaBrowserLibrary.inc
files[] = includes/MediaBrowserUpload.inc
files[] = includes/MediaBrowserView.inc
files[] = includes/media_views_plugin_display_media_browser.inc
......
......@@ -709,3 +709,10 @@ function media_update_7201() {
function media_update_7202() {
module_enable(array('views'));
}
/**
* Empty update function to trigger cache clear.
*/
function media_update_7203() {
// Do nothing.
}
......@@ -15,13 +15,8 @@ function media_media_browser_plugin_info() {
'class' => 'MediaBrowserUpload',
);
$info['library'] = array(
'title' => t('Library'),
'weight' => 10,
'class' => 'MediaBrowserLibrary',
);
// Add a plugin for each View display using the 'media_browser' display type.
$view_weight = 10;
foreach (views_get_enabled_views() as $view) {
foreach ($view->display as $display) {
if ($display->display_plugin == 'media_browser') {
......@@ -31,7 +26,7 @@ function media_media_browser_plugin_info() {
}
$info["{$view->name}--{$display->id}"] = array(
'title' => $title,
'weight' => 11, // @TODO make this configurable.
'weight' => $view_weight++, // @TODO make this configurable.
'class' => 'MediaBrowserView',
'view_name' => $view->name,
'view_display_id' => $display->id,
......@@ -42,3 +37,81 @@ function media_media_browser_plugin_info() {
return $info;
}
/**
* Implements hook_query_media_browser_alter().
*/
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'];
$params = drupal_get_query_parameters();
// How do we validate these? I don't know.
// I think PDO should protect them, but I'm not 100% certain.
array_walk_recursive($params, '_media_recursive_check_plain');
$remote_types = !empty($params['types']) ? $params['types'] : NULL;
$url_include_patterns = !empty($params['url_include_patterns']) ? $params['url_include_patterns'] : NULL;
$url_exclude_patterns = !empty($params['url_exclude_patterns']) ? $params['url_exclude_patterns'] : NULL;
$allowed_schemes = !empty($params['schemes']) ? array_filter($params['schemes']) : array();
// Add conditions based on remote file type *or* local allowed extensions.
$or_condition = db_or();
// Include local files with the allowed extensions.
if (!empty($params['file_extensions'])) {
$extensions = array_filter(explode(' ', $params['file_extensions']));
$local_wrappers = array_intersect_key(media_get_local_stream_wrappers(), $allowed_schemes);
if (!empty($local_wrappers) && !empty($extensions)) {
$local_condition = db_or();
foreach (array_keys($local_wrappers) as $scheme) {
foreach ($extensions as $extension) {
$local_condition->condition($alias . '.uri', db_like($scheme . '://') . '%' . db_like('.' . $extension), 'LIKE');
}
}
$or_condition->condition($local_condition);
}
}
// Include remote files with the allowed file types.
if (!empty($remote_types)) {
$remote_wrappers = array_intersect_key(media_get_remote_stream_wrappers(), $allowed_schemes);
if (!empty($remote_wrappers)) {
$remote_condition = db_and();
$wrapper_condition = db_or();
foreach (array_keys($remote_wrappers) as $scheme) {
$wrapper_condition->condition($alias . '.uri', db_like($scheme . '://') . '%', 'LIKE');
}
$remote_condition->condition($wrapper_condition);
$remote_condition->condition($alias . '.type', $remote_types, 'IN');
$or_condition->condition($remote_condition);
}
}
if ($or_condition->count()) {
$query->condition($or_condition);
}
if ($url_include_patterns) {
$query->condition($alias . '.uri', '%' . db_like($url_include_patterns) . '%', 'LIKE');
// Insert stream related restrictions here.
}
if ($url_exclude_patterns) {
$query->condition($alias . '.uri', '%' . db_like($url_exclude_patterns) . '%', 'NOT LIKE');
}
if (!user_access('administer files')) {
$query->condition($alias . '.uri', db_like('private://') . '%', 'NOT LIKE');
}
// @todo This is possibly redundant since it's already filtered in the view.
$query->condition($alias. '.status', FILE_STATUS_PERMANENT);
foreach (array_keys(media_get_hidden_stream_wrappers()) as $name) {
$query->condition($alias . '.uri', db_like($name . '://') . '%', 'NOT LIKE');
}
}
......@@ -41,6 +41,7 @@ function media_hook_info() {
'media_wysiwyg_allowed_view_modes_alter',
'media_format_form_prepare_alter',
'media_token_to_markup_alter',
'query_media_browser_alter',
);
return array_fill_keys($hooks, array('group' => 'media'));
......@@ -175,31 +176,6 @@ function media_menu() {
'file' => 'includes/media.browser.inc',
);
/**
* Browser callbacks
* @var unknown_type
*/
$items['media/browser/list'] = array(
'title' => 'Media browser list',
'description' => 'Ajax Callback for getting media',
'page callback' => 'media_browser_list',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'includes/media.browser.inc',
);
$items['media/browser/library'] = array(
'title' => 'Media browser library',
'description' => 'Media Browser for picking media and uploading new media',
'page callback' => 'media_browser_library',
'access callback' => 'media_access',
'access arguments' => array('view'),
'type' => MENU_CALLBACK,
'file' => 'includes/media.browser.inc',
);
$items['media/%file/format-form'] = array(
'title' => 'Style selector',
'description' => 'Choose a format for a piece of media',
......
......@@ -91,8 +91,6 @@ function template_preprocess_media_views_view_media_browser(&$vars) {
// Add media browser javascript and CSS.
drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.views.js');
// We need to see if we can depricate this
drupal_add_js(drupal_get_path('module', 'media') . '/js/plugins/media.library.js');
}
/**
......
......@@ -91,7 +91,7 @@ $handler->display->display_options['filters']['type']['expose']['identifier'] =
/* Display: Media browser */
$handler = $view->new_display('media_browser', 'Media browser', 'media_browser_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'View Library';
$handler->display->display_options['title'] = 'Library';
/* Display: My files */
$handler = $view->new_display('media_browser', 'My files', 'media_browser_my_files');
......@@ -159,7 +159,7 @@ $translatables['media_default'] = array(
t('File name'),
t('Type'),
t('Media browser'),
t('View Library'),
t('Library'),
t('My files'),
t('User who uploaded'),
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment