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.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 = array();
if (module_exists('views')) {
$items['mediafront_getplaylist'] = array(
'page callback' => 'mediafront_get_playlist_json',
'type' => MENU_CALLBACK,
'page arguments' => array(
1,
2,
3,
),
'access arguments' => array(
'access content',
),
);
}
$items = array_merge($items, mediafront_preset_menu());
return $items;
}
function mediafront_modules_enabled($modules) {
if (in_array('views', $modules)) {
variable_set('menu_rebuild_needed', TRUE);
}
}
function mediafront_modules_disabled($modules) {
if (in_array('views', $modules)) {
variable_set('menu_rebuild_needed', TRUE);
}
}
function mediafront_theme() {
$theme = array();
$theme['mediafront_player'] = array(
'render element' => 'element',
'arguments' => array(
'element' => NULL,
),
);
return $theme;
}
function theme_mediafront_player($variables) {
$variables = !empty($variables['element']) ? array_shift($variables) : $variables;
$entity = $variables['#entity'];
if (empty($variables['#fields'])) {
return '';
}
$params = array(
'node' => mediafront_get_node('node', $entity, $variables['#fields']),
);
$params['disablePlaylist'] = true;
$params['variables'] = $variables;
return mediafront_get_player($variables['#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',
),
'mediafront_views' => array(
'name' => t('MediaFront Views Options'),
'default_hook' => 'mediafront_views_default_options',
'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_views_default_options() {
$default_options =& drupal_static(__FUNCTION__);
if ($default_options) {
return $default_options;
}
else {
$default_options = module_invoke_all('mediafront_views_default_options');
drupal_alter('mediafront_views_default_options', $default_options);
return $default_options;
}
}
function mediafront_views_get_options($handler = NULL) {
$options = variable_get('mediafront_views_options', array());
foreach (mediafront_views_default_options() as $view => $fields_options) {
if (!isset($options[$view])) {
$options[$view] = $fields_options;
}
else {
foreach ($fields_options as $field_name => $field_options) {
if (!isset($options[$view][$field_name])) {
$options[$view][$field_name] = $field_options;
}
}
}
}
if (!$handler || empty($handler->options['mediafront'])) {
return $options;
}
else {
if ($options && !empty($options[$handler->view->name][$handler->field])) {
return $options[$handler->view->name][$handler->field];
}
else {
return $handler->options['mediafront'];
}
}
}
function mediafront_views_set_options(&$handler, $options) {
$handler->options['mediafront'] = $options;
$settings = mediafront_views_get_options();
$settings[$handler->view->name][$handler->field] = $options;
variable_set('mediafront_views_options', $settings);
}
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) {
$settings = array();
$get_params = $player . '_get_player_params';
if (function_exists($get_params)) {
$settings = $get_params();
}
drupal_alter('mediafront_player_params', $settings);
return $settings;
}
function mediafront_get_player_settings($player) {
$settings = array();
$get_settings = $player . '_get_player_settings';
if (function_exists($get_settings)) {
$settings = $get_settings();
}
drupal_alter('mediafront_player_settings', $settings);
return $settings;
}
function mediafront_get_settings($player, $overrides = array()) {
$settings = array_merge(mediafront_get_player_params($player), mediafront_get_player_settings($player));
return $overrides ? array_merge($settings, $overrides) : $settings;
}
function mediafront_get_playlist_json($playlist = null, $limit = null, $start = null) {
$playlist = $playlist ? check_plain($playlist) : '';
$playlist = !empty($_GET['playlist']) ? check_plain($_GET['playlist']) : $playlist;
$limit = $limit ? check_plain($limit) : 10;
$limit = !empty($_GET['max-results']) ? check_plain($_GET['max-results']) : $limit;
$start = $start ? check_plain($start) : 0;
$start = !empty($_GET['start-index']) ? check_plain($_GET['start-index']) : $start;
drupal_json_output(mediafront_get_playlist($playlist, $limit, $start));
}
function mediafront_get_playlist($playlist, $limit = 10, $start = 0) {
$view = views_get_view($playlist);
if (empty($view)) {
return array(
'error' => 'Playlist not found.',
);
}
if (!$view
->access('default')) {
return array(
'error' => 'Access Denied.',
);
}
if (method_exists($view, 'set_use_pager')) {
$view
->set_use_pager(FALSE);
}
else {
$view->display_handler
->set_option('pager', array(
'type' => 'some',
'options' => $view->display_handler->options['pager']['options'],
));
}
$view
->set_items_per_page($limit);
$view
->set_offset($start);
$view->get_total_rows = TRUE;
$view
->execute();
return mediafront_get_playlist_from_view($view, $limit, $start);
}
function mediafront_get_playlist_from_view($view, $limit = 10, $start = 0) {
$playlist = array();
$playlist['nodes'] = array();
$playlist['name'] = $view->name;
$endpoint = 'mediafront_getplaylist/' . $view->name;
$playlist['endpoint'] = url($endpoint, array(
'absolute' => TRUE,
));
$total = !empty($view->total_rows) ? $view->total_rows : (isset($view->query->pager) ? $view->query->pager
->get_total_items() : 0);
$total = empty($total) ? count($view->result) : $total;
$total += $start;
$playlist['total_rows'] = $total;
$fields = array();
foreach ($view->field as $name => $field) {
$options = mediafront_views_get_options($field);
if (!empty($options['field_type'])) {
$fields[$field->field] = array(
'type' => $options['field_type'],
'field' => $field,
'options' => $options,
);
}
}
if ($fields) {
foreach ($view->result as $row) {
if ($node = mediafront_get_node('view', $row, $fields)) {
if (!empty($row->nid)) {
$node['nid'] = $row->nid;
}
$playlist['nodes'][] = $node;
}
}
}
return $playlist;
}
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_field_form($options, $id_prefix = '') {
ctools_include('dependent');
$field_types = array(
0 => 'None',
'title' => 'Title',
'media' => 'Media',
'image' => 'Image',
'stream' => 'Stream',
'custom' => 'Custom',
);
$type = !empty($options['field_type']) ? $options['field_type'] : 0;
$form['field_type'] = array(
'#type' => 'select',
'#title' => 'Field Type',
'#description' => t('Select the way this field should be handled by a MediaFront player.'),
'#options' => $field_types,
'#default_value' => $type,
);
$media_type = !empty($options['media_type']) ? $options['media_type'] : 'media';
$form['media_type'] = mediafront_media_field_form($media_type);
$form['media_type']['#process'] = array(
'ctools_dependent_process',
);
$form['media_type']['#dependency'] = array(
$id_prefix . 'field-type' => array(
'media',
),
);
$preview = !empty($options['preview']) ? $options['preview'] : 0;
$thumb = !empty($options['thumbnail']) ? $options['thumbnail'] : 0;
$form = array_merge($form, mediafront_preview_field_form($preview, $thumb));
$form['preview']['#process'] = array(
'ctools_dependent_process',
);
$form['preview']['#dependency'] = array(
$id_prefix . 'field-type' => array(
'image',
),
);
$form['thumbnail']['#process'] = array(
'ctools_dependent_process',
);
$form['thumbnail']['#dependency'] = array(
$id_prefix . 'field-type' => array(
'image',
),
);
$custom = !empty($options['custom']) ? $options['custom'] : "";
$form['custom'] = array(
'#type' => 'textfield',
'#title' => 'Field Name',
'#description' => 'Enter the machine name for the field you would like to bring into the media player.',
'#default_value' => $custom,
'#process' => array(
'ctools_dependent_process',
),
'#dependency' => array(
$id_prefix . 'field-type' => array(
'custom',
),
),
);
$form['always_show_player'] = array(
'#type' => 'checkbox',
'#title' => t('Always show player for any media attached to this entity'),
'#default_value' => !empty($options['always_show_player']) ? $options['always_show_player'] : 0,
);
return $form;
}
function mediafront_media_field_form($default = 'media') {
$description = t('Select how you would like to use this file field for MediaFront.');
$description .= '<br/>';
$description .= '<ul>';
$description .= '<li><strong>' . t('Introduction') . '</strong>: ' . t('This selection will make this file field be used as an introduction, before the commercial') . '</li>';
$description .= '<li><strong>' . t('Commercial') . '</strong>: ' . t('To use this file field as the commercial for this media type') . '</li>';
$description .= '<li><strong>' . t('Pre-Reel') . '</strong>: ' . t('The pre-reel is shown right before the main media content, but after the commercial.') . '</li>';
$description .= '<li><strong>' . t('Media Content') . '</strong>: ' . t('The media content is the main media piece to be played to the user.') . '</li>';
$description .= '<li><strong>' . t('Post-Reel') . '</strong>: ' . t('The post-reel is shown right after the main media content.') . '</li>';
$description .= '</ul>';
return array(
'#type' => 'select',
'#title' => t('Media Type'),
'#description' => $description,
'#default_value' => $default,
'#options' => array(
0 => t('None'),
'intro' => t('Introduction'),
'commercial' => t('Commercial'),
'prereel' => t('Pre-Reel'),
'media' => t('Media Content'),
'postreel' => t('Post-Reel'),
),
);
}
function mediafront_preview_field_form($preview_default = 'mediafront_original', $thumb_default = 'mediafront_original') {
$form = array();
$styles = image_styles();
$options = array();
$options[0] = t('None');
$options['mediafront_original'] = t('Original Image');
foreach ($styles as $name => $style) {
$options[$name] = $style['name'];
}
$form['preview'] = array(
'#type' => 'select',
'#title' => t('Preview Style'),
'#description' => t('Select the Image Style to be used when this field is loaded as a preview in a MediaFront player.'),
'#options' => $options,
'#default_value' => $preview_default,
);
$form['thumbnail'] = array(
'#type' => 'select',
'#title' => t('Playlist Style'),
'#description' => t('Select the Image Style to be used when this field is used as a thumbnail in a MediaFront playlist.'),
'#options' => $options,
'#default_value' => $thumb_default,
);
return $form;
}
function mediafront_get_media_preview($media) {
if ($media->class == 'media' && module_exists('media') && !empty($media->file) && !empty($media->file->type)) {
$preview = media_get_thumbnail_preview($media->file);
if ($preview) {
$preview['#file'] = (object) $preview['#file'];
$markup = drupal_render($preview);
$matches = array();
preg_match('/img.*src\\=\\"(.*)\\"/U', $markup, $matches);
if (!empty($matches[1])) {
$preview = new MediaFile($matches[1]);
if ($preview->class == 'image') {
return $preview;
}
}
}
}
return '';
}
function mediafront_get_node_field_instance_value($value) {
if (!empty($value['value'])) {
return $value['value'];
}
else {
if (!empty($value['fid'])) {
if (!empty($value['data'])) {
return $value['data'];
}
else {
if (!empty($value['filename'])) {
return $value;
}
else {
return file_load($value['fid']);
}
}
}
else {
return $value;
}
}
}
function mediafront_get_node_field_value($entity, $field) {
$values = array();
$field_name = $field['field'];
if (isset($entity->{$field_name})) {
$type = gettype($entity->{$field_name});
if ($type != 'array') {
$values[] = $entity->{$field_name};
}
elseif ($items = field_get_items('node', $entity, $field_name)) {
foreach ($items as $item) {
if ($value = mediafront_get_node_field_instance_value($item)) {
$values[] = $value;
}
}
}
}
return $values;
}
function mediafront_get_view_field_value($row, $field) {
$value = $field['field']
->get_value($row);
if (!$value) {
$field_name = $field['field']->handler_type . '_' . $field['field']->field;
$offset = $field['field']->options['delta_offset'];
$value = !empty($row->{$field_name}[$offset]['raw']) ? $row->{$field_name}[$offset]['raw'] : array();
}
$values = !$value || is_array($value) && !empty($value[0]) ? $value : array(
$value,
);
foreach ($values as $key => $value) {
if (is_array($value)) {
$values[$key] = mediafront_get_node_field_instance_value($value);
}
}
return $values;
}
function mediafront_get_field_value($type, $entity, $field) {
$func = 'mediafront_get_' . $type . '_field_value';
return $func($entity, $field);
}
function mediafront_get_node($type, $entity, $fields) {
$node = array();
if (is_array($fields)) {
$stream = '';
foreach ($fields as $field) {
$options = $field['options'];
$values = mediafront_get_field_value($type, $entity, $field);
if (!empty($values)) {
if ($field['type'] == 'media') {
foreach ($values as $value) {
$node['mediafiles']['media'][$options['media_type']][] = new MediaFile($value);
}
}
else {
if ($field['type'] == 'image') {
foreach (array(
'thumbnail',
'preview',
) as $image_type) {
if ($options[$image_type] == 'mediafront_original') {
$file = new MediaFile($values[0]);
$node['mediafiles']['image']['image'] = $file;
}
else {
if (!empty($options[$image_type])) {
$file = new MediaFile($values[0]);
if (empty($file->path)) {
$file->path = image_style_url($options[$image_type], $file->file->uri);
}
$node["mediafiles"]['image'][$image_type] = $file;
}
}
}
}
else {
if ($field['type'] == 'title') {
$node['title'] = is_array($values[0]) ? $values[0]['safe_value'] : $values[0];
}
else {
if ($field['type'] == 'custom') {
$node[$options['custom']] = is_array($values[0]) ? $values[0]['safe_value'] : $values[0];
}
else {
if ($field['type'] == 'stream') {
$stream = $values[0];
}
else {
$node[$field['field']] = is_array($values[0]) ? $values[0]['safe_value'] : $values[0];
}
}
}
}
}
}
}
if ($stream && !empty($node['mediafiles']['media']['media'][0])) {
$node['mediafiles']['media']['media'][0]->stream = $stream;
}
if ($type == 'node' && !empty($entity->title)) {
$node['title'] = $entity->title;
}
if ($type == 'node' && !empty($entity->nid)) {
$node['nid'] = $entity->nid;
}
if (!empty($node['mediafiles']['media']) && empty($node['mediafiles']['image'])) {
if ($image = mediafront_get_media_preview($node['mediafiles']['media']['media'][0])) {
$node['mediafiles']['image']['image'] = $image;
}
}
}
else {
if (!empty($entity->fid)) {
$node['mediafiles']['media']['media'] = new MediaFile($entity);
$node['title'] = $entity->filename;
}
}
drupal_alter('mediafront_node', $entity, $fields, $node, $type);
return $node;
}
function mediafront_token_info() {
$type = array(
'name' => t('MediaFront'),
'description' => t('Adds mediafront players using tokens.'),
);
$presets = mediafront_preset_get_presets();
if ($presets) {
foreach ($presets as $preset) {
$mediafront[$preset['name']] = array(
'name' => 'MediaFront ' . $preset['name'] . ' preset',
'description' => $preset['description'],
);
}
return array(
'types' => array(
'mediafront' => $type,
),
'tokens' => array(
'mediafront' => $mediafront,
),
);
}
return array();
}
function mediafront_tokens($type, $tokens, array $data = array(), array $options = array()) {
$players =& drupal_static(__FUNCTION__);
$replacements = array();
if ($type == 'mediafront') {
foreach ($tokens as $name => $original) {
if (!isset($players[$name])) {
$players[$name] = mediafront_get_player($name);
}
$replacements[$original] = $players[$name];
}
}
return $replacements;
}
function mediafront_get_preset_params($preset, $params = array()) {
$players =& drupal_static(__FUNCTION__);
$preset = mediafront_get_preset($preset, is_array($params) && !empty($params['admin']));
$params = !empty($params) ? !empty($preset['settings']) ? array_merge($preset['settings'], $params) : $params : $preset['settings'];
$params['preset'] = $preset['name'];
$params['player'] = $preset['player'];
$params['protocol'] = 'json';
$params['connect'] = !empty($params['connect']) ? $params['connect'] : (!empty($preset['connect']) ? $preset['connect'] : array());
if (empty($params['id'])) {
$params['id'] = 'mediafront_' . $preset['name'];
$i = 1;
$id = $params['id'];
while (isset($players[$id])) {
$id = $params['id'] . '_' . $i++;
}
$params['id'] = $id;
}
$players[$params['id']] = $params['id'];
return $params;
}
function mediafront_get_player() {
$args = func_get_args();
$params = $args[0];
if (gettype($params) == 'string') {
$preset = $params;
$params = !empty($args[1]) ? $args[1] : array();
$params = mediafront_get_preset_params($preset, $params);
}
if ($params && is_array($params)) {
drupal_alter('mediafront_settings', $params);
$params = mediafront_get_settings($params['player'], $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');
}
}