123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- <?php
- /**
- * @file video_filter_field.module
- */
- /**
- * Implements hook_field_info().
- */
- 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',
- ),
- );
- }
- /**
- * Implements magic callback / psuedo-hook mymodule_field_schema().
- */
- 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;
- }
- }
- /**
- * Implements magic callback / psuedo-hook mymodule_field_is_empty().
- */
- 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;
- }
- }
- }
- /**
- * Implements magic callback / psuedo-hook mymodule_field_settings_form().
- */
- /*
- function video_filter_field_settings_form($field, $instance, $has_data) {
- if ($field['type'] == 'video_filter') {
- $settings = $field['setings'];
- $form = '';
- return $form;
- }
- }
- // */
- /**
- * Implements hook_field_validate().
- */
- function video_filter_field_field_validate($obj_type, $object, $field,
- $instance, $langcode, &$items,
- &$errors) {
- if ($field['type'] == 'video_filter') {
- foreach ($items as $delta => $item) {
- // Test URL.
- if (!empty($item['url'])) {
- //$errors[$field['field_name']][$langcode][$delta][] = array(
- // 'error' => 'video_filter_url',
- // 'message' => t('Video Filter Field: Invalid video URL'),
- //);
- // If URL isn't empty, we need a height.
- 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 URL isn't empty, we need a width.
- 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'),
- );
- }
- }
- // URL field is empty. Set everything else to empty.
- else {
- unset($items);
- unset($object->field_media_url);
- }
- }
- }
- }
- /**
- * Implements hook_field_widget_info().
- */
- 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,
- ),
- ),
- );
- }
- /**
- * Implements hook_field_widget_form().
- */
- function video_filter_field_field_widget_form(&$form, &$form_state, $field,
- $instance, $langcode, $items,
- $delta, $element) {
- $base = $element;
- //dsm($element);
- //dsm($instance,' instance');
-
- 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;
- }
- /**
- * Implements hook_field_formatter_info().
- */
- function video_filter_field_field_formatter_info() {
- return array(
- 'video_filter_field_default' => array(
- 'label' => t('Video Filter'),
- 'field types' => array('text', 'video_filter'),
- ),
- );
- }
- /**
- * Implements hook_field_formatter_view().
- */
- function video_filter_field_field_formatter_view($obj_type, $object, $field,
- $instance, $langcode, $items,
- $display) {
- /*
- dsm($obj_type, 'obj_type');
- dsm($object, 'object');
- dsm($field, 'field');
- dsm($instance,'instance');
- dsm($langcode,'langcode');
- dsm($display,'display');
- dsm($items,'items');
- // */
- $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, $item),
- );
- }
- return $elements;
- }
- /**
- * Implements hook_theme().
- */
- function video_filter_field_theme() {
- return array(
- 'video_filter_field_default_formatter' => array(
- 'variables' => array('item' => NULL),
- ),
- );
-
- }
- /**
- * Returns HTML for a video_filter_field_formatter
- */
- function theme_video_filter_field_default_formatter($variables) {
- $output = '<div class="video-filter-field">' . $variables['item'] . '</div>';
- return $output;
- }
- /**
- * Implement video_filter processing from video_filter module.
- *
- * @see _video_filter_process()
- */
- function video_filter_field_process($text, $video) {
- $filter = new stdClass();
- //$filter->format = '';
- //$filter->module = 'video_filter';
- //$filter->name = 'video_filter';
- //$filter->weight = -1;
- $filter->status = 1;
- $filter->settings = array(
- 'video_filter_width' => 400,
- 'video_filter_height' => 400,
- 'video_filter_autoplay' => 0,
- 'video_filter_related' => 1,
- );
- # this added to avoid php 5.4 notice : array to string conversion
- if(is_array($text)) $text = "";
-
- $text = (string)$text; // Make sure $text is a string.
- if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
- foreach ($matches_code[0] as $ci => $code) {
- $video = (array)$video + array(
- 'source' => $matches_code[2][$ci],
- 'autoplay' => $filter->settings['video_filter_autoplay'],
- 'related' => $filter->settings['video_filter_related'],
- );
- // Pick random out of multiple sources separated by ','
- if (strstr($video['source'], ',')) {
- $sources = explode(',', $video['source']);
- $random = array_rand($sources, 1);
- $video['source'] = $sources[$random];
- }
- // Load all codecs
- $codecs = module_invoke_all('codec_info');
- // Find codec
- foreach ($codecs AS $codec_name => $codec) {
- if (!is_array($codec['regexp'])) {
- $codec['regexp'] = array($codec['regexp']);
- }
- // Try different regular expressions
- 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; // used in theme function
- $video['codec']['control_bar_height'] = 0; // default
- break 2;
- }
- }
- }
- // Codec found
- if (isset($video['codec'])) {
- // Override default attributes
- 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];
- }
- }
- // Use configured ratio if present, use that from the codec otherwise
- $ratio = 1;
- if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio) ) {
- //validate given ratio parameter
- $ratio = $tratio[1] / $tratio[2];
- }
- else {
- $ratio = $video['codec']['ratio'];
- }
- // Sets video width & height after any user input has been parsed.
- // First, check if user has set a width.
- if (isset($video['width']) && !isset($video['height'])) {
- $video['height'] = variable_get('video_filter_height_'.$format, 400);
- }
- // Else, if user has set height.
- elseif (isset($video['height']) && !isset($video['width'])) {
- $video['width'] = $video['height'] * $ratio;
- }
- // Maybe both?
- elseif (isset($video['height']) && isset($video['width'])) {
- $video['width'] = $video['width'];
- $video['height'] = $video['height'];
- }
- // Fall back to defaults.
- elseif (!isset($video['height']) && !isset($video['width'])) {
- $video['width'] = $filter->settings['video_filter_width'];
- $video['height'] = $filter->settings['video_filter_height'];
- }
- // Default value for control bar height
- $control_bar_height = 0;
- if (isset($video['control_bar_height'])) {
- // respect control_bar_height option if present
- $control_bar_height = $video['control_bar_height'];
- }
- elseif (isset($video['codec']['control_bar_height'])) {
- // respect setting provided by codec otherwise
- $control_bar_height = $video['codec']['control_bar_height'];
- }
- // Resize to fit within width and height repecting aspect ratio
- 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 {
- // Invalid callback
- $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
- }
- // Invalid format
- }
- else {
- $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
- }
- $text = str_replace($code, $replacement, $text);
- }
- }
- return $text;
- }
- /**
- * Test support for URL.
- *
- * @todo Implement hook_codec_info() to...
- * - add support for whatever media module uses as an alternative to video_filter module.
- * - add support for http://embed.ly/.
- *
- * @param $url
- * string, URL which may or may not include media of some kind.
- *
- * @return
- * TRUE, video_filter recognizes and supports media at this URL
- * FALSE, video_filter doesn't recognize it. This could be either because
- * the URL doesn't actually provide media or because we just doen't have
- * the codec for it.
- */
- function video_filter_field_url_is_supported($url) {
- // Load all codecs
- module_load_include('inc', 'video_filter', 'video_filter.codecs');
- $codecs = module_invoke_all('codec_info');
- // Find codec
- foreach ($codecs AS $codec_name => $codec) {
- if (!is_array($codec['regexp'])) {
- $codec['regexp'] = array($codec['regexp']);
- }
- // Try different regular expressions
- $video = array();
- foreach ($codec['regexp'] as $delta => $regexp) {
- if (preg_match($regexp, $url)) {
- $video['codec'] = TRUE;
- }
- break;
- }
- break;
- }
- // If we found a codec, this URL is supported.
- $is_supported = (isset($video['codec'])) ? TRUE : FALSE;
- return $is_supported;
- }
|