video_filter_field.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * @file video_filter_field.module
  4. */
  5. /**
  6. * Implements hook_field_info().
  7. */
  8. function video_filter_field_field_info() {
  9. return array(
  10. 'video_filter' => array(
  11. 'label' => t('Video Filter'),
  12. 'description' => t('This field stores URLs and settings to be '
  13. . 'handled by the Video Filter module.'),
  14. 'settings' => array(),
  15. 'instance_settings' => array(
  16. 'max_height' => 400,
  17. 'max_width' => 400,
  18. 'autoplay' => 0,
  19. ),
  20. 'default_widget' => 'video_filter_field_default',
  21. 'default_formatter' => 'video_filter_field_default',
  22. ),
  23. );
  24. }
  25. /**
  26. * Implements magic callback / psuedo-hook mymodule_field_schema().
  27. */
  28. function video_filter_field_field_schema($field) {
  29. if ($field['type'] == 'video_filter') {
  30. $schema['columns']['url'] = array(
  31. 'type' => 'varchar',
  32. 'length' => 255,
  33. 'not null' => FALSE,
  34. );
  35. $schema['columns']['height'] = array(
  36. 'type' => 'int',
  37. 'not null' => FALSE,
  38. );
  39. $schema['columns']['width'] = array(
  40. 'type' => 'int',
  41. 'not null' => FALSE,
  42. );
  43. $schema['columns']['autoplay'] = array(
  44. 'type' => 'int',
  45. 'not null' => FALSE,
  46. );
  47. $schema['indexes'] = array(
  48. 'height' => array('height'),
  49. 'width' => array('width'),
  50. );
  51. return $schema;
  52. }
  53. }
  54. /**
  55. * Implements magic callback / psuedo-hook mymodule_field_is_empty().
  56. */
  57. function video_filter_field_field_is_empty($item, $field) {
  58. if ($field['type'] == 'video_filter') {
  59. if (empty($item['url']) && empty($item['height'])
  60. && empty($item['width']) && empty($item['autoplay']))
  61. {
  62. return TRUE;
  63. } else {
  64. return FALSE;
  65. }
  66. }
  67. }
  68. /**
  69. * Implements magic callback / psuedo-hook mymodule_field_settings_form().
  70. */
  71. /*
  72. function video_filter_field_settings_form($field, $instance, $has_data) {
  73. if ($field['type'] == 'video_filter') {
  74. $settings = $field['setings'];
  75. $form = '';
  76. return $form;
  77. }
  78. }
  79. // */
  80. /**
  81. * Implements hook_field_validate().
  82. */
  83. function video_filter_field_field_validate($obj_type, $object, $field,
  84. $instance, $langcode, &$items,
  85. &$errors) {
  86. if ($field['type'] == 'video_filter') {
  87. foreach ($items as $delta => $item) {
  88. // Test URL.
  89. if (!empty($item['url'])) {
  90. //$errors[$field['field_name']][$langcode][$delta][] = array(
  91. // 'error' => 'video_filter_url',
  92. // 'message' => t('Video Filter Field: Invalid video URL'),
  93. //);
  94. // If URL isn't empty, we need a height.
  95. if (empty($item['height']) || !is_numeric($item['height'])) {
  96. $errors[$field['field_name']][$langcode][$delta][] = array(
  97. 'error' => 'video_filter_height',
  98. 'message' => t('Video Filter Field: Invalid video height'),
  99. );
  100. }
  101. // If URL isn't empty, we need a width.
  102. if (empty($item['width']) || !is_numeric($item['width'])) {
  103. $errors[$field['field_name']][$langcode][$delta][] = array(
  104. 'error' => 'video_filter_width',
  105. 'message' => t('Video Filter Field: Invalid video width'),
  106. );
  107. }
  108. }
  109. // URL field is empty. Set everything else to empty.
  110. else {
  111. unset($items);
  112. unset($object->field_media_url);
  113. }
  114. }
  115. }
  116. }
  117. /**
  118. * Implements hook_field_widget_info().
  119. */
  120. function video_filter_field_field_widget_info() {
  121. return array(
  122. 'video_filter' => array(
  123. 'label' => t('Video Filter'),
  124. 'description' => t('Enable user to determine height, width, and autoplay settings'),
  125. 'field types' => array('video_filter'),
  126. 'behaviors' => array(
  127. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  128. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  129. ),
  130. ),
  131. );
  132. }
  133. /**
  134. * Implements hook_field_widget_form().
  135. */
  136. function video_filter_field_field_widget_form(&$form, &$form_state, $field,
  137. $instance, $langcode, $items,
  138. $delta, $element) {
  139. $base = $element;
  140. //dsm($element);
  141. //dsm($instance,' instance');
  142. if ($instance['widget']['type'] == 'video_filter') {
  143. $element['url'] = array(
  144. '#type' => 'textfield',
  145. '#title' => t('Video URL'),
  146. '#default_value' => isset($items[$delta]['url']) ?
  147. $items[$delta]['url'] : NULL,
  148. '#weight' => 0,
  149. ) + $base;
  150. $element['height'] = array(
  151. '#type' => 'textfield',
  152. '#title' => t('Height'),
  153. '#default_value' => isset($items[$delta]['height']) ?
  154. $items[$delta]['height'] : NULL,
  155. '#weight' => 1,
  156. ) + $base;
  157. $element['width'] = array(
  158. '#type' => 'textfield',
  159. '#title' => t('Width'),
  160. '#default_value' => isset($items[$delta]['width']) ?
  161. $items[$delta]['width'] : NULL,
  162. '#weight' => 2,
  163. ) + $base;
  164. $element['autoplay'] = array(
  165. '#type' => 'checkbox',
  166. '#title' => t('Autoplay'),
  167. '#default_value' => isset($items[$delta]['autoplay']) ?
  168. $items[$delta]['autoplay'] : FALSE,
  169. '#weight' => 3,
  170. ) + $base;
  171. }
  172. return $element;
  173. }
  174. /**
  175. * Implements hook_field_formatter_info().
  176. */
  177. function video_filter_field_field_formatter_info() {
  178. return array(
  179. 'video_filter_field_default' => array(
  180. 'label' => t('Video Filter'),
  181. 'field types' => array('text', 'video_filter'),
  182. ),
  183. );
  184. }
  185. /**
  186. * Implements hook_field_formatter_view().
  187. */
  188. function video_filter_field_field_formatter_view($obj_type, $object, $field,
  189. $instance, $langcode, $items,
  190. $display) {
  191. /*
  192. dsm($obj_type, 'obj_type');
  193. dsm($object, 'object');
  194. dsm($field, 'field');
  195. dsm($instance,'instance');
  196. dsm($langcode,'langcode');
  197. dsm($display,'display');
  198. dsm($items,'items');
  199. // */
  200. $elements = array();
  201. foreach ($items as $delta => $item) {
  202. $url = url($item['url']);
  203. $text = "[video:$url]";
  204. $elements[$delta] = array(
  205. '#theme' => 'video_filter_field_default_formatter',
  206. '#item' => video_filter_field_process($text, $item),
  207. );
  208. }
  209. return $elements;
  210. }
  211. /**
  212. * Implements hook_theme().
  213. */
  214. function video_filter_field_theme() {
  215. return array(
  216. 'video_filter_field_default_formatter' => array(
  217. 'variables' => array('item' => NULL),
  218. ),
  219. );
  220. }
  221. /**
  222. * Returns HTML for a video_filter_field_formatter
  223. */
  224. function theme_video_filter_field_default_formatter($variables) {
  225. $output = '<div class="video-filter-field">' . $variables['item'] . '</div>';
  226. return $output;
  227. }
  228. /**
  229. * Implement video_filter processing from video_filter module.
  230. *
  231. * @see _video_filter_process()
  232. */
  233. function video_filter_field_process($text, $video) {
  234. $filter = new stdClass();
  235. //$filter->format = '';
  236. //$filter->module = 'video_filter';
  237. //$filter->name = 'video_filter';
  238. //$filter->weight = -1;
  239. $filter->status = 1;
  240. $filter->settings = array(
  241. 'video_filter_width' => 400,
  242. 'video_filter_height' => 400,
  243. 'video_filter_autoplay' => 0,
  244. 'video_filter_related' => 1,
  245. );
  246. # this added to avoid php 5.4 notice : array to string conversion
  247. if(is_array($text)) $text = "";
  248. $text = (string)$text; // Make sure $text is a string.
  249. if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
  250. foreach ($matches_code[0] as $ci => $code) {
  251. $video = (array)$video + array(
  252. 'source' => $matches_code[2][$ci],
  253. 'autoplay' => $filter->settings['video_filter_autoplay'],
  254. 'related' => $filter->settings['video_filter_related'],
  255. );
  256. // Pick random out of multiple sources separated by ','
  257. if (strstr($video['source'], ',')) {
  258. $sources = explode(',', $video['source']);
  259. $random = array_rand($sources, 1);
  260. $video['source'] = $sources[$random];
  261. }
  262. // Load all codecs
  263. $codecs = module_invoke_all('codec_info');
  264. // Find codec
  265. foreach ($codecs AS $codec_name => $codec) {
  266. if (!is_array($codec['regexp'])) {
  267. $codec['regexp'] = array($codec['regexp']);
  268. }
  269. // Try different regular expressions
  270. foreach ($codec['regexp'] as $delta => $regexp) {
  271. if (preg_match($regexp, $video['source'], $matches)) {
  272. $video['codec'] = $codec;
  273. $video['codec']['delta'] = $delta;
  274. $video['codec']['matches'] = $matches;
  275. $video['codec']['codec_name'] = $codec_name; // used in theme function
  276. $video['codec']['control_bar_height'] = 0; // default
  277. break 2;
  278. }
  279. }
  280. }
  281. // Codec found
  282. if (isset($video['codec'])) {
  283. // Override default attributes
  284. if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
  285. foreach ($matches_attributes[0] AS $ai => $attribute) {
  286. $video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
  287. }
  288. }
  289. // Use configured ratio if present, use that from the codec otherwise
  290. $ratio = 1;
  291. if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio) ) {
  292. //validate given ratio parameter
  293. $ratio = $tratio[1] / $tratio[2];
  294. }
  295. else {
  296. $ratio = $video['codec']['ratio'];
  297. }
  298. // Sets video width & height after any user input has been parsed.
  299. // First, check if user has set a width.
  300. if (isset($video['width']) && !isset($video['height'])) {
  301. $video['height'] = variable_get('video_filter_height_'.$format, 400);
  302. }
  303. // Else, if user has set height.
  304. elseif (isset($video['height']) && !isset($video['width'])) {
  305. $video['width'] = $video['height'] * $ratio;
  306. }
  307. // Maybe both?
  308. elseif (isset($video['height']) && isset($video['width'])) {
  309. $video['width'] = $video['width'];
  310. $video['height'] = $video['height'];
  311. }
  312. // Fall back to defaults.
  313. elseif (!isset($video['height']) && !isset($video['width'])) {
  314. $video['width'] = $filter->settings['video_filter_width'];
  315. $video['height'] = $filter->settings['video_filter_height'];
  316. }
  317. // Default value for control bar height
  318. $control_bar_height = 0;
  319. if (isset($video['control_bar_height'])) {
  320. // respect control_bar_height option if present
  321. $control_bar_height = $video['control_bar_height'];
  322. }
  323. elseif (isset($video['codec']['control_bar_height'])) {
  324. // respect setting provided by codec otherwise
  325. $control_bar_height = $video['codec']['control_bar_height'];
  326. }
  327. // Resize to fit within width and height repecting aspect ratio
  328. if ($ratio) {
  329. $scale_factor = min(array(($video['height']-$control_bar_height), $video['width']/($ratio)));
  330. $video['height'] = round($scale_factor + $control_bar_height);
  331. $video['width'] = round($scale_factor * $ratio);
  332. }
  333. $video['autoplay'] = (bool) $video['autoplay'];
  334. $video['align'] = (isset($video['align']) && in_array($video['align'], array('left', 'right', 'center'))) ? $video['align'] : NULL;
  335. if (is_callable($video['codec']['callback'], FALSE)) {
  336. $replacement = call_user_func($video['codec']['callback'], $video);
  337. }
  338. else {
  339. // Invalid callback
  340. $replacement = '<!-- VIDEO FILTER - INVALID CALLBACK IN: ' . $pattern . ' -->';
  341. }
  342. // Invalid format
  343. }
  344. else {
  345. $replacement = '<!-- VIDEO FILTER - INVALID CODEC IN: ' . $code . ' -->';
  346. }
  347. $text = str_replace($code, $replacement, $text);
  348. }
  349. }
  350. return $text;
  351. }
  352. /**
  353. * Test support for URL.
  354. *
  355. * @todo Implement hook_codec_info() to...
  356. * - add support for whatever media module uses as an alternative to video_filter module.
  357. * - add support for http://embed.ly/.
  358. *
  359. * @param $url
  360. * string, URL which may or may not include media of some kind.
  361. *
  362. * @return
  363. * TRUE, video_filter recognizes and supports media at this URL
  364. * FALSE, video_filter doesn't recognize it. This could be either because
  365. * the URL doesn't actually provide media or because we just doen't have
  366. * the codec for it.
  367. */
  368. function video_filter_field_url_is_supported($url) {
  369. // Load all codecs
  370. module_load_include('inc', 'video_filter', 'video_filter.codecs');
  371. $codecs = module_invoke_all('codec_info');
  372. // Find codec
  373. foreach ($codecs AS $codec_name => $codec) {
  374. if (!is_array($codec['regexp'])) {
  375. $codec['regexp'] = array($codec['regexp']);
  376. }
  377. // Try different regular expressions
  378. $video = array();
  379. foreach ($codec['regexp'] as $delta => $regexp) {
  380. if (preg_match($regexp, $url)) {
  381. $video['codec'] = TRUE;
  382. }
  383. break;
  384. }
  385. break;
  386. }
  387. // If we found a codec, this URL is supported.
  388. $is_supported = (isset($video['codec'])) ? TRUE : FALSE;
  389. return $is_supported;
  390. }