View source
<?php
function eminline_filter($op, $delta = 0, $format = -1, $text = '', $langcode = '', $cache_id = 0) {
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;
}
}
function eminline_filter_tips($delta, $format, $long = FALSE) {
$output = '';
switch ($delta) {
case '0':
$providers = variable_get('eminline_providers_' . $format, array_keys(emfield_system_list('emvideo', NULL, FALSE)));
$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;
}
function _eminline_url($text, $format) {
$text = ' ' . $text . ' ';
$callback = _eminline_curry('_eminline_url_parse_full_links', 2);
$text = preg_replace_callback("`(?<=[\\s(>])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))(?=[.,?!\\s)<])`i", $callback($format), $text);
return $text;
}
function _eminline_url_parse_full_links($format, $match) {
$match[1] = check_url(decode_entities($match[1]));
$video_default = variable_get('eminline_video_' . $format, _eminline_default_filter_settings('video'));
$preview_default = variable_get('eminline_preview_' . $format, _eminline_default_filter_settings('preview'));
$tn_default = variable_get('eminline_tn_' . $format, _eminline_default_filter_settings('tn'));
$field = array(
'field_name' => 'eminline',
'type' => 'emvideo',
'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_keys(emfield_system_list('emvideo', NULL, FALSE))),
);
$item = emfield_parse_embed($field, $match[1], 'emvideo');
if ($field['providers'][$item['provider']]) {
$item['data'] = (array) emfield_include_invoke('emvideo', $item['provider'], 'data', $field, $item);
}
else {
return $match[0];
}
$display_format = variable_get('eminline_full_node_' . $format, 'video_video');
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'],
);
}
}
return theme('eminline_url_wrapper', emfield_emfield_field_formatter($field, $item, $display_format, NULL, 'emvideo', $options));
}
function _eminline_url_settings($format) {
$form['format'] = array(
'#type' => 'value',
'#value' => $format,
);
$form['eminline_' . $format] = emvideo_widget_settings('form', array(
'type' => 'emvideo_textfields',
));
$form['eminline_' . $format]['provider_list']['providers']['#default_value'] = variable_get('eminline_providers_' . $format, array_keys($form['eminline_' . $format]['provider_list']['providers']['#options']));
$video_default = variable_get('eminline_video_' . $format, _eminline_default_filter_settings('video'));
foreach ($video_default as $key => $value) {
$form['eminline_' . $format]['video'][$key]['#default_value'] = $value;
}
$preview_default = variable_get('eminline_preview_' . $format, _eminline_default_filter_settings('preview'));
foreach ($preview_default as $key => $value) {
$form['eminline_' . $format]['preview'][$key]['#default_value'] = $value;
}
$tn_default = variable_get('eminline_tn_' . $format, _eminline_default_filter_settings('tn'));
foreach ($tn_default as $key => $value) {
$form['eminline_' . $format]['tn'][$key]['#default_value'] = $value;
}
$form['eminline_' . $format]['node_display'] = array(
'#type' => 'select',
'#title' => t('Video display in node view'),
'#default_value' => variable_get('eminline_full_node_' . $format, 'video_video'),
'#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;
return $form;
}
function eminline_form_filter_admin_configure_alter(&$form, $form_state) {
if (isset($form['format']) && isset($form['eminline_' . $form['format']['#value']])) {
$form['#validate'][] = '_eminline_url_settings_form_validate';
array_unshift($form['#submit'], '_eminline_url_settings_form_submit');
}
}
function _eminline_url_settings_form_validate($form, &$form_state) {
$format = $form_state['values']['format'];
if (!is_numeric($form_state['values']['eminline_' . $format]['video']['video_width']) || intval($form_state['values']['eminline_' . $format]['video']['video_width']) != $form_state['values']['eminline_' . $format]['video']['video_width'] || $form_state['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_state['values']['eminline_' . $format]['video']['video_height']) || intval($form_state['values']['eminline_' . $format]['video']['video_height']) != $form_state['values']['eminline_' . $format]['video']['video_height'] || $form_state['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_state['values']['eminline_' . $format]['preview']['preview_width']) || intval($form_state['values']['eminline_' . $format]['preview']['preview_width']) != $form_state['values']['eminline_' . $format]['preview']['preview_width'] || $form_state['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_state['values']['eminline_' . $format]['preview']['preview_height']) || intval($form_state['values']['eminline_' . $format]['preview']['preview_height']) != $form_state['values']['eminline_' . $format]['preview']['preview_height'] || $form_state['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_state['values']['eminline_' . $format]['tn']['thumbnail_height']) || intval($form_state['values']['eminline_' . $format]['tn']['thumbnail_height']) != $form_state['values']['eminline_' . $format]['tn']['thumbnail_height'] || $form_state['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_state['values']['eminline_' . $format]['tn']['thumbnail_width']) || intval($form_state['values']['eminline_' . $format]['tn']['thumbnail_width']) != $form_state['values']['eminline_' . $format]['tn']['thumbnail_width'] || $form_state['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, &$form_state) {
$format = $form_state['values']['format'];
variable_set('eminline_providers_' . $format, $form_state['values']['eminline_' . $format]['provider_list']['providers']);
variable_set('eminline_video_' . $format, $form_state['values']['eminline_' . $format]['video']);
variable_set('eminline_preview_' . $format, $form_state['values']['eminline_' . $format]['preview']);
variable_set('eminline_tn_' . $format, $form_state['values']['eminline_' . $format]['tn']);
variable_set('eminline_full_node_' . $format, $form_state['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 ");
}
function _eminline_default_filter_settings($type = 'video') {
$return = array();
$widget = emvideo_widget_settings('form', array(
'type' => 'emvideo_textfields',
));
if (is_array($widget[$type])) {
foreach (element_children($widget[$type]) as $key) {
$return[$key] = $widget[$type][$key][$default_value];
}
}
return $return;
}
function eminline_theme($existing, $type, $theme, $path) {
return array(
'eminline_url_wrapper' => array(
'arguments' => array(
'content' => NULL,
),
),
);
}
function theme_eminline_url_wrapper($content) {
$output = '';
$output .= '<div class="eminline-wrapper">';
$output .= $content;
$output .= '</div>';
return $output;
}