eminline.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/emfield/contrib/eminline/eminline.module
    2. 6 contributions/emfield/contrib/eminline/eminline.module

Functions & methods

NameDescription
eminline_filterImplemenation of hook_filter().
eminline_filter_tipsImplementation of hook_filter_tips().
theme_eminline_url_wrapperCreate additional theme layer.
_eminline_curry
_eminline_urlMatch all http(s) URLs and pass to link checker.
_eminline_url_parse_full_linksIf one of our allowed providers knows what to do with the url let them embedd the video.
_eminline_url_settingsSettings form for filter.
_eminline_url_settings_form_submit
_eminline_url_settings_form_validate

File

View source
  1. <?php
  2. /**
  3. * Implemenation of hook_filter().
  4. */
  5. function eminline_filter($op, $delta = 0, $format = -1, $text = '') {
  6. switch ($op) {
  7. case 'list':
  8. return array(0 => t('Embedded Inline Media'));
  9. case 'description':
  10. switch ($delta) {
  11. case 0:
  12. return t('Embed videos from specified URL.');
  13. default:
  14. return;
  15. }
  16. case "process":
  17. switch ($delta) {
  18. case 0:
  19. return _eminline_url($text, $format);
  20. default:
  21. return;
  22. }
  23. case 'settings':
  24. switch ($delta) {
  25. case 0:
  26. return _eminline_url_settings($format);
  27. default:
  28. return;
  29. }
  30. default:
  31. return $text;
  32. }
  33. }
  34. /**
  35. * Implementation of hook_filter_tips().
  36. */
  37. function eminline_filter_tips($delta, $format, $long = FALSE) {
  38. $output = '';
  39. switch ($delta) {
  40. case '0':
  41. $providers = variable_get('eminline_providers_' . $format, array());
  42. $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))));
  43. }
  44. return $output;
  45. }
  46. /**
  47. * Match all http(s) URLs and pass to link checker.
  48. *
  49. * @param string $text
  50. * The contents of the text area.
  51. * @param int $format
  52. * The format id.
  53. *
  54. * @return string
  55. */
  56. function _eminline_url($text, $format) {
  57. $text = ' '. $text .' ';
  58. // Need to attach the variables to the callback after the regex.
  59. $callback = _eminline_curry('_eminline_url_parse_full_links', 2);
  60. // Match absolute URLs.
  61. $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);
  62. return $text;
  63. }
  64. /**
  65. * If one of our allowed providers knows what to do with the url let them embedd the video.
  66. *
  67. * @param int $format
  68. * The format id.
  69. * @param array $match
  70. * The matched text from our regex.
  71. *
  72. * @return string
  73. * The replacement text for the url.
  74. */
  75. function _eminline_url_parse_full_links($format, $match) {
  76. // Get just the URL.
  77. $match[2] = check_url(decode_entities($match[2]));
  78. // Get our filter settings.
  79. $video_default = variable_get('eminline_video_' . $format, array());
  80. $preview_default = variable_get('eminline_preview_' . $format, array());
  81. $tn_default = variable_get('eminline_tn_' . $format, array());
  82. // Build a fake field, remember that emfield is well a field.
  83. $field = array(
  84. 'field_name' => 'eminline',
  85. 'type' => 'video_cck',
  86. 'video_width' => $video_default['video_width'],
  87. 'video_height' => $video_default['video_height'],
  88. 'video_autoplay' => $video_default['video_autoplay'],
  89. 'preview_width' => $preview_default['preview_width'],
  90. 'preview_height' => $preview_default['preview_height'],
  91. 'preview_autoplay' => $preview_default['preview_autoplay'],
  92. 'thumbnail_width' => $tn_default['thumbnail_width'],
  93. 'thumbnail_height' => $tn_default['thumbnail_height'],
  94. 'thumbnail_default_path' => $tn_default['thumbnail_default_path'],
  95. 'providers' => variable_get('eminline_providers_' . $format, array()),
  96. );
  97. // Build our embed item.
  98. $item = emfield_parse_embed($field, $match[2], 'video_cck');
  99. // Check to make sure the provider that was found is an allowed provider.
  100. if ($field['providers'][$item['provider']]) {
  101. $item['data'] = (array)emfield_include_invoke('video_cck', $item['provider'], 'data', $field, $item);
  102. }
  103. else {
  104. return $match[0];
  105. }
  106. // Get how the video should be displayed.
  107. $display_format = variable_get('eminline_full_node_' . $format, '');
  108. // Change the options based on how the video should be displayed.
  109. if ($display_format == 'video_thumbnail') {
  110. $options = array(
  111. 'width' => $field['thumbnail_width'],
  112. 'height' => $field['thumbnail_height'],
  113. 'default_path' => $field['thumbnail_default_path'],
  114. );
  115. }
  116. else if ($display_format == 'video_preview') {
  117. $options = array(
  118. 'width' => $field['preview_width'],
  119. 'height' => $field['preview_height'],
  120. 'autoplay' => $field['preview_autoplay'],
  121. );
  122. }
  123. else {
  124. $options = array(
  125. 'width' => $field['video_width'],
  126. 'height' => $field['video_height'],
  127. 'autoplay' => $field['video_autoplay'],
  128. );
  129. }
  130. // Theme the video w00t!
  131. return theme('eminline_url_wrapper', emfield_emfield_field_formatter($field, $item, $display_format, NULL, 'video_cck', $options));
  132. }
  133. /**
  134. * Settings form for filter.
  135. *
  136. * @param int format
  137. * The format id.
  138. *
  139. * @return array
  140. */
  141. function _eminline_url_settings($format) {
  142. // Get the settings that are avaliable to the cck widget.
  143. $form['eminline_'. $format] = video_cck_widget_settings('form', array('type' => 'video_cck_textfields'));
  144. $form['eminline_'. $format]['provider_list']['providers']['#default_value'] = variable_get('eminline_providers_' . $format, array());
  145. $video_default = variable_get('eminline_video_' . $format, array());
  146. foreach ($video_default as $key => $value) {
  147. $form['eminline_'. $format]['video'][$key]['#default_value'] = $value;
  148. }
  149. $preview_default = variable_get('eminline_preview_' . $format, array());
  150. foreach ($preview_default as $key => $value) {
  151. $form['eminline_'. $format]['preview'][$key]['#default_value'] = $value;
  152. }
  153. $tn_default = variable_get('eminline_tn_' . $format, array());
  154. foreach ($tn_default as $key => $value) {
  155. $form['eminline_'. $format]['tn'][$key]['#default_value'] = $value;
  156. }
  157. // Add select for choosing display format.
  158. $form['eminline_'. $format]['node_display'] = array(
  159. '#type' => 'select',
  160. '#title' => t('Video display in node view'),
  161. '#default_value' => variable_get('eminline_full_node_' . $format, ''),
  162. '#options' => array(
  163. 'video_video' => t('Video'),
  164. 'video_preview' => t('Preview'),
  165. 'video_thumbnail' => t('Thumbnail'),
  166. ),
  167. '#weight' => -999
  168. );
  169. $form['eminline_'. $format]['#type'] = 'fieldset';
  170. $form['eminline_'. $format]['#title'] = t('Embed inline');
  171. $form['eminline_'. $format]['#collapsible'] = TRUE;
  172. $form['eminline_'. $format]['#tree'] = TRUE;
  173. // My submit && validator plz, kthxbi.
  174. $form['#validate'] = array('_eminline_url_settings_form_validate' => array($format));
  175. $form['#submit'] = array('_eminline_url_settings_form_submit' => array($format));
  176. return $form;
  177. }
  178. /*
  179. TODO Write validation function...
  180. */
  181. function _eminline_url_settings_form_validate($form_id, $form_values, $form, $format) {
  182. if (!is_numeric($form_values['eminline_'. $format]['video']['video_width'])
  183. || intval($form_values['eminline_'. $format]['video']['video_width']) != $form_values['eminline_'. $format]['video']['video_width']
  184. || $form_values['eminline_'. $format]['video']['video_width'] < 1) {
  185. form_set_error('eminline_'. $format .'][video][video_width', t('"Video width" must be a positive integer.'));
  186. }
  187. if (!is_numeric($form_values['eminline_'. $format]['video']['video_height'])
  188. || intval($form_values['eminline_'. $format]['video']['video_height']) != $form_values['eminline_'. $format]['video']['video_height']
  189. || $form_values['eminline_'. $format]['video']['video_height'] < 1) {
  190. form_set_error('eminline_'. $format .'][video][video_height', t('"Video height" must be a positive integer.'));
  191. }
  192. if (!is_numeric($form_values['eminline_'. $format]['preview']['preview_width'])
  193. || intval($form_values['eminline_'. $format]['preview']['preview_width']) != $form_values['eminline_'. $format]['preview']['preview_width']
  194. || $form_values['eminline_'. $format]['preview']['preview_width'] < 1) {
  195. form_set_error('eminline_'. $format .'][preview][preview_width', t('"Preview width" must be a positive integer.'));
  196. }
  197. if (!is_numeric( $form_values['eminline_'. $format]['preview']['preview_height'])
  198. || intval( $form_values['eminline_'. $format]['preview']['preview_height']) != $form_values['eminline_'. $format]['preview']['preview_height']
  199. || $form_values['eminline_'. $format]['preview']['preview_height'] < 1) {
  200. form_set_error('eminline_'. $format .'][preview][preview_height', t('"Preview height" must be a positive integer.'));
  201. }
  202. if (!is_numeric($form_values['eminline_'. $format]['tn']['thumbnail_height'])
  203. || intval($form_values['eminline_'. $format]['tn']['thumbnail_height']) != $form_values['eminline_'. $format]['tn']['thumbnail_height']
  204. || $form_values['eminline_'. $format]['tn']['thumbnail_height'] < 1) {
  205. form_set_error('eminline_'. $format .'][tn][thumbnail_height', t('"Thumbnail height" must be a positive integer.'));
  206. }
  207. if (!is_numeric($form_values['eminline_'. $format]['tn']['thumbnail_width'])
  208. || intval($form_values['eminline_'. $format]['tn']['thumbnail_width']) != $form_values['eminline_'. $format]['tn']['thumbnail_width']
  209. || $form_values['eminline_'. $format]['tn']['thumbnail_width'] < 1) {
  210. form_set_error('eminline_'. $format .'][tn][thumbnail_width', t('"Thumbnail width" must be a positive integer.'));
  211. }
  212. }
  213. function _eminline_url_settings_form_submit($form_id, $form_values, $format) {
  214. variable_set('eminline_providers_' . $format, $form_values['eminline_'. $format]['provider_list']['providers']);
  215. variable_set('eminline_video_' . $format, $form_values['eminline_'. $format]['video']);
  216. variable_set('eminline_preview_' . $format, $form_values['eminline_'. $format]['preview']);
  217. variable_set('eminline_tn_' . $format, $form_values['eminline_'. $format]['tn']);
  218. variable_set('eminline_full_node_' . $format, $form_values['eminline_'. $format]['node_display']);
  219. }
  220. function _eminline_curry($func, $arity) {
  221. return create_function('', "
  222. \$args = func_get_args();
  223. if(count(\$args) >= $arity)
  224. return call_user_func_array('$func', \$args);
  225. \$args = var_export(\$args, 1);
  226. return create_function('','
  227. \$a = func_get_args();
  228. \$z = ' . \$args . ';
  229. \$a = array_merge(\$z,\$a);
  230. return call_user_func_array(\'$func\', \$a);
  231. ');
  232. ");
  233. }
  234. /**
  235. * Create additional theme layer.
  236. *
  237. * @param string $content
  238. *
  239. * @return string
  240. */
  241. function theme_eminline_url_wrapper($content) {
  242. $output = '';
  243. $output .= '<div class="eminline-wrapper">';
  244. $output .= $content;
  245. $output .= '</div>';
  246. return $output;
  247. }