video_filter.module 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. /**
  3. * @file
  4. * Video filter is a highly flexible and easy extendable filter module to embed
  5. * any type of video in your site using a simple tag.
  6. */
  7. module_load_include('inc', 'video_filter', 'video_filter.codecs');
  8. /**
  9. * Implements hook_filter_info().
  10. */
  11. function video_filter_filter_info() {
  12. $filters = array();
  13. $filters['video_filter'] = array(
  14. 'title' => t('Video Filter'),
  15. 'description' => t('Substitutes [video:URL] with embedded HTML.'),
  16. 'process callback' => '_video_filter_process',
  17. 'settings callback' => '_video_filter_settings',
  18. 'default settings' => array(
  19. 'video_filter_width' => '400',
  20. 'video_filter_height' => '400',
  21. 'video_filter_autoplay' => 1,
  22. 'video_filter_related' => 1,
  23. 'video_filter_html5' => 1,
  24. 'video_filter_codecs' => _video_filter_map_codecs_name(video_filter_get_codec_info()),
  25. 'video_filter_multiple_sources' => TRUE,
  26. ),
  27. 'tips callback' => '_video_filter_tips',
  28. // See http://drupal.org/node/1061244.
  29. 'weight' => -1,
  30. );
  31. return $filters;
  32. }
  33. function _video_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
  34. $settings['video_filter_width'] = array(
  35. '#type' => 'textfield',
  36. '#title' => t('Default width setting'),
  37. '#default_value' => isset($filter->settings['video_filter_width']) ? $filter->settings['video_filter_width'] : $defaults['video_filter_width'],
  38. '#maxlength' => 4,
  39. );
  40. $settings['video_filter_height'] = array(
  41. '#type' => 'textfield',
  42. '#title' => t('Default height setting'),
  43. '#default_value' => isset($filter->settings['video_filter_height']) ? $filter->settings['video_filter_height'] : $defaults['video_filter_height'],
  44. '#maxlength' => 4,
  45. );
  46. $settings['video_filter_autoplay'] = array(
  47. '#type' => 'radios',
  48. '#title' => t('Default autoplay setting'),
  49. '#description' => t('Not all video formats support this setting.'),
  50. '#default_value' => isset($filter->settings['video_filter_autoplay']) ? $filter->settings['video_filter_autoplay'] : $defaults['video_filter_autoplay'],
  51. '#options' => array(
  52. 0 => t('No'),
  53. 1 => t('Yes'),
  54. ),
  55. );
  56. $settings['video_filter_related'] = array(
  57. '#type' => 'radios',
  58. '#title' => t('Related videos setting'),
  59. '#description' => t('Show "related videos"? Not all video formats support this setting.'),
  60. '#default_value' => isset($filter->settings['video_filter_related']) ? $filter->settings['video_filter_related'] : $defaults['video_filter_related'],
  61. '#options' => array(
  62. 0 => t('No'),
  63. 1 => t('Yes'),
  64. ),
  65. );
  66. $settings['video_filter_html5'] = array(
  67. '#type' => 'radios',
  68. '#title' => t('Use HTML5'),
  69. '#description' => t('Use HTML5 if the codec provides it. Makes your videos more device agnostic.'),
  70. '#default_value' => isset($filter->settings['video_filter_html5']) ? $filter->settings['video_filter_html5'] : $defaults['video_filter_html5'],
  71. '#options' => array(
  72. 0 => t('No'),
  73. 1 => t('Yes'),
  74. ),
  75. );
  76. $settings['video_filter_multiple_sources'] = array(
  77. '#type' => 'radios',
  78. '#title' => t('Allow multiple sources'),
  79. '#description' => t('Allow the use of multiple sources (used source is selected at random).'),
  80. '#default_value' => isset($filter->settings['video_filter_multiple_sources']) ? $filter->settings['video_filter_multiple_sources'] : $defaults['video_filter_multiple_sources'],
  81. '#options' => array(
  82. 0 => t('No'),
  83. 1 => t('Yes'),
  84. ),
  85. );
  86. $settings['video_filter_codecs'] = array(
  87. '#type' => 'checkboxes',
  88. '#title' => t('Codecs'),
  89. '#description' => t('Choose which codecs will be available.'),
  90. '#default_value' => isset($filter->settings['video_filter_codecs']) ? $filter->settings['video_filter_codecs'] : $defaults['video_filter_codecs'],
  91. '#options' => _video_filter_map_codecs_name(video_filter_get_codec_info()),
  92. );
  93. return $settings;
  94. }
  95. function _video_filter_map_codecs_name($codecs) {
  96. $codecs_map = array();
  97. foreach ($codecs as $codec_cod => $codec) {
  98. $codecs_map[$codec_cod] = $codec['name'];
  99. }
  100. return $codecs_map;
  101. }
  102. function _video_filter_tips($filter, $format, $long = FALSE) {
  103. if ($long) {
  104. $codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
  105. $supported = array();
  106. $instructions = array();
  107. foreach ($codecs as $codec) {
  108. $supported[] = $codec['name'];
  109. $instructions[] = isset($codec['instructions']) ? '<li>' . $codec['name'] . ':<br/>' . $codec['instructions'] . '</li>' : '';
  110. }
  111. return t('
  112. <p><strong>Video Filter</strong></p>
  113. <p>You may insert videos from popular video sites by using a simple tag <code>[video:URL]</code>.</p>
  114. <p>Examples:</p>
  115. <ul>
  116. <li>Single video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId]</code></li>
  117. <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>
  118. <li>Override default autoplay setting: <code>[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]</code></li>
  119. <li>Override default width and height:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]</code></li>
  120. <li>Override default aspect ratio:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId ratio:4/3]</code></li>
  121. <li>Align the video:<br /><code>[video:http://www.youtube.com/watch?v=uN1qUeId align:right]</code></li>
  122. </ul>
  123. <p>Supported sites: @codecs.</p>
  124. <p>Special instructions:</p>
  125. <small>Some codecs need special input. You\'ll find those instructions here.</small>
  126. <ul>!instructions</ul>', array(
  127. '@codecs' => implode(', ', $supported),
  128. '!instructions' => implode('', $instructions),
  129. )
  130. );
  131. }
  132. else {
  133. return t('You may insert videos with [video:URL]');
  134. }
  135. }
  136. function _video_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  137. if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
  138. foreach ($matches_code[0] as $ci => $code) {
  139. $video = array(
  140. 'source' => $matches_code[2][$ci],
  141. 'autoplay' => $filter->settings['video_filter_autoplay'],
  142. 'related' => $filter->settings['video_filter_related'],
  143. );
  144. // Pick random out of multiple sources separated by comma (,).
  145. if ($filter->settings['video_filter_multiple_sources'] && strstr($video['source'], ',')) {
  146. $sources = explode(',', $video['source']);
  147. $random = array_rand($sources, 1);
  148. $video['source'] = $sources[$random];
  149. }
  150. // Load all codecs.
  151. $codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
  152. // Find codec.
  153. foreach ($codecs as $codec_name => $codec) {
  154. if (!is_array($codec['regexp'])) {
  155. $codec['regexp'] = array($codec['regexp']);
  156. }
  157. // Try different regular expressions.
  158. foreach ($codec['regexp'] as $delta => $regexp) {
  159. if (preg_match($regexp, $video['source'], $matches)) {
  160. $video['codec'] = $codec;
  161. $video['codec']['delta'] = $delta;
  162. $video['codec']['matches'] = $matches;
  163. // Used in theme function:
  164. $video['codec']['codec_name'] = $codec_name;
  165. break 2;
  166. }
  167. }
  168. }
  169. // Codec found.
  170. if (isset($video['codec'])) {
  171. // Override default attributes.
  172. if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
  173. foreach ($matches_attributes[0] as $ai => $attribute) {
  174. $video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
  175. }
  176. }
  177. // Use configured ratio if present, otherwise use that from the codec,
  178. // if set. Fall back to 1.
  179. $ratio = 1;
  180. if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio)) {
  181. // Validate given ratio parameter.
  182. $ratio = $tratio[1] / $tratio[2];
  183. }
  184. elseif (isset($video['codec']['ratio'])) {
  185. if (is_float($video['codec']['ratio']) || is_int($video['codec']['ratio'])) {
  186. $ratio = $video['codec']['ratio'];
  187. }
  188. elseif (preg_match('/(\d+)\s*\/\s*(\d+)/', $video['codec']['ratio'], $cratio)) {
  189. $ratio = $cratio[1] / $cratio[2];
  190. }
  191. }
  192. // Sets video width & height after any user input has been parsed.
  193. // First, check if user has set a width.
  194. if (isset($video['width']) && !isset($video['height'])) {
  195. if ($ratio) {
  196. $video['height'] = ceil($video['width'] / $ratio);
  197. }
  198. else {
  199. $video['height'] = $filter->settings['video_filter_height'];
  200. }
  201. }
  202. // Else, if user has set height.
  203. elseif (isset($video['height']) && !isset($video['width'])) {
  204. if ($ratio) {
  205. $video['width'] = ceil($video['height'] * $ratio);
  206. }
  207. else {
  208. $video['width'] = $filter->settings['video_filter_height'];
  209. }
  210. }
  211. // Maybe both?
  212. elseif (isset($video['height']) && isset($video['width'])) {
  213. $video['width'] = $video['width'];
  214. $video['height'] = $video['height'];
  215. }
  216. // Fall back to defaults.
  217. elseif (!isset($video['height']) && !isset($video['width'])) {
  218. $video['width'] = $filter->settings['video_filter_width'] != '' ? $filter->settings['video_filter_width'] : 400;
  219. $video['height'] = $filter->settings['video_filter_height'] != '' ? $filter->settings['video_filter_height'] : 400;
  220. }
  221. // Default value for control bar height.
  222. $control_bar_height = 0;
  223. if (isset($video['control_bar_height'])) {
  224. // Respect control_bar_height option if present.
  225. $control_bar_height = $video['control_bar_height'];
  226. }
  227. elseif (isset($video['codec']['control_bar_height'])) {
  228. // Respect setting provided by codec otherwise.
  229. $control_bar_height = $video['codec']['control_bar_height'];
  230. }
  231. $video['height'] += $control_bar_height;
  232. $video['autoplay'] = (bool) $video['autoplay'];
  233. $video['align'] = (isset($video['align']) && in_array($video['align'], array(
  234. 'left',
  235. 'right',
  236. 'center',
  237. ))) ? $video['align'] : NULL;
  238. // Let modules have final say on video parameters.
  239. drupal_alter('video_filter_video', $video);
  240. if (isset($video['codec']['html5_callback']) && $filter->settings['video_filter_html5'] == 1 && is_callable($video['codec']['html5_callback'], FALSE)) {
  241. $replacement = call_user_func($video['codec']['html5_callback'], $video);
  242. }
  243. elseif (is_callable($video['codec']['callback'], FALSE)) {
  244. $replacement = call_user_func($video['codec']['callback'], $video);
  245. }
  246. else {
  247. // Invalid callback.
  248. $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
  249. }
  250. }
  251. // Invalid format.
  252. else {
  253. $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
  254. }
  255. $text = str_replace($code, $replacement, $text);
  256. }
  257. }
  258. return $text;
  259. }
  260. /**
  261. * Wrapper that calls the theme function.
  262. */
  263. function video_filter_flash($video, $params = array()) {
  264. return theme('video_filter_flash', array('video' => $video, 'params' => $params));
  265. }
  266. /**
  267. * Wrapper that calls the theme function.
  268. */
  269. function video_filter_iframe($video) {
  270. return theme('video_filter_iframe', array('video' => $video));
  271. }
  272. /**
  273. * Get a list of all available video codecs.
  274. */
  275. function video_filter_get_codec_info() {
  276. static $codecs;
  277. if (!isset($codecs)) {
  278. $codecs = module_invoke_all('codec_info');
  279. drupal_alter('video_filter_codec_info', $codecs);
  280. }
  281. return $codecs;
  282. }
  283. /**
  284. *
  285. */
  286. function _video_filter_merge_format_codecs($filters_codecs) {
  287. $codecs = array_pop($filters_codecs);
  288. foreach ($filters_codecs as $format_name => $format_codecs) {
  289. foreach ($format_codecs as $codec_name => $codec_value) {
  290. if (!empty($codec_value) && empty($codecs[$codec_name])) {
  291. $codecs[$codec_name] = $codec_value;
  292. }
  293. }
  294. }
  295. return $codecs;
  296. }
  297. /**
  298. * Get a list of enabled video codecs.
  299. */
  300. function video_filter_get_codec_enabled($video_filter_codecs) {
  301. $codecs = array_intersect_key(
  302. video_filter_get_codec_info(),
  303. array_filter($video_filter_codecs)
  304. );
  305. return $codecs;
  306. }
  307. /**
  308. * Function that outputs the <object> element.
  309. *
  310. * @ingroup themeable
  311. */
  312. function theme_video_filter_flash($variables) {
  313. $output = '';
  314. $video = $variables['video'];
  315. $params = isset($variables['params']) ? $variables['params'] : array();
  316. $classes = video_filter_get_classes($video);
  317. $attributes = '';
  318. if (!empty($video['attributes'])) {
  319. $attributes = drupal_attributes($video['attributes']);
  320. }
  321. $output .= '<div class="video-filter"><object class="' . implode(' ', $classes) . '" type="application/x-shockwave-flash" ';
  322. $output .= 'width="' . $video['width'] . '" height="' . $video['height'] . '" data="' . $video['source'] . '" ' . $attributes . '>' . "\n";
  323. $defaults = array(
  324. 'movie' => $video['source'],
  325. 'wmode' => 'transparent',
  326. 'allowFullScreen' => 'true',
  327. );
  328. $params = array_merge($defaults, (is_array($params) && count($params)) ? $params : array());
  329. foreach ($params as $name => $value) {
  330. $output .= ' <param name="' . $name . '" value="' . $value . '" />' . "\n";
  331. }
  332. $output .= '</object></div>' . "\n";
  333. return $output;
  334. }
  335. /**
  336. * Function that outputs HTML5 compatible iFrame for codecs that support it.
  337. *
  338. * @ingroup themeable
  339. */
  340. function theme_video_filter_iframe($variables) {
  341. $video = $variables['video'];
  342. $classes = video_filter_get_classes($video);
  343. $attributes = '';
  344. if (!empty($video['attributes'])) {
  345. $attributes = drupal_attributes($video['attributes']);
  346. }
  347. $output = '<div class="video-filter"><iframe src="' . $video['source'] . '" width="' . $video['width'] . '" height="' . $video['height'] . '" class="' . implode(' ', $classes) . '" frameborder="0" allowfullscreen="true"' . $attributes . '></iframe></div>';
  348. return $output;
  349. }
  350. /**
  351. * Implements hook_theme().
  352. */
  353. function video_filter_theme($existing, $type, $theme, $path) {
  354. return array(
  355. 'video_filter_flash' => array(
  356. 'variables' => array('video' => NULL, 'params' => NULL),
  357. ),
  358. 'video_filter_iframe' => array(
  359. 'variables' => array('video' => NULL, 'params' => NULL),
  360. ),
  361. 'video_filter_dashboard' => array(
  362. 'variables' => array('form' => NULL),
  363. 'template' => 'video_filter_dashboard',
  364. ),
  365. );
  366. }
  367. /**
  368. * Helper function that extracts some classes from $video.
  369. */
  370. function video_filter_get_classes($video) {
  371. $classes = array(
  372. 'video-filter',
  373. // Add codec name.
  374. 'video-' . $video['codec']['codec_name'],
  375. );
  376. // Add alignment.
  377. if (isset($video['align'])) {
  378. $classes[] = 'video-' . $video['align'];
  379. }
  380. // First match is the URL, we don't want that as a class.
  381. unset($video['codec']['matches'][0]);
  382. foreach ($video['codec']['matches'] as $match) {
  383. $classes[] = 'vf-' . strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $match));
  384. }
  385. return $classes;
  386. }
  387. /**
  388. * Implements hook_menu().
  389. */
  390. function video_filter_menu() {
  391. $items = array();
  392. $items['video_filter/dashboard/%'] = array(
  393. 'title' => 'Videofilter',
  394. 'description' => 'Dashboard',
  395. 'page callback' => 'video_filter_dashboard_page',
  396. 'access arguments' => array('access content'),
  397. 'type' => MENU_CALLBACK,
  398. 'page arguments' => array(2),
  399. 'theme callback' => '_video_filter_dashboard_theme',
  400. );
  401. $items['video_filter/instructions'] = array(
  402. 'title' => 'Videofilter instructions',
  403. 'description' => 'instructions',
  404. 'page callback' => 'video_filter_instructions_page',
  405. 'access arguments' => array('access content'),
  406. 'type' => MENU_CALLBACK,
  407. 'theme callback' => '_video_filter_dashboard_theme',
  408. );
  409. return $items;
  410. }
  411. /**
  412. * Return the theme name to be used when showing linkit dashboard
  413. */
  414. function _video_filter_dashboard_theme() {
  415. return variable_get('admin_theme', 'seven');
  416. }
  417. /**
  418. * Template preprocess function for video_filter_dashboard().
  419. */
  420. function template_preprocess_video_filter_dashboard(&$variables) {
  421. // Construct page title.
  422. $variables['head_title'] = t('Video filter dashboard');
  423. $variables['head'] = drupal_get_html_head();
  424. $variables['help'] = theme('help');
  425. $variables['language'] = $GLOBALS['language'];
  426. $variables['language']->dir = $GLOBALS['language']->direction ? 'rtl' : 'ltr';
  427. $variables['messages'] = isset($variables['show_messages']) ? theme('status_messages') : '';
  428. $variables['css'] = drupal_add_css();
  429. $variables['styles'] = drupal_get_css();
  430. $variables['scripts'] = drupal_get_js();
  431. }
  432. /**
  433. * Creates the dashboard.
  434. */
  435. function video_filter_dashboard_page($editor) {
  436. module_invoke('admin_menu', 'suppress');
  437. // Add CSS.
  438. drupal_add_css(drupal_get_path('module', 'video_filter') . '/css/video_filter.css');
  439. switch ($editor) {
  440. case 'wysiwyg_tinymce':
  441. // Add JavaScript. First, we'll need to determine what version we're on.
  442. $has_added_js = FALSE;
  443. // Solves bug that causes tinymce.inc to not be loaded.
  444. wysiwyg_load_includes('editors', 'editor', 'tinymce');
  445. // Check for TinyMCE 4.x first.
  446. if (function_exists('wysiwyg_tinymce_editor')) {
  447. $loaded_editor = wysiwyg_tinymce_editor();
  448. $version = wysiwyg_tinymce_version($loaded_editor['tinymce']);
  449. if (version_compare($version, '4', '>=')) {
  450. drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/tinymce/video_filter-4.js');
  451. $has_added_js = TRUE;
  452. }
  453. }
  454. // Add JS for <= TinyMCE 3.x.
  455. if (!$has_added_js) {
  456. drupal_add_js(wysiwyg_get_path('tinymce') . '/jscripts/tiny_mce/tiny_mce_popup.js');
  457. drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/tinymce/video_filter.js');
  458. }
  459. break;
  460. case 'ckeditor':
  461. case 'wysiwyg_ckeditor':
  462. // Add JavaScript.
  463. drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/ckeditor/video_filter_dialog.js');
  464. break;
  465. case 'fckeditor':
  466. case 'wysiwyg_fckeditor':
  467. // Add JavaScript.
  468. drupal_add_js(drupal_get_path('module', 'video_filter') . '/editors/fckeditor/video_filter/video_filter_dialog.js');
  469. break;
  470. }
  471. $form = drupal_get_form('_video_filter_form');
  472. print theme('video_filter_dashboard', array('form' => render($form)));
  473. exit();
  474. }
  475. /**
  476. * Creates the instructions page.
  477. */
  478. function video_filter_instructions_page() {
  479. module_invoke('admin_menu', 'suppress');
  480. $form = drupal_get_form('_video_filter_instructions_form');
  481. print theme('video_filter_dashboard', array('form' => render($form)));
  482. exit();
  483. }
  484. function _video_filter_form() {
  485. $form['video_filter'] = array(
  486. '#type' => 'fieldset',
  487. '#title' => t('Insert Video'),
  488. '#weight' => 0,
  489. '#collapsible' => FALSE,
  490. '#collapsed' => FALSE,
  491. '#attributes' => array('class' => array('clearfix')),
  492. );
  493. $form['video_filter']['file_url'] = array(
  494. '#type' => 'textfield',
  495. '#title' => t('Video URL'),
  496. '#maxlength' => 255,
  497. '#size' => 80,
  498. '#default_value' => '',
  499. '#weight' => 1,
  500. );
  501. $form['video_filter']['width'] = array(
  502. '#type' => 'textfield',
  503. '#title' => t('Width'),
  504. '#maxlength' => 255,
  505. '#size' => 80,
  506. '#default_value' => '',
  507. '#weight' => 2,
  508. );
  509. $form['video_filter']['height'] = array(
  510. '#type' => 'textfield',
  511. '#title' => t('Height'),
  512. '#maxlength' => 255,
  513. '#size' => 80,
  514. '#default_value' => '',
  515. '#weight' => 3,
  516. );
  517. $form['video_filter']['align'] = array(
  518. '#type' => 'select',
  519. '#title' => t('Align'),
  520. '#default_value' => 'none',
  521. '#options' => array(
  522. 'none' => t('None'),
  523. 'left' => t('Left'),
  524. 'right' => t('Right'),
  525. 'center' => t('Center'),
  526. ),
  527. '#weight' => 4,
  528. );
  529. $form['video_filter']['autoplay'] = array(
  530. '#type' => 'checkbox',
  531. '#title' => t('Autoplay'),
  532. '#weight' => 5,
  533. );
  534. $form += _video_filter_instructions_form();
  535. $form['cancel'] = array(
  536. '#type' => 'button',
  537. '#value' => t('Cancel'),
  538. '#weight' => 98,
  539. );
  540. $form['insert'] = array(
  541. '#type' => 'button',
  542. '#value' => t('Insert'),
  543. '#weight' => 99,
  544. );
  545. return $form;
  546. }
  547. function _video_filter_instructions_form() {
  548. $form['instructions'] = array(
  549. '#type' => 'fieldset',
  550. '#title' => t('Instructions'),
  551. '#collapsible' => TRUE,
  552. '#collapsed' => TRUE,
  553. '#attributes' => array('class' => array('clearfix')),
  554. '#weight' => 97,
  555. );
  556. $text = '<p>' . t('Insert a 3rd party video from one of the following providers; this list may vary depending on the text format being used.') . '</p>';
  557. $text .= _video_filter_instructions();
  558. $form['instructions']['text'] = array(
  559. '#type' => 'item',
  560. '#markup' => $text,
  561. );
  562. return $form;
  563. }
  564. /**
  565. * Implements hook_ckeditor_plugin().
  566. */
  567. function video_filter_ckeditor_plugin() {
  568. $plugins = array();
  569. $plugins['video_filter'] = array(
  570. 'name' => 'video_filter',
  571. 'desc' => t('Plugin to directly embed videos with the video filter module.'),
  572. 'path' => drupal_get_path('module', 'video_filter') . '/editors/ckeditor/',
  573. 'buttons' => array(
  574. 'video_filter' => array(
  575. 'label' => t('Video filter'),
  576. 'icon' => 'video_filter.png',
  577. ),
  578. ),
  579. );
  580. return $plugins;
  581. }
  582. function video_filter_wysiwyg_plugin($editor, $version) {
  583. _video_filter_add_settings('wysiwyg_' . $editor);
  584. $plugins = array();
  585. switch ($editor) {
  586. case 'ckeditor':
  587. $plugins['video_filter'] = array(
  588. 'path' => drupal_get_path('module', 'video_filter') . '/editors/ckeditor/',
  589. 'buttons' => array('video_filter' => t('Video filter')),
  590. 'url' => 'http://drupal.org/project/video_filter',
  591. 'load' => TRUE,
  592. );
  593. break;
  594. case 'fckeditor':
  595. $plugins['video_filter'] = array(
  596. 'path' => drupal_get_path('module', 'video_filter') . '/editors/fckeditor/',
  597. 'buttons' => array('video_filter' => t('Video filter')),
  598. 'url' => 'http://drupal.org/project/video_filter',
  599. 'load' => TRUE,
  600. );
  601. break;
  602. case 'tinymce':
  603. $plugins['video_filter'] = array(
  604. 'path' => drupal_get_path('module', 'video_filter') . '/editors/tinymce',
  605. 'filename' => 'editor_plugin.js',
  606. 'buttons' => array('video_filter' => t('Video filter')),
  607. 'url' => 'http://drupal.org/project/video_filter',
  608. 'load' => TRUE,
  609. );
  610. break;
  611. }
  612. return $plugins;
  613. }
  614. /**
  615. * Implements hook_element_info_alter().
  616. */
  617. function video_filter_element_info_alter(&$types) {
  618. if (isset($types['text_format']['#pre_render']) && is_array($types['text_format']['#pre_render'])) {
  619. if (in_array('ckeditor_pre_render_text_format', $types['text_format']['#pre_render'])) {
  620. _video_filter_add_settings('ckeditor');
  621. }
  622. }
  623. }
  624. function _video_filter_add_settings($editor) {
  625. static $editor_settings_added = array();
  626. static $global_settings_added = FALSE;
  627. if (!isset($editor_settings_added[$editor])) {
  628. $editor_settings_added[$editor] = TRUE;
  629. // Add popup url.
  630. $settings = array(
  631. 'video_filter' => array(
  632. 'url' => array($editor => url('video_filter/dashboard/' . $editor)),
  633. 'instructions_url' => url('video_filter/instructions'),
  634. ),
  635. );
  636. drupal_add_js($settings, 'setting');
  637. }
  638. if (!$global_settings_added) {
  639. $global_settings_added = TRUE;
  640. // Add global settings for video_filter.
  641. $settings = array(
  642. 'video_filter' => array(
  643. 'modulepath' => drupal_get_path('module', 'video_filter'),
  644. ),
  645. );
  646. drupal_add_js($settings, 'setting');
  647. }
  648. }
  649. /**
  650. * Parses Codec into instructions for WYSIWYG popup.
  651. */
  652. function _video_filter_instructions() {
  653. // Get all codecs the user has permission to use in at least one text format.
  654. global $user;
  655. $formats = filter_formats($user);
  656. $filters_codecs = array();
  657. foreach ($formats as $format) {
  658. $format_filters = filter_list_format($format->format);
  659. if (isset($format_filters['video_filter'])) {
  660. $filters_codecs[$format->name] = $format_filters['video_filter']->settings['video_filter_codecs'];
  661. }
  662. }
  663. $video_filter_codecs = _video_filter_merge_format_codecs($filters_codecs);
  664. $codecs = video_filter_get_codec_enabled($video_filter_codecs);
  665. $output = '<ul>';
  666. foreach ($codecs as $codec) {
  667. $output .= '<li><strong>' . $codec['name'] . '</strong><br />' . t('e.g.') . ' ' . $codec['sample_url'] . '</li>';
  668. }
  669. $output .= '</ul>';
  670. return $output;
  671. }
  672. /**
  673. * Requests data from an oEmbed provider.
  674. *
  675. * Note: This function currently only supports JSON responses.
  676. *
  677. * @param string $endpoint
  678. * The provider endpoint URL.
  679. * @param array $arguments
  680. * An associative array of URL arguments to send the provider.
  681. *
  682. * @return array|FALSE
  683. * An array of data if the request is successful, otherwise FALSE.
  684. *
  685. * @todo Support other response formats than JSON?
  686. */
  687. function video_filter_oembed_request($endpoint, array $arguments) {
  688. // Make HTTP request.
  689. $result = drupal_http_request(url($endpoint, array('query' => $arguments)));
  690. if ($data = json_decode($result->data)) {
  691. // Success.
  692. return (array) $data;
  693. }
  694. else {
  695. // Failure. Either the resource doesn't exist or there was an error with the
  696. // request.
  697. return FALSE;
  698. }
  699. }