Commit 3ae599fe authored by JacobSingh's avatar JacobSingh

Should be adding -a to commits ;)

parent 94e466cf
<?php
/**
* DO NOT EDIT THIS. IT WILL BE REMOVED IN THE NEXT RELEASE.
* This file is only here so that the class autoloader doesn't barf.
*
* It has been moved to includes
*/
/**
* Extends DrupalEntityControllerInterface.
*
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
<?php
/**
* @file
* Media Theming
*
* Theming functions for the Media module.
*/
/**
* Display the media file browser.
* @TODO this is depreciated I think
* @param array $element
* The form element.
* @return string
*/
function theme_media_file_browser($element) {
// Add the CSS for our display.
$output = '<div class="media browser">' . $element . '</div>';
return $output;
}
/**
* Display a media file list.
* @TODO this is depreciated I think
* @param array $element
* The form element.
* @return string
*/
function theme_media_file_list($element) {
// Add the CSS for our display.
return '<div class="media-file-list">' . theme('form_element', $element, $element['#children']) . '</div>';
}
/**
* Display a browser pane.
* @TODO this is depreciated I think
*
* @param array $form
* The form element.
* @return string
*/
function theme_media_browser_pane($form) {
return;
$output = array();
// render the drawers
$output[] = '<div' . drupal_attributes($form['#attributes']) . '>';
// render the drawer list
$output[] = ' <div class="browser drawers">';
$output[] = drupal_render_form(null, $form['drawers']);
$output[] = ' </div>';
// render the drawer displays
$output[] = drupal_render_form(null, $form);
$output[] = '</div>';
return implode("\n", $output);
}
/**
* Default theming function for creating the browser frame.
* Assumes an array of file objects as $files and an
* array of $parameters
* @param $variables
* array of variables
* @return unknown_type
*/
function theme_media_browser_content_frame($variables) {
// Pull out all the variables into a usable form
extract($variables);
// Did we get any files back?
if (! count($files)) {
// @TODO display no files found
}
$html = array();
// On the first invocation, load javascript and build the browser frame
if ($invoke) {
}
// Render the results limiter
$html[] = theme('media_browser_control_result_limit', array('parameters' => $parameters));
// Render the actual content
$form = drupal_get_form('media_file_listing_form', $files, $parameters);
$html[] = drupal_render($form);
// Make sure to close the wrapping div
if ($invoke) {
$html[] = '</div>';
}
return implode("\n", $html);
}
/**
* Display a item list of files as thumbnails. Implements
* the admin thumbnail theme for now- serves as a wrapper
*
* @param $files
* An array of file objects to display.
* @return
*/
function theme_media_browser_thumbnails($variables) {
$files = $variables['files'];
$style_name = $variables['style_name'];
$thumbnails = array();
foreach ($files as $file) {
$thumbnails[] = theme('media_admin_thumbnail', array('file' => $file, 'style_name' => $style_name));
}
return theme('item_list', array('items' => $thumbnails, 'attributes' => array('class' => 'media_content_navigator results')));
}
/**
* Theme a thumbnail.
* @param $variables
* array items being passed in
*/
function theme_media_admin_thumbnail($variables) {
$path = drupal_get_path('module', 'media');
$file = $variables['file'];
$style_name = $variables['style_name'];
if (isset($file)) {
$file_url = file_create_url($file->uri);
}
else {
return '';
}
$output = '';
if (module_exists('styles')) {
$thumbnail = theme('styles',
array(
'field_type' => 'file',
'style_name' => $style_name,
'uri' => $file->uri,
'description' => t('Thumbnail for !filename.', array('!filename' => $file->filename)),
'object' => $variables['file'],
));
}
else {
// Display a thumbnail for images.
if (strstr($file->filemime, 'image')) {
$thumbnail = theme('image_style',
array(
'style_name' => 'thumbnail',
'path' => $file->uri,
'alt' => t('Thumbnail for !filename.', array('!filename' => $file->filename)),
)
);
}
// Display the 'unknown' icon for other file types.
else {
$thumbnail = theme('image',
array(
'path' => $path . '/images/file-unknown.png',
'alt' => t('Thumbnail for !filename.', array('!filename' => $file->filename)),
'attributes' => array('class' => 'file-unknown'),
));
}
}
$output .= l($thumbnail,
$file_url,
array(
'html' => TRUE,
'attributes' => array('class' => 'media-thumbnail'),
));
return $output;
}
/**
* Theme operations for a thumbnail.
*/
function theme_media_admin_thumbnail_operations($variables) {
$destination = drupal_get_destination();
$file = $variables['file'];
$output = l(t('edit'), 'media/' . $file->fid . '/edit', array('query' => $destination));
return $output;
}
/**
* Add messages to the page.
*/
function template_preprocess_media_dialog_page(&$variables) {
$variables['messages'] = theme('status_messages');
}
/* ******************************************** */
/* Content navigation controls */
/* ******************************************** */
/**
* Theme function to display the results limiting- 10, 30, 50 results
* per page.
*
* @param $variables
* array parameters
* @return unknown
*/
function theme_media_browser_control_result_limit($variables) {
// Pull out all the variables into a usable form
extract($variables);
if (! isset($limits)) {
$limits = array(10, 30, 50);
}
// @NOTE these do not need to be aware of the current
// page because clicking them will reset the
// display to 1 -> $limit
$parameters['page'] = 0;
// save the active limit
$current_limit = $parameters['limit'];
foreach ($limits as $limit) {
if ($limit == $current_limit) {
$class = 'active';
}
else {
$class = '';
}
// set the value of this limit parameter to this limit value
$parameters['limit'] = $limit;
$per_display[] = l($limit, $limit, array('query' => $parameters, 'attributes' => array('class' => $class)));
}
return theme('item_list', array('items' => $per_display, 'attributes' => array('class' => 'result_limit')));
}
/**
* Stolen from file.module theme_file_link
*
* @param $variables
* An associative array containing:
* - file: A file object to which the link will be created.
*/
function theme_media_link($variables) {
$file = $variables['file'];
$url = 'media/' . $file->fid;
$icon = theme('file_icon', array('file' => $file));
// Set options as per anchor format described at
// http://microformats.org/wiki/file-format-examples
$options = array(
'attributes' => array(
'type' => $file->filemime . '; length=' . $file->filesize,
),
);
// Use the description as the link text if available.
if (empty($file->description)) {
$link_text = check_plain($file->filename);
}
else {
$link_text = check_plain($file->description);
$options['attributes']['title'] = check_plain($file->filename);
}
return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
}
/**
* Adds a wrapper around a preview of a media file.
* @param unknown_type $element
* @return unknown_type
*/
function theme_media_thumbnail($variables) {
$label = '';
$element = $variables['element'];
$destination = drupal_get_destination();
// Wrappers to go around the thumbnail
$prefix = '<div class="media-item"><div class="media-thumbnail">';
$suffix = '</div></div>';
// Arguments for the thumbnail link
$thumb = $element['#children'];
$target = 'media/' . $element['#object']->fid . '/edit';
$options = array('query' => $destination, 'html' => true, 'attributes' => array('title' => t('Click to edit details')));
// Element should be a field renderable array... This is a little wonky - admitted.
if (!empty($element['#show_names']) && $element['#name']) {
$label = '<div class="label-wrapper"><label class="media-filename">' . $element['#name'] . '</label></div>';
}
// How can I attach CSS here?
//$element['#attached']['css'][] = drupal_get_path('module', 'media') . '/css/media.css';
drupal_add_css(drupal_get_path('module', 'media') . '/css/media.css');
$output = $prefix;
if (!empty($element['#add_link'])) {
$output .= l($thumb, $target, $options);
}
else {
$output .= $thumb;
}
$output .= $label . $suffix;
return $output;
}
function template_preprocess_media_thumbnail(&$variables) {
// Set the name for the thumbnail to be the filename. This is done here so
// that other modules can hijack the name displayed if it should not be the
// filename.
$variables['element']['#name'] = isset($variables['element']['#object']->filename) ? $variables['element']['#object']->filename : NULL;
}
/**
* Field formatter for displaying a file as a large icon.
*/
function theme_media_formatter_large_icon($variables) {
$file = $variables['file'];
$icon_dir = media_variable_get('icon_base_directory') . '/' . media_variable_get('icon_set');
$icon = file_icon_path((object)$file, $icon_dir);
return theme('image', array('path' => $icon));
}
<?php
/**
* @file
* Helper functions related to media types. CRUD for saving their settings mainly.
*
* Also contains the media entity class definition.
* @see media.install for a list of the base types.
*/
/**
* Update an existing media type or create a new one.
*
* The default media types are currently 'Audio', 'Image', 'Video', and
* 'Other', which are defined in media_install().
*
* @param object &$type
* $type is an object with the following fields:
* ->name => The name of the media asset type, such as 'video';
* ->label => The human readable name;
* ->base => boolean: If the media type cannot be removed.
* ->type_callback => Function call to filter an instance to its bundle.
* ->type_callback_args => An array of args to be passed to type_callback.
* @return void;
*/
function media_type_save(&$type) {
if (empty($type->name)) {
throw new Exception('Enable to add type, name not provided');
}
$type = media_type_set_defaults($type);
if (!is_array($type->type_callback_args)) {
throw new Exception('type_callback_args should be an array');
}
$type->type_callback_args = serialize($type->type_callback_args);
$ret = db_merge('media_type')
->key(array('name' => $type->name))
->fields((array)$type)
->execute();
media_type_configure_fields($type);
// Clear the caches
drupal_static_reset('media_type_get_types');
drupal_static_reset('media_type_get_mime_map');
return;
}
/**
* Assigns formatters to view_modes for the file field of a media type.
*
* Each media type contains a field called "file" and certain stock view modes.
* This function will assign a format to each view mode. For instance, images
* would want to use a thumbnail format for files when in the "preview"
* view_mode. The called would pass in 'image' for $name and
* array('media_preview' => 'styles_file_square_thumbnail') to $view_modes_to_formatters.
*
* This will also set the "custom_settings" bit to make sure it actually takes effect.
*
* @param string $name
* @param array $view_modes_to_formatters
* @throws Exception
* @return void
*/
function media_type_configure_formatters($name, $view_modes_to_formatters) {
$instance = field_info_instance('media', 'file', $name);
$bundle_settings = field_bundle_settings('media', $name);
if (!$instance) {
throw new Exception('Unable to set formatter preferences for '. $name);
}
foreach ($view_modes_to_formatters as $view_mode => $formatter) {
$bundle_settings['view_modes'][$view_mode]['custom_settings'] = 1;
if (!isset($instance['display'][$view_mode])) {
$instance['display'][$view_mode] = $instance['display']['default'];
}
$instance['display'][$view_mode]['type'] = $formatter;
}
field_bundle_settings('media', $name, $bundle_settings);
field_update_instance($instance);
}
/**
* Loads a media type based on its machine name.
*
* @param string $name
* @return StdClass
*/
function media_type_load($name) {
$types = media_type_get_types();
if (isset($types[$name])) {
return $types[$name];
}
}
/**
* Sets up the default fields which a media type needs.
*
* Currently, this is just a "file" field which contains the reference to the actual file.
* @TODO Do we also want to make the file 'description/caption' a field?
*
* @param StdClass $media_type
* @return void
*/
function media_type_configure_fields($media_type) {
$field = field_info_field('file');
$instance = field_info_instance('media', 'file', $media_type->name);
if (empty($field)) {
$field = array(
'field_name' => 'file',
'type' => 'file',
);
$field = field_create_field($field);
}
// Perhaps the file field exists already, if so skip.
if (empty($instance)) {
$weight = -5;
$instance = array(
'field_name' => 'file',
'entity_type' => 'media',
'bundle' => $media_type->name,
'label' => 'File',
'widget_type' => 'file_file',
'widget' => array(
'weight' => $weight,
),
'required' => TRUE,
'locked' => TRUE,
'display' => array(
'media_preview' => array(
'label' => 'hidden',
'type' => 'file_default',
'weight' => $weight,
),
'media_original' => array(
'label' => 'hidden',
'type' => 'file_default',
'weight' => $weight,
),
),
);
field_create_instance($instance);
}
}
/**
* Loads all media types into an array keyed by machine name and sorted
* and weighted lexographically.
*
* @return array
* Media types keyed by machine name.
*/
function media_type_get_types() {
$types =& drupal_static(__FUNCTION__);
if (!$types) {
$types = db_select('media_type', 'mt')
->orderBy('weight')
->fields('mt')
->execute()
->fetchAllAssoc('name'); // Will key by the name field.
foreach ($types as &$type) {
// I really hate this.
$type->type_callback_args = unserialize($type->type_callback_args);
}
}
return $types;
}
/**
* Create the basic class and defaults for a media entity bundle type.
*/
function media_type_set_defaults($info) {
$type = new StdClass();
// This is used to filter a file to the proper bundle.
$type->type_callback = 'media_is_type';
$type->type_callback_args = array();
$type->weight = 0;
foreach ($info as $k => $v) {
$type->{$k} = $v;
}
return $type;
}
/**
* Determines the type of media a passed in $file is.
*
* @todo: integrate this properly with other APIs in media when fields is done
* @param unknown_type $file
* @return unknown_type
*/
function media_get_type($file) {
$types = media_type_get_types();
foreach ($types as $name => $type) {
if (call_user_func_array($type->type_callback, array($file, $type->type_callback_args))) {
return $name;
}
}
throw new Exception('Unable to determine type of media from ' . var_export($file, 1));
}
/**
* Default callback used to determine if a piece of media is of a given type.
*
* @TODO: document 'any' and 'all' matching.
*
* @param $media
* The media file asset object.
* @param $args
*
* @return unknown_type
*/
function media_is_type($media, $args) {
$match_type = !empty($args['match_type']) ? 'any' : $args['match_type'];
$match_all = $match_type == 'all';
if (!empty($args['mimetypes'])) {
foreach ($args['mimetypes'] as $expression) {
if (preg_match($expression, $media->filemime)) {
if (!$match_all) {
return TRUE;
}
}
}
// Was not matched, so return
if ($match_all) {
return FALSE;
}
}
if (!empty($args['extensions'])) {
if (in_array(pathinfo($media->uri, PATHINFO_EXTENSION), $args['extensions'])) {
if (!$match_all) {
return TRUE;
}
}
// Was not matched, so return
if ($match_all) {
return FALSE;
}
}
if (!empty($args['streams'])) {
}
}
/**
* Implement hook_media_format_form_prepare_alter
*/
function media_media_format_form_prepare_alter(&$form, &$form_state, $media) {
switch($media->type) {
case 'image':
// @TODO: Isn't there a $file->description?
$description = $media->filename;
$form['options']['alt'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => $description,
'#description' => t('Alternate text a user will see if the image is not available'),
);
break;
}
}
/**
* Returns the number of files that need to be converted to media.
*/
function media_type_invalid_files_count() {
return db_select('file_managed', 'fm')
->condition('type', NULL)
->countQuery()
->execute()
->fetchField();
}
/**
* Adds a value for the type column in files_managed.
*
* If $update_existing is TRUE, will update the type of files with an existing type value.
*
* @param boolean $update_existing
* @param integer $no_to_update
* @param integer $offset
*
* @return array
* A list of updated file ids
*/
function media_type_batch_update($update_existing = FALSE, $no_to_update = NULL, $offset = 0) {
$results = array();
$query = db_select('file_managed', 'fm')
->fields('fm', array('fid'));
if (!$update_existing) {
$query->condition('type', NULL);
}
if ($no_to_update) {
$query->range($offset, $no_to_update);
}
elseif ($offset) {
$query->range($offset);
}
$fids = $query->execute()->fetchCol();
foreach ($fids as $fid) {
$file = file_load($fid);
if (!$file->fid) {
throw new Exception('Unable to continue, file was not loaded.');
}
$file->type = media_get_type($file);
file_save($file);
$results[] = $fid;
}
return $results;
}
<?php
/**
* @file
* XML data retrieval and storage API for Media.
*/
/**
* A wrapper around simplexml to retrieve a given XML file.
*
* @param $url
* The URL to the XML to retrieve.
* @param $display_errors
* Optional; if TRUE, then we'll display errors to the end user. They'll be
* logged to the watchdog in any case.
* @param $refresh
* Optional; if TRUE, then we'll force a new load of the XML. Otherwise,
* a cached version will be retrieved if possible.
* @return
* A fully populated object, or FALSE on an error.
*/
function _media_retrieve_xml($url, $display_errors = FALSE, $refresh = FALSE) {
$xmls = &drupal_static(__FUNCTION__, array());
// Return our cached XML if allowed, and it exists.
if (!$refresh && isset($xmls[$url])) {
return $xmls[$url];
}
else if (!$refresh && $cache = cache_get('media:xml:'. $url, 'cache_media_xml')) {
$xmls[$url] = $cache->data;
return $xmls[$url];
}
// Enable user error handling.
libxml_use_internal_errors(TRUE);
// Load the document
$xml = simplexml_load_file($url);
if (!$xml) {
foreach (libxml_get_errors() as $error) {
$message = 'Error retrieving XML from %url: %error';
$params = array('%url' => $url, '%error' => $error->message);
// Handle errors here.
if ($display_errors) {
drupal_set_message(t($message, $params), 'error');
}
watchdog('media', $message, $params, WATCHDOG_WARNING);
}
// Clear our error cache.
libxml_clear_errors();
// Set the static cache, but not Drupal's cache, so we can attempt to
// retrieve the file another time if possible.
$xmls[$url] = FALSE;
}
else {
$xmls[$url] = _media_unserialize_xml($xml);
cache_set('media:xml:'. $url, $xmls[$url], 'cache_media_xml', media_variable_get('xml_cache_expire', 3600));
}
return $xmls[$url];
}
/**
* Recursively converts a SimpleXMLElement object into an array.
* @param $xml
* The original XML object.
*/
function _media_unserialize_xml($xml) {
if ($xml instanceof SimpleXMLElement) {
$xml = (array) $xml;
}
if (is_array($xml)) {
foreach ($xml as $key => $item) {