View source
<?php
function video_filter_field_field_info() {
return array(
'video_filter' => array(
'label' => t('Video Filter'),
'description' => t('This field stores URLs and settings to be ' . 'handled by the Video Filter module.'),
'settings' => array(),
'instance_settings' => array(
'max_height' => 400,
'max_width' => 400,
'autoplay' => 0,
),
'default_widget' => 'video_filter_field_default',
'default_formatter' => 'video_filter_field_default',
),
);
}
function video_filter_field_field_schema($field) {
if ($field['type'] == 'video_filter') {
$schema['columns']['url'] = array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
);
$schema['columns']['height'] = array(
'type' => 'int',
'not null' => FALSE,
);
$schema['columns']['width'] = array(
'type' => 'int',
'not null' => FALSE,
);
$schema['columns']['autoplay'] = array(
'type' => 'int',
'not null' => FALSE,
);
$schema['indexes'] = array(
'height' => array(
'height',
),
'width' => array(
'width',
),
);
return $schema;
}
}
function video_filter_field_field_is_empty($item, $field) {
if ($field['type'] == 'video_filter') {
if (empty($item['url']) && empty($item['height']) && empty($item['width']) && empty($item['autoplay'])) {
return TRUE;
}
else {
return FALSE;
}
}
}
function video_filter_field_field_validate($obj_type, $object, $field, $instance, $langcode, &$items, &$errors) {
if ($field['type'] == 'video_filter') {
foreach ($items as $delta => $item) {
if (!empty($item['url'])) {
if (empty($item['height']) || !is_numeric($item['height'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'video_filter_height',
'message' => t('Video Filter Field: Invalid video height'),
);
}
if (empty($item['width']) || !is_numeric($item['width'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'video_filter_width',
'message' => t('Video Filter Field: Invalid video width'),
);
}
}
else {
unset($items);
unset($object->field_media_url);
}
}
}
}
function video_filter_field_field_widget_info() {
return array(
'video_filter' => array(
'label' => t('Video Filter'),
'description' => t('Enable user to determine height, width, and autoplay settings'),
'field types' => array(
'video_filter',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
function video_filter_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$base = $element;
if ($instance['widget']['type'] == 'video_filter') {
$element['url'] = array(
'#type' => 'textfield',
'#title' => t('Video URL'),
'#default_value' => isset($items[$delta]['url']) ? $items[$delta]['url'] : NULL,
'#weight' => 0,
) + $base;
$element['height'] = array(
'#type' => 'textfield',
'#title' => t('Height'),
'#default_value' => isset($items[$delta]['height']) ? $items[$delta]['height'] : NULL,
'#weight' => 1,
) + $base;
$element['width'] = array(
'#type' => 'textfield',
'#title' => t('Width'),
'#default_value' => isset($items[$delta]['width']) ? $items[$delta]['width'] : NULL,
'#weight' => 2,
) + $base;
$element['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#default_value' => isset($items[$delta]['autoplay']) ? $items[$delta]['autoplay'] : FALSE,
'#weight' => 3,
) + $base;
}
return $element;
}
function video_filter_field_field_formatter_info() {
return array(
'video_filter_field_default' => array(
'label' => t('Video Filter'),
'field types' => array(
'text',
'video_filter',
),
),
);
}
function video_filter_field_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
$elements = array();
foreach ($items as $delta => $item) {
$url = url($item['url']);
$text = "[video:{$url}]";
$elements[$delta] = array(
'#theme' => 'video_filter_field_default_formatter',
'#item' => video_filter_field_process($text),
);
}
return $elements;
}
function video_filter_field_theme() {
return array(
'video_filter_field_default_formatter' => array(
'variables' => array(
'item' => NULL,
),
),
);
}
function theme_video_filter_field_default_formatter($variables) {
$output = '<div class="video-filter-field">' . $variables['item'] . '</div>';
return $output;
}
function video_filter_field_process($text) {
$filter = new stdClass();
$filter->status = 1;
$filter->settings = array(
'video_filter_width' => 400,
'video_filter_height' => 400,
'video_filter_autoplay' => 0,
'video_filter_related' => 1,
);
$text = (string) $text;
if (preg_match_all('/\\[video(\\:(.+))?( .+)?\\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$video = array(
'source' => $matches_code[2][$ci],
'autoplay' => $filter->settings['video_filter_autoplay'],
'related' => $filter->settings['video_filter_related'],
);
if (strstr($video['source'], ',')) {
$sources = explode(',', $video['source']);
$random = array_rand($sources, 1);
$video['source'] = $sources[$random];
}
$codecs = module_invoke_all('codec_info');
foreach ($codecs as $codec_name => $codec) {
if (!is_array($codec['regexp'])) {
$codec['regexp'] = array(
$codec['regexp'],
);
}
foreach ($codec['regexp'] as $delta => $regexp) {
if (preg_match($regexp, $video['source'], $matches)) {
$video['codec'] = $codec;
$video['codec']['delta'] = $delta;
$video['codec']['matches'] = $matches;
$video['codec']['codec_name'] = $codec_name;
$video['codec']['control_bar_height'] = 0;
break 2;
}
}
}
if (isset($video['codec'])) {
if ($matches_code[3][$ci] && preg_match_all('/\\s+([a-zA-Z_]+)\\:(\\s+)?([0-9a-zA-Z\\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
$ratio = 1;
if (isset($video['ratio']) && preg_match('/(\\d+)\\/(\\d+)/', $video['ratio'], $tratio)) {
$ratio = $tratio[1] / $tratio[2];
}
else {
$ratio = $video['codec']['ratio'];
}
if (isset($video['width']) && !isset($video['height'])) {
$video['height'] = variable_get('video_filter_height_' . $format, 400);
}
elseif (isset($video['height']) && !isset($video['width'])) {
$video['width'] = $video['height'] * $ratio;
}
elseif (isset($video['height']) && isset($video['width'])) {
$video['width'] = $video['width'];
$video['height'] = $video['height'];
}
elseif (!isset($video['height']) && !isset($video['width'])) {
$video['width'] = $filter->settings['video_filter_width'];
$video['height'] = $filter->settings['video_filter_height'];
}
$control_bar_height = 0;
if (isset($video['control_bar_height'])) {
$control_bar_height = $video['control_bar_height'];
}
elseif (isset($video['codec']['control_bar_height'])) {
$control_bar_height = $video['codec']['control_bar_height'];
}
if ($ratio) {
$scale_factor = min(array(
$video['height'] - $control_bar_height,
$video['width'] / $ratio,
));
$video['height'] = round($scale_factor + $control_bar_height);
$video['width'] = round($scale_factor * $ratio);
}
$video['autoplay'] = (bool) $video['autoplay'];
$video['align'] = isset($video['align']) && in_array($video['align'], array(
'left',
'right',
'center',
)) ? $video['align'] : NULL;
if (is_callable($video['codec']['callback'], FALSE)) {
$replacement = call_user_func($video['codec']['callback'], $video);
}
else {
$replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
}
}
else {
$replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
}
$text = str_replace($code, $replacement, $text);
}
}
return $text;
}
function video_filter_field_url_is_supported($url) {
$is_supported = FALSE;
module_load_include('inc', 'video_filter', 'video_filter.codecs');
$codecs = module_invoke_all('codec_info');
foreach ($codecs as $codec_name => $codec) {
if ($is_supported == TRUE) {
break;
}
if (!is_array($codec['regexp'])) {
$codec['regexp'] = array(
$codec['regexp'],
);
}
foreach ($codec['regexp'] as $delta => $regexp) {
if (preg_match($regexp, $url)) {
$is_supported = TRUE;
break;
}
}
}
return $is_supported;
}