View source
<?php
define('VIEWS_MEDIA_BROWSER_VAR_NAMESPACE', 'views_media_browser');
define('FILE_DEFAULT_MEDIA_URL', 'media/browser/list');
define('VIEWS_MEDIA_BROWSER_DEFAULT_VIEW_NAME', 'views_media_browser');
define('VIEWS_MEDIA_BROWSER_DEFAULT_VIEW_DISPLAY', 'default');
define('VIEWS_MEDIA_BROWSER_VAR_VIEW_NAME', VIEWS_MEDIA_BROWSER_VAR_NAMESPACE . '__view_name');
define('VIEWS_MEDIA_BROWSER_VAR_VIEW_DISPLAY', VIEWS_MEDIA_BROWSER_VAR_NAMESPACE . '__view_display');
function views_media_browser_views_api() {
list($module, $api) = func_get_args();
if ($module == "views" && $api == "views_default") {
return array(
"version" => "3.0",
);
}
}
function views_media_browser_media_browser_plugin_info() {
return array(
'views_media_browser' => array(
'#weight' => 11,
),
);
}
function views_media_browser_get_view_display() {
return variable_get(VIEWS_MEDIA_BROWSER_VAR_VIEW_DISPLAY, VIEWS_MEDIA_BROWSER_DEFAULT_VIEW_DISPLAY);
}
function views_media_browser_add_browser_javascript() {
drupal_add_js(base_path() . 'misc/jquery.ba-bbq.js');
drupal_add_js(drupal_get_path('module', 'views_media_browser') . '/js/views_media_browser.js');
}
function views_media_browser_media_browser_plugins_alter(&$plugins) {
unset($plugins['views_media_browser']);
if (!array_key_exists('library', $plugins)) {
return;
}
views_media_browser_add_browser_javascript();
$view_name = views_media_browser_get_view_name();
$view = views_get_view($view_name);
if (!$view) {
drupal_set_message(t('View %view not found.'), array(
'%view' => $view_name,
));
return;
}
$success = $view
->set_display(views_media_browser_get_view_display());
if (!$success) {
}
$view
->init_handlers();
$form_state = array(
'view' => $view,
'display' => $view->display_handler->display,
'exposed_form_plugin' => $view->display_handler
->get_plugin('exposed_form'),
'method' => 'get',
'rerender' => TRUE,
'no_redirect' => TRUE,
);
$form = drupal_build_form('views_exposed_form', $form_state);
$form['exposed_button'] = array(
'#markup' => '<a class="exposed-button button">Filter</a>',
);
unset($form['submit']);
$form = drupal_render($form);
$plugins['library']['#markup'] = $form . $plugins['library']['#markup'];
$plugins['library']['#attached']['js'] = array(
drupal_get_path('module', 'views_media_browser') . '/js/views_media_browser_library.js',
);
}
function views_media_browser_menu_alter(&$menu) {
$menu['media/browser/list']['page callback'] = 'views_media_browser_list';
}
function views_media_browser_get_paged_view($start = 0, $limit = 15) {
$name = views_media_browser_get_view_name();
$display_id = views_media_browser_get_view_display();
$view = views_get_view($name);
$view
->set_items_per_page($limit);
$view
->set_offset($start);
if (is_object($view)) {
if (is_string($display_id)) {
$view
->set_display($display_id);
}
else {
$view
->init_display();
}
$display = $view->display_handler;
$display
->set_option('items_per_page', $limit);
$display
->set_option('offset', $start);
$view
->pre_execute();
$view
->execute();
return $view->result;
}
else {
return array();
}
}
function views_media_browser_list() {
$params = drupal_get_query_parameters();
$view_filters = isset($params['filters']) ? $params['filters'] : array();
$_GET = array_merge($_GET, $view_filters);
$limit = isset($params['limit']) ? $params['limit'] : 15;
$start = isset($params['start']) ? $params['start'] : 0;
$results = views_media_browser_get_paged_view($start, $limit);
$files = $fids = array();
foreach ($results as $row) {
$fids[] = $row->fid;
}
$files = file_load_multiple($fids);
foreach ($files as $file) {
media_browser_build_media_item($file);
}
drupal_json_output(array(
'media' => array_values($files),
));
exit;
}
function views_media_browser_media_browser_plugin_view($key, $plugins) {
return $plugins;
}
function views_media_browser_get_view_name() {
return variable_get(VIEWS_MEDIA_BROWSER_VAR_VIEW_NAME, VIEWS_MEDIA_BROWSER_DEFAULT_VIEW_NAME);
}
function views_media_browser_preprocess_media_thumbnail(&$variables) {
$file = $variables['element']['#file'];
if (is_object($file) && isset($file->field_title) && !empty($file->field_title)) {
$display_title = NULL;
$title_items = field_get_items('file', $file, 'field_title');
if (!$title_items) {
$title_items = reset($file->field_title);
}
if (!empty($title_items)) {
$title = $title_items[0];
if ($title) {
$display_title = isset($title['safe_value']) ? $title['safe_value'] : NULL;
$display_title = $display_title || isset($title['value']) ? check_plain($title['value']) : NULL;
if ($display_title) {
$variables['element']['#name'] = $display_title;
}
}
}
}
if (is_object($file) && isset($file->language)) {
$variables['element']['#name'] .= ' [' . $file->language . ']';
}
}
function views_media_browser_theme() {
return array();
}
function views_media_browser_views_pre_render($view) {
if ($view->name === 'views_media_browser' && empty($view->result)) {
$view->attachment_after = '<div class="view-no-results">' . t('There are no matching files for the selected filters.') . '</div>';
}
}
function views_media_browser_preprocess_views_view_unformatted(&$elements) {
$view = $elements['view'];
if ($view->current_display === 'browser_thumbs' && $view->name === 'views_media_browser') {
$fids = array();
foreach ($view->result as $delta => $row) {
$fids[] = $row->fid;
}
$files = file_load_multiple($fids);
foreach ($view->result as $delta => $row) {
$preview = media_get_thumbnail_preview($files[$row->fid], TRUE);
$rendered_thumbnail = drupal_render($preview);
$elements['rows'][$delta] = $rendered_thumbnail;
}
drupal_add_css(drupal_get_path('module', 'views_media_browser') . '/css/media_thumbs_view.css', array(
'preprocess' => FALSE,
));
drupal_add_js(drupal_get_path('module', 'views_media_browser') . '/js/media_thumbs_smart_wrap.js');
}
}
function views_media_browser_preprocess_views_view_table(&$elements) {
$view = $elements['view'];
if ($view->current_display === 'browser_list' && $view->name === 'views_media_browser') {
$elements['header']['actions'] = t('Actions');
$elements['header_classes']['actions'] = t('Actions');
$elements['fields']['actions'] = 'actions';
$elements['field_attributes']['actions'] = array();
$elements['field_classes']['actions'] = array();
foreach ($elements['rows'] as $delta => &$row) {
$fid = $view->result[$delta]->fid;
$elements['field_attributes']['actions'][$delta] = array();
$elements['field_classes']['actions'][$delta] = array();
$row['actions'] = l(t('Edit'), sprintf('file/%d/edit', $fid)) . ' ' . l(t('Delete'), sprintf('file/%d/delete', $fid));
}
unset($row);
}
}
function views_media_browser_admin_paths() {
return array(
'admin/content/media_browser' => TRUE,
'admin/content/media_browser/*' => TRUE,
);
}