You are here

eminline.module in Embedded Media Field 5

File

contrib/eminline/eminline.module
View source
<?php

/**
 * Implemenation of hook_filter().
 */
function eminline_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Embedded Inline Media'),
      );
    case 'description':
      switch ($delta) {
        case 0:
          return t('Embed videos from specified URL.');
        default:
          return;
      }
    case "process":
      switch ($delta) {
        case 0:
          return _eminline_url($text, $format);
        default:
          return;
      }
    case 'settings':
      switch ($delta) {
        case 0:
          return _eminline_url_settings($format);
        default:
          return;
      }
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function eminline_filter_tips($delta, $format, $long = FALSE) {
  $output = '';
  switch ($delta) {
    case '0':
      $providers = variable_get('eminline_providers_' . $format, array());
      $output .= t('You may embed videos from the following providers %providers. Just add the video URL to your textarea in the place where you would like the video to appear, i.e. http://www.youtube.com/watch?v=pw0jmvdh.', array(
        '%providers' => implode(', ', array_filter($providers)),
      ));
  }
  return $output;
}

/**
 * Match all http(s) URLs and pass to link checker.
 *
 * @param string $text
 *  The contents of the text area.
 * @param int $format
 *  The format id.
 *
 * @return string
 */
function _eminline_url($text, $format) {
  $text = ' ' . $text . ' ';

  // Need to attach the variables to the callback after the regex.
  $callback = _eminline_curry('_eminline_url_parse_full_links', 2);

  // Match absolute URLs.
  $text = preg_replace_callback("`(<p>|<li>|<br\\s*/?>|[ \n\r\t\\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\\s*/?>|[ \n\r\t\\)]))`i", $callback($format), $text);
  return $text;
}

/**
 * If one of our allowed providers knows what to do with the url let them embedd the video.
 *
 * @param int $format
 *  The format id.
 * @param array $match
 *  The matched text from our regex.
 *
 * @return string
 *  The replacement text for the url.
 */
function _eminline_url_parse_full_links($format, $match) {

  // Get just the URL.
  $match[2] = check_url(decode_entities($match[2]));

  // Get our filter settings.
  $video_default = variable_get('eminline_video_' . $format, array());
  $preview_default = variable_get('eminline_preview_' . $format, array());
  $tn_default = variable_get('eminline_tn_' . $format, array());

  // Build a fake field, remember that emfield is well a field.
  $field = array(
    'field_name' => 'eminline',
    'type' => 'video_cck',
    'video_width' => $video_default['video_width'],
    'video_height' => $video_default['video_height'],
    'video_autoplay' => $video_default['video_autoplay'],
    'preview_width' => $preview_default['preview_width'],
    'preview_height' => $preview_default['preview_height'],
    'preview_autoplay' => $preview_default['preview_autoplay'],
    'thumbnail_width' => $tn_default['thumbnail_width'],
    'thumbnail_height' => $tn_default['thumbnail_height'],
    'thumbnail_default_path' => $tn_default['thumbnail_default_path'],
    'providers' => variable_get('eminline_providers_' . $format, array()),
  );

  // Build our embed item.
  $item = emfield_parse_embed($field, $match[2], 'video_cck');

  // Check to make sure the provider that was found is an allowed provider.
  if ($field['providers'][$item['provider']]) {
    $item['data'] = (array) emfield_include_invoke('video_cck', $item['provider'], 'data', $field, $item);
  }
  else {
    return $match[0];
  }

  // Get how the video should be displayed.
  $display_format = variable_get('eminline_full_node_' . $format, '');

  // Change the options based on how the video should be displayed.
  if ($display_format == 'video_thumbnail') {
    $options = array(
      'width' => $field['thumbnail_width'],
      'height' => $field['thumbnail_height'],
      'default_path' => $field['thumbnail_default_path'],
    );
  }
  else {
    if ($display_format == 'video_preview') {
      $options = array(
        'width' => $field['preview_width'],
        'height' => $field['preview_height'],
        'autoplay' => $field['preview_autoplay'],
      );
    }
    else {
      $options = array(
        'width' => $field['video_width'],
        'height' => $field['video_height'],
        'autoplay' => $field['video_autoplay'],
      );
    }
  }

  // Theme the video w00t!
  return theme('eminline_url_wrapper', emfield_emfield_field_formatter($field, $item, $display_format, NULL, 'video_cck', $options));
}

/**
 * Settings form for filter.
 *
 * @param int format
 *  The format id.
 *
 * @return array
 */
function _eminline_url_settings($format) {

  // Get the settings that are avaliable to the cck widget.
  $form['eminline_' . $format] = video_cck_widget_settings('form', array(
    'type' => 'video_cck_textfields',
  ));
  $form['eminline_' . $format]['provider_list']['providers']['#default_value'] = variable_get('eminline_providers_' . $format, array());
  $video_default = variable_get('eminline_video_' . $format, array());
  foreach ($video_default as $key => $value) {
    $form['eminline_' . $format]['video'][$key]['#default_value'] = $value;
  }
  $preview_default = variable_get('eminline_preview_' . $format, array());
  foreach ($preview_default as $key => $value) {
    $form['eminline_' . $format]['preview'][$key]['#default_value'] = $value;
  }
  $tn_default = variable_get('eminline_tn_' . $format, array());
  foreach ($tn_default as $key => $value) {
    $form['eminline_' . $format]['tn'][$key]['#default_value'] = $value;
  }

  // Add select for choosing display format.
  $form['eminline_' . $format]['node_display'] = array(
    '#type' => 'select',
    '#title' => t('Video display in node view'),
    '#default_value' => variable_get('eminline_full_node_' . $format, ''),
    '#options' => array(
      'video_video' => t('Video'),
      'video_preview' => t('Preview'),
      'video_thumbnail' => t('Thumbnail'),
    ),
    '#weight' => -999,
  );
  $form['eminline_' . $format]['#type'] = 'fieldset';
  $form['eminline_' . $format]['#title'] = t('Embed inline');
  $form['eminline_' . $format]['#collapsible'] = TRUE;
  $form['eminline_' . $format]['#tree'] = TRUE;

  // My submit && validator plz, kthxbi.
  $form['#validate'] = array(
    '_eminline_url_settings_form_validate' => array(
      $format,
    ),
  );
  $form['#submit'] = array(
    '_eminline_url_settings_form_submit' => array(
      $format,
    ),
  );
  return $form;
}

/*
  TODO Write validation function...
*/
function _eminline_url_settings_form_validate($form_id, $form_values, $form, $format) {
  if (!is_numeric($form_values['eminline_' . $format]['video']['video_width']) || intval($form_values['eminline_' . $format]['video']['video_width']) != $form_values['eminline_' . $format]['video']['video_width'] || $form_values['eminline_' . $format]['video']['video_width'] < 1) {
    form_set_error('eminline_' . $format . '][video][video_width', t('"Video width" must be a positive integer.'));
  }
  if (!is_numeric($form_values['eminline_' . $format]['video']['video_height']) || intval($form_values['eminline_' . $format]['video']['video_height']) != $form_values['eminline_' . $format]['video']['video_height'] || $form_values['eminline_' . $format]['video']['video_height'] < 1) {
    form_set_error('eminline_' . $format . '][video][video_height', t('"Video height" must be a positive integer.'));
  }
  if (!is_numeric($form_values['eminline_' . $format]['preview']['preview_width']) || intval($form_values['eminline_' . $format]['preview']['preview_width']) != $form_values['eminline_' . $format]['preview']['preview_width'] || $form_values['eminline_' . $format]['preview']['preview_width'] < 1) {
    form_set_error('eminline_' . $format . '][preview][preview_width', t('"Preview width" must be a positive integer.'));
  }
  if (!is_numeric($form_values['eminline_' . $format]['preview']['preview_height']) || intval($form_values['eminline_' . $format]['preview']['preview_height']) != $form_values['eminline_' . $format]['preview']['preview_height'] || $form_values['eminline_' . $format]['preview']['preview_height'] < 1) {
    form_set_error('eminline_' . $format . '][preview][preview_height', t('"Preview height" must be a positive integer.'));
  }
  if (!is_numeric($form_values['eminline_' . $format]['tn']['thumbnail_height']) || intval($form_values['eminline_' . $format]['tn']['thumbnail_height']) != $form_values['eminline_' . $format]['tn']['thumbnail_height'] || $form_values['eminline_' . $format]['tn']['thumbnail_height'] < 1) {
    form_set_error('eminline_' . $format . '][tn][thumbnail_height', t('"Thumbnail height" must be a positive integer.'));
  }
  if (!is_numeric($form_values['eminline_' . $format]['tn']['thumbnail_width']) || intval($form_values['eminline_' . $format]['tn']['thumbnail_width']) != $form_values['eminline_' . $format]['tn']['thumbnail_width'] || $form_values['eminline_' . $format]['tn']['thumbnail_width'] < 1) {
    form_set_error('eminline_' . $format . '][tn][thumbnail_width', t('"Thumbnail width" must be a positive integer.'));
  }
}
function _eminline_url_settings_form_submit($form_id, $form_values, $format) {
  variable_set('eminline_providers_' . $format, $form_values['eminline_' . $format]['provider_list']['providers']);
  variable_set('eminline_video_' . $format, $form_values['eminline_' . $format]['video']);
  variable_set('eminline_preview_' . $format, $form_values['eminline_' . $format]['preview']);
  variable_set('eminline_tn_' . $format, $form_values['eminline_' . $format]['tn']);
  variable_set('eminline_full_node_' . $format, $form_values['eminline_' . $format]['node_display']);
}
function _eminline_curry($func, $arity) {
  return create_function('', "\n    \$args = func_get_args();\n    if(count(\$args) >= {$arity})\n        return call_user_func_array('{$func}', \$args);\n    \$args = var_export(\$args, 1);\n    return create_function('','\n        \$a = func_get_args();\n        \$z = ' . \$args . ';\n        \$a = array_merge(\$z,\$a);\n        return call_user_func_array(\\'{$func}\\', \$a);\n    ');\n  ");
}

/**
 * Create additional theme layer.
 *
 * @param string $content
 *
 * @return string
 */
function theme_eminline_url_wrapper($content) {
  $output = '';
  $output .= '<div class="eminline-wrapper">';
  $output .= $content;
  $output .= '</div>';
  return $output;
}

Functions

Namesort descending Description
eminline_filter Implemenation of hook_filter().
eminline_filter_tips Implementation of hook_filter_tips().
theme_eminline_url_wrapper Create additional theme layer.
_eminline_curry
_eminline_url Match all http(s) URLs and pass to link checker.
_eminline_url_parse_full_links If one of our allowed providers knows what to do with the url let them embedd the video.
_eminline_url_settings Settings form for filter.
_eminline_url_settings_form_submit
_eminline_url_settings_form_validate