You are here

video_filter.module in Video Filter 6.2

File

video_filter.module
View source
<?php

module_load_include('inc', 'video_filter', 'video_filter.codecs');

/**
 * Implementation of hook_filter().
 */
function video_filter_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Video Filter'),
      );
    case 'description':
      return t('Substitutes [video:URL] with embedded HTML.');
    case 'process':
      return video_filter_process($text, $format);
    case 'settings':
      return video_filter_settings($format);
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function video_filter_filter_tips($delta, $format, $long = FALSE) {
  if ($long) {
    $codecs = module_invoke_all('codec_info');
    $supported = array();
    $instructions = array();
    foreach ($codecs as $codec) {
      $supported[] = $codec['name'];
      $instructions[] = $codec['instructions'] != '' ? '<li>' . $codec['name'] . ':<br/>' . $codec['instructions'] . '</li>' : '';
    }
    return t('
      <p><strong>Video Filter</strong></p>
      <p>You may insert videos from popular video sites by using a simple tag <code>[video:URL]</code>.</p>
      <p>Examples:</p>
      <ul>
        <li>Single video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId]</code></li>
        <li>Random video out of multiple:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId1,http://www.youtube.com/watch?v=uN1qUeId2]</code></li>
        <li>Override default autoplay setting: <code>[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]</code></li>
        <li>Override default width and height:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]</code></li>
        <li>Override default aspect ratio:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId ratio:4/3]</code></li>
        <li>Align the video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId align:right]</code></li>
      </ul>
      <p>Supported sites: @codecs.</p>
      <p>Special instructions:</p>
      <small>Some codecs need special input. You\'ll find those instructions here.</small>
      <ul>!instructions</ul>', array(
      '@codecs' => implode(', ', $supported),
      '!instructions' => implode('', $instructions),
    ));
  }
  else {
    return t('You may insert videos with [video:URL]');
  }
}
function video_filter_process($text, $format = -1) {
  if (preg_match_all('/\\[video(\\:(.+))?( .+)?\\]/isU', $text, $matches_code)) {
    foreach ($matches_code[0] as $ci => $code) {
      $video = array(
        'source' => $matches_code[2][$ci],
        'autoplay' => variable_get('video_filter_autoplay_' . $format, 0),
        'related' => variable_get('video_filter_related_' . $format, 1),
      );

      // 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);
        }
        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'] = variable_get('video_filter_width_' . $format, 400);
          $video['height'] = variable_get('video_filter_height_' . $format, 400);
        }

        // 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;
}
function video_filter_settings($format) {
  $form['video_filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('Video filter'),
    '#collapsible' => TRUE,
  );
  $form['video_filter']['video_filter_width_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('Default width setting'),
    '#default_value' => variable_get('video_filter_width_' . $format, 400),
    '#maxlength' => 4,
  );
  $form['video_filter']['video_filter_height_' . $format] = array(
    '#type' => 'textfield',
    '#title' => t('Default height setting'),
    '#default_value' => variable_get('video_filter_height_' . $format, 400),
    '#maxlength' => 4,
  );
  $form['video_filter']['video_filter_autoplay_' . $format] = array(
    '#type' => 'radios',
    '#title' => t('Default autoplay setting'),
    '#description' => t('Not all video formats support this setting.'),
    '#default_value' => variable_get('video_filter_autoplay_' . $format, 1),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
  );
  $form['video_filter']['video_filter_related_' . $format] = array(
    '#type' => 'radios',
    '#title' => t('Related videos setting'),
    '#description' => t('Show "related videos"? Not all video formats support this setting.'),
    '#default_value' => variable_get('video_filter_related_' . $format, 1),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
  );
  return $form;
}

/**
 * Implementation of hook_init().
 */
function video_filter_init() {
  drupal_add_css(drupal_get_path('module', 'video_filter') . '/video_filter.css');
}

/**
 * Wrapper that calls the theme function.
 */
function video_filter_flash($video, $params = array()) {
  return theme('video_filter_flash', $video, $params);
}

/**
 * Function that outputs the <object> element.
 *
 * @ingroup themeable
 */
function theme_video_filter_flash($video, $params = array()) {
  $output = '';

  // Create classes
  $classes = array(
    'video-filter',
    'video-' . $video['codec']['codec_name'],
  );

  // Adds alignment
  if (isset($video['align'])) {
    $classes[] = 'video-' . $video['align'];
  }

  // First match is the URL, we don't want that as a class.
  unset($video['codec']['matches'][0]);
  foreach ($video['codec']['matches'] as $match) {
    $classes[] = 'vf-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $match));
  }
  $output .= '<object class="' . implode(' ', $classes) . '" type="application/x-shockwave-flash" ';
  $output .= 'width="' . $video['width'] . '" height="' . $video['height'] . '" data="' . $video['source'] . '">' . "\n";
  $defaults = array(
    'movie' => $video['source'],
    'wmode' => 'transparent',
    'allowFullScreen' => 'true',
  );
  $params = array_merge($defaults, is_array($params) && count($params) ? $params : array());
  foreach ($params as $name => $value) {
    $output .= '  <param name="' . $name . '" value="' . $value . '" />' . "\n";
  }
  $output .= '</object>' . "\n";
  return $output;
}

/**
 * Implementation of hook_theme().
 */
function video_filter_theme($existing, $type, $theme, $path) {
  return array(
    'video_filter_flash' => array(
      'arguments' => array(
        'video' => NULL,
        'params' => array(),
      ),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function video_filter_menu() {
  $items = array();
  $items['video_filter/load'] = array(
    'title' => t('Video Filter'),
    'page callback' => 'video_filter_loader',
    'access arguments' => array(
      'access content',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_wysiwyg_plugin().
 */
function video_filter_wysiwyg_plugin($editor, $version) {
  switch ($editor) {
    case 'tinymce':
      if ($version > 3) {
        drupal_add_css(drupal_get_path('module', 'video_filter') . '/video_filter.css');
        return array(
          'videofilter' => array(
            'path' => drupal_get_path('module', 'video_filter') . '/wysiwyg/tinymce/editor_plugin.js',
            'buttons' => array(
              'videofilter' => t('Video Filter'),
            ),
            'url' => 'http://drupal.org/project/video_filter',
            'load' => TRUE,
          ),
        );
      }
      break;
  }
}

/**
 * Output tinymce popup html.
 * 
 * @todo Remove hard-coded TinyMCE integration.
 */
function video_filter_loader() {
  $output = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 //EN">' . "\n";
  $output .= "<html>\n";
  $output .= "<head>\n";
  $output .= '<title>{#videofilter_dlg.title}</title>' . "\n";
  $path = base_path() . drupal_get_path('module', 'video_filter') . '/wysiwyg';
  $tinymce_js = wysiwyg_get_path('tinymce', TRUE) . '/jscripts/tiny_mce/tiny_mce_popup.js';
  $output .= '<script type="text/javascript" src="' . $tinymce_js . '"></script>' . "\n";
  $output .= '<script type="text/javascript" src="' . $path . '/tinymce/langs/en_dlg.js"></script>' . "\n";
  $output .= '<script type="text/javascript" src="' . $path . '/tinymce/jscripts/video_filter.js"></script>' . "\n";
  $output .= '<link href="' . base_path() . drupal_get_path('module', 'video_filter') . '/video_filter.css" rel="stylesheet" type="text/css" />' . "\n";
  $output .= "</head>\n\n";
  $output .= '<body id="mceVideoPopup">';
  $output .= '<form onsubmit="insertVideo();return false;" action="#">' . "\n";
  $output .= '<h3 style="clear:both;">{#videofilter_dlg.title}</h3>' . "\n";
  $output .= '<!-- Gets filled with the selected elements name -->' . "\n";
  $output .= '<div style="margin-top: 10px; margin-bottom: 10px">' . "\n";
  $output .= '<label for="file">' . t('File url') . '</label>' . "\n";
  $output .= '<input id="file" name="file" type="text" value="" onfocus="this.select();" />' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div style="margin-top: 10px; margin-bottom: 10px">' . "\n";
  $output .= '<label for="width">' . t('Width') . '</label>' . "\n";
  $output .= '<input id="width" name="width" type="text" value="" onfocus="this.select();" />' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div style="margin-top: 10px; margin-bottom: 10px">' . "\n";
  $output .= '<label for="height">' . t('Height') . '</label>' . "\n";
  $output .= '<input id="height" name="height" type="text" value="" onfocus="this.select();" />' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div style="margin-top: 10px; margin-bottom: 10px">' . "\n";
  $output .= '<label for="align">' . t('Align') . '</label>' . "\n";
  $output .= '<select id="align" name="align" onfocus="this.select();">' . "\n";
  $output .= '<option value="none">' . t('None') . '</option>' . "\n";
  $output .= '<option value="left">' . t('Left') . '</option>' . "\n";
  $output .= '<option value="right">' . t('Right') . '</option>' . "\n";
  $output .= '<option value="center">' . t('Center') . '</option>' . "\n";
  $output .= '</select>' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div style="margin-top: 10px; margin-bottom: 10px">' . "\n";
  $output .= '<label for="autoplay">' . t('Autoplay') . '</label>' . "\n";
  $output .= '<input id="autoplay" name="autoplay" type="checkbox" value="1" onfocus="this.select();" />' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div class="mceActionPanel">' . "\n";
  $output .= '<div style="float: left">' . "\n";
  $output .= '<input type="submit" id="insert" name="insert" value="' . t('Insert') . '" />' . "\n";
  $output .= '</div>' . "\n";
  $output .= '<div style="float: right">' . "\n";
  $output .= '<input type="button" id="cancel" name="cancel" value="' . t('Cancel') . '" onclick="tinyMCEPopup.close();" />' . "\n";
  $output .= '</div></div>' . "\n";
  $output .= '</form>' . "\n";
  $output .= '<div id="instructions" style="clear:both;"><h3>' . t('Instructions') . '</h3>' . "\n";
  $output .= '<p>' . t('Insert a 3rd party video from one of the following providers.') . '</p>' . "\n";
  $output .= _video_filter_instructions();
  $output .= '</div>' . "\n";
  $output .= '</body>' . "\n";
  $output .= "</html>\n";
  print $output;
}

/**
 * Parses Codec into instructions for tinymce popup
 */
function _video_filter_instructions() {
  $codecs = module_invoke_all('codec_info');
  $output = '<ul>';
  foreach ($codecs as $codec) {
    $output .= '<li><strong>' . $codec['name'] . '</strong><br />' . $codec['sample_url'] . '</li>';
  }
  $output .= '</ul>';
  return $output;
}

Functions

Namesort descending Description
theme_video_filter_flash Function that outputs the <object> element.
video_filter_filter Implementation of hook_filter().
video_filter_filter_tips Implementation of hook_filter_tips().
video_filter_flash Wrapper that calls the theme function.
video_filter_init Implementation of hook_init().
video_filter_loader Output tinymce popup html.
video_filter_menu Implementation of hook_menu().
video_filter_process
video_filter_settings
video_filter_theme Implementation of hook_theme().
video_filter_wysiwyg_plugin Implementation of hook_wysiwyg_plugin().
_video_filter_instructions Parses Codec into instructions for tinymce popup