View source
<?php
include_once dirname(__FILE__) . '/uploadfield_file.inc';
include_once dirname(__FILE__) . '/uploadfield_widget.inc';
function uploadfield_init() {
drupal_add_css(drupal_get_path('module', 'video') . '/types/uploadfield/uploadfield.css');
if (!module_exists('filefield')) {
module_disable(array(
'uploadfield',
));
drupal_set_message(t('The uploadfield module has been disabled. The <a href="http://drupal.org/project/filefield">FileField module</a> needs to be installed for it to work properly.'));
return;
}
}
function uploadfield_theme() {
$theme = array();
$theme = array(
'uploadfield_image' => array(
'arguments' => array(
'file' => NULL,
'alt' => '',
'title' => '',
'attributes' => NULL,
'getsize' => TRUE,
),
),
'uploadfield_item' => array(
'arguments' => array(
'item' => NULL,
),
),
'uploadfield_widget' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'uploadfield_widget.inc',
),
'uploadfield_widget_preview' => array(
'arguments' => array(
'item' => NULL,
),
),
'uploadfield_widget_item' => array(
'arguments' => array(
'element' => NULL,
),
),
'uploadfield_admin_thumbnail' => array(
'arguments' => array(
'item' => NULL,
),
),
'uploadfield_widget_video_thumb' => array(
'arguments' => array(
'item' => NULL,
),
),
'uploadfield_formatter_video_plain' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'uploadfield_formatter.inc',
),
'uploadfield_formatter_video_nodelink' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'uploadfield_formatter.inc',
),
'uploadfield_formatter_video_imagelink' => array(
'arguments' => array(
'element' => NULL,
),
'file' => 'uploadfield_formatter.inc',
),
'video_format_play' => array(
'arguments' => array(
'output' => NULL,
'url' => NULL,
'title' => NULL,
'link_text' => NULL,
),
),
'video_play_dcr' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_divx' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_flash' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_ogg_theora' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_quicktime' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_realmedia' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_swf' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_play_windowsmedia' => array(
'arguments' => array(
'node' => NULL,
),
),
'video_encoding_failed' => array(
'arguments' => array(),
),
'video_inprogress' => array(
'arguments' => array(),
),
);
if (module_exists('imagecache')) {
foreach (imagecache_presets() as $preset) {
$theme['uploadfield_formatter_' . $preset['presetname'] . '_videolinked'] = array(
'arguments' => array(
'element' => NULL,
),
'function' => 'theme_uploadfield_formatter_videolinked',
'file' => 'uploadfield_formatter.inc',
);
}
}
return $theme;
}
function uploadfield_elements() {
$elements = array();
$filefield_elements = module_invoke('filefield', 'elements');
$elements['uploadfield_widget'] = $filefield_elements['filefield_widget'];
$elements['uploadfield_widget']['#process'][] = 'uploadfield_widget_process';
$elements['uploadfield_widget']['#element_validate'][] = 'uploadfield_widget_validate';
$elements['uploadfield_widget']['#value_callback'] = 'uploadfield_widget_value';
return $elements;
}
function uploadfield_file_download($filepath) {
if (strpos($filepath, 'video_thumbs') !== FALSE) {
$full_path = file_create_path($filepath);
if ($info = getimagesize($full_path)) {
return array(
'Content-Type: ' . $info['mime'],
'Content-Length: ' . filesize($full_path),
);
}
}
}
function uploadfield_widget_info() {
$module_path = drupal_get_path('module', 'uploadfield');
return array(
'uploadfield_widget' => array(
'label' => t('Video'),
'field types' => array(
'filefield',
),
'multiple values' => CONTENT_HANDLE_CORE,
'callbacks' => array(
'default value' => CONTENT_CALLBACK_CUSTOM,
),
'description' => t('An edit widget for video files, including video thumbnails and transcoding to flash.'),
),
);
}
function uploadfield_widget_settings($op, $widget) {
switch ($op) {
case 'form':
return uploadfield_widget_settings_form($widget);
case 'validate':
return uploadfield_widget_settings_validate($widget);
case 'save':
return uploadfield_widget_settings_save($widget);
}
}
function uploadfield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
if (empty($items[$delta])) {
$items[$delta] = array(
'alt' => '',
'title' => '',
);
}
$element = filefield_widget($form, $form_state, $field, $items, $delta);
return $element;
}
function uploadfield_widget_upload_validators($field) {
$validators = array();
$web_extensions = array(
'mov',
'mp4',
'3gp',
'3g2',
'mpg',
'mpeg',
'divx',
'rm',
'flv',
'f4v',
'swf',
'dir',
'dcr',
'asf',
'wmv',
'avi',
'mpg',
'mpeg',
'ogg',
);
$extensions = array_filter(explode(' ', $field['widget']['file_extensions']));
if (empty($extensions)) {
$extensions = $web_extensions;
}
$validators['filefield_validate_extensions'][0] = implode(' ', array_intersect($extensions, $web_extensions));
return $validators;
}
function uploadfield_field_formatter_info() {
$module_path = drupal_get_path('module', 'uploadfield');
$formatters = array(
'video_plain' => array(
'label' => t('video'),
'field types' => array(
'filefield',
),
'description' => t('Displays video files with player embedded.'),
),
'video_nodelink' => array(
'label' => t('video thumbnail linked to video'),
'field types' => array(
'filefield',
),
'description' => t('Displays video thumb files then the video.'),
),
);
if (module_exists('imagecache')) {
foreach (imagecache_presets() as $preset) {
$formatters[$preset['presetname'] . '_videolinked'] = array(
'label' => t('Preset @preset of video thumbnail linked to video', array(
'@preset' => $preset['presetname'],
)),
'field types' => array(
'filefield',
),
);
}
}
return $formatters;
}
function uploadfield_default_value(&$form, &$form_state, $field, $delta) {
return filefield_default_value($form, $form_state, $field, $delta);
}
function uploadfield_form_content_field_overview_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'uploadfield_form_content_field_overview_submit';
}
function uploadfield_form_content_field_overview_submit(&$form, &$form_state) {
if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
$new_field = $form_state['fields_added']['_add_new_field'];
$node_type = $form['#type_name'];
$field = content_fields($new_field, $node_type);
if ($field['widget']['module'] == 'uploadfield') {
foreach ($field['display_settings'] as $display_type => $display_settings) {
if ($field['display_settings'][$display_type]['format'] == 'default') {
$field['display_settings'][$display_type]['format'] = 'video_plain';
}
}
content_field_instance_update($field);
}
}
}
function theme_uploadfield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
$file = (array) $file;
if (!is_file($file['filepath'])) {
return '<!-- File not found: ' . $file['filepath'] . ' -->';
}
if ($getsize) {
if (!empty($file['data']['width']) && !empty($file['data']['height'])) {
$attributes['width'] = $file['data']['width'];
$attributes['height'] = $file['data']['height'];
}
elseif (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])) {
$attributes['width'] = $width;
$attributes['height'] = $height;
}
}
if (!empty($title)) {
$attributes['title'] = $title;
}
$attributes['alt'] = $alt;
$query_string = '';
if (isset($file['timestamp'])) {
$query_character = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PRIVATE && variable_get('clean_url', '0') == '0' ? '&' : '?';
$query_string = $query_character . $file['timestamp'];
}
$url = file_create_url($file['filepath']) . $query_string;
$attributes['src'] = $url;
$attributes = drupal_attributes($attributes);
return '<span></span><img ' . $attributes . ' />';
}
function theme_uploadfield_item($item) {
return theme('uploadfield_image', $item, $item['alt'], $item['title']);
}
function theme_uploadfield_widget_preview($item = NULL) {
return '<div class="uploadfield-preview">' . theme('uploadfield_admin_thumbnail', $item) . '</div>';
}
function theme_uploadfield_widget_item($element) {
return theme('filefield_widget_item', $element);
}
function theme_uploadfield_admin_thumbnail($item = NULL) {
if (is_null($item) || empty($item['filepath'])) {
return '<!-- link to default admin thumb -->';
}
$thumb_path = uploadfield_file_admin_thumb_path($item);
return '<img src="' . file_create_url($thumb_path) . '" title="' . check_plain($item['filename']) . '" />';
}
function theme_uploadfield_widget_video_thumb($item = NULL) {
return '<div class="uploadfield-video-thumb">' . theme('uploadfield_image', $item, '', '', '', FALSE) . '</div>';
}
function uploadfield_v_perm() {
return array(
'use default thumbnail',
'bypass conversion video',
);
}
function video_upload_v_help() {
$help = array();
$help['upload']['data'] = '<b>' . t('Upload support') . '</b>';
$help['upload']['children'] = array(
t('You can upload a video file from your computer to this website.'),
);
return $help;
}
function uploadfield_v_auto_resolution($vidfile) {
module_load_include('inc', 'video', '/plugins/ffmpeg');
if (module_hook('ffmpeg', 'auto_playtime')) {
return ffmpeg_auto_playtime($vidfile);
}
return false;
}
function uploadfield_v_auto_playtime($vidfile) {
module_load_include('inc', 'video', '/plugins/ffmpeg');
if (module_hook('ffmpeg', 'auto_playtime')) {
return ffmpeg_auto_playtime($vidfile);
}
return false;
}
function uploadfield_v_video($op, &$element) {
global $user;
switch ($op) {
case 'insert':
case 'update':
$field = content_fields($element['#field_name'], $element['#type_name']);
if ($field['widget']['autothumbnail']) {
$update = NULL;
$vidfile = $element['#value'];
$vid = $vidfile['fid'];
$vid_path = $vidfile['data']['video_thumb'];
$vid_dir = explode('/', $vid_path);
$vid_name = $vid_dir[count($vid_dir) - 1];
$file = new stdClass();
$file->uid = $user->uid;
$file->status = FILE_STATUS_PERMANENT;
$file->filename = $vid_name;
$file->filepath = $vid_path;
$file->filemime = file_get_mimetype($vid_name);
$file->filesize = filesize($vid_path);
$file->timestamp = time();
if ($op == 'update') {
$old_fid = $element['#default_value']['data']['video_thumb_fid'];
$update = 'fid';
$file->fid = $old_fid;
}
drupal_write_record('files', $file, $update);
if ($op == 'insert') {
$file->fid = db_last_insert_id('files', 'fid');
}
$element['data']['video_thumb_fid'] = array(
'#type' => 'value',
'#value' => $file->fid,
);
}
if ($field['widget']['autoconversion'] && !$element['#value']['data']['bypass_autoconversion']) {
video_auto_transcode_add_to_queue($element, $op);
}
break;
case 'delete':
break;
case 'thumbs':
include_once dirname(__FILE__) . '/uploadfield_thumb.inc';
video_auto_thumb_process($element);
break;
case 'convert':
include_once dirname(__FILE__) . '/uploadfield_convert.inc';
break;
}
}