media_library.module in Media Library 6
Same filename and directory in other branches
Main Media Library module file.
File
media_library.moduleView source
<?php
/**
* @file
* Main Media Library module file.
*/
/**
* Defines default per page limit for browsing media
*/
define('MEDIA_LIBRARY_LIMIT', 24);
/**
* Implementation of hook_filter()
* This function works as a filter routing.
* The hook_filter_media() receives the attributes already parsed from the tag
* and returns an array with the structure to be rendered.
*/
function media_library_filter($op, $delta = 0, $format = -1, $text = '', $cache_id = 0) {
switch ($op) {
/* WARNING: this is for development purpose. Disable later. */
/* TODO: Remove this after dev to re-enable caching of filter. */
case 'no cache':
return TRUE;
case 'list':
return array(
0 => t('Media Library filter'),
);
case 'description':
return t('The filter used by Media Library to display all multimedia content.');
// TODO: check if this is needed.
// TODO: clean the '='s and '|'s from text
case 'prepare':
return $text;
case 'process':
/**
* Here we get our tags, parse all the attributes, render the $content and return.
* Then, we allow other modules to alter the content with hook_filter_media_alter()
* After this, we call the hook_filter_media of the module indicated in the tag,
* which is, media:type. This last hook should return the rendered content.
*
* NOTICE: hook_media_filter_alter should handle a referenced $content. See
* example below.
*/
$tags = media_library_get_tags($text);
foreach ($tags as $tag => $content) {
drupal_alter('filter_media', $content);
$types = media_library_get_types();
$content = module_invoke($types[$content['type']]['module'], 'filter_media', $content);
$text = str_replace($tag, $content, $text);
}
return $text;
default:
return $text;
}
}
/**
* Returns an array of all types declared by modules via the hook
* hook_media_types
*/
function media_library_get_types() {
static $types;
if (isset($types)) {
return $types;
}
$types = array();
foreach (module_implements('media_types') as $module) {
$module_types = module_invoke($module, 'media_types');
foreach ($module_types as $type => $info) {
$types[$type] = $info;
$types[$type]['module'] = $module;
}
}
return $types;
}
/**
* Helper function to parse all tags from content.
* Code based on imagefield_assist_get_macros()
*/
function media_library_get_tags($text) {
$tags = array();
preg_match_all('/
\\[media: (
[^\\[\\]]+
)* \\]
/x', $text, $matches);
// Don't process duplicates.
// TODO: Really necessary?
$tag_match = (array) array_unique($matches[1]);
foreach ($tag_match as $tag) {
$current_tag = '[media:' . $tag . ']';
$params = array_map('trim', explode('|', $tag));
// The type name is now the first parameter on the array
$type = array_shift($params);
$vars = array();
$vars['type'] = $type;
foreach ($params as $param) {
$pos = strpos($param, '=');
$name = trim(substr($param, 0, $pos));
$value = substr($param, $pos + 1);
//TODO: sanitization back-replacement for [,],|,=
//$value = urldecode($value);
$vars[$name] = trim($value);
}
// The full unaltered filter string is the key for the array of filter
// attributes.
$tags[$current_tag] = $vars;
}
return $tags;
}
/**
* Implementation of hook_filter_media_alter()
* This is the main filter that will be used to process the common attributes.
* @param
* attributes - array with the attributes parsed by the filter.
*/
function media_library_filter_media_alter(&$content) {
// Nothing to be done here now.
}
/**
* This function does everything needed to create the tag to be inserted and return it.
* @param
* object - The media object, with all its attributes set.
*/
function media_library_create_filter_tag($object) {
$tag = '';
if (!is_object($object)) {
return $tag;
}
/**
* hook_media_tag() is the one to generate the tag. The tag is an array of
* key=>value attributes, and the tag will be rendered as
* [media:att1=value|att2=...|...]
*/
$tag_array = module_invoke_all('media_tag', $object);
/**
* hook_media_tag_alter() allow the modules to change
* attributes of the object before all the tag is actually processed. This is useful if
* you have to change attributes used by other modules, like the common media attributes.
* NOTICE: The implementation of this hook must use &$tag_array as the parameter, since the
* object is being referenced for changing.
*/
drupal_alter('media_tag', $tag_array);
/* Finally, renders the attributes accordingly.*/
if (!empty($tag_array)) {
$type = $tag_array['type'];
unset($tag_array['type']);
foreach ($tag_array as $key => $value) {
// TODO: Sanitize value for [,],|,=
//$value = urlencode($value);
$tag_array[$key] = $key . '=' . $value;
}
$tag = '[media:' . $type . '|' . implode('|', $tag_array) . ']';
}
return $tag;
}
/**
* Implementation of hook_media_tag_alter()
*/
function media_library_media_tag_alter(&$tag_array) {
// Nothing to do here for now. ;)
}
/**
* Implementation of hook_media_tag()
* Our basic tag attributes go here. They are supposed to be common and can be used by
* all sub-modules.
*/
function media_library_media_tag($object) {
$tag_array = array();
// TODO: Get object attributes an place them inside the tag array, like:
$tag_array['type'] = $object->type;
return $tag_array;
}
/**
* Implementation of hook_menu()
*/
function media_library_menu() {
$items = array();
// Main settings
$items['admin/settings/media-library'] = array(
'title' => t('Media Library'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'media_library_admin',
),
'access arguments' => array(
'administer media library',
),
'file' => 'media_library.admin.inc',
);
$items['admin/settings/media-library/main'] = array(
'title' => t('General'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Automated per-media settings tab
$module_info = module_invoke_all('media_types');
foreach ($module_info as $type => $info) {
if (isset($info['settings'])) {
$items['admin/settings/media-library/' . $type] = array(
'title' => $info['title'],
'description' => $info['description'],
'page callback' => 'drupal_get_form',
'page arguments' => array(
$info['settings'],
),
'access arguments' => array(
'administer media library',
),
'type' => MENU_LOCAL_TASK,
);
}
}
/* Our main form (modal) */
$items['media-library/main'] = array(
'title' => t('Add Media Content (Ajax)'),
'page callback' => 'media_library_modal',
'access arguments' => array(
'insert media',
),
'file' => 'media_library.modal.inc',
'type' => MENU_CALLBACK,
);
$items['media-library/main/add/%'] = array(
'title' => t('Add Media Content (Ajax)'),
'page callback' => 'media_library_modal_add',
'page arguments' => array(
3,
),
'access arguments' => array(
'insert media',
),
'file' => 'media_library.modal.inc',
'type' => MENU_CALLBACK,
);
// Ajax utilities
$items['media-library/preview/%'] = array(
'title' => t('Generates a preview for ajax'),
'page callback' => 'media_library_ajax_preview',
'page arguments' => array(
2,
),
//TODO: think about a permission here
'access arguments' => array(
'insert media',
),
'file' => 'media_library.modal.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_perm()
*/
function media_library_perm() {
return array(
'administer media library',
);
}
/**
* Implementation of hook_init()
*/
function media_library_init() {
global $theme_key;
// CTools necessary stuff
ctools_include('modal');
ctools_include('ajax');
ctools_modal_add_js();
// Our own basic js.
$js = drupal_get_path('module', 'media_library') . '/media_library.js';
drupal_add_js($js);
drupal_add_css(drupal_get_path('module', 'media_library') . '/media_library_browse.css');
drupal_add_js(array(
'media_library' => array(
'types' => media_library_get_types(),
'theme' => $theme_key,
),
), 'setting');
}
/**
* Implementation of hook_elements()
*/
function media_library_elements() {
$type['textarea'] = array(
'#process' => 'media_library_textarea',
);
return $type;
}
/**
* Process textareas do add media library link with class
*/
function media_library_textarea($element) {
if (!isset($element['#attributes']['class'])) {
$element['#attributes']['class'] = 'media-library';
}
else {
$element['#attributes']['class'] .= ' media-library';
}
return $element;
}
/**
* Implementation of hook_wysiwyg_plugin()
*/
function media_library_wysiwyg_plugin($editor, $version) {
switch ($editor) {
case 'tinymce':
if ($version > 3) {
// Prepare buttons
$types = media_library_get_types();
$buttons = array();
foreach ($types as $type => $info) {
$buttons['media_library_' . $type] = t('Media Library - !type', array(
'!type' => $info['title'],
));
}
return array(
'media_library' => array(
'path' => drupal_get_path('module', 'media_library') . '/ml_tiny/editor_plugin.js',
'buttons' => $buttons,
'url' => 'http://drupal.org/project/media_library',
'extended_valid_elements' => array(
'img[class|src|border=0|alt|title|width|height|align|name|style]',
'div[class|id]',
),
'load' => TRUE,
),
);
}
}
}
/**
* Implementation of hook_theme()
*/
function media_library_theme($existing, $type, $theme, $path) {
return array(
'media_library_browse_form' => array(
'arguments' => array(
'form' => NULL,
),
),
'media_library_thumb' => array(
'arguments' => array(
'image' => NULL,
),
),
'media_library_browse_footer' => array(
'arguments' => array(
'total' => NULL,
'pages' => NULL,
),
),
);
}
/**
* Implementation of hook_imagecache_default_presets()
*/
function media_library_imagecache_default_presets() {
$items['media_library_thumb'] = array(
'presetname' => 'media_library_thumb',
'actions' => array(
'0' => array(
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale_and_crop',
'data' => array(
'width' => '100',
'height' => '100',
),
),
),
);
return $items;
}
/**
* Themes a media browse form
*/
function theme_media_library_browse_form($form) {
$output = '<div id="media-library-browse-form">';
$output .= drupal_render($form);
// Nice info (not working yet);
//$output .= '<div class>' . t('of !pages pages containing !images images.', array('!pages' => $result['pages'], '!images' => $result['total'])) . '</div>';
$output .= '</div>';
return $output;
}
/**
* Themes a thumbnail for browsing
*/
function theme_media_library_thumb($image) {
if (is_object($image)) {
return theme('imagecache', 'media_library_thumb', $image->filepath, $image->title, $image->title, NULL, FALSE);
}
return '';
}
/**
* Themes nice footer for browsing media
*/
function theme_media_library_browse_footer($total, $pages) {
$output = '<div class="ml-footer">' . t('of !pages pages containing !results results.', array(
'!pages' => $pages,
'!results' => $total,
)) . '</div>';
return $output;
}
Functions
Name | Description |
---|---|
media_library_create_filter_tag | This function does everything needed to create the tag to be inserted and return it. |
media_library_elements | Implementation of hook_elements() |
media_library_filter | Implementation of hook_filter() This function works as a filter routing. The hook_filter_media() receives the attributes already parsed from the tag and returns an array with the structure to be rendered. |
media_library_filter_media_alter | Implementation of hook_filter_media_alter() This is the main filter that will be used to process the common attributes. |
media_library_get_tags | Helper function to parse all tags from content. Code based on imagefield_assist_get_macros() |
media_library_get_types | Returns an array of all types declared by modules via the hook hook_media_types |
media_library_imagecache_default_presets | Implementation of hook_imagecache_default_presets() |
media_library_init | Implementation of hook_init() |
media_library_media_tag | Implementation of hook_media_tag() Our basic tag attributes go here. They are supposed to be common and can be used by all sub-modules. |
media_library_media_tag_alter | Implementation of hook_media_tag_alter() |
media_library_menu | Implementation of hook_menu() |
media_library_perm | Implementation of hook_perm() |
media_library_textarea | Process textareas do add media library link with class |
media_library_theme | Implementation of hook_theme() |
media_library_wysiwyg_plugin | Implementation of hook_wysiwyg_plugin() |
theme_media_library_browse_footer | Themes nice footer for browsing media |
theme_media_library_browse_form | Themes a media browse form |
theme_media_library_thumb | Themes a thumbnail for browsing |
Constants
Name | Description |
---|---|
MEDIA_LIBRARY_LIMIT | Defines default per page limit for browsing media |