View source
<?php
define('MEDIAFRONT_DEFAULT_WIDTH', 500);
define('MEDIAFRONT_DEFAULT_HEIGHT', 350);
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'mediafront') . '/includes/mediafront.field.inc';
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'mediafront') . '/includes/mediafront.filefield.inc';
require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'mediafront') . '/includes/mediafront.preset.inc';
function mediafront_permission() {
$perms = array(
'administer mediafront' => array(
'title' => t('Administer MediaFront'),
'description' => t('Perform administration tasks for the MediaFront module.'),
),
);
return array_merge($perms, mediafront_preset_permission());
}
function mediafront_menu() {
$items['mediafront_getplaylist'] = array(
'page callback' => 'mediafront_get_playlist_json',
'type' => MENU_CALLBACK,
'page arguments' => array(
1,
2,
3,
4,
),
'access arguments' => array(
'access content',
),
);
$items['mediafront_getnode'] = array(
'page callback' => 'mediafront_get_node_json',
'type' => MENU_CALLBACK,
'page arguments' => array(
1,
2,
),
'access arguments' => array(
'access content',
),
);
$items = array_merge($items, mediafront_preset_menu());
return $items;
}
function mediafront_theme() {
$theme = array();
$theme['mediafront_player_view'] = array(
'render element' => 'element',
'arguments' => array(
'view' => NULL,
'preset' => NULL,
),
);
$theme['mediafront_player_formatter'] = array(
'render element' => 'element',
'arguments' => array(
'items' => NULL,
),
);
$theme['mediafront_player'] = array(
'render element' => 'element',
'arguments' => array(
'entity' => NULL,
'preset' => NULL,
),
);
$theme['mediafront_field'] = array(
'render element' => 'element',
'arguments' => array(
'entity' => NULL,
'preset' => NULL,
),
);
return $theme;
}
function theme_mediafront_player_view($variables) {
$view = $variables['view'];
$preset = $variables['preset'];
$params['playlistObj'] = mediafront_get_playlist_from_view($view);
$params['playlist'] = $view->name;
$params['args'] = $view->args;
$params['pageLimit'] = method_exists($view, 'get_items_per_page') ? $view
->get_items_per_page() : $view->pager['items_per_page'];
return mediafront_get_player($preset, $params);
}
function theme_mediafront_player($variables) {
$entity = $variables['entity'];
$preset = $variables['preset'];
$params['nodeObj'] = mediafront_invoke_node($entity, array());
$params['node'] = $entity->nid;
$params['disablePlaylist'] = true;
return mediafront_get_player($preset, $params);
}
function theme_mediafront_field($variables) {
$entity = $variables['entity'];
$preset = $variables['preset'];
$params['nodeObj'] = (array) $entity;
if (isset($entity->nid)) {
$params['node'] = $entity->nid;
}
$params['disablePlaylist'] = true;
return mediafront_get_player($preset, $params);
}
function mediafront_features_api() {
return array(
'mediafront' => array(
'name' => t('MediaFront Presets'),
'default_hook' => 'mediafront_default_presets',
'file' => drupal_get_path('module', 'mediafront') . '/includes/mediafront.features.inc',
),
);
}
function mediafront_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'mediafront') . '/views',
);
}
function mediafront_get_players() {
$return = array();
foreach (module_implements('player_info') as $name) {
$function = $name . '_player_info';
$return = array_merge($return, $function());
}
return $return;
}
function mediafront_get_player_params($player) {
$get_params = $player . '_get_player_params';
if (function_exists($get_params)) {
return $get_params();
}
return array();
}
function mediafront_get_player_settings($player) {
$get_settings = $player . '_get_player_settings';
if (function_exists($get_settings)) {
return $get_settings();
}
return array();
}
function mediafront_get_settings($player, $overrides = array()) {
static $defaults = array();
$settings = isset($defaults[$player]) ? $defaults[$player] : array();
if (!$settings) {
$settings = array_merge(mediafront_get_player_params($player), mediafront_get_player_settings($player));
$defaults[$player] = $settings;
}
return $overrides ? array_merge($settings, $overrides) : $settings;
}
function mediafront_template_preprocess(&$variables) {
$variables['player_params'] = $variables['params'];
$variables['params'] = mediafront_get_settings($variables['params']['player'], $variables['params']);
}
function mediafront_service() {
return array(
array(
'#method' => 'mediafront.getPlaylist',
'#callback' => 'mediafront_get_playlist',
'#access arguments' => array(
'access content',
),
'#key' => FALSE,
'#args' => array(
array(
'#name' => 'playlist',
'#type' => 'string',
'#description' => t('Playlist name.'),
),
array(
'#name' => 'limit',
'#type' => 'int',
'#optional' => TRUE,
'#description' => t('The limit for the view to show.'),
),
array(
'#name' => 'page',
'#type' => 'int',
'#optional' => TRUE,
'#description' => t('The page number to show.'),
),
array(
'#name' => 'args',
'#type' => 'array',
'#optional' => TRUE,
'#description' => t('An array of arguments to pass to the view.'),
),
),
'#return' => 'array',
'#help' => t('Retrieves a playlist.'),
),
array(
'#method' => 'mediafront.getNode',
'#callback' => 'mediafront_get_node',
'#access arguments' => array(
'access content',
),
'#key' => FALSE,
'#args' => array(
array(
'#name' => 'nid',
'#type' => 'int',
'#description' => t('The node Id for the node you wish to get.'),
),
array(
'#name' => 'args',
'#type' => 'array',
'#optional' => TRUE,
'#description' => t('An array of arguments to pass to the node.'),
),
),
'#return' => 'array',
'#help' => t('Retrieves a node.'),
),
);
}
function mediafront_parse_params($json) {
$json = trim($json, ' []');
$params = null;
if ($json) {
$params = explode(',', $json);
foreach ($params as $index => $param) {
$params[$index] = trim($param, ' "');
}
}
return $params;
}
function mediafront_get_playlist_json($playlist = null, $limit = null, $page = null, $args = null) {
$params = mediafront_parse_params($_POST['params']);
$playlist = $playlist ? check_plain($playlist) : $params[1];
$limit = $limit ? check_plain($limit) : $params[2];
$page = $page ? check_plain($page) : $params[3];
$args = $args ? $args : mediafront_parse_params($params[4]);
print drupal_json_encode(mediafront_get_playlist($playlist, $limit, $page, $args));
}
function mediafront_get_playlist($playlist, $limit = 10, $page = 0, $args = array()) {
$view = views_get_view($playlist);
if (is_null($view)) {
return services_error($playlist . ' does not exist.');
}
if (!$view
->access('default')) {
return services_error('You do not have access to ' . $playlist . '.');
}
if (method_exists($view, 'set_use_pager')) {
$view
->set_use_pager(FALSE);
}
$view
->set_items_per_page($limit);
$view
->set_offset($page * $limit);
$view
->set_arguments($args);
$view->get_total_rows = TRUE;
$view
->execute();
return mediafront_get_playlist_from_view($view, $args);
}
function mediafront_get_playlist_from_view($view, $args = null) {
$playlist = array();
$playlist['nodes'] = array();
$playlist['name'] = $view->name;
$playlist['total_rows'] = $view->total_rows ? $view->total_rows : ($view->query->pager ? $view->query->pager
->get_total_items() : 0);
if ($view->base_table == 'node') {
foreach ($view->result as $node) {
if ($node = mediafront_get_node($node->nid, $args)) {
$playlist['nodes'][] = $node;
}
}
}
else {
if ($view->base_table == 'file_managed') {
foreach ($view->result as $file) {
$media = file_load($file->fid);
$node = array(
'title' => $media->filename,
);
mediafront_add_media($node, $media);
if ($preview = mediafront_get_media_preview($media)) {
$node["mediafiles"]["images"]["thumbnail"] = $preview;
$node["mediafiles"]["images"]["preview"] = str_replace('square_thumbnail', 'large', $preview);
}
$playlist['nodes'][] = $node;
}
}
}
return $playlist;
}
function mediafront_get_node_json($node = null, $args = null) {
$params = mediafront_parse_params($_POST['params']);
$node = $node ? check_plain($node) : check_plain(trim($params[1]));
$args = $args ? check_plain($args) : check_plain(trim($params[2]));
print drupal_json_encode(mediafront_get_node($node, $args));
}
function mediafront_get_node($nid, $args = null) {
return mediafront_invoke_node(node_load($nid), $args);
}
function mediafront_array_replace_recursive($toArray, $fromArray) {
foreach ($fromArray as $key => $value) {
if (is_array($toArray)) {
$toArray[$key] = is_array($value) || is_object($value) ? mediafront_array_replace_recursive($toArray[$key], $value) : $value;
}
else {
$toArray->{$key} = is_array($value) || is_object($value) ? mediafront_array_replace_recursive($toArray->{$key}, $value) : $value;
}
}
return $toArray;
}
function mediafront_playlist_node_merge($original, $additions) {
$merged = $original;
if (count($additions) > 0) {
foreach ($additions as $field => $value) {
$merged[$field] = isset($original[$field]) ? mediafront_array_replace_recursive($original[$field], $value) : $value;
}
}
return $merged;
}
function mediafront_invoke_node($node, $args) {
$additions = array();
if ($node) {
foreach (module_implements('playlist_node') as $name) {
$function = $name . '_playlist_node';
$additions = mediafront_playlist_node_merge($additions, $function($node, $args));
}
}
return $additions;
}
function mediafront_preset_select_form($default, $key = 'mediafront_preset') {
$presets = mediafront_preset_get_presets();
$options = array();
foreach ($presets as $preset) {
$options[$preset['name']] = $preset['name'];
}
return array(
'#type' => 'select',
'#title' => t('MediaFront Presets'),
'#options' => $options,
'#default_value' => isset($default[$key]) ? $default[$key] : '',
);
}
function mediafront_block_info() {
$blocks['mediaplayer'] = array(
'info' => t('Media Player'),
);
return $blocks;
}
function mediafront_block_configure($delta = '') {
$form = array();
if ($delta == 'mediaplayer') {
$form['mediafront'] = array(
'#type' => 'fieldset',
'#title' => 'MediaFront Settings',
'#description' => 'This is a special media player block that will allow you to place a media player preset within any block region on your website. In addition, you can link to any view within that page to act as the playlist for this media player.',
);
$form['mediafront']['mediafront_block_preset'] = mediafront_preset_select_form(array(
'mediafront_preset' => variable_get('mediafront_block_preset', ''),
));
$views = views_get_enabled_views();
$options = array(
'' => 'none',
);
foreach ($views as $view) {
$options[$view->name] = $view->name;
}
$form['mediafront']['mediafront_block_playlist'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => 'Link to View',
'#description' => 'Select the view you would like to link to this media player. This view must be on the same page as this player to be linked, and must include a visible Content Id (node Id) within the fields of the view display to work as expected.',
'#options' => $options,
'#default_value' => variable_get('mediafront_block_playlist', ''),
);
$form['mediafront']['mediafront_block_playlist_exclude'] = array(
'#type' => 'textarea',
'#title' => 'Exclude View Display',
'#description' => 'Provide any displays you would like to exclude from the selected view above.',
'#default_value' => implode("\n", variable_get('mediafront_block_playlist_exclude', array())),
);
}
return $form;
}
function mediafront_block_save($delta = '', $edit = array()) {
if ($delta == 'mediaplayer') {
variable_set('mediafront_block_preset', $edit['mediafront_block_preset']);
variable_set('mediafront_block_playlist', $edit['mediafront_block_playlist']);
$exclude = preg_split("/[\r\n]+/", $edit['mediafront_block_playlist_exclude']);
$exclude = array_map(create_function('$item', 'return trim($item);'), array_filter($exclude));
variable_set('mediafront_block_playlist_exclude', $exclude);
}
}
function mediafront_block_view($delta = '') {
$block = array();
if ($delta == 'mediaplayer') {
$block['subject'] = t('Media Player');
$block['content'] = mediafront_block_content();
}
return $block;
}
function mediafront_block_content() {
$preset = variable_get('mediafront_block_preset', '');
return $preset ? mediafront_get_player($preset) : '';
}
function mediafront_block_show($view) {
$exclude = variable_get('mediafront_block_playlist_exclude', array());
$playlists = variable_get('mediafront_block_playlist', '');
$enabled = is_array($playlists) ? isset($playlists[$view->name]) : $playlists == $view->name;
return $enabled && !in_array($view->current_display, $exclude);
}
function mediafront_views_query_alter(&$view, &$query) {
if (mediafront_block_show($view)) {
if (!isset($view->field['mediafront_nid'])) {
drupal_add_css('.media-nid-hidden { display:none; }', array(
'type' => 'inline',
));
$handler = views_get_handler('node', 'nid', 'field');
$handler
->set_default_options();
$handler->field_alias = 'mediafront_nid';
$handler->table = 'node';
$handler->field = 'nid';
$handler->real_field = 'nid';
$handler->options['label'] = '';
$handler->options['alter']['alter_text'] = 1;
$handler->options['alter']['text'] = '<div class="media-nid-hidden">[mediafront_nid]</div>';
$handler->options['id'] = 'mediafront_nid';
$handler
->init($view, $handler->options);
$query
->add_field('node', 'nid', 'mediafront_nid');
$view->field['mediafront_nid'] = $handler;
$view->display_handler->handlers['field']['mediafront_nid'] = $handler;
}
}
if ($view->style_plugin->definition['handler'] == 'mediafront_plugin_style_player') {
if (!isset($query->fields) || !in_array($view->base_field, array_keys($query->fields))) {
$query
->add_field($query->base_table, $view->base_field);
}
}
}
function mediafront_views_pre_render($view) {
if (mediafront_block_show($view)) {
$preset = variable_get('mediafront_block_preset', '');
$js = 'jQuery.media = jQuery.media ? jQuery.media : {};';
$js .= 'jQuery.media.nodes = jQuery.media.nodes ? jQuery.media.nodes : {};';
foreach ($view->result as &$result) {
$js .= 'jQuery.media.nodes["' . $result->nid . '"] = ' . drupal_json_encode(mediafront_invoke_node(node_load($result->nid), NULL)) . ';';
}
$view_id = '.view-' . drupal_clean_css_identifier($view->name);
$js .= 'jQuery.media.fieldSelector = "' . $view_id . ' .views-field-mediafront-nid";';
$js .= 'jQuery.media.playerId = "mediafront_' . $preset . '";';
drupal_add_js($js, 'inline');
drupal_add_js(drupal_get_path('module', 'mediafront') . '/js/mediafront.js');
}
}
function mediafront_get_extension($file) {
return drupal_strtolower(drupal_substr($file, strrpos($file, '.') + 1));
}
function mediafront_is_image($mimetype, $extension) {
$image = in_array($extension, array(
'jpg',
'jpeg',
'png',
'gif',
));
$image |= in_array($mimetype, array(
'image/jpeg',
'image/jpg',
'image/png',
'image/gif',
'jpg',
'jpeg',
'png',
'gif',
));
return $image;
}
function mediafront_is_media($mimetype, $extension) {
if (in_array(substr($mimetype, 0, 5), array(
'video',
'audio',
))) {
return true;
}
$media = in_array($extension, array(
'swf',
'mov',
'mp4',
'mp3',
'm4v',
'flv',
'f4v',
'ogg',
'oga',
'ogv',
'3g2',
'wav',
'aif',
'm4a',
'aac',
'wma',
'webm',
));
return $media;
}
function mediafront_array_to_object($array = array()) {
$object = new stdClass();
foreach ($array as $akey => $aval) {
$object->{$akey} = $aval;
}
return $object;
}
function mediafront_get_media_filepath($file) {
return isset($file->url) ? $file->url : (isset($file->path) ? $file->path : (!isset($file->filename) ? '' : (preg_match('/^http(s)?\\:\\/\\//', $file->uri) ? $file->uri : file_create_url($file->uri))));
}
function mediafront_get_media_file($file) {
$media_file = new stdClass();
$media_file->path = mediafront_get_media_filepath($file);
$media_file->extension = mediafront_get_extension($media_file->path);
if (isset($file->class) && isset($file->mediaType)) {
$media_file->class = $file->class;
$media_file->mediaType = $file->mediaType;
}
else {
if (mediafront_is_image($file->filemime, $media_file->extension)) {
$media_file->class = "images";
$media_file->mediaType = "image";
}
else {
if (mediafront_is_media($file->filemime, $media_file->extension)) {
$media_file->class = "media";
$media_file->mediaType = isset($file->mediaType) && $file->mediaType ? $file->mediaType : "media";
}
else {
$media_file->class = "";
$media_file->mediaType = "";
}
}
}
$media_file->filemime = isset($file->filemime) ? $file->filemime : '';
$media_file->player = isset($file->player) ? $file->player : '';
$media_file->bytesTotal = isset($file->filesize) ? $file->filesize : 0;
$media_file->duration = isset($file->duration) ? $file->duration : 0;
$media_file->width = isset($file->width) ? $file->width : 0;
$media_file->height = isset($file->height) ? $file->height : 0;
return $media_file;
}
function mediafront_add_media(&$additions, $file) {
$media_added = false;
if ($file) {
$file = is_array($file) ? mediafront_array_to_object($file) : $file;
if ($media_file = mediafront_get_media_file($file)) {
if ($media_file->class && $media_file->mediaType) {
if ($media_file->mediaType != "image" && isset($additions["mediafiles"][$media_file->class][$media_file->mediaType])) {
$media = $additions["mediafiles"][$media_file->class][$media_file->mediaType];
$files = is_array($media) ? $media : array(
$media,
);
array_push($files, $media_file);
$additions["mediafiles"][$media_file->class][$media_file->mediaType] = $files;
$media_added = true;
}
else {
$additions["mediafiles"][$media_file->class][$media_file->mediaType] = $media_file;
$media_added = true;
}
}
}
}
return $media_added;
}
function _mediafront_get_field($node, $field_name, $index = -1) {
$langs = array(
$node->language,
'und',
);
foreach ($langs as $lang) {
if (isset($node->{$field_name}) && is_array($node->{$field_name}) && isset($node->{$field_name}[$lang]) && is_array($node->{$field_name}[$lang]) && isset($node->{$field_name}[$lang][0])) {
if ($index >= 0) {
$field = (array) $node->{$field_name}[$lang][$index];
return $field;
}
else {
$fields = array();
foreach ($node->{$field_name}[$lang] as $index => $field) {
$fields[$index] = (array) $field;
}
return $fields;
}
}
}
return null;
}
function mediafront_get_media_preview($media) {
$media = (object) $media;
if (isset($media->type) && $media->type != 'image' && mediafront_is_media($media->type, mediafront_get_extension($media->filename))) {
$preview = media_get_thumbnail_preview((object) $media);
if ($preview) {
$preview['#file'] = (object) $preview['#file'];
$markup = drupal_render($preview);
$matches = array();
preg_match('/img.*src\\=\\"(.*)\\"/U', $markup, $matches);
if (isset($matches[1]) && mediafront_is_image('', mediafront_get_extension($matches[1]))) {
return $matches[1];
}
}
}
return '';
}
function mediafront_get_directories($module, $player_path) {
$directories = array();
$base_directory = getcwd() . '/';
$base_directory .= drupal_get_path('module', $module) . '/';
$base_directory .= $player_path;
if (is_dir($base_directory) && ($handle = opendir($base_directory))) {
while (FALSE !== ($directory = readdir($handle))) {
if (strpos($directory, '.') === FALSE && strtolower($directory) !== 'cvs' && is_dir($base_directory . '/' . $directory)) {
$directories[$directory] = $directory;
}
}
closedir($handle);
}
return $directories;
}
function mediafront_playlist_node($node) {
$media_added = false;
$additions = array();
$preview = '';
$field_additions = array();
if ($field = _mediafront_get_field($node, 'field_mediafront_media', 0)) {
$media_added = true;
$additions["mediafiles"]["media"]["media"] = $field['value'];
}
if ($field = _mediafront_get_field($node, 'field_mediafront_image', 0)) {
$media_added = true;
$additions["mediafiles"]["images"]["image"] = $field['value'];
}
$media_module = module_exists('media');
foreach ($node as $fieldname => $field) {
if ($field = _mediafront_get_field($node, $fieldname)) {
if (isset($field[0]['value']) && is_string($field[0]['value'])) {
$field_additions[$fieldname] = $field[0]['value'];
}
if (isset($field[0]['fid']) || isset($field[0]['filename'])) {
$media_field = mediafront_filefield_get('node', $node->type, $fieldname);
if (!$media_field || !empty($media_field->media_type) && $media_field->media_type != 'none' || !($media_field->node_preset == 'none' && $media_field->thumb_preset == 'none')) {
foreach ($field as $index => $instance) {
if ($instance['fid'] && !isset($instance['filename']) && !isset($instance['data'])) {
$media = file_load($instance['fid']);
$instance['data'] = (array) $media;
}
if ($media_module && isset($instance['fid'])) {
$media = isset($instance['data']) ? $instance['data'] : $instance;
$preview = mediafront_get_media_preview($media);
}
if (isset($instance['data']) && $instance['data']) {
$fileData = $instance['data'];
unset($instance['data']);
$file = array_merge($instance, $fileData);
}
else {
$file = $instance;
}
$file['mediaType'] = $media_field ? $media_field->media_type : '';
$media_added |= mediafront_add_media($additions, $file);
}
if ($media_field) {
$filename = $field[0]['filename'];
if ($media_field->node_preset && $media_field->node_preset != 'mediafront_original') {
$file['path'] = image_style_url($media_field->node_preset, $file['uri']);
$file['class'] = 'images';
$file['mediaType'] = 'preview';
$media_added |= mediafront_add_media($additions, $file);
}
if ($media_field->thumb_preset && $media_field->thumb_preset != 'mediafront_original') {
$file['path'] = image_style_url($media_field->thumb_preset, $file['uri']);
$file['class'] = 'images';
$file['mediaType'] = 'thumbnail';
$media_added |= mediafront_add_media($additions, $file);
}
}
}
}
}
}
$additions['nid'] = $node->nid;
$additions['title'] = $node->title;
if ($preview && (!isset($additions["mediafiles"]) || !isset($additions["mediafiles"]["images"]))) {
$additions["mediafiles"]["images"]["thumbnail"] = $preview;
$additions["mediafiles"]["images"]["preview"] = str_replace('square_thumbnail', 'large', $preview);
}
foreach ($field_additions as $field => $value) {
$additions[$field] = $value;
}
return $additions;
}
function mediafront_get_player() {
$args = func_get_args();
$params = $args[0];
if (gettype($params) == 'string') {
$preset = mediafront_get_preset($params);
$params = isset($args[1]) ? $preset['settings'] ? array_merge($preset['settings'], $args[1]) : $args[1] : $preset['settings'];
$params['preset'] = $preset['name'];
$params['player'] = $preset['player'];
$params['protocol'] = 'json';
$params['connect'] = isset($params['connect']) && $params['connect'] ? $params['connect'] : $preset['connect'];
$params['id'] = isset($params['id']) && $params['id'] ? $params['id'] : 'mediafront_' . $preset['name'];
}
if ($params && is_array($params)) {
$player = $params && $params['player'] ? $params['player'] : 'osmplayer';
$getPlayer = $player . '_get_player';
return function_exists($getPlayer) ? $getPlayer($params) : t('Player not available');
}
else {
return t('Player not available');
}
}